xxxxxxxxxx
168
// Define global variables
let player, bullets;
function setup() {
createCanvas(400, 400);
player = new Player(width / 2, height / 2); // create a player object
bullets = []; // create an empty array for bullets
}
function draw() {
background(220);
// Handle player movement and shooting
player.handleInput();
player.update();
player.display();
// Handle bullet movement and collision detection
for (let i = bullets.length - 1; i >= 0; i--) {
bullets[i].update();
bullets[i].display();
if (bullets[i].offscreen()) {
bullets.splice(i, 1); // remove bullet if it goes offscreen
}
// } else if (bullets[i].hits(player)) {
// console.log("Player hit!"); // handle player being hit by bullet
// bullets.splice(i, 1);
// }
}
}
// Player class definition
class Player {
constructor(x, y) {
this.pos = createVector(x, y); // player position
this.vel = createVector(0, 0); // player velocity
this.speed = 3; // player movement speed
this.angle = 0; // player facing direction (in radians)
this.size = 20; // player size
this.lastShotTime = 0;
}
handleInput() {
let dx = 0;
let dy = 0;
// Turn the player based on arrow keys
if (keyIsDown(LEFT_ARROW) || keyIsDown(65)) {
dx -= this.speed;
}
if (keyIsDown(RIGHT_ARROW) || keyIsDown(68)) {
dx += this.speed;
}
if (keyIsDown(UP_ARROW) || keyIsDown(87)) {
dy -= this.speed;
}
if (keyIsDown(DOWN_ARROW) || keyIsDown(83)) {
dy += this.speed;
}
// Update the player's angle based on the direction of movement
if (dx === 0 && dy === 0) {
// Don't update the angle if the player is not moving
} else if (dx === 0) {
// Vertical movement only
if (dy < 0) {
this.angle = radians(270); // Set angle to up
} else {
this.angle = radians(90); // Set angle to down
}
} else if (dy === 0) {
// Horizontal movement only
if (dx < 0) {
this.angle = radians(180); // Set angle to left
} else {
this.angle = radians(0); // Set angle to right
}
} else {
// Diagonal movement
if (dx < 0 && dy < 0) {
this.angle = radians(225);//); // Set angle to up-left
} else if (dx > 0 && dy < 0) {
this.angle = radians(315);//225); // Set angle to up-right
} else if (dx < 0 && dy > 0) {
this.angle = radians(135);//); // Set angle to down-left
} else if (dx > 0 && dy > 0) {
console.log('here')
this.angle = radians(45); // Set angle to down-right
}
}
// Normalize diagonal movement
if (dx !== 0 && dy !== 0) {
dx *= Math.sqrt(2) / 2;
dy *= Math.sqrt(2) / 2;
}
// Update the player's position
this.pos.x += dx;
this.pos.y += dy;
let currentTime = millis();
let timeSinceLastShot = currentTime - this.lastShotTime;
if (keyIsDown(32) && timeSinceLastShot > 500) {
// Enough time has passed, so update lastShotTime and fire a bullet
this.lastShotTime = currentTime;
bullets.push(new Bullet(this.pos.x, this.pos.y, this.angle));
}
}
update() {
this.pos.add(this.vel); // update player position based on velocity
this.pos.x = constrain(this.pos.x, 0, width); // keep player within screen bounds
this.pos.y = constrain(this.pos.y, 0, height);
}
display() {
push();
translate(this.pos.x, this.pos.y); // translate to player position
rotate(this.angle); // rotate to player facing direction
fill(0, 255, 0);
rectMode(CENTER);
rect(0, 0, this.size, this.size); // draw player as a green square
fill(0,0,0);
circle(this.size/2,this.size/3,5);
circle(this.size/2,-this.size/3,5);
pop();
}
}
// Bullet class definition
class Bullet {
constructor(x, y, angle) {
this.pos = createVector(x, y); // bullet position
this.vel = p5.Vector.fromAngle(angle); // bullet velocity in direction of player facing
this.vel.mult(10); // bullet speed
this.size = 5; // bullet size
this.angle = angle; // Store the angle for future use
}
// Bullet update method
update() {
this.pos.add(this.vel); // update bullet position based on velocity
}
// Bullet display method
display() {
fill(255, 0, 0);
ellipse(this.pos.x, this.pos.y, this.size, this.size); // draw bullet as a red circle
}
// Bullet offscreen method
offscreen() {
// return true if bullet is offscreen
return (this.pos.x < 0 || this.pos.x > width || this.pos.y < 0 || this.pos.y > height);
}
// Bullet hits method
hits(player) {
// return true if bullet hits player
let d = dist(this.pos.x, this.pos.y, player.pos.x, player.pos.y);
return (d < player.size / 2);
}
}