xxxxxxxxxx
237
let score = 0;
let lives = 3;
let gameStartTime, timeDied;
let ants = [];
let zigAnts = [];
let bees = [];
let gameState = 0; //0 for startscreen, 1 for game, 2 for game over
let ant, bee, blood, zigAnt;
class Ant {
constructor(img, size, speed, n, lr) {
this.x = random(50, width)-50;
this.y = 0;
this.leftright = lr;
this.size = size;
this.speed = speed;
this.time = round(random(60));
this.tint = 255;
this.img = img;
this.n = n;
this.dead = false;
this.timeDied = 0;
}
display() {
tint(255, this.tint);
image(this.img, this.x, this.y, this.size, this.size)
if (this.dead) this.tint-=20;
}
move() {
if (mouseX>this.x && mouseX<this.x+this.size && mouseY>this.y &&mouseY<this.y+this.size && mouseIsPressed) {
this.speed = 0;
image(blood, this.x, this.y, this.size, this.size)
this.dead=true;
score++
} else {
let randList = [-this.n, this.n];
this.x+=random(randList)*this.leftright;
this.y += this.speed + random(randList);
}
}
}
function setup() {
//images
//backgrounds
bg = loadImage('wood.jpeg');
//fonts
titleFont = loadFont('anthrax.ttf');
bodyFont = loadFont('autumn.ttf');
//sprites
zigAnt = loadImage('zigant.png');
bee = loadImage('bee.png');
ant = loadImage('ant1.png');
blood = loadImage('blood8.png');
//canvas
createCanvas(400, 500);
}
function draw() {
if (gameState==0) {
startScreen();
} else if (gameState==1) {
game();
} else if (gameState==2) {
gameOver();
}
}
function startScreen() {
background(220);
textAlign(CENTER);
textFont(titleFont);
let title = "ANT SMASHER";
textLeading(60);
textSize(80);
text(title, (width-textWidth(title))/2, height/2-120, textWidth(title))
//button vars
let btnWidth = 200;
let btnHeight = 60;
let btnX = width/2-btnWidth/2;
let btnY = height/2-btnHeight/2+100;
//button rect
fill("#edb424");
noStroke();
rect(btnX, btnY, btnWidth, btnHeight, 40)
textSize(14);
//button text
fill(0);
textFont(bodyFont);
textSize(20);
let startText = "START";
text(startText, (width-textWidth(startText))/2, btnY+btnHeight/2+5, textWidth(startText));
//if button clicked, do sth
if (mouseX>btnX && mouseX<btnX+btnWidth && mouseY>btnY &&mouseY<btnY+btnHeight && mouseIsPressed) {
restartGame();
gameState = 1;
}
//end of button
}
function restartGame() {
let now = millis(); // time since sketch started
print('Restarting game at ' + now);
gameStartTime = now;
}
// Returns time in millis since game started
function millisInGame() {
// Subtract when the game started from current time
return millis() - gameStartTime;
}
function timeAntDied(i) {
let x = millis();
ants[i].timeDied = x;
}
function game() {
background(bg);
if (frameCount % 60==0) {
ants.push(new Ant(ant, 40, 3, noise(1), 1));
}
if (frameCount % 100==0) {
ants.push(new Ant(zigAnt, 60, 4, noise(50), 4));
}
fill(0);
textSize(20);
text("Score: " + score, 0, 30, 100); //score
text("Lives: " + lives, 100, 30, 100); //score
let x = round(millisInGame()/1000);
text(x, width-textWidth(x)-50, 30, 60);
for (let i=ants.length-1; i>0; i--) {
ants[i].move();
ants[i].display();
if (ants[i].y>=height) {
lives--;
print("SPLICED OUT");
ants.splice(i, 1);
}
if (ants[i].dead==true) {
// print("DIED");
// ants.splice(i, 1);
timeAntDied(i);
print("time of death: " + ants[i].timeDied);
if (ants[i].timeDied + 3000 <= millis()) {
print("SPLICED");
ants.splice(i, 1);
}
}
}
if (lives<=0) {
gameState=2;
}
// if (stopwatch >= 60) {
// gameState = 2;
// }
}
function gameOver() {
background(255);
textSize(60);
textFont(titleFont);
let gOver = "GAME OVER";
text(gOver, (width-textWidth(gOver))/2-40, height/2-110, textWidth(gOver)+80)
//button vars
let btnWidth = 200;
let btnHeight = 60;
let btnX = width/2-btnWidth/2;
let btnY = height/2-btnHeight/2+100;
//button rect
fill("#edb424");
noStroke();
rect(btnX, btnY, btnWidth, btnHeight, 40)
textSize(14);
//button text
fill(0);
textSize(24);
textFont(bodyFont);
let startText = "PLAY AGAIN";
text("Your Score: " + score, (width-textWidth(startText))/2-30, btnY+btnHeight/2-100, textWidth(startText)+60);
text(startText, (width-textWidth(startText))/2-15, btnY+btnHeight/2+5, textWidth(startText)+30);
//if button clicked, do sth
if (mouseX>btnX && mouseX<btnX+btnWidth && mouseY>btnY &&mouseY<btnY+btnHeight && mouseIsPressed) {
score = 0;
ants=[];
lives = 3;
gameState = 0;
}
//end of button
}