



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
In this document, students are given instructions for creating a game in the computer science i (cis 201) course. The game, named 'rakingleaves', involves using arrow keys to move a rake around the game canvas to pick up leaves and deposit them in a leaf pile. The setup and advance methods, as well as instructions for creating a larger game canvas, defining a leaf pile, making leaves, adding leaves to the yard, and making a rake. Students are also instructed on how to move the rake using arrow keys and how to pile up leaves when the rake hits them.
Typology: Lab Reports
1 / 6
This page cannot be seen from the preview
Don't miss anything!
In this lab you will create a game that will incorporate
The purpose of this game is to use the arrow keys to move a “rake” around the active part of the game canvas to pick up leaves and to deposit them in a separate leaf pile.
Create a Lab05 in which you will do your work, and change to that directory. Begin editing a game with the class name RakingLeaves as outlined below. Remember that this game will extend the FANG Game class and that you will need to use an import statement for this to work. Include a header comment, a class declaration, and our standard two methods. Here is your starting point.
import fang.core.Game;
} // class RakingLeaves
Change the size of the game. We want to give up some of the screen to a “scoring box” so you will want to make the game field larger than it normally is. Remember that though we change the size (and shape) of the screen, location coordinates are still given in screens so the upper-left corner is still at (0.0, 0.0) and the lower-right corner is still at (1.0, 1.0).
Game size is set during construction of the game. You must add a constructor to your game. A constructor is a special method that is called when an object of the given type is created (when new is called). Your game extends the FANG Game class, and the FANG game engine automatically constructs an instance of your game to get things going. If you don’t provide your own constructor, the FANG library gives you one free of charge – the one you have already seen on your earlier games.
The signature of a constructor has no return type and has the same name as the class itself (two ways to recognize that a method is a constructor if you were ever asked on a test or quiz, for example). So, a public constructor for our RakingLeaves class with an empty parameter list would look like this:
public RakingLeaves () { ... }
In a constructor, the very first line can call the superclass constructor (in this case, the constructor for the FANG Game class), possibly providing it with different parameters. The way you specify the “other” constructor is with the keyword super. This must be the first non-comment line of the constructor. In our case, it will be the only line: we will call the Game constructor that takes a width and height for the game field in pixels. Your game should be 640 by 640 pixels (width by height), so you should put this constructor in your code, just after your class declaration:
public RakingLeaves() {
public PolygonSprite makeLeaf() { PolygonSprite leaf = new PolygonSprite(...); // fill in... //
If you were to try to compile this code, it would give you errors, particularly because of the three dots! You need to replace it with an expression that returns a random integer value between 3 and 6 (inclusive). You should use the randomInt method, part of the Game class, to get such an integer. Read the documentation to find out what parameters this method requires.
After you have created the leaf, you will need to set its color randomly. You can use the getColor method to set the color appropriately, using random values for red, green, and blue. But don’t make the colors too dark, otherwise you won’t be able to see them on the canvas. Thus you should use random values between 128 and 255 for each of the red, green, and blue values. Set the scale of the leaf to 0.05 using the setScale method.
We want to randomly position the leaf to the right of the leaf pile. Use the randomDouble method (see the documentation) to position the leaf to the right of the leaf pile. Remember that the split variable gives the x -coordinate where the leaf pile ends and the rest of the game canvas (the yard) starts. Use this code:
leaf.setLocation(randomDouble(0.33, 1.0),randomDouble());
Finally, add the leaf to the game canvas.
Do all of this before you return the leaf to the caller.
In your setup method, You will add 10 leaves to the yard (to the right of the leaf pile). You will need to keep track of each leaf, but you don’t want to have a separate variable for each. To do this, you declare a field variable leaves which will be an ArrayList of PolygonSprites. Here is the declaration:
private ArrayList
You will need to use an import statement to load the ArrayList class from the Java class library:
import java.util.ArrayList;
Near the beginning in your setup method, define your leaves variable as follows:
leaves = new ArrayList
The
After you have defined the leaves variable in the setup method, you can create 10 leaves, keep track of them in the leaves array, and add them to the game canvas. Here is the code:
for (int i = 0; i < 10; i++) { PolygonSprite nextLeaf = makeLeaf(); // why we made a method leaves.add(nextLeaf); }
Show us your work at this point. When you run your program, you should see a bunch of leaves randomly placed in the “yard”.
A rake is a yellow triangle with a scale of 0.10. Initially position it in the middle of the screen. The rake variable will be a PolygonSprite with three sides. Declare it as a field variable and initialize it in the setup method. Be sure to add it to your game canvas!
In the advance method, use the arrow keys to move the rake. The rake should move at the rate of about 2 screens/second. Create a field variable defining the rake speed and use it in the advance method. Be sure that the rake doesn’t leave the yard. This means that you need to should not move the rake horizontally unless its x coordinate is between split and 1.0, and vertically unless its y coordinate is between 0.0 and 1.0.