CSE 113 - Spring 2011 - Banner
  • CSE 113 - Spring 2011
CSE 113 - Spring 2011 - Navigation
CSE 113 - Spring 2011 - Lab 7

Lab 7
Last modified: April 20 2011 12:55:33 PM

CSE 113 - Spring 2011 - Lab 7

An Adventure Game

We will now work with the ideas of actually controlling the movement of our actors with Vectors and using the methods in the Actor class to detect edges and other actors on the screen. This assignment will be the first of two assignments creating this game. In the game, the "hero" will move around the world and encounter various "obstacles". These "obstacles" will either be good or bad interactions. The "obstacles" will appear and disappear from the screen randomly.

If you don't remember how to do any of the steps (like, copying, zipping, etc), please refer back to Lab 1.

First, you will need to copy over the scenario for this assignment. If you are working from home, you can download the scenario here. If you are in lab, the scenario is located in /eng/class/notes/cse113/scenarios/Lab7.

Take note of the following classes in the scenario:

  • AdventureWorld - the world for this scenario
  • SmoothMover - the class we looked at in lecture and is discussed in Chapter 6
  • Vector - the Vector to store the information about the movement of the Hero
  • Hero - a subclass of SmoothMover
  • Obstacle - a subclass of SmoothMover (that the Hero will interact with)

Required Lab Tasks

Populate the Scenario

Your scenario should open with a hero at some location in the world. The location does not have to be random and does not necessarily have to change each time, but it may make for an interesting game to have it do so. Note that the Hero class does not have an image associated with it. You should set the image of the hero using the setImage method in the constructor of the hero class. Use whatever image you feel is best for your hero. Remember, that the image you select must be in the images folder of the scenario in order for it to be read. If you don't know how to do this, please make sure to ask the TAs during recitation or stop by office hours. (Probably the easiest way is to select the image as the image for the Hero class.)

Moving Hero

The hero in this scenario should move "forward" whenever act is called. Note that Hero is a subclass of SmoothMover, so calling the move() method (as is already in act()) will do the job IF a vector whose values are greater than (0,0) has been set as the SmoothMover's vector. You will need to fill in the constructor of Hero so that a proper Vector is given to the superclass via a call to the superclass' constructor.

Hero Checks for Edges and Wrap Around

The hero must check for the edges of the world and wrap around if it reaches an edge left/right and top/bottom. This code needs to be written in the SmoothMover class (there are comments in that file to direct you). Note that in the method in SmoothMover, it first changes the x & y position. From there, you need to make sure that the x & y have not gone off the screen, and if so move the SmoothMover to the opposite end of the world.

Hero Turns Right & Left

When the user presses the right arrow key, the hero should turn right (visually on the screen). The amount of degrees is up to you. Analogously, if the user presses the left arrow key, the hero should turn left. At this point, don't worry about the hero actually changing direction, simply have the graphic change. Note the methods turnRight() and turnLeft() and fill them in appropriately.

Hero Actually Rotates

Now go back and make the Hero actually rotate and then move in the direction of the rotation. Remember what we know about Vectors. Vectors are a direction (!) and a length. The direction will be the rotation of the actor and the length will remain unchanged. Recall that you can get the movement vector by calling getMovement() and storing it in a local variable. Then, you can setDirection() on that vector to be the new direction.

Obstacle Constructor

Add a String parameter to the constructor of the Obstacle class that will represent the name of the file that contains the image for the obstacle. Use the parameter as the arugment to a call to the setImage method inside the constructor. This way, each obstacle will have its image set with an image when you create it. The obstacles could have a number of different images. In fact, some of them may even change their image while they are in the world.

Making "skull" obstacles

Create an obstacle whose image is a skull and place it in the world. Run the scenario to make sure the skull appears. When you know it works, you can remove the code and move on to the next step.

Skull obstacles appear randomly

In the act method of the world, create a skull obstacles randomly (like the food from Lab 5). That is, get a random number, and if that number is within a certain range, a skull obstacle should appear at a random location in the world. Make this happen rather infrequently. Try getting a number out of 5000 and seeing if it is less than 15. If you'd like more, change the numbers, if you'd like less, change the numbers again. Run your code to see that the skulls eventually appear.

Hero Encounters a Skull

If the hero encounters a skull, the scenario should stop. Fill in the method named checkForSkull in your Hero class appropriately.

Obstacles Disappear Randomly

Fill in the code for disappear in the Obstacle class, so that at random times an obstacle may disappear (that is, be removed from the world). Make this happen relatively infrequently (like the 5000, 15 numbers I suggested above, or even greater, get numbers out of 10000 and look for numbers less than 15).


Submitting the Assignment

Once you have completed your work, you will submit the zipped up version of the Lab 7 scenario.

For directions on submitting, please see here.

Please recall that you must achieve a grade of at least 50% on each of the labs to be allowed to take the third practical exam. This lab is technically due on Monday, May 2nd at 11:59:59pm. However, if you want to stay current with the course, I would recommend having this assignment completed within a week. You will need mastery of the skills you learn in this lab to be successful at practical exam 3.


Grading Information

Please make sure to look at your grading information on Web-CAT. See the grading information page for more details about how to interpret Web-CAT grade reports. (Currently, the grading is not enabled for this assignment.)

The following tests were run on your assignment submission. Each test was given equal weight (11.1 points each)

  1. When the world is created, there is a Hero positioned at some place in the world.
  2. The Hero moves every time act is called.
  3. The Hero wraps around the edges of the world.
  4. The Hero turns right and left when the user presses the right and left arrow keys.
  5. The constructor of the Obstacle class takes a String parameter.
  6. There are not Skull obstacles in the world when it is created.
  7. Skull obstacles appear in the world randomly.
  8. If a hero encounters a Skull obstacle, the scenario stops.
  9. Obstacles randomly disappear from the world.

Lab authored by Adrienne Decker

CSE 113 - Spring 2011 - Footer

Page maintained by Adrienne Decker

Contact: adrienne@buffalo.edu | 130 Bell Hall | (716)645-3184