import java.util.ArrayList; import processing.core.*; public class D104_Yiting_Long_Assignment3_301183087 extends PApplet{ float pondWidth; float pondHeight; float numOfFish; float pondX; float pondY; boolean up; boolean down; boolean left; boolean right; float playerSize; float maxPlayerSize; int state; int life; int score; PFont font; PFont title; public ArrayList enemyFishList = new ArrayList(); //EnemyFish enemyFishObject; PlayerFish playerFishObject; public void keyPressed() { if (key == CODED) { if (keyCode == UP) up = true; if (keyCode == DOWN) down = true; if (keyCode == LEFT) left = true; if (keyCode == RIGHT) right = true; } } public void keyReleased() { if (key == CODED) { if (keyCode == UP) up = false; if (keyCode == DOWN) down = false; if (keyCode == LEFT) left = false; if (keyCode == RIGHT) right = false; } } public void setup(){ size(600, 400); smooth(); state = 0; font = createFont("Arial", 20, true); title = createFont("Tahoma", 40, true); pondWidth = 580; pondHeight = 380; pondX = 10; pondY = 10; score = 0; life = 5; numOfFish = 10; playerSize = 35; maxPlayerSize = 100; for (int i = 0; i < numOfFish; i++) { PVector randomPosition = new PVector(random(pondX+50, pondWidth-50), random(pondY+70, pondHeight-50)); EnemyFish EnemyFishObject = new EnemyFish(this, randomPosition, random(2, 10)); enemyFishList.add(EnemyFishObject); } PVector playerPosition = new PVector(pondWidth/2, pondHeight/2); playerFishObject = new PlayerFish(this, playerPosition , playerSize); } PlayerFish repawn() { playerSize = 30; PVector playerPosition = new PVector(pondWidth/2, pondY + 50); life--; return new PlayerFish(this, playerPosition , playerSize); } void destroy(int i) { enemyFishList.remove(i); } public void draw() { switch(state){ case 0: startScreen(); break; case 1: playing(); break; case 2: win(); break; case 3: lose(); break; } } void startScreen(){ background(0); fill(106, 168, 222); rect(pondX, pondY, pondWidth, pondHeight); fill(255); textFont(title); textAlign(CENTER); text("Fish! Fish!", pondWidth/2, pondHeight/2); textFont(title, 15); text("Press left mouse button to start game :)",pondWidth/2, pondHeight/2+ 50); if (mousePressed == true && mouseButton == LEFT){ state = 1; } } void playing(){ background(0); fill(106, 168, 222); rect(pondX, pondY, pondWidth, pondHeight); fill(255); textFont(font); text("Scroe: " + score, 50, 50); text("Life: " + life, 420,50); if (right) playerFishObject.moveRight(); if (left) playerFishObject.moveLeft(); if (up) playerFishObject.moveUp(); if (down) playerFishObject.moveDown(); playerFishObject.detectBound(); playerFishObject.drawFish(); for (int i = 0; i < enemyFishList.size(); i++) { EnemyFish currentEnemy = enemyFishList.get(i); currentEnemy.update(); currentEnemy.detectBound(); for (int k = 0; k < enemyFishList.size(); k++) { EnemyFish otherEnemy = enemyFishList.get(k); if (playerFishObject.detectCollision(otherEnemy)) { if (playerFishObject.eat(otherEnemy)) { destroy(k); score += playerFishObject.plus; } else { playerFishObject.drawWaves(); if (frameCount % 5 == 0){ playerFishObject = repawn(); } } } if (i != k) { if (currentEnemy.detectCollision(otherEnemy)) { currentEnemy.bounce(otherEnemy); if (currentEnemy.detectOffScreen()) { destroy(i); } } } } currentEnemy.drawFish(); } //} if (enemyFishList.size() < numOfFish){ if (random(0,2)>1){ EnemyFish EnemyFishObject = new EnemyFish(this, new PVector(pondX+90,random(pondY+50, pondHeight-50)), random(2, 10)); enemyFishList.add(EnemyFishObject); }else{ EnemyFish EnemyFishObject = new EnemyFish(this, new PVector(pondWidth-90, random(pondY+50, pondHeight-50)), random(2, 10)); enemyFishList.add(EnemyFishObject); } } if (life <= 0){ state = 3; } if (playerFishObject.size > maxPlayerSize || score >= 500){ state = 2; } } void win(){ background(0); fill(106, 168, 222); rect(pondX, pondY, pondWidth, pondHeight); fill(255); textFont(title); textAlign(CENTER); text("Congradulation! You Win!", pondWidth/2, pondHeight/2); textFont(title, 15); text("Thank you for playing :)",pondWidth/2, pondHeight/2+ 50); } void lose(){ background(0); fill(247,12,24); rect(pondX, pondY, pondWidth, pondHeight); fill(0); textFont(title); textAlign(CENTER); text("OPPS! YOU LOSE!", pondWidth/2, pondHeight/2); textFont(title, 15); //text(" :)",pondWidth/2, pondHeight/2+ 50); } }