xxxxxxxxxx
483
// sound effects from Zapsplat.com
let sceneNum = 0;
let guy, xPos, yPos;
let tX, tY, tXspeed, tySpeed, tSize;
let blocks = [];
let speed = 5;
let collided = false;
let gotT = false
let myFont;
let tFont;
let music;
let ouchSound;
let gotitSound;
let moveSound;
let T;
let hazard;
//let vid;
//let gif_loadImg, gif_createImg;
function preload() {
myFont = loadFont("bodyfont.otf");
tFont = loadFont("titlefont.otf");
music = loadSound("bitmapsound.mp3");
ouchSound = loadSound("ouch.mp3");
gotitSound = loadSound("gotit.mp3");
moveSound = loadSound("move.mp3")
T = loadImage("T.png");
hazard = loadImage("hazard.png");
//vid = createVideo("gotit.mp4");
//gif_loadImg = loadImage("gotit.gif");
//gif_createImg = createImg("gotit.gif");
}
function setup() {
music.loop();
music.setVolume(0.2);
createCanvas(500, 500);
imageMode(CENTER);
//pre seed the blocks in a falling constellation
for (let i = 0; i < 1000; i++) {
blocks[i] = createBlock(width * 0.25, width * 0.75, -150 * i, 25);
}
//starting point for guy
xPos = 80;
yPos = 300;
//create guy reference with position and size
guy = new Character(xPos, yPos, 50);
//target position and size
tX = 400 + random(width);
tY = 300 + random(height);
tSize = T.width / 10;
tXspeed = 3;
tYspeed = 3;
}
function draw() {
background(1, 254, 1);
//draw guy
guy.readInput();
guy.display();
//draw target
fill(255);
image(T, tX, tY, tSize, T.height / 10);
// ellipse(tX, tY, tSize);
tX = tX + tXspeed;
tY = tX + tYspeed;
if (tX + 40 >= width) {
tXspeed = -tXspeed;
tX = width - tSize;
} else if (tX <= 0) {
tXspeed = -tXspeed;
tX = 0;
}
if (tY + tSize >= height) {
tYspeed = -tYspeed;
tY = height - tSize;
} else if (tY <= 0) {
tYspeed = -tYspeed;
tY = 0;
}
//reaching target
if (dist(guy.x, guy.y, tX, tY) < tSize / 2 && collided == false) {
textSize(25);
text("got it", 300, 50);
gotitSound.play();
//vid.play();
gotT = true;
console.log('got it')
}
if (gotT === true && sceneNum <= 7) {
sceneNum = 8;
}
//collide with a block
//if( sceneNum == 1 || sceneNum == 3 ){
for (let i = 0; i < blocks.length; i++) {
let b = blocks[i]; //every block position in this loop
//if collided
if (dist(guy.x, guy.y, b.x, b.y) < (guy.size + b.size) * 0.5 && collided === false ) {
//say ouch
textSize(25);
text("ouch", 300, 50);
ouchSound.play();
ouchSound.setVolume(0.01);
collided = true;
}
if (collided === true && sceneNum === 1) {
sceneNum = 2
}
if (collided === true && sceneNum === 3) {
sceneNum = 4
}
}
//}
console.log(sceneNum,collided,gotT)
//switch scenes to change speed/background color/sound
switch (sceneNum) {
case 0:
scene0();
break;
case 1:
scene1();
break;
case 2:
scene2();
break;
case 3:
scene3();
break;
case 4:
scene4();
break;
case 5:
scene5();
break;
case 6:
scene6();
break;
case 7:
scene7();
break;
case 8:
scene8();
break;
}
//removing blocks
let outIndex = []; //this is a list of blocks outside of the screen
//go through every block in the array
//update its position with fall()
//then draw it with display()
//and track if it is now outside of the screen
for (let i = 0; i < blocks.length; i++) {
blocks[i].fall(); // make sure fall happens before draing
blocks[i].display();
//capturing the index of the out block
if (blocks[i].out == true) {
outIndex.push(i);
}
}
//for every block that is outside of the screen
//splice it out of the blocks[] array
//actually removing the block
for (let i = 0; i < outIndex.length; i++) {
//outIndex[i] is the index of the blocks that are outside
blocks.splice(outIndex[i], 1);
}
} //draw loop closing bracket
//scenes
//show welcome screen
function scene0() {
background(0);
fill(1, 254, 1);
push();
textFont(tFont);
textSize(50);
textAlign(CENTER);
strokeWeight(2);
stroke(255);
//filter(BLUR);
text('Press space to start', width / 2 + random(0, 3), height / 2 + random(0, 3));
pop();
// console.log('scene 0');
}
// press space to enter game
function keyPressed() {
if (keyCode === 32 && sceneNum === 0) {
sceneNum = 1
}
//press enter to go to to lvl 2 scene 3
else if (keyCode === ENTER && sceneNum === 2) {
sceneNum = 3;
collided = false;
} else if (keyCode === ENTER && sceneNum === 3) {
sceneNum = 4
collided = false;
} else if (keyCode === ENTER && sceneNum === 4) {
sceneNum = 5
collided = false;
} else if (keyCode === ENTER && sceneNum === 6) {
sceneNum = 7
collided = false;
}
}
//show the game
function scene1() {
//not drawing background here
//eveything in the draw loop plays normally
// console.log('scene 1');
}
//show msg 1
function scene2() {
//redraw the background
push();
background(220);
textFont(myFont);
textSize(30);
textAlign(CENTER);
fill(203, 72, 252);
strokeWeight(2);
stroke(255);
text('Two months \n waiting for an appointment\n -10 speed \n \n Press enter to continue', width / 2 + random(0, 2), height / 2 - 40 + random(0, 2));
pop();
// console.log('scene 2');
}
//return to game - level 2
//press enter
function scene3() {
//should go back to game at level 2
//change the speed here though
//and some other stuff
speed = 1;
// console.log('scene 3');
}
//collison happens again
//show scene 4/msg 2
function scene4() {
//redraw the background!
// collided = false;
push();
background(220);
textFont(myFont);
textSize(30);
textAlign(CENTER);
fill(203, 72, 252);
strokeWeight(2);
stroke(255);
text('Preauthorization required \n -20 speed \n \n Press enter to continue', width / 2 + random(0, 2), height / 2 - 40 + random(0, 2));
pop();
// console.log('scene 4');
}
// // which brings you to level 3/scene 5
function scene5() {
//should go back to game at level 3
//change the speed here though
//and some other stuff
// console.log('scene 5');
}
//another collision
function scene6() {
//redraw the background!
// collided = false;
push();
background(220);
textFont(myFont);
textSize(30);
textAlign(CENTER);
fill(203, 72, 252);
strokeWeight(2);
stroke(255);
text('Insurance pending \n medical director review. \n +10 obstacle speed \n \n Press enter to continue', width / 2 + random(0, 2), height / 2 - 40 + random(0, 2));
pop();
// console.log('scene 6');
}
function scene7() {
//should go back to game at level 4
//change the speed here though
//and some other stuff
//I want to increase the falling speed but it's not working
this.speed = 50;
// console.log('scene 7');
}
//reach target for final scene
// //got it - celebration
function scene8() {
// collided = false;
//celebrate screen
// image(gif_loadImg, 50, 50);
// gif_createImg.position(50, 350);
//vid.play();
// console.log('scene 8');
push();
fill(0)
text('You Did It',width/2,height/2);
pop();
}
//character class
class Character {
constructor(x, y, size) {
this.x = x;
this.y = y;
this.size = size;
this.dead = false;
}
bounds() {
let topLeft = {
x: this.x,
y: this.y
};
let topRight = {
x: this.x + this.size,
y: this.y
};
let bottomLeft = {
x: this.x,
y: this.y + this.size
};
let bottomRight = {
x: this.x + this.size,
y: this.y + this.size
};
let bounds = { //use these bounds() functions for AABB rectangle collisions
tl: topLeft,
tr: topRight,
bl: bottomLeft,
br: bottomRight
}
return bounds;
}
readInput() {
//if (this.dead) return; //if dead, leave this function using return
moveSound.setVolume(0.01);
//charcter's movement
if (keyIsDown(LEFT_ARROW) === true) {
this.x -= speed;
moveSound.play();
} else if (keyIsDown(RIGHT_ARROW) === true) {
this.x += speed;
moveSound.play();
} else if (keyIsDown(UP_ARROW) === true) {
this.y -= speed;
moveSound.play();
} else if (keyIsDown(DOWN_ARROW) === true) {
this.y += speed;
moveSound.play();
}
//character stays on screen
if (this.x > width) {
this.x = -1 * this.size / 2;
} else if (this.x < -1 * this.size) {
this.x = 500;
}
if (this.y > height) {
this.y = -1 * this.size / 2;
} else if (this.y < -1 * this.size) {
this.y = 500;
}
}
display() {
//guy.dead --> jitter motion maybe
rect(this.x, this.y, this.size, this.size);
}
}
//block class
class BlockGenerator {
createBlock(x, y) {}
}
function createBlock(lb, rb, y, size) {
return new Block(random(lb, rb), y, size);
}
class Block {
constructor(x, y, size) {
this.x = x;
this.y = y;
this.size = size;
this.speed = 5; //falling speed
this.out = false; // true or false to know where it is
}
bounds() {
let topLeft = {
x: this.x,
y: this.y
};
let topRight = {
x: this.x + this.size,
y: this.y
};
let bottomLeft = {
x: this.x,
y: this.y + this.size
};
let bottomRight = {
x: this.x + this.size,
y: this.y + this.size
};
let bounds = {
tl: topLeft,
tr: topRight,
bl: bottomLeft,
br: bottomRight
}
return bounds;
}
display() {
fill(24, 35, 9);
// image(hazard, this.x, this.y, this.size);
rect(this.x, this.y, this.size);
}
fall() {
this.y += this.speed;
if (this.y > height) {
this.out = true;
}
}
}