xxxxxxxxxx
182
let spritesheet;
let sprites = [];
let direction = 2; // 0 up
let step = 0;
let x, y;
let speed = 5;
let cameraPan;
let hallway;
let leftpost, rightpost;
let lampleftX, lampleftY;
let lamprightX, lamprightY;
let blockers = [];
let blocker;
let blockerA;
let timer = 30;
let gameCon = true;
class blockerClass {
constructor(speed, x, y) {
this.speed = speed;
this.x = x;
this.y = y;
this.bottom = y;
}
updateBlocker() {
this.y += this.speed;
this.bottom += this.speed;
if (this.y > height + 50) {
this.y = random(-50, -500)
this.speed = random(5,10);
this.bottom = this.y;
}
}
drawBlocker() {
image(blocker, this.x, this.y, 55, 50);
}
run() {
this.drawBlocker();
this.updateBlocker();
}
}
function preload() {
spritesheet = loadImage("Characterr.png");
hallway = loadImage("background.jpg");
leftpost = loadImage("leftpost.png");
rightpost = loadImage("rightpost.png");
blocker =loadImage("roadblocker.png");
song = loadSound("crash.mp3");
}
function setup() {
createCanvas(300, 500);
imageMode(CENTER);
let w = spritesheet.width / 4;
let h = spritesheet.height / 3;
for (let y = 0; y < 3; y++) {
sprites[y] = [];
for (let x = 0; x < 4; x++) {
sprites[y][x] = spritesheet.get(x * w, y * h, w, h);
}
}
x = width*1/2;
y = height/8*6;
lampleftX = width*0.15;
lampleftY = height*0.5;
lamprightX = width*0.85;
lamprightY = height*0.5;
blockerY = height*0.5;
for( let i=0; i<3; i++){
let x = width * 0.3;
let y = random(-100,-150)
if (i % 2 == 0) {
x = width * 0.5;
y = -2000
}
if (i % 3 == 0) {
x = width * 0.7;
y = random(-400,-450)
}
// console.log(x)
blockers.push(new blockerClass(10, x, y));
}
// blockerA = new blockerClass(10, width*0.7, 50);
}
function draw() {
background(255);
push();
imageMode(CORNER);
image(hallway, 0, 0, width, height);
pop();
imageMode(CENTER);
image(leftpost, lampleftX, lampleftY, 50, 100);
image(rightpost, lamprightX, lamprightY, 50, 100);
updateLeftPost();
updateRightPost();
// blockerA.run();
for( let i = 0 ; i < blockers.length; i++){
blockers[i].run();
if ( blockers[i].x <= x && blockers[i].x >= x && blockers[i].y <= y + 5 && blockers[i].y >= y-5) {
console.log("GAME END");
song.play();
}
}
if (frameCount % 60 == 0 && timer > 0) {
timer --;
}
if (timer == 0) {
text("YOU WIN", width/2, height*0.7);
}
if (frameCount % 10 == 0) {
step = (step + 1) % 4;
}
image(sprites[direction][step], x, y);
textAlign(CENTER);
rectMode(CENTER);
rect(width*0.1, 40, 55,30);
text(timer, width*0.1,50);
text("Time Left:", width*0.1, 40);
}
function keyPressed() {
if (keyCode == LEFT_ARROW) {
if (x/width == 0.5) {
direction = 2
xAdjustleft = 0.3
xAdjustleft = constrain(xAdjustleft, 0.30, 0.70)
x= width*xAdjustleft;
}
else if (x/width == 0.7) {
direction = 2
xAdjustleft = 0.5
xAdjustleft = constrain(xAdjustleft, 0.30, 0.70)
x= width*xAdjustleft;
}
}
if (keyCode == RIGHT_ARROW) {
if (x/width == 0.5) {
direction = 2
xAdjustleft = 0.7
xAdjustleft = constrain(xAdjustleft, 0.30, 0.70)
x= width*xAdjustleft;
}
else if (x/width == 0.3) {
direction = 2
xAdjustleft = 0.5
xAdjustleft = constrain(xAdjustleft, 0.30, 0.70)
x= width*xAdjustleft;
}
}
}
function updateLeftPost() {
lampleftY+= 7;
if (lampleftY > height + 50) {
lampleftY = -50
}
}
function updateRightPost() {
lamprightY+= 7;
if (lamprightY > height + 50) {
lamprightY = -50
}
}
function game() {
}