This site requires JavaScript, please enable it in your browser!
Greenfoot back
Frogfrote
Frogfrote wrote ...

2015/2/16

Detecting if a Key is correct?

Frogfrote Frogfrote

2015/2/16

#
I have ran into an issue coding a 2-player Algebra question game. I am trying to detect from whether 3 keys, which one is correct, based on an array ANSWERS1. My code is as follows. The issue I am having is under the questionTime() method. Thanks!
import greenfoot.*;

/**
 * Write a description of class AlgebrawlWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class AlgebrawlWorld extends World
{
    private String gameState;
    
    private final String DIRECTIONS = "directions";
    private final String STARTED = "started";
    private final String PLAYING = "playing";
    private final String PLAYER1 = "player1";
    private final String PLAYER2 = "player2";
    private final String QUESTION = "question";
    private final String[] BACKGROUND = {"answer1.png", "answer2.png", "answer3.png", "answer4.png", "answer5.png", "answer6.png", "answer7.png", "answer8.png", "answer9.png", "answer10.png", "answer11.png", "answer12.png", "answer13.png", "answer14.png", "answer15.png", "answer16.png", "answer17.png", "answer18.png", "answer19.png", "answer20.png", "answer21.png", "answer22.png", "answer23.png", "answer24.png", "answer25.png"};
    private final String[] ANSWERS1 = {"a", "d", "d", "w", "d", "a", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25"};
    private final String[] ANSWERS2 = {"up", "right", "right", "up", "right", "left", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25"};
    
    private int baby1StartingX = 0;
    private int baby1StartingY = 0;
    private int baby2StartingX = 0;
    private int baby2StartingY = 0;
    private int player1Score = 0;
    private int player2Score = 0;
    private boolean e = false;
    private final String A = "a";
    private final String W = "w";
    private final String D = "d";
    /**
     * Constructor for objects of class AlgebrawlWorld.
     * 
     */
    public AlgebrawlWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 

        prepare();
        
        gameState = STARTED;
    }
    public void act()
    {
        if (gameState.equals(STARTED) && Greenfoot.isKeyDown("space"))
        {
            gameState = DIRECTIONS;
            loadDirections();
            Greenfoot.delay(50);
        }
        else if (gameState.equals(DIRECTIONS) && Greenfoot.isKeyDown("space"))
        {
            gameState = (PLAYING);
            loadPlaying();
            Greenfoot.delay(100);
        }
        else if (gameState.equals(PLAYING) && Greenfoot.isKeyDown("space"))
        {
            gameState = (QUESTION);
            Greenfoot.delay(2);
            questionTime();
            }
        {
            if (player1Score == 10)
            {
                gameState = (PLAYER1);
                loadWin1();
            }
            if (player2Score == 10)
            {
                gameState = (PLAYER2);
                loadWin2();
            }
        }
    }

    
    private void loadDirections()
    {
        setBackground("directions.png");
    }
    
    private void loadPlaying()
    {
        setBackground("arena.png");

    }
    
    private void loadWin1()
    {
        setBackground("");
    }
    
    private void loadWin2()
    {
        setBackground("");
    }
    private void questionTime()
    {
        //Find array number
        int arrayLocation = Greenfoot.getRandomNumber(25);
           //set background with that array number
           setBackground(BACKGROUND[arrayLocation]);
           //test for key, if so test if answer right
           if (Greenfoot.isKeyDown("a"))
           {
               if( A == ANSWERS1[arrayLocation])
               {
                   setBackground("correct1.png");
                   player1Score = player1Score + 1;
                   Greenfoot.delay(300);
                   gameState = (PLAYING);
                   setBackground("arena.png");
                }
            }
           else if (Greenfoot.isKeyDown("w"))
           {
               if( W == ANSWERS1[arrayLocation])
               {
                   setBackground("correct1.png");
                   player1Score = player1Score + 1;
                   Greenfoot.delay(300);
                   gameState = (PLAYING);
                   setBackground("arena.png");
                }
            }
           else if (Greenfoot.isKeyDown("d"))
           {
               if( D ==  ANSWERS1[arrayLocation])
               {
                   setBackground("correct1.png");
                   player1Score = player1Score + 1;
                   Greenfoot.delay(300);
                   gameState = (PLAYING);
                   setBackground("arena.png");
                }
          }
    }
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
    }
}
Frogfrote Frogfrote

2015/2/16

#
Sorry! Code change :/ still having issues. Help! New code is :
import greenfoot.*;

