On this page:
Problem 1: Designing a simple game
Game overview
Imports
Posns!
Circles!
Designing The World
Adding Randomness
7.7

Lab 5: Working with the image library

Goals: The goals of this lab are to practice using the image library function.

You may work with your current assignment partner in this lab. Any lab quiz should still be taken individually.

Please also refer to the sample code and video posted in Assignment 5.

Problem 1: 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

The javalib library provides the support for the design of interactive games and creating images composed by combining geometric shapes as well as image files. See The Image Library for more information.

To use the library, download the javalib file above and add it to your project the same way you have added the tester library.

At the top of the .java file where the library is used, add the following import statements:

import tester.*; // The tester library import javalib.worldimages.*; // images, like RectangleImage or OverlayImages import javalib.funworld.*; // the abstract World class and the big-bang library import java.awt.Color; // general colors (as triples of red,green,blue values) // and predefined colors (Color.RED, Color.GRAY, etc.)

You can test images using check-expect just as you can check other values:
boolean testImages(Tester t) {
return t.checkExpect(new RectangleImage(30, 20, OutlineMode.SOLID, Color.GRAY),
new RectangleImage(30, 20, OutlineMode.SOLID, Color.GRAY));
}

But note that you must construct the images in the same manner: for example, the following test will fail:
boolean testFailure(Tester t) {
return t.checkExpect(
new ScaleImageXY(new RectangleImage(60, 40, OutlineMode.SOLID, Color.GRAY), 0.5, 0.25),
new RectangleImage(30, 15, OutlineMode.SOLID, Color.GRAY));
}

Finally, you can display your images so that you can see whether you’re on the right track, as follows:

boolean testDrawTree(Tester t) {
WorldCanvas c = new WorldCanvas(500, 500);
WorldScene s = new WorldScene(500, 500);
return c.drawScene(s.placeImageXY(myTree.draw(), 250, 250))
&& c.show();
}

See The Image Library for more information.

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.