xxxxxxxxxx
127
var screen = 0;
var y = -20;
var x = 200;
var fall_speed = 2;
var score = 0;
var time_left = 2000;
var health = 70;
var max_health = 100;
function preload() {
background_image = loadImage("Images/background.jpg");
}
function setup() {
createCanvas(600, 400);
}
function draw() {
if (screen == 0) {
startScreen();
} else if (screen == 1) {
gameOn();
} else if (screen == 2) {
gameOverScreen();
} else if (screen == 3) {
gameWonScreen();
}
}
function startScreen() {
//background(96, 157, 255);
background(background_image);
fill(255);
textAlign(CENTER);
text("WELCOME TO < SURVIVE THE SEMESTER >", width / 2, height / 8);
textAlign(LEFT);
text("Instructions:",width/10,height/5.5);
text("Your health and joy levels decrease over time",width/10,height/5+20);
text("Catch some sleep and food to restore health",width/10,height/5+40);
text("Catch some fun activities to restore joy",width/10,height/5+60);
text("If you let health or joy decrease to 0 before the timer is over, it's game over for you",width/10,height/2.5);
textAlign(CENTER);
//print(mouseX,mouseY);
fill(255);
//rect(width/3,height/1.5,width/3,height/15);
//fill(0);
text("CLICK TO START", width/2,height/1.4);
start_game();
}
function gameOn() {
background(background_image);
text("score = " + score, 30, 20);
text("time left = " + time_left,45,50);
textAlign(CENTER);
text("Use the mouse pointer to catch any item!",300,50);
updateHealth(health,max_health);
fill(0);
ellipse(x, y, 20, 20);
fill(52, 64, 235);
rectMode(CENTER);
rect(mouseX, height - 10, 50, 30);
y += fall_speed;
time_left -= 1;
// health -= 10;
if (0 == health){
screen = 2;
}
if (y > height) {
health -= 10;
x = random(20, width - 20);
}
if (y > height - 10 && x > mouseX - 20 && x < mouseX + 20) {
y = -20;
fall_speed += 0.5;
score += 1;
}
if (-20 == y) {
x = random(20, width - 20);
}
if (0 == time_left){
screen = 3;
}
}
function updateHealth(health,max_health) {
stroke(0);
strokeWeight(4);
noFill();
rect(width/2-100,40,200,15);
noStroke();
fill(255,0,0);
rect(width/2-100,40,map(health,0,max_health,0,200),15);
console.log(health);
}
function gameOverScreen() {
background(background_image);
fill(255);
textAlign(CENTER);
text("GAME OVER", width / 2, height / 3);
text("SCORE = " + score, width / 2, height / 3 + 20);
text("click to play again", width / 2, height / 3 + 40);
}
function gameWonScreen() {
background(background_image);
textAlign(CENTER);
text("CONGRATULATIONS", width / 2, height / 2);
text("YOU SURVIVED THE GAME!", width / 2, height / 2 + 20);
text("click to play again", width / 2, height / 2 + 40);
}
function mousePressed() {
if (screen == 0) {
screen = 1;
} else if (screen == 2) {
screen = 0;
}
}
function start_game() {
score = 0;
speed = 2;
y = -20;
}