CSC 213 — Programming Fundamentals

Lab 07 - Space Ship

Assignments

Using the starter code we went through in class (shown below), modify the spaceship game in the following ways:

Some Bonus Point Opportunities:

Rocket Game

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.