xxxxxxxxxx
var froggy; // player
var swamp; // setting
var speed;
let x = 20;
let y = 20;
var foodBits = []; // Array for food bits
var score = 0;
var time;
var gameOver = false;
var fly; // fb pic
function preload() {
swamp = loadImage("swamp.jpeg");
froggy = loadImage("froggy.png");
fly = loadImage("fly.png");
}
function setup() {
createCanvas(800, 820);
time = setTimeout(ended, 30000);
// specified number of food bits
for (let i = 0; i < 20; i++) {
foodBits.push(new FoodBits());
}
}
function draw() {
if (gameOver == false) {
image(swamp, 0, 0, width, height); //bg image
// display, move, hide etc food (arrays)
for (let i = 0; i < foodBits.length; i++) {
foodBits[i].move();
foodBits[i].display();
foodBits[i].hide();
foodBits[i].checkCollision();
}
drawHero();
push();
textSize(35);
fill(4, 71, 7);
stroke(235, 174, 52);
strokeWeight(5);
text("Food for the day:" + score, 100, 30);
pop();
push();
textSize(80);
fill(4, 71, 7)
stroke(235, 174, 52);
strokeWeight(5);
text("Fly Swat! ", 400, 410);
pop();
//controls
if (keyIsDown(RIGHT_ARROW)) {
x += 10;
}
if (keyIsDown(LEFT_ARROW)) {
x -= 10;
}
if (keyIsDown(DOWN_ARROW)) {
y += 10;
}
if (keyIsDown(UP_ARROW)) {
y -= 10;
}
}
//end page
if (gameOver == true) {
background(0);
fill(235, 174, 52);
textSize(70);
text("GAME OVER! Great Job.", width / 4, height / 2);
textSize(20);
text("YOUR SCORE: " + score, width / 3, height / 1.5);
text("PRESS F TO RETRY", width / 3, height / 1.3);
}
}
function drawHero() {
// frog location
image(froggy, x + width / 3, y + height / 2, 100, 100);
}
function ended() {
print("IT IS FINISHED.");
gameOver = true;
}
class FoodBits {
constructor() {
this.x = random(width);
this.y = random(height);
this.size = random(10, 50);
this.speedX = random(-2, 2);
this.speedY = random(-2, 2);
this.haveIBeenEaten = false;
}
display() {
image(fly, this.x, this.y, this.size, this.size);
}
hide() {
if (dist(x + width / 3, y + height / 2, this.x, this.y) < 50) {
this.x = random(width);
this.y = random(height);
score += 1;
print(score);
}
}
checkCollision() {
let d = dist(x + width / 3, y + height / 2, this.x, this.y);
if (d < 50) {
this.hide();
}
}
move() {
this.x += this.speedX;
this.y += this.speedY;
if (this.x < 0 || this.x > width) {
this.speedX *= -1;
}
if (this.y < 0 || this.y > height) {
this.speedY *= -1;
}
}
}