xxxxxxxxxx
330
let myFont;
let tags = [];
let player;
let tagsNum = 15;
let flag;
let sceneNum = 0;
let playerLives = 5;
let playerrr;
let heart;
let treasure;
let label;
let arrow;
let gamestart;
let gamelose;
let wingame;
let hit;
let beep;
let bgmusic;
//********************************************************
function preload(){
myFont = loadFont('MinecraftR.otf');
playerrr = loadImage('assets/player.png')
heart = loadImage('assets/heart.png')
treasure = loadImage('assets/treasure.png')
label = loadImage('assets/tag.png')
arrow = loadImage('assets/arrow.png')
gamestart = loadSound('assets/gamestart.mp3');
gamelose = loadSound('assets/gameover.mp3');
wingame = loadSound('assets/wingame.mp3');
hit = loadSound('assets/hit.mp3');
beep = loadSound('assets/beep.mp3');
bgmusic = loadSound('assets/bgmusic.mp3');
}
//********************************************************
function setup() {
createCanvas(600, 400);
for (let i =0; i < tagsNum; i++){
tags[i] = new Tag (random(width), random(height-100), color(random(255), random(255), random(255)));
}
player = new Character();
flag = new Destination();
}
//********************************************************
function draw() {
background(224, 235, 235);
//change the scene
switch (sceneNum){
case 0:
startgame();
break;
}
switch (sceneNum){
case 1:
gamescene();
break;
}
switch (sceneNum){
case 2:
gameover();
break;
}
switch (sceneNum){
case 3:
endgame();
break;
}
//*************************startgame*******************************
//scene 0: start game
function startgame(){
imageMode(CENTER)
image(arrow, 300, 200, 150, 100)
fill('black');
textFont(myFont);
textSize(30);
text('Press Space To Start', 135, 300)
if (keyIsDown(32)){
gamestart.play();
bgmusic.loop();
sceneNum = 1
}
}
//*************************gamescene*******************************
//scene 1: avoid the tags
function gamescene(){
fill('black');
textFont(myFont);
textSize(20);
text('Avoid Being Labeled', 135, 376)
for (let i = 0; i < tagsNum; i++){
tags[i].show();
tags[i].move();
tags[i].checkCollision();
}
player.body();
player.move();
flag.show();
currentplayerLives();
if (player.x + player.w/2 > 30 && player.x < 30 + 30 && player.y + player. h/2 > 30 && player.y < 30 + 30){
if(bgmusic.isPlaying()){
bgmusic.stop();
wingame.play();
}
player.x = 570
player.y = height - 35;
sceneNum = 3
}
//how many lives before you restart the game?
function currentplayerLives(){
for (let i = 0; i < playerLives; i++){
//fill(0, 0, 255);
//ellipse(i*25, height-10, 20, 20);
image(heart, i*25, height-30, 20, 20)
}
if (playerLives == 0){
if (bgmusic.isPlaying()){
bgmusic.stop();
gamelose.play();
}
playerLives = 5;
sceneNum = 2
}
}
//***********************gameover*********************************
//scene 2: Lost all the playerlives and gameover
function gameover(){
fill('black');
textFont(myFont);
textSize(40);
text('Game Over', 195, 220);
textSize(15)
text('> Press Enter to Restart', 400, 380)
if (keyIsDown(13)){
bgmusic.loop();
sceneNum = 1
}
}
//***********************endgame*********************************
//scene 2: You Win!!!
function endgame(){
fill('black');
textFont(myFont);
textSize(30);
text('Congratulations! You Win!', 112, 200);
text('BE YOURSELF', 200, 300);
textSize(15)
text('> Press Enter To Restart', 400, 380)
if (keyIsDown(13)){
bgmusic.loop();
sceneNum = 1
}
}
}
//***********************class*********************************
//create character
class Character{
constructor(){
this.x = 570;
this.y = height - 35;
this.w = 30;
this.h = 30;
this.c = color(0, 0, 255);
}
body(){
//fill(this.c);
//ellipse(this.x, this.y, this.w, this.h);
image(playerrr, this.x, this.y, this.w, this.h)
}
//move the character
move(){
if (keyIsDown(39)){
//beep.play();
player.x+=3;
}
if (keyIsDown(37)){
//beep.play();
player.x-=3;
}
if (keyIsDown(38)){
//beep.play();
player.y-=3;
}
if (keyIsDown(40)){
//beep.play();
player.y+=3;
}
}
}
//create tags
class Tag{
constructor(){
this.x = x;
this.y = y;
this.w = 70;
this.h = 30;
this.c = c;
}
show(){
// fill(this.c)
// rect(this.x, this.y, this.w, this.h);
imageMode(CENTER);
image(label, this.x, this.y, this.w, this.h);
}
move(){
this.x++;
if (this.x > width){
this.x = 0;
}
}
//intersection
checkCollision(){
if (player.x + player.w/2 > this.x && player.x < this.x + this.w && player.y + player.h/2 > this.y && player.y < this.y + this.h){
hit.play();
player.x = 570
player.y = height - 35;
playerLives--;
}
}
}
//win
class Destination{
constructor(){
this.x = 30
this.y = 30
this.w = 30
this.h = 30
this.c = color(255, 0, 0)
}
show(){
//fill(this.c);
//ellipse(this.x, this.y, 30, 30);
image(treasure, this.x, this.y, 30, 25);
}
}
}