xxxxxxxxxx
375
//ANT SMASHER
//Resizeable & with Difficulty Settings
let score = 0;
let highScore = 0;
let lives = 3;
let gameStartTime, ant, blood, zigAnt, life;
let title = "Ant Smasher"
let ants = [];
//basic settings
let gameSpeed = 2.5;
let gameNoise = 1;
let gameState = 0; //0 for startscreen, 1 for game, 2 for game over
let gameTheme = 0; //0 for ant, 1 for rat, 2 for cat
function setup() {
//cursor
cursorSwat = loadImage('assets/swat.png');
//music
squish = loadSound('sound/splat.mp3');
over = loadSound('sound/gameOver.mp3');
soundtrack = loadSound('sound/urban.mp3');
//backgrounds
startBg = loadImage('assets/final_bg.png')
bg = loadImage('assets/finalwood.png');
endBg = loadImage('assets/endbg.png');
candy = loadImage('assets/candy.png');
//fonts
titleFont = loadFont('p22.otf');
//sprites
blood = loadImage('assets/blood8.png');
life = loadImage('assets/redheart.png');
dead = loadImage('assets/blackheart.png')
zigAnt = loadImage('assets/zigant.png');
ant = loadImage('assets/ant1.png');
cat = loadImage('cat/cat.png');
cat2 = loadImage('cat/cat2.png');
rat = loadImage('rat/remy.png');
rat2 = loadImage('rat/dad.png');
//settings
gear = loadImage('assets/gear.png');
//canvas - RESIZEABLE
createCanvas(450, 600);
}
//creates an ant object
class Ant {
constructor(img, size, speed, n) {
this.x = random(50, width)-50; //x position
this.y = 0; //y position
this.size = size;
this.speed = speed;
this.img = img;
this.n = n; //noise
this.dead = false;
this.timeDied = 100000;
}
display() {
image(this.img, this.x, this.y, this.size, this.size)
}
move() {
//if thing pressed
if (mouseX>this.x && mouseX<this.x+this.size && mouseY>this.y &&mouseY<this.y+this.size && mouseIsPressed) {
this.speed = 0; //stop moving
image(blood, this.x, this.y, this.size, this.size) //blood splat
this.dead=true; //mark dead
squish.play(); // audio
score++
} else {
let randList = [-this.n, this.n];
this.x+=random(randList);
this.y += this.speed + random(randList);
}
}
}
function draw() {
if (gameState==0) startScreen();
else if (gameState==1) game();
else if (gameState==2) gameOver();
else if (gameState==3) settings();
}
//HELPER FUNCTIONS
//restarts games and resets ant list, score time, lives
function restartGame() {
now = millis();
score = 0;
ants=[];
lives = 3;
}
// returns time in millis since game started
function millisInGame() {
// Subtract when the game started from current time
return millis() - gameStartTime;
}
//show remaining lives and decrease candy stack
function displayLives() {
//this is a bit messy but it works; will implement a more efficient method eventually
//variables for heart
let leftMargin = width/2-60;
let topMargin = 12;
let lifeXPos = 28;
let size = 24;
if (lives==3) { //3 red hearts
image(life, leftMargin + lifeXPos, topMargin, size, size);
image(life, leftMargin + lifeXPos*2, topMargin, size, size);
image(life, leftMargin + lifeXPos*3, topMargin, size, size);
//original candy stack
image(candy, 0, height-100, width, 200);
} else if (lives==2) { //2 red, 1 black heart
image(life, leftMargin + lifeXPos, topMargin, size, size);
image(life, leftMargin + lifeXPos*2, topMargin, size, size);
image(dead, leftMargin + lifeXPos*3, topMargin, size, size);
//decrease candy stack
image(candy, 0, height-80, width, 200);
} else if (lives==1) { //1 red, 2 black hearts
image(life, leftMargin + lifeXPos, topMargin, size, size);
image(dead, leftMargin + lifeXPos*2, topMargin, size, size);
image(dead, leftMargin + lifeXPos*3, topMargin, size, size);
//decrease candy stack
image(candy, 0, height-60, width, 200);
} else { //3 black hearts
image(dead, leftMargin + lifeXPos, topMargin, size, size);
image(dead, leftMargin + lifeXPos*2, topMargin, size, size);
image(dead, leftMargin + lifeXPos*3, topMargin, size, size);
}
}
//all buttons use this
function button(string, color, x, y, w, h) {
//if button clicked, do sth
if (mouseX>x && mouseX<x+w && mouseY>y &&mouseY<y+h) {
color = "#F6D047"; //hover color
//when pressed
if (mouseIsPressed) {
if (string=="start") { //start
restartGame();
gameState = 1;
} else if (string == "play again") { //replay
gameState = 0;
} else if (string == "easy") { //game modes
gameSpeed = 1;
gameNoise = 1;
gameState = 0;
} else if (string == "medium") {
gameSpeed = 2.5;
gameNoise = 1.5;
gameState = 0;
} else if (string == "hard") {
gameSpeed = 4;
gameNoise = 2;
gameState = 0;
} else if (string == "ant") {
gameTheme = 0;
gameState = 0;
} else if (string == "rat") {
gameTheme = 1;
gameState = 0;
} else if (string == "cat") {
gameTheme = 2;
gameState = 0;
}
}
}
//button rect
fill(color);
stroke(1.5);
rect(x, y, w, h, 40) //40 is border rounding
//button text
fill(0);
textFont(titleFont);
noStroke();
textSize(20);
text(string, x+w/2-textWidth(string), y+h/2+5,textWidth(string)*2);
//end of button
}
//title text
function titleText(title, size, h) {
textAlign(CENTER);
textFont(titleFont);
textLeading(56);
textSize(size);
text(title, (width-textWidth(title))/2, height/2-h, textWidth(title))
}
//body text
function bodyText(body, size, h) {
textAlign(CENTER);
textFont(titleFont);
textSize(size);
text(body, (width-textWidth(body))/2-20, height/2-h, textWidth(body)+40)
}
//GAME STATES
//start screen
function startScreen() {
background(startBg);
titleText("Ant Smasher", 60, 80)
//settings in upper right corner
image(gear, width-40 , 20, 20, 20)
if (mouseX>width-40 && mouseX<width-20 && mouseY>20 &&mouseY<40 && mouseIsPressed) {
gameState = 3; //go to settings
}
//start button
button("start", "#FFEB5A", width/2-100, height/2+10, 200, 60);
//cursor swatter
image(cursorSwat, mouseX-10, mouseY-10, 50, 50);
}
let main;
let bigMain;
//actual game
function game() {
if (gameTheme==0) {
main = ant;
bigMain = zigAnt;
} else if (gameTheme==1) {
main = rat;
bigMain = rat2;
} else if (gameTheme==2) {
main = cat;
bigMain = cat2;
}
if (!soundtrack.isPlaying()) soundtrack.play(); //play music if not playing already
background(bg);
if (frameCount % 60==0) { //new ant every second
ants.push(new Ant(main, 40, gameSpeed, noise(gameNoise)));
}
if (frameCount % 110==0) { //new big ant every 1.9 seconds
ants.push(new Ant(bigMain, 60, gameSpeed+1.5, noise(gameNoise+2)));
}
//score
fill(0);
textSize(18);
textAlign(LEFT);
text("Score: " + score, 20, 30, 120); //score
//remaining lives
displayLives();
//timer
textAlign(RIGHT);
let timer = round(millisInGame()/1000);
text(timer, width-textWidth(timer)-70, 30, 60);
//display ants
for (let i=ants.length-1; i>0; i--) {
//move and display as normal
ants[i].move();
ants[i].display();
//splice and subtract life if out of bounds
if (ants[i].y>=height) {
lives--;
ants.splice(i, 1);
}
//splice if dead
if (ants[i].dead==true) {
ants.splice(i, 1);
}
}
//if no more lives, game over
//game over sound and stop soundtrack
if (lives<=0) {
gameState=2;
over.play();
soundtrack.stop();
}
//cursor swatter
image(cursorSwat, mouseX-10, mouseY-10, 50, 50);
}
//settings page
function settings() {
background(bg);
titleText("Settings", 48, 170);
//difficulty settings
bodyText("Difficulty", 20, 105);
button("easy", "#FFEB5A", width/2-175, height/2-80, 100, 60);
button("medium", "#FFEB5A", width/2-65, height/2-80, 130, 60);
button("hard", "#FFEB5A", width/2+75, height/2-80, 100, 60);
//difficulty settings
bodyText("Theme", 20, -40);
button("ant", "#FFEB5A", width/2-160, height/2+60, 100, 60);
button("rat", "#FFEB5A", width/2-50, height/2+60, 100, 60);
button("cat", "#FFEB5A", width/2+60, height/2+60, 100, 60);
//cursor swatter
image(cursorSwat, mouseX-10, mouseY-10, 50, 50);
}
//game over
function gameOver() {
background(endBg);
titleText("Game Over", 48, 130);
//score
scoreText = "Your score: " + score;
bodyText(scoreText, 18, 10)
//highscore display and comparison
if (score>highScore) highScore = score;
let highScoreText = "High score: " + highScore;
bodyText(highScoreText, 18, -20)
//play again
button("play again", "#edb424", width/2-100, height/2+60, 200, 60);
//cursor swatter
image(cursorSwat, mouseX-10, mouseY-10, 50, 50);
}