/**
 * Write a description of class AlgebrawlWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class AlgebrawlWorld extends World
{
    private String gameState;
    
    private final String DIRECTIONS = "directions";
    private final String STARTED = "started";
    private final String PLAYING = "playing";
    private final String PLAYER1 = "player1";
    private final String PLAYER2 = "player2";
    private final String QUESTION = "question";
    private final String[] BACKGROUND = {"answer1.png", "answer2.png", "answer3.png", "answer4.png", "answer5.png", "answer6.png", "answer7.png", "answer8.png", "answer9.png", "answer10.png", "answer11.png", "answer12.png", "answer13.png", "answer14.png", "answer15.png", "answer16.png", "answer17.png", "answer18.png", "answer19.png", "answer20.png", "answer21.png", "answer22.png", "answer23.png", "answer24.png", "answer25.png"};
    private final String[] ANSWERS1 = {"a", "d", "d", "w", "d", "a", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25"};
    private final String[] ANSWERS2 = {"up", "right", "right", "up", "right", "left", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25"};
    
    private int baby1StartingX = 0;
    private int baby1StartingY = 0;
    private int baby2StartingX = 0;
    private int baby2StartingY = 0;
    private int player1Score = 0;
    private int player2Score = 0;
    private boolean e = false;
    private final String A = "a";
    private final String W = "w";
    private final String D = "d";
    /**
     * Constructor for objects of class AlgebrawlWorld.
     * 
     */
    public AlgebrawlWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 

        prepare();
        
        gameState = STARTED;
    }
    public void act()
    {
        if (gameState.equals(STARTED) && Greenfoot.isKeyDown("space"))
        {
            gameState = DIRECTIONS;
            loadDirections();
            Greenfoot.delay(50);
        }
        else if (gameState.equals(DIRECTIONS) && Greenfoot.isKeyDown("space"))
        {
            gameState = (PLAYING);
            loadPlaying();
            Greenfoot.delay(100);
        }
        else if (gameState.equals(PLAYING) && Greenfoot.isKeyDown("space"))
        {
            gameState = (QUESTION);
            Greenfoot.delay(2);
            questionTime();
            }
        {
            if (player1Score == 10)
            {
                gameState = (PLAYER1);
                loadWin1();
            }
            if (player2Score == 10)
            {
                gameState = (PLAYER2);
                loadWin2();
            }
        }
    }

    
    private void loadDirections()
    {
        setBackground("directions.png");
    }
    
    private void loadPlaying()
    {
        setBackground("arena.png");

    }
    
    private void loadWin1()
    {
        setBackground("");
    }
    
    private void loadWin2()
    {
        setBackground("");
    }
    private void questionTime()
    {
        //Find array number
        int arrayLocation = Greenfoot.getRandomNumber(25);
           //set background with that array number
           setBackground(BACKGROUND[arrayLocation]);
           //test for key, if so test if answer right
           if (Greenfoot.isKeyDown("a"))
           {
               if( A == ANSWERS1[arrayLocation])
               {
                   setBackground("correct1.png");
                   player1Score = player1Score + 1;
                   Greenfoot.delay(300);
                   gameState = (PLAYING);
                   setBackground("arena.png");
                }
            }
           if (Greenfoot.isKeyDown("w"))
           {
               if( W == ANSWERS1[arrayLocation])
               {
                   setBackground("correct1.png");
                   player1Score = player1Score + 1;
                   Greenfoot.delay(300);
                   gameState = (PLAYING);
                   setBackground("arena.png");
                }
            }
           if (Greenfoot.isKeyDown("d"))
           {
               if( D ==  ANSWERS1[arrayLocation])
               {
                   setBackground("correct1.png");
                   player1Score = player1Score + 1;
                   Greenfoot.delay(300);
                   gameState = (PLAYING);
                   setBackground("arena.png");
                }
          }
    }
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
    }
}
danpost danpost

2015/2/17

#
I believe your issue is due to the fact that 'questionTime' will only be called while the 'space' key is down (see condition of 'if' on line 60). In other words if you hold down the 'space' key and select an answer, you might find you made progress. Then, again, you may not. On lines 110, 121 and 132, you are comparing different String object to see if they are the same. They may contain the same internal character array, but the String objects are not the same. That is, the String objects created and assigned to the fields on lines 30 through 32 are not the same String objects created and assigned to elements in the array on line 20. To compare the character arrays of the String objects for equality, use the String method 'equals'. For example:
if (A.equals(ANSWERS1[arrayLocation])
As an alternative, you could declare A, W, and D before ANSWERS1 and use A, W, and D to assign the elements in the ANSWER1 array:
public final String A = "a";
public final String W = "w";
public final String D = "d";
public final String[] ANSWERS1 = { A, D, D, W, D, A, /* etc */};
Then, they could be compared for equality using '==' because they would be using the same String objects.
You need to login to post a reply.