xxxxxxxxxx
277
let level = 2;
let score = 0;
let spaceship;
let spaceshipImage;
let alienImage; // Added variable for alien image
let potVal = 450;
let left = 0;
let right = 0;
let shoot = 0;
let lastShootTime = 0;
let shootInterval = 500;
let alienShootInterval = 2000;
let startTime;
let gameDuration = 20 * 1000;
let isSerialConnected = false;
let lasers = [];
let aliens = [];
let isGameOver = false;
// Preload function to load images
function preload() {
spaceshipImage = loadImage("spaceship.png");
alienImage = loadImage("alien.png"); // Load the alien image
}
function setup() {
createCanvas(windowWidth, windowHeight);
spaceship = new Spaceship();
// Generate a total of 2 aliens at different locations
for (let i = 0; i < 2; i++) {
aliens.push(new Alien());
}
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
function draw() {
if (isGameOver) {
showGameOver();
return; // Stop executing the rest of the draw function
}
background(100);
if (isSerialConnected) {
spaceship.display();
spaceship.update();
updateLasers();
if (level === 1) {
fill(200);
text("this is level one", 100, 100);
} else if (level === 2) {
// Display and update aliens
for (let alien of aliens) {
alien.display();
alien.update();
// Check for collisions with spaceship lasers
for (let i = lasers.length - 1; i >= 0; i--) {
if (lasers[i].hits(spaceship) && isSerialConnected) {
lasers.splice(i, 1);
// Implement the desired behavior when an alien laser hits the spaceship
gameOver();
}
}
// Check for collisions with spaceship lasers
for (let i = lasers.length - 1; i >= 0; i--) {
if (lasers[i].hits(alien) && isSerialConnected) {
lasers.splice(i, 1);
alien.hit(); // Handle the alien being hit
score++; // Increase the score, for example
}
}
// Check if the alien should shoot
if (millis() - alien.lastShootTime >= alien.alienShootInterval) {
alien.shootTowards(spaceship.y);
alien.lastShootTime = millis(); // Update the last shoot time for the alien
}
}
}
} else {
fill(220);
textSize(30);
text("Galaxian 2.0", width / 3 + 20, height / 2 - 40);
text("Press Space Bar to start", width / 4, height / 2);
}
console.log(potVal);
console.log(shoot);
}
function gameOver() {
noLoop(); // Stop the draw loop
// Call a function from gameover.js to display the game over screen
showGameOver();
}
class Alien {
constructor() {
this.width = 40; // Set width to 40
this.height = 40; // Set height to 40
this.x = random(width - this.width);
this.y = -this.height; // Start from the top of the screen
this.speed = 2;
this.lastShootTime = 0;
this.alienShootInterval = alienShootInterval;
}
display() {
image(alienImage, this.x, this.y, this.width, this.height); // Draw alien image
}
update() {
// Move the alien down
this.y += this.speed;
// Reset if the alien goes off the bottom
if (this.y > height) {
this.y = -this.height;
this.x = random(width - this.width);
}
}
shootTowards(spaceshipY) {
// Calculate the angle to shoot towards the spaceship's y-coordinate
let angle = atan2(spaceshipY - this.y, spaceship.x - this.x);
// Shoot a laser towards the spaceship
let alienLaser = new Laser(
this.x + this.width / 2,
this.y + this.height / 2,
degrees(angle)
);
lasers.push(alienLaser);
}
}
// ... (The rest of your code remains the same)
class Spaceship {
constructor() {
this.width = 70;
this.height = 70;
this.x = width / 2 - this.width / 2;
this.y = height - this.height;
this.speed = 5;
this.rotation = 0; // Initial rotation angle
}
display() {
push();
translate(this.x + this.width / 2, this.y + this.height / 2);
rotate(radians(this.rotation));
imageMode(CENTER);
image(spaceshipImage, 0, 0, this.width, this.height);
pop();
}
update() {
if (keyIsDown(UP_ARROW)) {
this.y -= this.speed;
} else if (keyIsDown(DOWN_ARROW)) {
this.y += this.speed;
}
// Ensure the spaceship stays within the bounds of the canvas height
this.y = constrain(this.y, 0, height - this.height);
// Map potVal from the range 0-1023 to -90 to 90 for rotation
this.rotation = map(potVal, 100, 800, -90, 90);
// Check if enough time has passed since the last shoot
if (shoot === 1 && millis() - lastShootTime >= shootInterval) {
// Shoot a laser
let laser = new Laser(
this.x + this.width / 2,
this.y + this.height / 2,
this.rotation - 100
);
lasers.push(laser);
lastShootTime = millis(); // Update the last shoot time
shoot = 0; // Reset shoot to avoid continuous shooting
}
}
}
class Laser {
constructor(x, y, rotation) {
this.x = x;
this.y = y;
this.speed = 6;
this.rotation = rotation;
}
display() {
push();
translate(this.x, this.y);
rotate(radians(this.rotation));
stroke(255, 0, 0);
strokeWeight(2);
line(0, 0, 20, 0); // Adjust the length of the laser as needed
pop();
}
update() {
this.x += this.speed * cos(radians(this.rotation));
this.y += this.speed * sin(radians(this.rotation));
}
offscreen() {
return this.x > width || this.x < 0 || this.y > height || this.y < 0;
}
hits(target) {
// Check if the laser hits the target (e.g., spaceship or alien)
let d = dist(this.x, this.y, target.x, target.y);
return d < target.width / 2; // Assuming target has a width property
}
}
function updateLasers() {
for (let i = lasers.length - 1; i >= 0; i--) {
lasers[i].display();
lasers[i].update();
if (lasers[i].offscreen()) {
lasers.splice(i, 1);
}
}
}
function keyPressed() {
if (key == " ") {
// important to have in order to start the serial connection!!
setUpSerial();
isSerialConnected = true; // Update connection status
startTime = millis(); // Start the timer when the connection is made
}
}
// This function will be called by the web-serial library
// with each new *line* of data. The serial library reads
// the data until the newline and then gives it to us through
// this callback function
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 the right length, then proceed
if (fromArduino.length == 2) {
// 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
potVal = int(fromArduino[0]);
shoot = int(fromArduino[1]);
}
//////////////////////////////////
//SEND TO ARDUINO HERE (handshake)
//////////////////////////////////
let sendToArduino = left + "," + right + "\n";
writeSerial(sendToArduino);
}
}