xxxxxxxxxx
215
let game;
let blockImage;
let playerImage;
class Game {
constructor() {
// gamestate 0 for new game, 1 for playing, 2 for gameover
this.gameState = 0;
//empty variable for player to be added with addPlayer()
this.player;
//background to be added
this.bg;
//blocks
this.blocks = [];
this.addBlocks();
this.debugMode = true;
//left -right speed
this.gameSpeed = 4;
}
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
//TO BE CALLED IN SKETCH STUFF:
addPlayer() {
this.player = new Player(this);
// this.player.display();
}
addBackgroundImage(bg_file) {
this.bg = loadImage(bg_file);
}
addBlockImage(img){
blockImage = loadImage(img);
}
addPlayerImage(img){
playerImage = loadImage(img);
}
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
//SETUP STUFF:
addBlocks() {
//first add floor
this.blocks.push(new Block(0, height - 40, width, 40));
this.blocks[0].isGround = true;
}
addBlock(x, y) {
this.blocks.push(new Block(x, y, 100, 20));
}
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
//UPDATE STUFF:
update() {
if (this.gameState == 0) {
} else {
// play state:
if (this.player) {
this.player.update();
}
}
//check input
this.checkKeyboardInput();
//update game state
}
spaceBarAction() {
if (this.gameState == 0) {
this.resetGame();
} else {
if (this.player) {
this.player.jump();
}
}
}
resetGame() {
console.log("reset");
this.gameState = 1;
if (this.player) {
this.player.jump();
}
}
checkKeyboardInput() {
// these key-press checks are for 'continuous' keys, whereas
// keyPressed() function below and the spaceBarAction is for
// one-and-done keypresses
if (keyIsDown(LEFT_ARROW)) {
for (let i = 0; i < this.blocks.length; i++) {
let block = this.blocks[i];
if (!block.isGround) {
block.move(-1 * this.gameSpeed);
}
}
}
if (keyIsDown(RIGHT_ARROW)) {
for (let i = 0; i < this.blocks.length; i++) {
let block = this.blocks[i];
if (!block.isGround) {
block.move(this.gameSpeed);
}
}
}
if (keyIsDown(DOWN_ARROW)) {
}
}
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
//DISPLAY STUFF:
display() {
if (this.gameState == 0) {
//game over state
this.displayGameOver();
} else {
//play state
this.displayPlayState();
}
}
displayGameOver() {
background(0);
push();
translate(width / 2, height / 2);
fill(255);
textSize(36);
textAlign(CENTER);
text("GAME OVER \n Spacebar to Begin", 0, 0);
pop();
}
displayPlayState() {
//background with a blur
if (this.bg) {
image(this.bg, 0, 0, width, height);
} else {
background(200, 100, 100);
}
//then display blocks
for (let i = 0; i < this.blocks.length; i++) {
this.blocks[i].display();
}
//then display gremlins
//then display player
if (this.player) {
this.player.display();
}
}
}
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
//BLOCKS
class Block {
// top, left, width, height
constructor(x, y, w, h) {
this.top = y;
this.left = x;
this.w = w;
this.h = h;
this.bottom = this.top + this.h;
this.right = this.left + this.w;
this.underneathPlayer = false;
this.isGround = false;
}
move(amnt) {
this.left -= amnt;
this.right -= amnt;
}
display() {
push();
fill(0, 0, 255);
rectMode(CORNERS);
rect(this.left, this.top, this.right, this.bottom);
pop();
if (blockImage) {
image(blockImage, this.left, this.top, this.w, this.h);
}
}
}
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
function keyPressed() {
//spacebar OR up arrow
if (keyCode == 32 || keyCode == UP_ARROW) {
console.log("space is pressed");
game.spaceBarAction();
}
}