float x; float y; float x1; float y1; float rad2 = 40; float rad3 = 10; float xPos; float yPos; int fire; float fireReady = 0; boolean moveUp; boolean moveLeft; boolean moveRight; boolean moveDown; boolean keys; boolean active = true; int spawn = 0; int score = 0; PFont titleFont; PFont basicFont; PFont otherFont; PFont fourthFont; int INTRO = 1; int GAME = 2; int GAMEOVER = 3; int gameStage = INTRO; player player; Bullet bullet [] = new Bullet [2]; Enemy enemy[] = new Enemy[1]; Enemy2 enemy2[] = new Enemy2[1]; PowerUps powerUps; void setup() { size(600, 600); smooth(); xPos = width/2; yPos = height/2; player = new player(); powerUps = new PowerUps(); for (int i=0; i < bullet.length; i++) { bullet[i] = new Bullet(player.xPos, player.yPos); } for (int i=0; i < enemy.length; i++) { enemy[i] = new Enemy(); } for (int i=0; i < enemy2.length; i++) { enemy2[i] = new Enemy2(); } basicFont = createFont("Courier", 14); titleFont = createFont("Helvetica", 40); otherFont = createFont("Helvetica", 20); fourthFont = createFont("Helvetica", 14); } void draw() { if (gameStage == INTRO) intro(); if (gameStage == GAME) game(); if (gameStage == GAMEOVER) gameOver(); } void intro() { background(49, 157, 222); // add an image to title screen later fill(255); textAlign(CENTER); textFont(titleFont); text("Shoot Gang!", width/2, height/3); textFont(otherFont); text("How to play:", width/2, height/3*1.5); textFont(fourthFont); text("arrows keys to move,", width/2, height/3*1.7); text("space bar to shoot!", width/2, height/3*1.9); textFont(basicFont); text("Click anywhere to continue!", width/2, height/2*1.5); } void game() { background(0); fill(255); ellipseMode(CENTER); bullet[fire].displayShot(); if (y1 < 0 || active == false) { Bullet newBullet = new Bullet(player.xPos, player.yPos); bullet = (Bullet[]) append(bullet, newBullet); } powerUps.update(); if (powerUps.appear) { powerUps.display(); if (dist(player.xPos, player.yPos, powerUps.x2, powerUps.y2) < (player.rad/2)+(powerUps.rad5/2)) { powerUps.activate(); } } player.draw(); //Enemy for (int i=0; i < enemy.length; i++) { enemy[i].display(); enemy[i].move(); } spawn++; if (spawn % 200 == 0) { Enemy newEnemy = new Enemy(); enemy = (Enemy[]) append(enemy, newEnemy); } // Stronger Enemy for (int i=0; i < enemy2.length; i++) { enemy2[i].display(); enemy2[i].move(); } if (spawn % 500 == 0) { Enemy2 newEnemy2 = new Enemy2(); enemy2 = (Enemy2[]) append(enemy2, newEnemy2); } drawScore(); } void gameOver() { textFont(otherFont); fill(255); textAlign(CENTER); text("TOO BAD, YOU LOST YOUR MACHINE!", width/2, height/2); drawScore(); } void drawScore() { textAlign(LEFT); fill(0, 128, 0); textFont(basicFont); text("SCORE: " + score, 20, 20); } void keyPressed() { if (key == CODED) if (keyCode == UP) moveUp = true; if (key == CODED) if (keyCode == LEFT) moveLeft = true; if (key == CODED) if (keyCode == DOWN) moveDown = true; if (key == CODED) if (keyCode == RIGHT) moveRight = true; } void keyReleased() { if (key == CODED) if (keyCode == UP) moveUp = false; if (key == CODED) if (keyCode == LEFT) moveLeft = false; if (key == CODED) if (keyCode == DOWN) moveDown = false; if (key == CODED) if (keyCode == RIGHT) moveRight = false; } void mousePressed() { if(gameStage == INTRO || gameStage == GAMEOVER){ gameStage = GAME; score = 0; enemy = new Enemy[1]; enemy[0] = new Enemy(); enemy2 = new Enemy2[1]; enemy2[0] = new Enemy2(); } } class Bullet { float xPos; float yPos; float speed; float x; float y; boolean keys = false; Bullet(float _xPos, float _yPos) { x1 = _xPos; y1 = _yPos; active = true; if (powerUps.useBSpeed == true) speed = 1; else speed = 7.5; } void displayShot() { display(); space(); } void display() { if (active == true) { if (keys == true) { ellipse(x1, y1, rad3, rad3); y1 = y1 - speed; if (y1 < 0 || active == false) fire ++; } } else keys = false; } void space() { if (keyPressed) { if (key == ' ' && keys == false) { keys = true; x1 = player.xPos; y1 = player.yPos; } } } void keyReleased() { if (key == ' ' && keys == true) { keys = false; } } } class Enemy { float x; float y; float rad2; float SpeedX; float SpeedY; int damage; boolean overlap = false; Enemy() { x = random(25, width-25); y = random(25, height-25); if (powerUps.useShrink == true) rad2 = 25; else rad2 = 40; SpeedX = random(-3, 3); SpeedY = random(-3, 3); } void display() { if (overlap == false) { fill(200); ellipse(x, y, rad2, rad2); if (dist(x1, y1, x, y) < (rad3/2)+(rad2/2)) { score += 5; if (powerUps.useDamage == true) damage += 2; else damage += 1; if (damage > 1) { score += 20; overlap = true; } active = false; } // Death (Change gamestate) if (dist(player.xPos, player.yPos, x, y) < (player.rad/2)+(rad2/2)) { gameStage = GAMEOVER; fill(255, 0, 0, 120); rect(0, 0, width, height); } } } void move() { if (x > width - 20 || x < 20) SpeedX *= -1; if (y > height - 20 || y < 20) SpeedY *= -1; x = x + SpeedX; y = y + SpeedY; } } class Enemy2 { float x; float y; float rad; float SpeedX; float SpeedY; int damage; boolean overlap = false; Enemy2() { x = random(35, width - 35); y = random(35, height - 35); if (powerUps.useShrink == true) rad = 35; else rad = 65; SpeedX = random(-2, 2); SpeedY = random(-2, 2); } void display() { if (overlap == false) { fill (75); ellipse(x, y, rad, rad); if (dist(x1, y1, x, y) < (rad3/2)+(rad/2)) { score += 10; if (powerUps.useDamage == true) damage += 2; else damage += 1; if (damage > 2) { score += 50; overlap = true; } active = false; } // Death (Change gamestate) if (dist(player.xPos, player.yPos, x, y) < (player.rad/2)+(rad/2)) { gameStage = GAMEOVER; fill(255, 0, 0, 120); rect(0, 0, width, height); } } } void move() { if (x > width - 32.5 || x < 32.5) SpeedX *= -1; if (y > height - 32.5 || y < 32.5) SpeedY *= -1; x = x + SpeedX; y = y + SpeedY; } } // Most of the powerup class taken from the Dodgeball example class PowerUps { float x2; float y2; float rad5; boolean appear; boolean useShrink; boolean useDamage; boolean useSpeed; boolean useBSpeed; Timer timer; int power; PowerUps() { x2 = random(width); y2 = random(height); appear = false; useShrink = false; useDamage = false; useSpeed = false; useBSpeed = false; rad5 = 30; timer = new Timer(5000); power = (int) random(1, 3); } void update() { if (timer.finished()) { if (useShrink == true || useDamage == true || useSpeed == true || useBSpeed == true) { useShrink = false; useDamage = false; useSpeed = false; useBSpeed = false; appear = false; } else { appear = !appear; x2 = random(width); y2 = random(height); } timer.reset(); } } void display() { ellipse(x2, y2, rad5, rad5); } void activate() { power = (int) random(1, 6); if (power == 1) useShrink = true; if (power == 2) useDamage = true; if (power == 3) useSpeed = true; if (power == 4) useBSpeed = true; appear = false; timer.reset(); } void reset() { appear = false; useShrink = false; useDamage = false; useSpeed = false; useBSpeed = false; timer.reset(); } } // Timer class taken from Dodgeball example class Timer { long startTime; int timeout; Timer(int timeout) { this.timeout = timeout; this.startTime = millis(); } long elapsed() { return millis() - startTime; } boolean finished() { if (elapsed() > timeout) return true; return false; } void reset() { this.startTime = millis(); } void reset(int newTimeout) { this.reset(); timeout = newTimeout; } } class player { float xPos; float yPos; float tempX; float tempY; float rad; float speed; player() { xPos = width/2; yPos = height/3*2; rad = 50; speed = 3; } void draw() { if (powerUps.useSpeed == true) { if (moveUp == true) yPos = yPos - 6; if (moveLeft == true) xPos = xPos - 6; if (moveDown == true) yPos = yPos + 6; if (moveRight == true) xPos = xPos + 6; } else { if (moveUp == true) yPos = yPos - speed; if (moveLeft == true) xPos = xPos - speed; if (moveDown == true) yPos = yPos + speed; if (moveRight == true) xPos = xPos + speed; } constrain(xPos, 0, width); constrain(yPos, 0, height); ellipse(xPos, yPos, rad, rad); xPos = constrain(xPos, 0 + 25, width - 25); yPos = constrain(yPos, 0 + 25, height - 25); } }