Nifty Scrolling Game

<< Life | LabTrailIndex | Temp Converter >>

This is another nifty assignment from Dave Feinberg at Carnegie Mellon University in 2011.

Click here to see a video of the basic Game

Link to the assignment for instructions, be sure to download the starter code.


Step 1

  1. If you are using Eclipse, and you cannot run the starter code, check the Eclipse preferences(under the Window menu in Windows, the Eclipse menu in OSX). The Java->Build Path should have Project selected (not Folders) for source and output folder:

Step 2

  1. 0 is the top, and bigger numbers are below
  2. The up arrow key code is 38, down arrow is 40.
  3. You can find the maximum by using grid.getNumRows()
  4. You can store the current location by using Location loc = new Location( userRow,0 ); first thing. This is important so you can "erase" your old location later.
  5. To "erase" a location, set the image to null: grid.setImage( loc, null )
  6. You can get and and store the image to use it later: String userImageName = grid.getImage( loc )
  7. You can see if the current Location and the next Location are the same: loc.equals( nextLoc )

Step 3

  1. To choose a random row: int row = (int)(grid.getNumRows()*Math.random());
  2. To choose something 20% of the time, you can use an if statement: if ( Math.random < 0.2 )
  3. To find the last column: int lastCol = grid.getNumCols()-1;
  4. To make an avoidable object: grid.setImage(loc, "avoid.gif");
  5. To make a gettable object: grid.setImage(loc, "get.gif");
  6. To test your code, make sure your test method looks like:
    public static void test()
        Game game = new Game();
  7. Be sure to run it a number of times to make sure there are no "index out of bounds" trouble and that you get both A and G objects.

Step 4

  1. Be sure to not move the user. One way is to treat the first column separately, making note of userRow
    	  for (int r=0; r<grid.getNumRows();r++)
    		  if (r!=userRow){
    			  Location next = new Location(r,1);
    			  grid.setImage(new Location(r,0), grid.getImage(next));
  2. Use a nested for loop to go through each location in all but the first column.
  3. To make sure to avoid destroying things accidentally, Make the outer (first) loop the columns, working toward grid.getNumCols()-1
    	  for (int c= 1 ; c<grid.getNumCols()-1; c++)
    		  for (int r = 0 ; r< grid.getNumRows(); r++){
  4. Treat the last row separately, making sure it is all null (See Hint for Step 2, number 5)
  5. To test buy "running the game", make sure your test method looks like:
    public static void test()
        Game game = new Game();;

Step 7

  1. One idea for the updateTitle is to include how many times you've been hit grid.setTitle("Game: " + getScore()+ " Hit: "+timesAvoid+" times");

Step 8

  1. One idea is to end when you've been hit 3 times: return timesAvoid>2 ;

Step 9

Now that the game is functional, you want to be creative. Pick a theme, change the size and colors and graphics. You can see some examples here.

  1. Look in the Grid class to see things at your disposal like any public method.
  2. This project has its own Color class, so don't' import java.awt.Color! Here is an example: grid.setColor(loc, new Color(255,0,0));. The 3 parameters are red, green and blue, where 0 is none and 255 is all. You can use the ColorMixer Applet to find the right numbers for the color you want. For example, you can change the background from black to brown with this in the Game constructor:
        Color brown = new Color(140,80,0);
        for(int r=0; r<grid.getNumRows();r++)
        		for (int c=0; c<grid.getNumCols();c++)
    	    		Location loc = new Location(r,c);
    	    		grid.setColor(loc, brown);
  3. You can stop the game to send an alert with grid.showMessageDialog("you were hit!");
  4. You can change the lame graphics by making your own gif ( or png) files instead of the "user.gif" "avoid.gif" and "get.gif" and in fact SHOULD. You can have more than one type of good thing or bad thing. I like using GIMP, but any graphic editor will do. Its works well if you use a gif or a png file that is square and has a transparent background.