Search code examples
javacollision-detectionfilenames

Collision Detection returning a value derived from the image filename


I'm currently in the process of building a small Asteroids style game which is intended for use by the kids at a local junior school. The aim is to help them with their times-tables.

A randomly generated question is displayed on-screen, (example: 6 * ? = 12), and a number of star images with numbers on them are floating around, waiting to be be shot at! You control a ship and fly around hunting the correct answer.

Bullets collide with the stars and a test needs to be performed to see if the user has shot the correct answer or not.

The star's filenames are simply "1.png", "2.png" etc, and I have a small function to return the first character of the filename, and convert it to an int:

public rightAnswer() {

    String filename = "12.png";     //would be passed as parameters 
    int coefficient = 2;            //
    int answer = 24;                //

    char first = filename.charAt(0);
    char second = filename.charAt(1);       
    int target;

    if (second != '.') {    //dealing only with 1 or 2-digit numbers        

        String both = String.valueOf(first) + String.valueOf(second);           
        target = Integer.parseInt(both);
    }
    else {
        target = Character.digit(first, 10);
    }

    if (target * coefficient == answer) {
        System.out.println("Answer is correct! (target = " + target + ")");
    }
    else {
        System.out.println("Wrong! (target = " + target + ")");
    }
}

While writing I got struck by a problem. I can't figure out how to retrieve the filename of the collided star to pass it to the above test function.

edit:

taking a shot at Enno Shioji's solution, I've come to this:

//star map
Map<ImageEntity, Integer> star_number = new HashMap<ImageEntity, Integer>();

//later, while loading the images into the game...
for (int n = 0; n < 10; n++) {          // 10 loops

        stars[n] = new ImageEntity(this);
        String filename = (n+2) + ".png";   // 2- 12 range
        stars[n].load(filename);

        star_number.put(stars[n], n+2);
    }

//then later again, run when collision is detected between a bullet and a star...
public boolean correctAnswer(ImageEntity star, int expectedAnswer) {

    if (star_number.get(star) == expectedAnswer) 
        return true;

    else return false;
}

Is this looking right?


Solution

  • I would not try to grab the filename. Instead, you should keep track of which star corresponds to which number, in a Map.

    // Do this wherever you have easy access to the file name
    Map<Star,Integer> star_number = new HashMap<Star,Integer>();
    star_number.put(star1,1);
    star_number.put(star2,2);
    etc...
    
    //Then later...
    boolean collided(Star star, Integer expectedAnswer){
        Integer answerGiven = star_number.get(star);
        return expectedAnswer.equals(answerGiven);
    }
    

    or, you could also keep track by defining the Star object as such:

    class Star{
        Integer number;
        StarImage image;
        Star(StarImage image, Integer number){
            this.number = number;
            this.image = image;
    }
    // then later...
    boolean collided(Star star, Integer expectedAnswer){
        return star.number.equals(expectedAnswer);
    }
    

    Hope this helps. Good luck with your project :)