3.17k likes | 3.19k Views
Recreate the classic Asteroids arcade game in PowerPoint through a series of assignments. Learn to model space objects like space ships, asteroids, UFOs, and bullets. Understand the Floater class for floating objects' movements, rotations, and rendering.
E N D
New Asteroids PowerPoint • We’ll recreate the Asteroids arcade game over the next four assignments
Designing an Asteroids game • What objects do we need to model? • What do they have in common?
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 • space ships • asteroids • bullets • UFOs (optional)
Designing an Floater class • What do all objects that float in space do?
Designing an Floater class • What do all objects that float in space do? • Move • Turn (Rotate) • Get drawn or show
Designing an Floater class • What do all objects that float in space do? • Move • Turn (Rotate) • Get drawn or show • What do all objects that float in space have?
Designing an Floater class • What do all objects that float in space do? • Move • Turn (Rotate) • 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)
A sample Spaceship program • You may find this program helpful in understanding how the Floater class works
protected member variables in Floater protected double myCenterX, myCenterY; //holds center coordinates protected double myDirectionX,myDirectionY; //holds x and y coordinates of the vector //for direction of travel protected double myPointDirection; //holds current direction the floater is pointing //in degrees
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 intmyColor;
extending the Floater class • What functions would you need to write to extend Floater?
extending the Floater class • What functions would you need to write to extend Floater? • You would write a constructor • You would also write any “unfinished” abstract methods
Constructing a Spaceship (-8,-8) (16,0) (-8,8)
Constructing a slightly fancier Spaceship (-8,-8) (-2,0) • How would the constructor of my slightly fancier Spaceship be different? (16,0) (-8,8)
Constructing a slightly fancier Spaceship (-8,-8) (-2,0) (16,0) (-8,8)
A different way (-8,-8) (-2,0) (16,0) (-8,8)
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 (-8,-8) (-2,0) (16,0) (-8,8)
Imagine you are on a team • You are part of a programming team that is making a game • Some other team members wrote the Floater class • You are responsible for the Ship and Star classes • Don’t rewrite Floater!
abstract classes • Sometimes a class is used only for inheritance, you don't actually make objects of that class • The Floater class is an example: There are no actual "floaters" in an asteroids game, only instances of classes that extendsFloater • An abstract class is used only for inheritance
abstract means unfinished • Inside the Floater class definition are many abstract methods. They need to be finished in ay class that extends Floater abstract public void setX(int x); abstract public intgetX(); abstract public void setY(int y); abstract public intgetY(); abstract public void setDirectiox(double x); abstract public double getDirectiox(); //and so on. . .
inline methods • Good Style: If a method only needs one line of code, write it inline: public void setX(int x){myCenterX = x;} public intgetX(){return (int)myCenterX;} //instead of public void setX(int x) { myCenterX = x; }
Moving a Spaceship myCenterX 320 myCenterY 240 myDirectionX 2 myDirectionY 3 myPointDirection -50
Moving a Spaceship myCenterX 320 myCenterY 240 myDirectionX 2 2 myDirectionY 3 myPointDirection -50 3
Moving a Spaceship myCenterX 322 myCenterY 243 myDirectionX 2 2 myDirectionY 3 myPointDirection -50 3
Moving a Spaceship public void move () { //Moves the floater towards the coordinates //myDirectionX and myDirectionY //move the floater in the current direction of travel myCenterX += myDirectionX; myCenterY += myDirectionY; //wrap around screen if(myCenterX >width){ myCenterX = 0; } else if (myCenterX<0){ myCenterX = width; } if(myCenterY >height){ myCenterY = 0; } else if (myCenterY < 0){ myCenterY = height; } }
Accelerating a Spaceship myCenterX 320 myCenterY 240 Accelerate(.1) (.1)*sin(-50)=.064 myDirectionX 2 + .064 (.1)*cos(-50)=.076 myDirectionY 3 - .076 myPointDirection -50
Accelerating a Spaceship public void accelerate (double dAmount) { //Accelerates the ship in //the direction it is pointing //(myPointDirection) //convert the current direction the ship is //pointing to radians double dRadians =myPointDirection*(Math.PI/180); //change coordinates of direction of travel myDirectiox += ((dAmount) * Math.cos(dRadians)); myDirectioy += ((dAmount) * Math.sin(dRadians)); }
Rotating a Spaceship void turn(intnDegreesOfRotation) { myPointDirection+=nDegreesOfRotation; } turn(60) myCenterX 320 myCenterY 240 myDirectionX 2 myDirectionY 3 myPointDirection -50 10
Practice Quiz Question Given the following values • what direction is the ship pointing (e.g. up, down, left and/or right) • What direction is the ship moving? • what will the position of the Spaceship be after it moves once? • after it moves twice? You may find this program helpful. myCenterX 200 myCenterY 200 -5 myDirectionX myDirectionY 5 myPointDirection 180
abstract classes are unfinished • An abstract class is an incomplete class that is used only for inheritance
abstract classes are unfinished • An abstract class is an incomplete class that is used only for inheritance • Here I’ve got an abstract class with an unfinished abstract setX function abstract class WhatsIt { protected intmyX; public WhatsIt() {myX = 2;} public intgetX() {return myX; } abstract public void setX(int x); }
Practice Quiz Question: Copy and paste this program into Processing and finish the abstract method public void setup() { Thingy bob = new Thingy(); bob.setX(100); System.out.println("bobs x is " + bob.getX()); } abstract class WhatsIt { protected intmyX; public WhatsIt() {myX = 2;} public intgetX() {return myX; } abstract public void setX(int x); } class Thingy extends WhatsIt { public Thingy() {myX = 3;} //write one line of code here }
The solution public void setup() { Thingy bob = new Thingy(); bob.setX(100); System.out.println("bobs x is " + bob.getX()); } abstract class WhatsIt { protected intmyX; public WhatsIt() {myX = 2;} public intgetX() {return myX; } abstract public void setX(int x); } class Thingy extends WhatsIt { public Thingy() {myX = 3;} public void setX(int x){myX = x;} }
Hyperspace • Your spaceship program will need 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 positionand direction Spaceship bob = new Spaceship(); public void keyTyped() { if(key == 'h') { bob.??; } }
Is this OK? • Since we need to stop the ship, we’ll need to set myDirectionX and myDirectionY to zero Spaceship bob = new Spaceship(); public void keyTyped() { if(key == 'h') { bob.myDirectionX = 0; //other Java code not shown } }
NO! myDirectionX is protected • So, what is the right way to set myDirectionX to zero instead of this ? Spaceship bob = new Spaceship(); public void keyTyped() { if(key == 'h') { bob.myDirectionX = 0; //other Java code not shown } }
use setDirectionX() • Use the setter function setDirectionX() ! Spaceship bob = new Spaceship(); public void keyTyped() { if(key == 'h') { bob.setDirectionX(0); //other Java code not shown } }
What's the difference betweeninheritance (extends) andinterfaces (implements)? • When a class inherits from another class, it inherits everything the super (or base) class has (except constructors) class Spaceship extends Floater • The Spaceship class now has all methods and variables of the Floater class • When a class implements an interface, it has only the few methods that are listed in the interface class Cow implements Animal
Why Java doesn’t allow multiple inheritance • Let’s say we wanted to make a Bird-Cow class Bird-Cow extends Chick, Cow • Both Chick and Cow class have getSound methods • How does Java decide which method and what sound our Bird-Cow makes? "moo", "cluck", or "muck"? • Interfaces let us use a "weaker form" of inheritance that avoids these issues
Inheritance or an Interface? • Let’s say we’re creating a game where a character can shoot rocks from a slingshot a short distance or arrows from a bow a long distance • We’re going to model those objects with Rock and Arrow classes • Then we’ll group all those objects together in one array of things that can be shot. • Should we group instances of Rock and Arrow together with inheritance or an interface?
Rocks and Arrows • Rocks and Arrows are very different • If we were writing a Rock class, it wouldn't make ay sense to extend a Arrow class • They do, however, have one thing in common: both can be shot • If I wanted to make an array of shootable objects, it would make sense to make a Shootable interface
Rocks and Arrows interface Shootable { public void shoot(); } class Rock implements Shootable { //Java code not shown class Arrow implements Shootable { //Java code not shown Shootable [] stuff = new Shootable[2]; stuff[0] = new Arrow(); stuff[1] = new Rock(); for(intnI=0; nI<stuff.length; nI++) stuff[nI].shoot();
Rocks and Arrows • Note: Declaring an array of type Shootableis OK Shootable[] stuff = new Shootable[2]; • Trying to make a new Shootable()is NOT OK stuff[0] = new Shootable() //ERROR! • An interface lets us group together a bunch of otherwise different objects
Practice Quiz Question #2: Copy and paste this program into Processing and finish the abstract method public void setup() { SubClass bob = new SubClass(); bob.setX(27); System.out.println("bobs x is " + bob.getX()); } class SubClass extends SuperClass { public SubClass() {myX = 7;} //write one line of code here } abstract class SuperClass { protected intmyX; public SuperClass() {myX = 23;} abstract public intgetX(); public void setX(int x){myX = x;} }
Quiz Tomorrow • Covers through slide 51 of Asteroids PowerPoint. • abstract classes and functions (like practice quiz questions slide #33, 45 and 49) • public, private and protected, Encapsulation, Data Hiding and the encapsulation worksheet • Inheritance, extends and the inheritance worksheet. • Interfaces, implements. • Differences and similarities between interfacesand inheritance. • Previous Topics: • Classes, constructors and class members vs. local variables. • Identify (and fix) common interface mistakes • Important Vocabulary: • Client Programmers: Other programmers on your team who use your code, • Class members: The variables and functions (also called data and methods) that belong to a class, • Mutator Function: A "setter" function that assigns (makes equal to) the value of a private (or protected) member variable, • Accessor Function: A "getter" function that returns the value of a private (or protected) member variable
public member variables • 99% of the time, member variables are private and functions are public • Sometimes, though, you might do it the other way around • Constants are “locked” variables • It’s fine to make a make constant public because it can’t be changed or “messed up” publicfinal staticintLIFE_MEANING = 42; • Note that constant variable names are usually CAPITALIZED
private functions • Functions are private when the programmer doesn’t want any client programmers (think “team members”) to use them • A “cheat” might be a good example • For testing purposes, I might make a function that instantly destroys all the asteroids, but I want to restrict who can use it
Practice Quiz Question #3: Copy and paste this program into Processing and finish the DerivedClass public void setup() { DerivedClass bob = new DerivedClass(25); bob.setX(50); System.out.println("bobs x is " + bob.getX()); } abstract class BaseClass { protected intmyX; public BaseClass() {myX = 23;} public BaseClass(int x){myX = x;} public intgetX(){return myX;} abstract public void setX(int x); } class DerivedClass extends BaseClass { //write TWO lines of code here }
Practice Quiz Question #3: The solution (note argument) public void setup() { DerivedClass bob = new DerivedClass(25); bob.setX(50); System.out.println("bobs x is " + bob.getX()); } abstract class BaseClass { protected intmyX; public BaseClass() {myX = 23;} public BaseClass(int x){myX = x;} public intgetX(){return myX;} abstract public void setX(int x); } class DerivedClass extends BaseClass { public DerivedClass(int x){myX = x;} public void setX(int x){myX = x;} }