Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Laboratory Assignment 5: Creating a Raking Leaves Game in CIS 201, Lab Reports of Computer Science

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

Pre 2010

Uploaded on 08/09/2009

koofers-user-hf6
koofers-user-hf6 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CIS 201 Computer Science I
Laboratory Assignment 5
Introduction
In this lab you will create a game that will incorporate
Count controlled iteration (
for
statement)
Non-text score keeping
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.
Raking Leaves
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;
/**
* Describe class RakingLeaves here.
*
*
* Created: Tue Sep 2 09:37:57 2008
*
* @author <a href="mailto:student1@potsdam.edu">Indiana Jones</a>
* @author <a href="mailto:student2@potsdam.edu">Luke Skywalker</a>
* @version 1.0
*/
public class RakingLeaves extends Game {
/**
* Deposit the leaves into the raking area and set up the rake
*/
public void setup() {
1
pf3
pf4
pf5

Partial preview of the text

Download Laboratory Assignment 5: Creating a Raking Leaves Game in CIS 201 and more Lab Reports Computer Science in PDF only on Docsity!

CIS 201 – Computer Science I

Laboratory Assignment 5

Introduction

In this lab you will create a game that will incorporate

  • Count controlled iteration (for statement)
  • Non-text score keeping

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.

Raking Leaves

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;

  • Describe class RakingLeaves here.
  • Created: Tue Sep 2 09:37:57 2008
  • @author Indiana Jones
  • @author Luke Skywalker
  • @version 1. */ public class RakingLeaves extends Game {
  • Deposit the leaves into the raking area and set up the rake */ public void setup() {
  • Move the rake to pick up the leaves, and deposite them
  • into the leaf pile.
  • @param dT time (in fractional seconds) since the last call to advance */ public void advance(double dT) {

} // 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... // leaf.setLocation(/* see below */); return leaf; }

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.

Add leaves to the yard

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 leaves;

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 part tells Java that the array list can only hold PolygonSprites, and noth- ing else.

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); }

Checkpoint 2

Show us your work at this point. When you run your program, you should see a bunch of leaves randomly placed in the “yard”.

Raking leaves

Make a rake

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!

Moving the rake

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.