COSC 111 – Assignment 10
Side Scroller
In this assignment you will be implementing a simple arcade game. Here is a screen shot from the boring basic version of the game:
The user controls an image (U) on the left edge of the screen, that can be moved up and down. Images are created on the right edge of the screen, and these scroll toward the user’s image on the left edge. There are some images that the user tries to get (G), and there are other images that the user tries to avoid (A). The user earns a score, shown next to the title of the game, and the game ends when some condition is reached.
And the rest is up to you!
You get to choose the game’s theme/story, by choosing the title, images, distribution of images, game speed, scoring function, and game-over condition. And, of course, you can do more if you like, as long as your game still fits the description given above. (Be sure to choose a theme for your game that is appropriate for a general audience!)
You must at choose a theme and apply customizations to earn full credit. If your game is as lame as the one in the screenshot shown above, the assignment will earn at most 6/10.
You may find this assignment to be fairly challenging in places. There are many small methods that need to be completed, so if you find you have an available half-hour between classes, try to use that time to work on a small method.
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)
Grid Class
Grid(int numRows, int numCols)
int getNumRows()
int getNumCols()
Color getColor(Location loc)
void setColor(Location loc, Color color)
String getImage(Location loc) //returns null if empty
void setImage(Location loc, String imageFileName) //pass null image in order to remove an image
void setTitle(String title)
void pause(int milliseconds)
int checkLastKeyPressed() //returns -1 if no key pressed since last call
Your steps:
From Canvas, download and unzip ScrollingGame.zip, which contains all the files you will need for this assignment.
1. Open Game.java. All the code you write for this assignment will go in this file. You do not need to modify the other classes/files unless you want to customize
Game has several fields. The grid field holds the grid used to store and display images. The userRow field keeps track of which row the user-controlled image appears in, on the left edge of the grid. msElapsed keeps track of the total number of milliseconds that have elapsed since the start of the
game. timesGet keeps track of the total number of times the user has gotten the things they’re supposed to get in the game (collided with the images you want to hit). timesAvoid keeps track of the total number of times the user has been hit by the things they’re supposed to avoid.
The constructor has already been implemented for you, although you are encouraged to change the dimensions of the grid, the initial value of userRow, and the name of the image used to show the user.
If you construct a new Game, you’ll see a single image appear on the left edge of the grid window. The lame images “user.gif”, “get.gif”, and “avoid.gif” have been provided for you to use in your testing, but you must eventually replace them with images that are appropriate for your game’s theme.
2. Look at Game’s play method, which has already been implemented for you. This method serves as the game’s engine. Every 100 milliseconds, it handles a key press (if any), sometimes scrolls all images to the left and populates the right edge with new images, updates the title (to show the current score), and increments msElapsed. This repeats until the game is over.
Your job in this assignment will be to implement the methods called by play. We’ll start with handleKeyPress.
Complete the handleKeyPress method. Write the following line of code to check for a key press:
int key = grid.checkLastKeyPressed();
(You can see what value of keystroke the game detects in the console if you write a System.out.println(key) after that line). If the user pressed the up arrow (key == 38), move the user’s image up one row by reducing the userRow variable by 1 (unless the user is already in the top row). Likewise, if the user pressed the down arrow (key == 40), move the user’s image down one row (unless the user is already in the bottom row). Either way, make sure to
update userRow.
You will need to set the old location of the user to be blank, and then set the new location to show the user image. So, before you change the value of userRow, use the following line to clear the old U:
grid.setImage(new Location(userRow,0), null);
And after you set the new value of userRow, use:
grid.setImage(new Location(userRow,0), “user.gif”);
You may test your code by executing
Game.test();
which simply constructs a new Game and calls its play method. You should be able to move the user image up and down, and you should prevent the U from moving the image off the screen.
3. Complete the populateRightEdge method, which should randomly place images that you want to get (such as “get.gif”) and images that you want to avoid (such as “avoid.gif”) in the rightmost column of the grid. Of course, you get to choose what “randomly” means here, as long as you
use Math.random() or the Random() object generator somehow. Perhaps sometimes populateRightEdge shouldn’t place anything. Perhaps sometimes it should place several images. Perhaps one image should appear often, while another image might appear only on rare occasions. (If you’re unsure, you might just generate one “get.gif” and one “avoid.gif” item every iteration, and tweak it later.)
You will want to use these methods to get the number of rows in your game and the number of columns in your game. Remember that if your game has 15 columns, it means they are indexed from 0 to 14:
grid.getNumRows()
grid.getNumCols()
4. Complete the scrollLeft method, which should move every image on the screen one column to the left. For example, if the grid initially appears as follows
then after calling scrollLeft, it should appear as follows:
Note that the G that was in the leftmost column has disappeared, and that an A has moved into the leftmost column. Note that the rightmost column will always be empty after scrollLeft is called. Finally, note that the user- controlled U should remain exactly where it was. (For now, you should assume that there is no image immediately to the right of the user, so you will not need to worry about the possibility of a collision in this exercise.)
Hint: Use nested for-loops to iterate through every (row, col) position. At each (row, col) position, you should get the image to the right using:
String pic = grid.getImage(new Location(row,col+1));
and then set that image at your current (row, col) position using:
grid.setImage(new Location(row,col), pic);
5. Complete the handleCollision method, which should be called every time you are going to place the user image ‘U’ in the grid. If you are going to place the ‘U’ are some location (row, col) using
grid.setImage(new Location(userRow,0), “user.gif”);
you should make a call to handleCollision(new Location(userRow,0)). Your handleCollision method should see if there is already an object there. If there is a “get.gif” image there, you should increment the variable timesGet, which keeps track of the number of times you catch a “get” item. If it is an “avoid.gif” item, then increment timesAvoid.
6. There are two times you should be drawing your user:
(i) After a key press, your user gets erased from its old position and drawn on the new position, so use a handleCollion() call at the time of placing your User in the handleKeyPress method.
(ii) After a scrollLeft, you should draw your user again in its current position (the user should not be affected by the scroll). In the line before drawing the user again, your should make a call to handleCollision.
Test your code by playing your game, making sure that it behaves reasonably.
7. The fields’ timesGet and timesAvoid should each influence either the score, or the game-over condition, or both. For example, maybe getting something good increases the score, hitting something bad decreases the score, and the game ends after a set amount of time. Or alternatively, maybe getting something good increases the score, and hitting a certain number of bad things ends the game. Or alternatively, maybe the score depends only on how long the user has stayed alive, and the user dies when they’ve hit more bad things than good things. There are many possibilities here.
Complete the getScore method, which should return the current game score. In determining the score, you may use timesGet, timesAvoid, and msElapsed.
Then modify updateTitle to show the title of your game. (You’re welcome to show other statistics in the title, in addition to the score.)
Test that the score now updates correctly, as you play your game and collide with images.
8. Complete the isGameOver method, which should return true when the game should end. This may be a function of timesGet, timesAvoid, and msElapsed. Be sure to test that your game ends at the correct moment.
9. At this point, you’ve completed the code for this assignment. If you haven’t already done so, work on the theme of your game, with appropriate images, title, etc.
Grading
+1 for a comment at the top of your Game.java file with your name and student number and a description of any customizations you have added to the game
+1 drawing a user and having it respond with (at least) up and down keys, without going out of bounds.
+1 populateRightEdge works +2 scrollLeft works properly
+1 handleCollision detects the collisions with gets and avoids. This should be reflected in the score or in your lives or something.
That amount will earn 6/10, it is the minimal amount required to have some sort of working game. Then:
+2 for replacing the images of user.gif, avoid.gif, get.gif with any reasonable theme
+1 for another customization of your choice, such as speeding-up the game as your score increases
+1 for another customization of your choice, such as letting the user move off of the left-most column, or jumping motions, or shooting objects, or anything of your choice.
Please include a description of your customizations in your Game.java file comments!