202 lines
5.2 KiB
JavaScript
202 lines
5.2 KiB
JavaScript
//--------------------Game--------------------
|
|
// Game variables
|
|
const width = 1000;
|
|
const height = 600;
|
|
|
|
// Player variables
|
|
const playerSize = 10;
|
|
let radius = playerSize / 2;
|
|
let playerPosX = 500
|
|
let playerPosY = 300;
|
|
let playerSpeed = 3;
|
|
let booleanArray = window.booleanArray;
|
|
let lives = 2;
|
|
|
|
let bossPosX = 500;
|
|
let bossPosY = 100;
|
|
let shotSpeed = 15;
|
|
let projectile;
|
|
let projSize = 5;
|
|
let shot;
|
|
let hit;
|
|
let shotPosX;
|
|
let shotPosY;
|
|
|
|
let nextAttack;
|
|
let bounceX = bossPosX;
|
|
let bounceY = bossPosY;
|
|
let predictiveBounceX;
|
|
let predictiveBounceY;
|
|
|
|
let time = 0;
|
|
|
|
let myBullet = new bullet();
|
|
// the function setup() is called once when the page is loaded
|
|
function setup() {
|
|
// create a canvas element and append it to the body
|
|
createCanvas(width, height);
|
|
frameRate(120);
|
|
// disable the outline of shapes
|
|
Movementloop()
|
|
noStroke();
|
|
}
|
|
|
|
async function score() {
|
|
while (true) {
|
|
time += 1;
|
|
await sleep(1000);
|
|
}
|
|
}
|
|
|
|
function life() {
|
|
textAlign(CENTER);
|
|
text(lives, 500, 20);
|
|
if (hit == true) {
|
|
hit = false;
|
|
lives -= 1;
|
|
}
|
|
if (lives == 0) {
|
|
fill(255, 0, 0);
|
|
textSize(40);
|
|
textAlign(CENTER);
|
|
text("Game Over", 500, 300);
|
|
}
|
|
if (key == ' ') {
|
|
lives = 2;
|
|
time = 0;
|
|
bounceX = bossPosX;
|
|
bounceY = bossPosY;
|
|
}
|
|
}
|
|
|
|
function keyPressed() {
|
|
if (keyIsDown(LEFT_ARROW) && playerPosX > 0 + radius) {
|
|
playerPosX -= playerSpeed;
|
|
}
|
|
if (keyIsDown(RIGHT_ARROW) && playerPosX < width - radius) {
|
|
playerPosX += playerSpeed;
|
|
}
|
|
if (keyIsDown(UP_ARROW) && playerPosY > 0 + radius) {
|
|
playerPosY -= playerSpeed;
|
|
}
|
|
if (keyIsDown(DOWN_ARROW) && playerPosY < height - radius) {
|
|
playerPosY += playerSpeed;
|
|
}
|
|
}
|
|
|
|
async function Movementloop() {
|
|
window.addEventListener('booleanArrayUpdated', function (event) {
|
|
// event.detail contains the booleanArray
|
|
let booleanArray = event.detail;
|
|
// Use booleanArray here...
|
|
|
|
if (booleanArray[1]) {
|
|
playerPosX += playerSpeed;
|
|
}
|
|
if (booleanArray[3]) {
|
|
playerPosX -= playerSpeed;
|
|
}
|
|
if (booleanArray[2]) {
|
|
playerPosY += playerSpeed;
|
|
}
|
|
if (booleanArray[0]) {
|
|
playerPosY -= playerSpeed;
|
|
}
|
|
});
|
|
}
|
|
|
|
function shoot(directionX, directionY) {
|
|
shot = true;
|
|
hit = false;
|
|
|
|
if (!projectile) {
|
|
// Set the initial position of the projectile to the player's position
|
|
projectile = createVector(shotPosX, shotPosY);
|
|
targetX = directionX;
|
|
targetY = directionY;
|
|
|
|
// Calculate the initial direction
|
|
direction = createVector(targetX - projectile.x, targetY - projectile.y);
|
|
direction.normalize();
|
|
}
|
|
|
|
// Draw the small circle (projectile)
|
|
fill(255, 0, 0);
|
|
circle(projectile.x, projectile.y, projSize);
|
|
|
|
// Move the projectile towards the movable circle
|
|
projectile.add(p5.Vector.mult(direction, shotSpeed));
|
|
|
|
if (dist(projectile.x, projectile.y, directionX, directionY) <= radius+(projSize/2)) {
|
|
projectile = null;
|
|
hit = true;
|
|
}
|
|
else if (projectile.x < 0 - 10 || projectile.x > width + 10 || projectile.y < 0 - 10 || projectile.y > height + 10) {
|
|
bounceX = projectile.x;
|
|
bounceY = projectile.y;
|
|
projectile = null;
|
|
shot = false;
|
|
}
|
|
}
|
|
|
|
// the function draw() is called every frame
|
|
function draw() {
|
|
shot = false;
|
|
textSize(10);
|
|
keyPressed();
|
|
life();
|
|
// draw background
|
|
myBullet.draw();
|
|
background(0, 0, 0, 100);
|
|
if (!(lives == 0)) {
|
|
// draw player
|
|
fill(0, 255, 255)
|
|
circle(constrain(playerPosX, 0 + radius, width - radius), constrain(playerPosY, 0 + radius, height - radius), playerSize);
|
|
|
|
// draw boss
|
|
fill(255, 165, 0)
|
|
circle(500, 100, 50);
|
|
|
|
score();
|
|
textAlign(LEFT);
|
|
text(time, 10, 20);
|
|
|
|
if (shot == false || hit == true) {
|
|
let patern = random([1]);
|
|
if (patern == 1) {
|
|
nextAttack = random([1, 2, 3, 4, 5]);
|
|
text(nextAttack, 30, 50);
|
|
if (nextAttack == 1) {
|
|
shotPosX = 500;
|
|
shotPosY = 100;
|
|
shoot(playerPosX, playerPosY);
|
|
}
|
|
if (nextAttack == 2) {
|
|
shotPosX = random(0, 1000);
|
|
shotPosY = 600;
|
|
shoot(playerPosX, playerPosY);
|
|
}
|
|
if (nextAttack == 3) {
|
|
shotPosX = 0;
|
|
shotPosY = random(0, 600);
|
|
shoot(playerPosX, playerPosY);
|
|
}
|
|
if (nextAttack == 4) {
|
|
shotPosX = 1000;
|
|
shotPosY = random(0, 600);
|
|
shoot(playerPosX, playerPosY);
|
|
}
|
|
if (nextAttack == 5) {
|
|
shotPosX = bounceX;
|
|
shotPosY = bounceY;
|
|
shoot(playerPosX, playerPosY);
|
|
}
|
|
}
|
|
// if (patern == 2) {
|
|
// shotPosX = bounceX;
|
|
// shotPosY = bounceY;
|
|
// shoot(playerPosX, playerPosY);
|
|
// }
|
|
}
|
|
}
|
|
} |