On this page:
Game overview
Imports
Posns!
Circles!
Designing The World
Adding Randomness
8.10

Recitation 6: Designing a simple game

Goals: The goal of this lab is to get some practice with using bigBang in Java.

Related files:
  tester.jar     javalib.jar  

Game overview

The game we’re going to design is going to be simple, as we only have a lab to complete it. A player is going to click a point on the screen and a circle will appear. Then, it will move off the edge of the screen. The game will end once some amount of circles have left the screen.

Imports

import the necessary libraries, as seen in Assignment 3.

Posns!

Remember our good friend Posn? So do we! Our libraries provide us with a Posn class, which just has two int fields, x and y, and a standard constructor.

This isn’t too useful, however, because like with all of our basic structures, we want to be able to write our own methods.

To remedy this, we’re going to create our own MyPosn class, which will act just like a Posn, but give us the freedom to do more with it.

class MyPosn extends Posn {
 
// standard constructor MyPosn(int x, int y) {
super(x, y);
}
 
// constructor to convert from a Posn to a MyPosn MyPosn(Posn p) {
this(p.x, p.y);
}
}
Circles!

Since our game is all about circles moving across the a screen, we need a Circle class to represent one. At the bare minimum, we must know its current position and velocity, so we know where to place it and where it’s going.

class Circle {
 
MyPosn position; // in pixels MyPosn velocity; // in pixels/tick }
Designing The World

We now must design our class that will extend World.

Adding Randomness

Circles that only move up aren’t very fun! Let’s have them move left, right, up, and down.