Skip to content

Instantly share code, notes, and snippets.

@nickgs
Last active December 13, 2023 17:01
Show Gist options
  • Save nickgs/dced0ad7ed4f39dac110231a935375d2 to your computer and use it in GitHub Desktop.
Save nickgs/dced0ad7ed4f39dac110231a935375d2 to your computer and use it in GitHub Desktop.
ShipShooter.js WIP
var xpos = 200
var ypos = 200
var speed = 10
var ship
var enemies = [] // Our array of enemies. This will store ALL the enemies.
function setup() {
createCanvas(windowWidth, windowHeight);
background("black");
// Lets build a ship.
ship = new Ship()
}
function draw() {
background("black")
// Using dot notation to access the update and draw method.
ship.update()
ship.draw()
if(frameCount % 100 == 0) {
// console.log("Spawning new enemy")
enemies.push(new Enemy())
}
// Here we need to use the enemies but updating their position and drawing them.
for(var i = 0; i < enemies.length; i++) {
enemies[i].update()
enemies[i].draw()
// We loop over all lasers.
for(var j = 0; j < ship.lasers.length; j++) {
// Detect if we hit the enemy.
if(ship.lasers[j].ypos <= enemies[i].ypos) {
console.log("WE HIT AN ENEMY")
enemies[i].alive = false
}
}
}
if(keyIsDown(32)) {
ship.fire()
}
// Collision detection.
checkCollisions()
}
function checkCollisions() {
if(ship.lasers.length == 0) {
return
}
}
// Define our blueprint for a ship.
class Ship {
constructor() {
// Define properties in the constructor
this.xpos = 100;
this.ypos = 100;
// Bolts on a laser array.
this.lasers = []
}
draw() {
// console.log("We are drawing our ship")
fill("white");
noStroke();
triangle(this.xpos, this.ypos, this.xpos+28, this.ypos-45, this.xpos+56, this.ypos);
// We need to loop over every laser and update and draw them.
for(var i = 0; i < this.lasers.length; i++) {
this.lasers[i].update()
this.lasers[i].draw()
}
}
update() {
// console.log("Moving our ship")
console.log(keyCode)
// We need to detect if the keyCode == 68
if(keyIsDown(68)) {
// console.log("YOU HIT THE D KEY")
// Move code code would go here.
this.xpos = this.xpos + speed
}
if(keyIsDown(87)) {
//console.log("YOU HIT THE W KEY")
// Move code code would go here.
this.ypos = this.ypos - speed;
}
if(keyIsDown(65)) {
// console.log("YOU HIT THE A KEY")
// Move code code would go here.
this.xpos = this.xpos - speed;
}
if(keyIsDown(83)) {
// console.log("YOU HIT THE S KEY")
// Move code code would go here.
this.ypos = this.ypos + speed;
}
}
fire() {
console.log(this.lasers)
this.lasers.push(new Laser(this.xpos, this.ypos))
}
}
class Enemy {
constructor() {
this.xpos = random(windowWidth)
this.ypos = random(-100)
this.alive = true
}
draw() {
// console.log("Drawing an enemy")
if(this.alive) {
fill("blue");
noStroke();
rect(this.xpos, this.ypos, 100, 100)
}
}
update() {
// console.log("Updating an enemy")
this.ypos = this.ypos + 1;
}
}
class Laser {
constructor(x, y) {
this.xpos = x;
this.ypos = y;
}
update() {
this.ypos -= 5
}
draw() {
stroke("green")
strokeWeight(5)
line(this.xpos + 28, this.ypos - 50, this.xpos + 28, this.ypos - 50)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment