1 of 34

New Presentation: AsteroidsPt1

  • We’ll recreate the Asteroids arcade game over the next three assignments

1

2 of 34

Designing an Asteroids game

  • What objects do we need to model?
  • What do they have in common?

2

3 of 34

Designing an Floater class

  • A super or base class for all objects that float in space
  • Once we have a Floater class, we can extend it to make other sub or derived classes
    1. spaceships
    2. asteroids
    3. bullets
    4. UFOs (optional)

3

4 of 34

Designing an Floater class

  • What do all objects that float in space do?

4

5 of 34

Designing an Floater class

  • What do all objects that float in space do?
    1. Move
    2. Turn (Rotate)
    3. Get drawn or show

5

6 of 34

Designing an Floater class

  • What do all objects that float in space do?
    1. Move
    2. Turn (Rotate)
    3. Get drawn or show
  • What do all objects that float in space have?

6

7 of 34

Designing an Floater class

  • What do all objects that float in space do?
    1. Move
    2. Turn (Rotate)
    3. Get drawn or show
  • What do all objects that float in space have?
    • A number of corners
    • X and Y position
    • One direction (that they point)
    • And a different direction (that they move)

7

8 of 34

A sample Spaceship program

  • You may find this program helpful in understanding how the Floater class works
  • apcslowell.github.io/AsteroidsVariableDemoV2/

8

9 of 34

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

10 of 34

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

11 of 34

extending the Floater class

  • What functions would you need to write to extend Floater?

11

12 of 34

extending the Floater class

  • What functions would you need to write to extend Floater?
  • You would write a constructor
  • You would also write “getter” and “setter” functions as necessary

12

13 of 34

Constructing a Spaceship

  • Initialize the 9 inherited protected variables: corners, xCorners, yCorners, myColor, myCenterX, myCenterY, myXspeed, myYspeed and myPointDirection

13

(16,0)

(-8,-8)

(-8,8)

14 of 34

Constructing a slightly fancier Spaceship

  • How would the constructor of my slightly fancier Spaceship be different?

14

(16,0)

(-8,-8)

(-8,8)

(-2,0)

15 of 34

Constructing a slightly fancier Spaceship

15

(16,0)

(-8,-8)

(-8,8)

(-2,0)

16 of 34

A different way

16

(16,0)

(-8,-8)

(-8,8)

(-2,0)

17 of 34

Important: half of your coordinates should be negative!

  • The Floater class was written with the assumption that (0,0) is the center of rotation and the ship is pointing to the RIGHT (0 °)
  • I recommend sketching your design on graph paper

17

(16,0)

(-8,-8)

(-8,8)

(-2,0)

18 of 34

Spaceship design worksheet�(optional)

18

19 of 34

Hyperspace

  • The assignment requires a hyperspace feature
  • There is no requirement for any fancy visual effects, hyperspace just needs to stop the ship, and give it a new random position and direction

Spaceship bob = new Spaceship();

//other code (setup, draw) not shown

public void keyPressed(){

if(key == 'h'){

bob.??;

}

}

19

20 of 34

Is this OK?

  • Since we need to stop the ship, we’ll need to set myXspeed and myYspeed to zero

Spaceship bob = new Spaceship();

public void keyPressed()

{

if(key == 'h')

{

bob.myXspeed = 0;

//other Java code not shown

}

}

20

21 of 34

NO! myXspeed is protected

  • So, what is the right way to set myXspeed to zero instead of this ?

Spaceship bob = new Spaceship();

public void keyPressed()

{

if(key == 'h')

{

bob.myXspeed = 0;

//other Java code not shown

}

}

21

22 of 34

You could create a setter like setXspeed()

  • Since it is a member of the Spaceship class it has access to myXspeed

class Spaceship extends Floater

{

public SpaceShip() {

//construtor code not shown

}

public void setXspeed(double x) {

myXspeed = x;

}

//other functions not shown

}

22

23 of 34

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

24 of 34

Either way is fine

  • Then use the setter function setXspeed() in keyPressed

Spaceship bob = new Spaceship();

public void keyPressed()

{

if(key == 'h')

{

bob.setXspeed(0); //OK!

//or

bob.hyperspace(); //OK!

//other Java code not shown

}

}

24

25 of 34

Asteroids Pt 1: Star class

25

  • Your program should have a Star class that creates the background of the game

26 of 34

If you wrote this Star class on the AP, they would deduct points. Why?

26

27 of 34

Like every class on the AP exam the Star class needs to be encapsulated

27

28 of 34

Creating an Array of Stars

  • There is a common mistake in this code, do you see it?

28

29 of 34

Creating an Array of Stars

  • Every time we draw the screen we are creating 200 new Stars in new positions

29

30 of 34

This is what creating new Stars in new positions everytime you draw the screen looks like

30

31 of 34

Creating an Array of Stars

  • A better idea would be to create 200 new Stars once in setup() using an Array

31

32 of 34

Creating an Array of Stars

  • Then we would show() the same 200 Stars everytime we draw() the screen

32

33 of 34

Adding instructions in index.html

  • A requirement for this assignment is to put the instructions on how to control the spaceship in the webpage
  • One place would be the footer of index.html

33

34 of 34

Adding instructions in the html

  • <br> is the html code (called a tag) to insert a line break

34