+ All Categories
Home > Documents > Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse...

Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse...

Date post: 05-Jan-2016
Category:
Upload: shanna-campbell
View: 213 times
Download: 0 times
Share this document with a friend
Popular Tags:
21
Session 16 Pinball Game Construction Kit:
Transcript
Page 1: Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.

Session 16

Pinball Game Construction Kit:

Page 2: Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.

Pinball Version 1

• Replaced the fire button with a mouse event.

• Multiple balls can be in the air at once.– Uses a Vector to contain many balls

• Control is no longer in the paint method.

Page 3: Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.

Keeping track of multiple objects.

• In the past we have kept track of multiple items through an array. What is the limitation of that?

Page 4: Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.

Keeping track of multiple objects.

• In the past we have kept track of multiple items through an array. What is the limitation of that?– You have to state the number of elements at

construction time.

Page 5: Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.

Keeping track of multiple objects.

• In the past we have kept track of multiple items through an array. What is the limitation of that?– You have to state the number of elements at

construction time.

• If we don’t know the number in advance we can use the concept of a Vector. (Let’s go back to the code (also pp 102-103))

Page 6: Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.

Using Vector

• import java.util.Vector;

• private Vector balls; // holds only objects

• balls = new Vector( ); // create a Vector

• Using the Vector:balls.addElement(newBall); // add element

balls.size( ); // return # elements

// Retrieving an element requires a cast

PinBall aBall = (PinBall) balls.elementAt (i);

Page 7: Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.

Exercise

• Modify version 1 of the PinBallGame to allow users only six shots.

• Add a Label at the top of the Frame that displays the number of shots that have been fired so far.

• If the user tries to fire again after six shots, the label should display a “Sorry...” message.

Page 8: Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.

From javadoc for the Label class:Constructor SummaryLabel()Constructs an empty label.

Label(String text)Constructs a new label with the specified string of text, left

justified.

Method SummaryString getText()Gets the text of this label.

void setText(String text)Sets the text for this label to the specified text.

Page 9: Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.

A Possible Solutionpublic class PinBallGame extends Frame { ...private int numberOfShotsTaken;private Label numberOfShotsLabel;public PinBallGame () { ... // all the original code plus:

numberOfShotsTaken = 0;numberOfShotsLabel = new Label( "Number Of Shots Taken: 0" );add( "North", numberOfShotsLabel );

}...private class MouseKeeper extends MouseAdapter {

public void mousePressed (MouseEvent e) { ...if ( (x > FrameWidth-40) ... {

if ( numberOfShotsTaken < 6 ) {// make ball and thread, then:numberOfShotsTaken++;numberOfShotsLabel.setText("Number Of Shots Taken: " +

numberOfShotsTaken );} else

numberOfShotsLabel.setText( "Sorry, but you have already [...]!" );}

}}public void paint (Graphics g) { ... }}

Page 10: Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.

Threads of ExecutionWhat? How? Why?

The Thread class provides methods to start, run, sleep, and stop an independent path of computation, among other things. The start() method manages the overhead of threads for us; we can simply watch them go! (This is similar to the benefits we get from using Frames...)

The pinball game separates these responsibilities into different objects:

• painting the frame• controlling the movement/location of balls

Page 11: Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.

Threads of ExecutionThis separation simplifies all of the methods

involved, for writing, reading, and modifying. The cost is an increase in the number of methods needed.

You may also suffer some separation anxiety. To programmers used to writing the omniscient, omnipotent main(), decentralizing control can cause a sense of fragmentation. This will largely go away as you gain OO and Java experience.

Page 12: Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.

PinBall Version 2

• Adds targets for the PinBalls to bounce off of and score on

• Types of targets:– Spring– Wall– Hole– ScorePad

• What do all targets have in common?

Page 13: Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.

PinBallTarget Interface

interface PinBallTarget {

public boolean intersects (Ball aBall);

public void moveTo (int x, int y);

public void paint (Graphics g);

public void hitBy (Ball aBall);

}

Why use an interface?– we want to process targets uniformly, e.g., check if a ball hit it– the interface makes them the same “type” for storage in a Vector

Page 14: Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.

Hole target

• structurally similar to a ball – round like a ball– has a location on the frame like a ball

• behavioral– it must adhere to the interface

class Hole extends Ball implements PinBallTarget

Inherits moveTo and paint, but supplies intersects and hitBy

Page 15: Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.

More on Threads

• We can think of separate threads as separate programs running concurrently.

• They don’t literally run at the same time (unless you have a machine with more than one CPU). Instead, one thread gets the CPU for a while, then it gets put on hold while another thread gets the CPU, and so on.

• When separate threads are running, sometimes we need to worry about two threads taking actions that conflict with one another. We can use the keyword synchronized to have the JVM help maintain order.

Page 16: Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.

A Problem Caused bySeparate Threads of Control

Page 17: Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.

More on Threads

• Example: The second version of the pin ball game keeps track of the score the user earns for hitting targets in the field of play. It keeps track of the score in an instance variable named score:

private int score = 0;• When a pin ball strikes a target, the target tells the pin

ball game to add its point total to the instance variable by sending an addScore message:

public void addScore( int value ) {score = score + value;scoreLabel.setText( "score = " + score );

}

Page 18: Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.

A Problem Caused bySeparate Threads of Control

Page 19: Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.

The solution

synchronized public void addScore( int value ) {score = score + value;scoreLabel.setText( "score = "

+ score );}The keyword synchronized is used to ask

Java to guarantee that only one thread at a time can be executing the addScore() method.

Page 20: Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.

PinBall Contruction Kit Version 3

Page 21: Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.

Understanding the PinBallGame Kit• How is the “black box” in the PinBallGame drawn?• What is the difference between the items outside the box

of the game window and the items inside the box?• What messages can we send to a Peg, and where is

each behavior defined?• What method is used to determine the number of

elements held in a Vector? What method is used to access the values? What method is used to insert a new value into the collection?

• What is the purpose of the PinBallTarget interface?• Why can’t we do without it?• Why is the PinBallGame instance stored in a class

variable instead of an instance variable? (See class PinBallGame for the declaration, but study the code in class ScorePad to find the reason.)


Recommended