private Ship theShip; protected AsteroidHandler roidHandler; private final int w = 450; private final int h = 280; private int startLives = 3; private int lives; private float startTime; private boolean gameOver; private boolean gameWon; public int shotsFired; // public due to how Processing handles classes (would otherwise be a static member of Ship) private int noAsteroids = 1; private int level = 1; // debug private int textCnt = 1; boolean wait = true; void setup() { size(w, h); rectMode(CENTER); gameOver = false; gameWon = false; frameRate(30); startTime = millis(); shotsFired = 0; lives = startLives; roidHandler = new AsteroidHandler(noAsteroids); // no. of asteroids theShip = new Ship(roidHandler); } void draw() { if(!gameOver && !gameWon) { background(50); // GAME STATE // check if game if won if(roidHandler.getNoRoids() == 0) { gameWon(); } // SHIP // check if ship is hit by a roid if( theShip.isTouchable() && roidHandler.whichRoid(theShip.getX(), theShip.getY(), theShip.getW()/2) != null) { if(--lives < 0) { gameOver(); } else { // player died but has extra lives. continue. delay(500); theShip = null; theShip = new Ship(roidHandler); } } theShip.move(); theShip.render(); roidHandler.renderRoids(); renderDisplay(); // extra lives } else { // game won or lost; // wait for restart if(mousePressed) { gameOver = false; gameWon = false; setup(); } } } void mousePressed() { theShip.burnEngine(true); } void mouseReleased() { theShip.burnEngine(false); } // Spacebar shoots missiles. void keyPressed() { if(key==' ') { theShip.fireMissile(); } } void renderDisplay() { stroke(240, 0, 0); int x = 10; int y = 10; for(int i=1; i<=lives; i++) { point(x, y); x += 10; } } void gameOver() { gameOver = true; clearScreen(); noAsteroids = 1; level = 1; PFont font48 = loadFont("ProFont-48.vlw"); PFont font12 = loadFont("ProFont-12.vlw"); textFont(font48); text("GAME OVER", width/2-150, height/2); textFont(font12); text("Click to try again", width/2-150, height/2 + 14); roidHandler.renderRoids(); } void gameWon() { float endTime = millis(); float totalTime = endTime - startTime; String minutes = nf((int) totalTime / 60000, 2); String seconds = nf((int) (totalTime % 60000) / 1000, 2); int livesUsed = startLives - lives; level++; noAsteroids++; gameWon = true; clearScreen(); PFont font48 = loadFont("ProFont-48.vlw"); PFont font12 = loadFont("ProFont-12.vlw"); textFont(font48); text("YOU SURVIVED!", width/2-180, height/2); textFont(font12); text("Extra Lives Used " + livesUsed, width/2-180, height/2 + 50); text("Missiles Fired " + shotsFired, width/2-180, height/2 + 38); text("Level Time " + minutes + ":" + seconds, width/2-180, height/2 + 26); text("Click to play level " + level, width/2-180, height/2 + 74); // theShip.render(); renderDisplay(); } public void clearScreen() { fill(50); stroke(0); rectMode(CORNER); rect(0,0,width,height); rectMode(CENTER); fill(200); } // print debug values public void logthis(String txt) { PFont font12 = loadFont("ProFont-12.vlw"); textFont(font12); text(txt, 0, 12 * textCnt++); }