xxxxxxxxxx
111
let score = 0;
const lives = 3;
let gameStart = false;
class Ant {
constructor(start) {
this.x = start;
this.y = 0;
this.size = 40;
this.speed = 6;
this.time = round(random(60));
this.fill = "#34f5e";
this.n = noise(3);
}
displayAnt() {
fill(this.fill);
image(ant, this.x, this.y, this.size, this.size)
}
moveAnt() {
if (mouseX>this.x && mouseX<this.x+this.size && mouseY>this.y &&mouseY<this.y+this.size) {
this.fill = "#ff5050";
this.y += this.speed;
if (mouseIsPressed) {
this.speed = 0;
this.size=0;
score++
}
} else {
let randList = [-this.n, this.n];
this.x+=random(randList);
this.y += this.speed + random(randList);
}
}
}
let ants = [];
function setup() {
//image loading
bg = loadImage('wood.jpeg');
ant = loadImage('ant1.png');
blood = loadImage('blood8.png');
createCanvas(400, 500);
ants.push(new Ant(random(40, width)-40));
}
function draw() {
background(220);
startScreen();
game();
}
function startScreen() {
textAlign(CENTER);
let title = "ANT SMASHER";
textSize(40);
text(title, (width-textWidth(title))/2, height/2-110, textWidth(title))
textSize(14);
let startText = "PRESS ANYWHERE TO START";
text(startText, (width-textWidth(startText))/2, height/2, textWidth(startText))
textAlign(LEFT);
if (mouseIsPressed) {
gameStart = true;
}
}
function game() {
if (gameStart == true) {
background(bg);
if (frameCount % 50==0) {
ants.push(new Ant(random(40, width)-40))
}
fill(0);
text("Score: " + score, 20, 20, 100); //score
let stopwatch = round(millis()/1000); //time
text(stopwatch, width-textWidth(stopwatch)-20, 20, 60);
for (let i=0; i<ants.length; i++) {
ants[i].moveAnt();
ants[i].displayAnt();
}
if (stopwatch >= 20) {
gameOver();
}
}
}
function gameOver() {
gameStart = false;
background(255);
textSize(40);
let gOver = "GAME OVER";
text(gOver, (width-textWidth(gOver))/2, height/2-110, textWidth(gOver))
}