Grid-Based Game

This page will help you make a grid-based game in Java. Download and unzip gridgame.zip. A simple demo game has been provided, which you will learn from and modify to make your own game. Compile Game.java, and execute the following to run the demo game.

Game g = new Game();
g.play();

To get a feel for what this game does, try clicking on the user interface and pressing various keys.

Now open Game.java. What fields does the Game class keep track of? Take a look at the constructor, which initializes these fields to set up the game. Notice that the Game class uses a GridDisplay object to provide a graphical user interface. What features is the Game constructor using from the GridDisplay class?

Now look at the play method. This code repeatedly waits for the user to click or press a key. Whenever the user clicks on a location, that location is passed to the locationClicked method. Whenever the user presses a key, the corresponding key code is passed to the keyPressed method. The step method is called at regular intervals. You will probably not need to change the play method, but you will definitely be modifying the locationClicked, keyPressed, and step methods.

Take a look at locationClicked. Do you see how this code corresponds to the behavior you observed when you clicked in the game?

Take a look at keyPressed. Do you see how this code corresponds to the behavior you observed when you pressed keys in the game?

Take a look at the step method. Do you see how this code corresponds to the behavior you observed when you were not clicking or pressing any keys?

Your Challenge

Implement a simple arcade-style game of your own. You might design your own game, or you might implement your own version of an existing game. Whatever you decide to create, start small. Introduce one aspect of the game at a time, and test each behavior before implementing the next feature. All of the code you write for this assignment will appear in Game.java. You will need to:

    • Modify Game to use fields that are appropriate for your game

    • Modify the Game constructor, so that it initializes your fields and sets up your game

    • Modify locationClicked and keyPressed so that your game handles mouse and keyboard input appropriately. (If your game uses only one kind of user input, the other method will be empty.)

    • Modify the step method to support (or remove) any real-time effects, where the screen contents change even when you are not clicking or pressing a key.

Reference

Location Class

Location(int row, int col)

int getRow()

int getCol()

boolean equals(Location other)


Color Class

Color(int red, int green, int blue)

int getRed()

int getGreen()

int getBlue()

boolean equals(Color other)


GridDisplay Class

GridDisplay(int numRows, int numCols)

GridDisplay(String imageFileName) //for manipulating pixels

int getNumRows()

int getNumCols()

boolean isValid(Location loc)

Color getColor(Location loc)

void setColor(Location loc, Color color)

String getImage(Location loc) //null if empty

void setImage(Location loc, String image) //null to remove image

void setTitle(String title)

void setLineColor(Color color) //show grid lines

void showMessageDialog(String message)

String showInputDialog(String message)

String showInputDialog(String message, String[] choices)

void load(String imageFileName) //fill window with colors from file

void save(String imageFileName) //save window colors to file