New Presentation: AsteroidsPt1
1
Designing an Asteroids game
2
Designing an Floater class
3
Designing an Floater class
4
Designing an Floater class
5
Designing an Floater class
6
Designing an Floater class
7
A sample Spaceship program
8
The 9 protected member variables in Floater
protected double myCenterX, myCenterY;
//holds center coordinates
protected double myXspeed, myYspeed;
//holds the speed of travel
//in the x and y directions
protected double myPointDirection;
//holds current direction the floater is pointing
//in degrees
9
protected member variables in Floater
protected int corners;
//the number of corners, a triangular
//floater has 3
protected int[] xCorners;
protected int[] yCorners;
//The coordinates of the corners, with center of
//object at (0,0) and myPointDirection=0 (right)
protected int myColor;
10
extending the Floater class
11
extending the Floater class
12
Constructing a Spaceship
13
(16,0)
(-8,-8)
(-8,8)
Constructing a slightly fancier Spaceship
14
(16,0)
(-8,-8)
(-8,8)
(-2,0)
Constructing a slightly fancier Spaceship
15
(16,0)
(-8,-8)
(-8,8)
(-2,0)
A different way
16
(16,0)
(-8,-8)
(-8,8)
(-2,0)
Important: half of your coordinates should be negative!
17
(16,0)
(-8,-8)
(-8,8)
(-2,0)
Spaceship design worksheet�(optional)
18
Hyperspace
Spaceship bob = new Spaceship();
//other code (setup, draw) not shown
public void keyPressed(){
if(key == 'h'){
bob.??;
}
}
19
Is this OK?
Spaceship bob = new Spaceship();
public void keyPressed()
{
if(key == 'h')
{
bob.myXspeed = 0;
//other Java code not shown
}
}
20
NO! myXspeed is protected
Spaceship bob = new Spaceship();
public void keyPressed()
{
if(key == 'h')
{
bob.myXspeed = 0;
//other Java code not shown
}
}
21
You could create a setter like setXspeed()
class Spaceship extends Floater
{
public SpaceShip() {
//construtor code not shown
}
public void setXspeed(double x) {
myXspeed = x;
}
//other functions not shown
}
22
Or you could create a hyperspace()
class Spaceship extends Floater
{
public SpaceShip() {
//construtor code not shown
}
public void hyperspace() {
//code not shown
}
//other functions not shown
}
23
Either way is fine
Spaceship bob = new Spaceship();
public void keyPressed()
{
if(key == 'h')
{
bob.setXspeed(0); //OK!
//or
bob.hyperspace(); //OK!
//other Java code not shown
}
}
24
Asteroids Pt 1: Star class
25
If you wrote this Star class on the AP, they would deduct points. Why?
26
Like every class on the AP exam the Star class needs to be encapsulated
27
Creating an Array of Stars
28
Creating an Array of Stars
29
This is what creating new Stars in new positions everytime you draw the screen looks like
30
Creating an Array of Stars
31
Creating an Array of Stars
32
Adding instructions in index.html
33
Adding instructions in the html
34