xxxxxxxxxx
254
let obstacles;
let gameState = "menu";
let Birds;
let bullets;
let randint;
let score;
let lost;
let next;
let spread;
let slider;
let data;
function setup() {
createCanvas(700, 450);
bg = loadImage('assets/bck.png');
cactus = loadImage('assets/hindi.png');
birdy = loadImage ('assets/bird.png');
go_song= loadSound('assets/gameover.mp3');
bird_sound = loadSound('assets/birdsound.mp3');
textSize(24)
slider = createSlider(10, 20, 12, 1)
slider.position(width - slider.width, 0)
resetSketch()
text(score, 5, 24);
}
function keyPressed() {
if (key == ' ') {
fireBullet();
if (lost) {
resetSketch();
}
}
if (key == "c") {
// important to have in order to start the serial connection!!
setUpSerial();
}
if (key == "j") {
// important to have in order to start the serial connection!!
dinosaur.jump();
}
if (key == 'f' || key == 'F') { // Change 'f' to any key you want to use for firing bullets
fireBullet();
}
if (keyCode == ENTER) {
if (gameState == "menu") {
gameState = "playing";
} else if (gameState == "gameOver") {
resetSketch();
gameState = "playing";
}
}
}
function displayMenu() {
background(bg);
if (!serialActive) {
text("Press C to select Serial Port", 130, 30);
} else {
text("Connected",130, 30 );
}
textAlign(CENTER);
textSize(32);
text("Neuro-jump", width / 2, height / 2 - 50);
textSize(24);
text("Instructions:", width / 2, height / 2);
textSize(16);
text("FLEX to jump", width / 2, height / 2 + 30);
text("Press SPACE to shoot", width / 2, height / 2 + 60);
text("Press ENTER to start the game", width / 2, height / 2 + 90);
}
function displayGameOver() {
background(bg);
textAlign(CENTER);
textSize(32);
text("Game Over", width / 2, height / 2 - 50);
textSize(24);
//text("Score: " + score, width / 2, height / 2);
textSize(16);
text("Press ENTER to restart ", width / 2, height / 2 + 30);
}
function resetSketch() {
console.log("Restarting game")
score = 0;
lost = false;
obstacles = [];
Birds = []; // Make sure to initialize the Birds array
bullets = [];
next = 0;
dinosaur = new Dinosaur();
new Obstacle();
randint = int(random(50, 150));
loop();
}
function fireBullet() {
let bullet = new Bullet(dinosaur.x, dinosaur.y);
bullets.push(bullet);
}
function draw() {
if (gameState == "menu") {
displayMenu();
}
else if (gameState == "playing") {
background(bg);
text(score, 5, 24)
if (data != null) {
text('strength = ' + str(data), 130, 30);
}
text(score, 5, 24)
next += 1
if (next == randint) {
obstacles.push(new Obstacle())
// Random chance to add a bird with a 50% probability
if (random(1) < 0.5) {
Birds.push(new Bird()); // This line should work now
}
score += 1
next = 0
randint = int(random(40, width / 5))
}
for (let o of obstacles) {
if (o.x < 0) {
if (obstacles.length > 3) {
obstacles.shift()
}
}
o.move()
o.show();
if (dinosaur.hits(o)) {
console.log("Game Over!")
go_song.play()
gameState = "gameOver";
lost = true;
noLoop();
}
}
// Move and display birds
for (let b of Birds) {
if (b.x < 0) {
if (Birds.length > 3) {
Birds.shift()
}
}
b.move()
b.display();
if (dinosaur.hits(b)) {
console.log("Game Over! Hit by bird")
gameState = "gameOver";
bird_sound.play()
lost = true;
noLoop();
}
}
for (let b of bullets) {
b.move();
b.display();
// Check for bullet-bird intersection and remove the bullet and bird if they collide
for (let i = 0; i < Birds.length; i++) {
if (b.testIntersection(Birds[i])) {
console.log("Bullet hit bird!");
bullets.splice(bullets.indexOf(b), 1);
Birds.splice(i, 1);
break;
}
}
}
// if (!serialActive) {
// text("Press C to select Serial Port", 20, 30);
// } else {
// text("Connected", 20, 30);
// }
dinosaur.show();
dinosaur.move();
}
else if (gameState == "gameOver") {
displayGameOver();
if (data != null) {
text('strength = ' + str(data), 20, 50);
}
}
}
function readSerial(newData) {
////////////////////////////////////
//READ FROM ARDUINO HERE
////////////////////////////////////
data = newData;
if (data != null) {
// make sure there is actually a message
// split the message
//0102print(data);
// text('strength = ' + str(data), 9, 100);
if (data>400){
print("jump");
dinosaur.jump();
}
// let fromArduino = split(trim(data), ",");
// if (fromArduino.length == 2) {
// rVal = int(fromArduino[0]);
// alpha = int(fromArduino[1]);
//////////////////////////////////
//SEND TO ARDUINO HERE (handshake)
}
}