xxxxxxxxxx
372
// global variables REQUIRED in the game
let gamePlay = false;
let up = false;
let down = false;
let bx = -40;
let bx2 = 550;
let tx = 200;
let tx2 = 600;
let t2x = 500;
let t2x2 = 600;
let ex = 0;
let ex2 = 600;
let distance;
let startTime;
let endTime;
let timerStarted = false;
let score = 0;
let display = 0;
//loading all the images
function preload(){
sprite_shiz = loadImage("Images/Naruto2.png");
ground = loadImage("Images/ground.png");
tree = loadImage("Images/tree.png");
mountain = loadImage("Images/mount.png");
sky = loadImage("Images/sky2.jpg");
obstacle = loadImage("Images/fire1.png");
obstacle2 = loadImage("Images/fire2.png");
Nfont = loadFont("assets/naruto.ttf");
mainbg = loadImage("Images/mainbg.png");
}
// putting the main background images of the and moving them in parallex view
function drawLandscape() {
image(sky, ex,0, 600, 500, 0,0);
image(sky, ex2,0, 600, 500, 0,0);
image(tree, tx, height-130, 70,80, 0, 0);
image(tree, tx2, height-130, 70,80, 0, 0);
image(tree, t2x, height-130, 70,80, 0, 0);
image(tree, t2x2, height-130, 70,80, 0, 0);
image(ground, bx, height-50);
image(ground, bx2, height-50);
if (gamePlay == true){
bx-=5;bx2-=5;
tx-=2;tx2-=2; t2x-=2;t2x2-=2;
ex-=3;ex2-=3;
if (bx<-600){bx = 550;} if (bx2<-600){bx2 = 550;}
if (tx<-600){tx = 600;} if (tx2<-600){tx2 = 600;}
if (t2x<-600){t2x = 600;} if (t2x2<-600){t2x2 = 600;}
if (ex<-600){ex = 590;} if (ex2<-600){ex2 = 590;}
}
}
// drawing and controlling making it go up and down using the arduino values recieved
function sprite(shiz, x, y, size, frames){
this.shiz = shiz;
this.x = x;
this.y = y;
this.size = size;
this.h = shiz.height;
this.w = shiz.width;
this.frame = 0;
this.frames = frames;
this.velY = 0;
this.draw = function(){
image(this.shiz, this.x, this.y, this.size,this.size,this.h*floor(this.frame),0, this.h, this.w);
if (gamePlay == true){
this.frame +=0.3;
if (this.frame>this.frames){
this.frame = 0;
}
if (distance> 10 && distance<height){
this.y = distance;
}
}
}
}
function setup() {
createCanvas(600, 500);
naruto = new sprite(sprite_shiz, 20, height-94, 60, 8);
obstacles = [];
for (let i = 0; i < 3; i++) {
let randX = width + getRandomValue(100, 600,80);
let randY = getRandomValue(10, 400, 50);
obstacles.push(new fire(obstacle2, randX, randY, 100, 1));
}
for (let i = 0; i < 3; i++) {
let randiX = width + getRandomValue(100, 600,80);
let randiY = getRandomValue(10, 400, 50);
obstacles.push(new fire(obstacle,randiX, randiY, 50, 0));
}
}
function draw() {
background("rgb(44,195,236)")
if (gamePlay== false && display == 0){
mainScreen();
}
else if(gamePlay == false && display ==1 ){
gameOver();
}
else{
fill(255, 255, 0);
noStroke();
ellipse(20, 10, 70, 70);
drawLandscape();
naruto.draw();
if (timerStarted) {
let ctime = millis();
let timePassed = (ctime - startTime) / 1000;
textSize(12);
text("Score: " + timePassed.toFixed(0), width -40,20);
score = timePassed.toFixed(0);
}
for (let i = 0; i < obstacles.length; i++) {
obstacles[i].draw(obstacles);
}
}
}
function fire(enemy, x, y, size, type) {
this.x = x;
this.y = y;
this.size = size;
this.velX = 0;
this.collidesWith = function (obj) {
if (
// (obj.x>=this.x && obj.x<= this.x+this.size && obj.y >=this.y && obj.y <= this.y+this.size && type == 1)
// // this.x + this.size / 2 > obj.x && && enemy == "fire2"
// // this.x - this.size / 2 < obj.x + obj.size / 2 &&
// // this.y + this.size / 2 > obj.y && && enemy == "fire2"
// // this.y - this.size / 2 < obj.y + obj.size / 2
// ) {
// return true;
// } else if(
// this.x + this.size / 4 > obj.x &&
// this.x - this.size / 4 < obj.x + obj.size / 4 &&
// this.y + this.size / 4 > obj.y &&
// this.y - this.size / 4 < obj.y + obj.size / 4 &&
// type == 0
obj.x + obj.size / 4 > this.x &&
obj.x - obj.size / 4 < this.x + this.size / 4 &&
obj.y + obj.size / 2 > this.y &&
obj.y - obj.size / 2 < this.y + this.size / 4
) {
console.log("kjbvkjsbd")
console.log(this.x, this.y)
console.log("nnnnnnn")
console.log(obj.x, obj.y)
return true;
} else {
return false;
}
};
// ){
// return true;
// }
// else {
// return false;
// }
;
this.collidesWithObstacle = function (obstacles) {
for (let i = 0; i < obstacles.length; i++) {
if (obstacles[i] !== this && this.collidesWith(obstacles[i])) {
return true;
}
}
return false;
};
this.draw = function (obstacles) {
image(enemy, this.x, this.y, this.size, this.size, 0, 0);
if (gamePlay == true) {
if (score<20)
{this.velX = -4;}
else if (score>20){
this.velX = -8
}
else if (score>40){
this.velX = -16
}
this.x = this.x + this.velX;
// Check if the obstacle collides with another obstacle
while (this.collidesWithObstacle(obstacles)) {
this.x = width + getRandomValue(100, 600, 80);
this.y = getRandomValue(10, 400, 50);
}
if (this.x < -this.size) {
this.x = width + getRandomValue(100, 600, 80);
this.y = getRandomValue(10, 400, 50);
}
// Check for collision with the player
if (this.collidesWith(naruto)) {
timerStarted = false;
gamePlay = false;
display = 1;
}
}
};
}
function mainScreen(){
noStroke();
image(mainbg, 0,0,600, 500, 0, 0);
// image(ground, -70, height- 80);
// let c1 = color()
fill("#F17201")
textFont(Nfont);
textSize(50);
text("S H I N O B I S P R I N T", 300, 180);
textAlign(CENTER);
// image(sprite_shiz, 75, 400);
fill("#A9CC0D60");
rect(140, 250, 300, 100);
fill("#03BF97")
text("PLAY" , 290, 320);
fill("#F17201")
textSize(30);
text("C L I C K PLAY T O START!", 300, 430);
}
function gameOver(){
noStroke();
image(mainbg, 0,0,600, 500, 0, 0);
// image(ground, -70, height- 80);
// let c1 = color()
fill("#F10101")
textFont(Nfont);
textSize(40);
textAlign(CENTER);
text("G A M E O V E R", 300, 120);
fill("#F17201")
textSize(50);
text("S C O R E : "+ score, 300 , 200);
image(sprite_shiz, 75, 400);
fill("#A9CC0D60");
rect(155, 250, 300, 100);
// fill("#03BF97")
textSize(35);
fill("#F10101")
text("P L A Y A G A I N" , 305, 320);
}
function getRandomValue(min, max, step) {
// Calculate the range of values
let range = (max - min) / step;
// Generate a random index within the range
let index = floor(random(range));
// Calculate the random value based on the index and step
let value = min + (index * step);
console.log(value);
return value;
}
function readSerial(data) {
////////////////////////////////////
//READ FROM ARDUINO HERE
////////////////////////////////////
if (data != null) {
// make sure there is actually a message
// split the message
let fromArduino = trim(data);
// if the right length, then proceed
distance = fromArduino;
distance = int(distance);
//////////////////////////////////
//SEND TO ARDUINO HERE (handshake)
//////////////////////////////////
// let sendToArduino = light + "\n";
// writeSerial(sendToArduino);
// light = 0;
}
}
function mousePressed() {
if (mouseX > 140 && mouseX<=440 && mouseY>250 && mouseY<350){
fullscreen();
gamePlay = true;
if (!timerStarted) {
startTime = millis();
timerStarted = true;
}
if (!serialActive){
setUpSerial();
}
// res
score = 0;
bx = -40;
bx2 = 550;
tx = 200;
tx2 = 600;
t2x = 500;
t2x2 = 600;
ex = 0;
ex2 = 600;
naruto.y = height - 94;
// Reset obstacle positions
for (let i = 0; i < obstacles.length; i++) {
obstacles[i].x = width + getRandomValue(100, 600, 80);
obstacles[i].y = getRandomValue(0, height - 50, 70);
}
}
}