xxxxxxxxxx
291
let pinkiePie,back1,pinkieImg,dracoImg,star,song,walkSheet,dracoSheet,bgImg, myFont,bgEnd, sound1,sound2,loseSound;
let dracos=[];
let stars=[];
let walk = [];
let enemy=[];
var x1 = 0;
var x2;
var scrollSpeed = 1.5;
let gameState='start';
let score = 0;
let lastFrame = 0;
let frameDelay = 2000;
let lastFrame1 = 0;
let frameDelay1 = 500;
let gameLost;
let lives=3;
let sensor1;
let sensor2;
let lives1=3;
function preload(){
back1=loadImage("back2.png");
walkSheet = loadImage('pinkie.png');
draco = loadImage('draco.png');
bgEnd=loadImage("gameover2.gif");
bgImg=loadImage("start.jpeg");
myFont=loadFont('Blomberg.otf');
star=loadImage("point.png");
//sounds
song=loadSound('sound/song.mp3');
sound1=loadSound("sound/lose.mp3");
sound2=loadSound("sound/point.mp3");
loseSound=loadSound("sound/losesound.mp3");
}
function setup() {
createCanvas(600, 500);
//crop sprites and store in array
let w = int(walkSheet.width / 6);
let h=int(walkSheet.height);
for (let x = 0; x < 6; x++) {
walk[x] = walkSheet.get(x * w, 0, w, h);
}
//create the character
pinkiePie= new PinkiePie(walk);
x2 = width;
song.loop();
}
function draw() {
//draw screens according to game states
if (gameState == 'start') {
drawOpening();
} else if (gameState == 'playing') {
drawGame();
} else if (gameState == 'lose') {
drawLose();
}
}
function keyPressed(){
//when tab is pressed PinkiePie jumps
if(key==" "){
//pinkiePie.jump();
setUpSerial();
}
}
function mousePressed(){
//to start the game
if (gameState == 'start') {
gameState = 'playing';
}
//when lost, to restart
else if (gameState=='lose') {
restartGame();
gameState='playing';
}
}
function drawOpening(){
background(bgImg);
//instructions
fill(204,0,102);
textSize(25);
text("Instructions:",230,25);
//instructions
fill(97,9,174);
textSize(20);
text("2.JUMP to Jump the Pinkie Pie", 140,80);
if (!serialActive) {
text("1.Press Space Bar to select Serial Port", 140, 50);
} else {
text("Connected", 20, 30);
}
text("3.Jump over the dracos&collect pink magic boxes", 140,110);
text("4.You have 3 lives!Press Start when you are ready!", 140,140);
text("5.Press Start when you are ready!", 140,170);
//start button
rectMode(CENTER);
rect(300,240,200,100);
fill(255,204,229);
rect(300,240,160,60);
textFont(myFont);
textSize(50);
fill(255,51,153);
text("START",340,270,200,100);
}
function drawGame(){
//parallax effect-scrolling backgrund
image(back1, x1, 0, width, height);
image(back1, x2, 0, width, height);
x1 -= scrollSpeed;
x2 -= scrollSpeed;
if (x1 < -width){
x1 = width;
}
if (x2 < -width){
x2 = width;
}
//creates dracos
if (millis() >= lastFrame + frameDelay) {
dracos.push(new Draco());
frameDelay = random(0.6, 1) * 2500;
lastFrame = millis();
score++;
}
//draw dracos and check for collision
for(let d of dracos){
d.move();
d.display();
if(pinkiePie.hits(d)){
lives=(lives-1/17);
lives1=round(lives);
sound1.playMode('restart');
sound1.play();
}
else{
sound1.playMode('sustain');
}
}
if(lives1<=0){
gameState='lose';
console.log("game over");
//gameLost=true;
loseSound.play();
}
//create stars for +1 points
if (millis() >= lastFrame1 + frameDelay1) {
stars.push(new Star());
frameDelay1 = random(0.6, 1) * 10000;
lastFrame1 = millis();
score++;
}
//draw stars and when hit, add to score
for(let s of stars){
s.move();
s.display();
if(pinkiePie.hits(s)){
s.remove();
sound2.play();
}
}
//draw and animate PinkiePie
pinkiePie.display();
pinkiePie.run();
//score counter
countScore();
countlives();
}
function drawLose(){
background(bgEnd);
//lose message
rectMode(CENTER);
fill(255,204,229);
rect(300,45,500,70);
//rect(300,240,160,60);
textSize(30);
fill(204,0,102);
text("You lost<_>! Mouse click to restart", 50,40);
text("Score:"+score, 50, 80);
}
const countScore = () =>{
//display the score;
fill(0);
noStroke();
textSize(20);
text("Score:" + score, 5, 20);
}
const countlives = () =>{
//display the score;
fill(0);
noStroke();
textSize(20);
text("Lives:" + lives1, 300, 20);
}
//to restart the game and scores
const restartGame = () => {
gameLost=false;
score=0;
lives=3;
lives1=3;
lastFrame=0;
frameDelay=500;
lastFrame1=0;
frameDelay1=200;
dracos = [];
stars=[];
pinkiePie= new PinkiePie(walk);
new Draco();
new Star();
loop();
}
function readSerial(data) {
////////////////////////////////////
//READ FROM ARDUINO HERE
////////////////////////////////////
//console.log("data"+data);
if (data != null) {
//for button
//if(data==0)
let fromArduino = split(trim(data), ",");
// if the right length, then proceed
if (fromArduino.length == 2) {
sensor1= fromArduino[0];
sensor2 = fromArduino[1];
console.log("s1 "+sensor1);
console.log("s2 "+sensor2);
if((sensor1 <= 1) && (sensor2<=1)){
pinkiePie.jump();
}
}
}
}