xxxxxxxxxx
378
let cmv
let enter = false
let level = 0
let R,G,B,A
//bouncing ball
let xPos =0, yPos = 0
rectSize = 150
let velocity = 1
//
//stage 1
let snails = []
let snailNum = 15
let snailImg
let timer1 = 7
let win1 = false
let gameover = false
//stage 2
let vx = 0,vy=0;
let particleSystem = []
let parNum = 4
let timer2 = 10
function preload(){
snailImg = loadImage('assets/eye.gif')
loadingscreen = loadImage('assets/load.png')
level1screen = loadImage('assets/level1.png')
level1screen2 = loadImage('assets/level1screen2.png')
run = loadImage('assets/run.gif')
butterfly = loadImage('assets/butterfly.gif')
bed = loadImage('assets/bed.png')
level2screen2 = loadImage('assets/level2screen2.png')
level3screen1 = loadImage('assets/level3screen1.png')
level3screen2 = loadImage('assets/level3screen2.png')
}
function setup() {
cnv = createCanvas(800,800);
cnv.position((windowWidth - 800) / 2, (windowHeight - 800) / 2)
angleMode(DEGREES)
for (let i=0; i<snailNum; i++){
snails.push(new snail())
}
for(let i =0; i<parNum; i++){
let newPar = new particle()
particleSystem.push(newPar)
}
}
function draw() {
// START: loading screen
if (level== 0) {
background('white')
push()
scale(0.7)
image(loadingscreen, 300, 200);
pop()
textAlign(CENTER)
textFont('monospace')
fill('black')
text('press', 270,200)
text('[enter]',270,200+14)
text('to begin',270,200+28)
push();
translate(200, 200)
scale(.2,.2)
let a
// a = map( yPos, -rectSize / 2, rectSize /2, 0, 255 )
// fill(0,0,0,a);
// noStroke();
yPos = yPos + velocity;
image(butterfly, xPos, yPos);
if (yPos > rectSize / 2 || yPos < -rectSize / 2) {
velocity = -velocity;
}
pop();
}
// END: loading screen
//START: STAGE 1
else if( level == 1){
// frameRate(30)
let score=snailNum - snails.length;
background(level1screen)
//timer1
fill('black')
textAlign(CENTER)
if(frameCount%60===0 && timer1>0){
timer1 --;
} else if (timer1 ===0){
if(score < 15){
gameover = true
}
if(score == 15 ){
background(level1screen2)
fill('black')
textAlign(CENTER)
text("level 1 complete", width/2,height/2+100);
text("press any key to continue", width/2, height/2+150)
keyPressed(
level++)
}
}
//
text(timer1, width/2, 30)
for (let i=0; i<snails.length; i++){
// snails[i].move();
snails[i].show();
}
}//END: STAGE 1
//START: STAGE 2
else if (level ==2){
background('white')
// frameRate(60)
image(bed,600,600)
for (let i =0; i<particleSystem.length; i++){
particleSystem[i].move();
particleSystem[i].show();
particleSystem[i].checkEdge()
if (particleSystem[i].contain(vx,vy)){
gameover = true
}
//vehicle
noCursor();
push()
image(run,vx,vy);
pop()
// move the vehicle
if (keyIsDown(LEFT_ARROW)) {
vx -= 5;
}
if (keyIsDown(RIGHT_ARROW)) {
vx += 5;
}
if (keyIsDown(UP_ARROW)) {
vy -= 5;
}
if (keyIsDown(DOWN_ARROW)) {
vy += 5;
}
if (vx>600 || vy>600) {
background(level2screen2)
fill('black')
textAlign(CENTER)
text("level 2 complete", width/2,height/2+250);
text("press any key to continue", width/2, height/2+300)
keyPressed(
level++)
}
}
//timer
fill('red')
textAlign(CENTER)
if(frameCount%60===0 && timer2>0){
timer2 --;
} else if (timer2 ===0){
text("game over", width/2,height/2);
}
text(timer2, width/2, 30)
} // END: STAGE 2
//START: STAGE 3
else if (level == 3){
let a = 0
background('white')
background(level3screen1)
fill('white')
text('press any key fast', width/2, 30)
fill(0,0,0,a)
square(width/2,height/2,800)
keyPressed(
a+20)
if (a == 255) {
background(level3screen3)
fill('black')
textAlign(CENTER)
text("you win", width/2,height/2+250);
}
fill('red')
textAlign(CENTER)
if(frameCount%60===0 && timer2>0){
timer2 --;
} else if (timer2 ===0){
text("game over", width/2,height/2);
}
text(timer2, width/2, 30)
}
if (gameover == true){
background('white')
fill('black')
text("game over", width/2,height/2);
text("your score is "+ score, width/2, height/2-40)
}
}//END: draw
class snail{
constructor(){
this.x = random(100,700)
this.y = random(100,700)
this.r = random(-10,10) //rotation
this.s = 150 //size
}
// move(){
// this.x = random(-4,4)+this.x
// this.y = random(-4,4)+this.y
// }
show(){
//image(variable'name,xpos,ypos, width,height)
push()
translate(this.x,this.y)
// let angle = map(mouseX, 0, width, 30, -30)
// rotate(angle)
imageMode(CENTER)
image(snailImg, 0,0,this.s,this.s)
pop()
}
}
class particle{
constructor(){
this.speed = createVector(random(-1,1), random(-1,1))
this.x = random(width)
this.y = random(height)
this.r = random(20)
this.c = color('yellow')
}
move(){
this.x = this.x+this.speed.x
this.y = this.y+this.speed.y
}
show(){
fill(this.c)
image(butterfly, this.x,this.y,this.s,this.s)
// circle(this.x,this.y,this.r*2)
}
checkEdge(){
if(this.x > width || this.x<0){
this.speed.x = -this.speed.x
}
if(this.y > height || this.y<0){
this.speed.y = -this.speed.y
}
}
contain(x2,y2){
let d = dist(x2,y2,this.x,this.y)
if (d<this.r*2){
return true;
} else{
return false;
}
}
changeColor(newColor){
this.c = color(newColor)
}
}
function mousePressed() {
for (let i = 0; i < snails.length; i++) {
let distance = dist(mouseX, mouseY, snails[i].x, snails[i].y)
if (distance < snails[i].s) {
snails.splice(i,1);
}
}
}
function keyPressed() {
if (keyCode == ENTER ) {
level = 1;
}}