xxxxxxxxxx
182
let x1;
let x2;
let scrollSpeed = 2;
let startTime;
let timeLimit = 60; //countdown timer duration
let countdown;
let atkCharge = 0; //charge for attacks
let score = 0;
let gameState = "start";
let serial;
let protagImg, copImg;
let cops = [];
let numberOfCops = 2;
//game keys
let START = 0;
let ATTACK = 0;
let JUMP = 0;
let gameBG;
let protag;
function preload() {
gameBG = loadImage("bg.png");
protagImg = loadImage("protag.png");
copImg = loadImage("cop.png");
}
function setup() {
createCanvas(600, 400);
x1 = 0;
x2 = width;
protag = new Protag();
for (let i = 0; i < numberOfCops; i++) {
cops[i] = new Cop();
}
}
function draw() {
if (gameState == "start") {
drawStartScreen();
} else if (gameState == "playing") {
runGame();
} else if (gameState == "end") {
drawEndScreen();
}
}
function runGame() {
//setup countdown timer
let currentTime = millis();
let elapsedTime = (currentTime - startTime) / 1000;
image(gameBG, x1, 0, width, height);
image(gameBG, x2, 0, width, height);
fill("rgb(61,65,70)");
noStroke();
rectMode(CENTER);
rect(width / 2, 360, width, 100);
//background loop
x1 -= scrollSpeed;
x2 -= scrollSpeed;
if (x1 < -width) {
x1 = x2 + width; // Reset x1 position
}
if (x2 < -width) {
x2 = x1 + width; // Reset x2 position
}
protag.display();
protag.move();
if (JUMP === 1){
protag.jump();
}
for (let i = 0; i < cops.length; i++) {
cops[i].display();
cops[i].move();
}
//display score and countdown in real time
fill(0);
textFont("times new roman");
text("Score:" + score, 500, 60);
text("Time:" + countdown, 500, 80);
//end game when time is up
if (countdown < 0) {
gameState = "end";
}
}
function drawStartScreen() {
//start screen placeholder
background(220);
fill(0);
textAlign(CENTER);
text("Press space bar to set up serial", width / 2, height / 2);
if (START === 1) {
// Start the game
startTime = millis();
gameState = "playing";
}
}
function drawEndScreen() {
//display end screen with final score and restart instruction
background(0);
fill(255);
text("Score:" + score, width / 2, height / 2);
text("Press START to restart", width / 2, 350);
}
function restartGame() {
score = 0;
startTime = millis();
timeLimit = 60;
countdown = timeLimit;
gameState = "start";
}
function readSerial(data) {
////////////////////////////////////
//READ FROM ARDUINO HERE
////////////////////////////////////
if (data != null) {
// make sure there is actually a message
// split the message
let fromArduino = split(trim(data), ",");
if (fromArduino.length == 3) {
// only store values here
// do everything with those values in the main draw loop
// We take the string we get from Arduino and explicitly
// convert it to a number by using int()
// e.g. "103" becomes 103
JUMP = int(fromArduino[0]);
ATTACK = int(fromArduino[1]);
START = int(fromArduino[2]);
}
// console.log(fromArduino);
// if the right length, then proceed
//////////////////////////////////
//SEND TO ARDUINO HERE (handshake)
//////////////////////////////////
// let sendToArduino =
// START + "," + JUMP + "," + ATTACK + ", \n";
// writeSerial(sendToArduino);
}
}
function windowResized() {
print("resized to " + windowWidth + "," + windowHeight);
resizeCanvas(windowWidth, windowHeight);
}
function keyPressed() {
if (key === " ") {
setUpSerial();
}
}
function keyTyped() {
// $$$ For some reason on Chrome/Mac you may have to press f twice to toggle. Works correctly on Firefox/Mac
if (key === 'f') {
toggleFullscreen();
}
// uncomment to prevent any default behavior
// return false;
}
function toggleFullscreen() {
let fs = fullscreen(); // Get the current state
fullscreen(!fs); // Flip it!
}