Lab 07 - Space Ship
Assignments
Using the starter code we went through in class (shown below), modify the spaceship game in the following ways:
-
Add the ability to move the ship left and right, in a similar way to how the upward thrust works.
-
Add some visual display for bottom, left, and right thrusters that appear in the correct locations only when the ship is applying thrust.
-
Have the
Main
class add a fuel label in the upper-left hand corner that says: "Fuel: 0" -
Add a
getter
for the ship's fuel. Have theMain
class get the fuel from the ship after calling theadvance()
method, and update the fuel label with the new fuel amount. -
Make it so that once the ship runs out of fuel, it can no longer thrust. (Gravity should still apply)
-
Make it so if the ship falls below the ground (the height of the screen), it stops moving and "GAME OVER" is displayed in the center of the screen.
Some Bonus Point Opportunities:
-
Create a rectangular platform on the ground. If the ship lands on that platform with a downward speed less than 30px per frame, turn off gravity and display a "You Win" message.
-
Make your ship look cool.
-
Style the background and ground to make it look like we're in space.
-
Change the game loop in run so that it can be ended by the user pressing a certain key.
-
Make it so the ship explodes if it hits the ground too hard.
Starter Code
Main
import acm.program.GraphicsProgram;
import java.awt.event.KeyEvent;
public class Main extends GraphicsProgram {
// Ship instance variable
Ship s;
// Constants to control game physics
public static final double GRAVITY = 0.02;
public static final double PAUSE_TIME = 10;
public static final double VERTICAL_THRUST = .5;
public void init() {
// Create the ship and save it as an instance variable
// Tell it about the canvas so it can draw itself
s = new Ship(this.getGCanvas());
// Start listening for keyboard presses
addKeyListeners(this);
}
public void run() {
// Begin the game loop
while(true) {
// Apply gravity
s.applyGravity(GRAVITY);
// Move time forward
s.advance();
// Brief pause between frames
pause(PAUSE_TIME);
}
}
// This function is called whenever the user presses a key
public void keyPressed(KeyEvent e) {
// Handle the DOWN thrusters
if(e.getKeyCode() == KeyEvent.VK_DOWN) {
s.applyThrust(0, -VERTICAL_THRUST);
}
}
public static void main(String[] args) {
new Main().start(args);
}
}
Ship Class
// Make sure this file has the same package declaration above as your Main class does
import acm.graphics.GCanvas;
import acm.graphics.GRect;
import java.awt.Color;
public class Ship {
// Ship fuel
int fuel;
// Ship position
double x;
double y;
// Ship inertia
double dx;
double dy;
// Ugly ship graphics
GRect body;
// The world the ship belongs to
GCanvas world;
// Ship constructor, executed when the ship is first created
public Ship(GCanvas canvas) {
// Set initial values
fuel = 100;
dx = 0;
dy = 0;
x = 250;
y = 300;
world = canvas;
// Create the visual representation of the ship and add it to the world
body = new GRect(x, y, 100, 100);
body.setFillColor(Color.RED);
body.setFilled(true);
world.add(body);
}
public void advance() {
// Move time forward by applying inertia to the ship's position
body.move(dx, dy);
}
public void applyThrust(double x_thrust, double y_thrust) {
// Add force to the ship, affecting its inertia
dx += x_thrust;
dy += y_thrust;
// Use up fuel each time we thrust
fuel--;
}
public void applyGravity(double amount) {
// Gravity affecting our downward inertia
dy += amount;
}
}
Submission
To submit your assignment, follow the instructions for code submission. Make sure you submit your entire project so that both Java files are included.