Last active
December 8, 2020 15:25
-
-
Save nickgs/238aee196b9ee6c81655f1661bdef2df to your computer and use it in GitHub Desktop.
Ship Shooter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Our Ship Shooter Game. | |
let ship; | |
function setup() { | |
createCanvas(windowWidth, windowHeight); | |
background("black"); | |
// Build the ship and store it in our ship variable. | |
ship = new Ship(); | |
} | |
function draw() { | |
background("black"); | |
// we are calling the ships behaviors. | |
ship.update(); | |
ship.draw(); | |
if(keyIsDown(32)) { | |
ship.fire(); | |
} | |
} | |
// Lets define our Ship | |
class Ship { | |
// All the behavior of our ship will be created here. | |
constructor() { | |
// This is our setup function for our ship. | |
this.x = windowWidth / 2; | |
this.y = windowHeight / 2; | |
this.bullets = []; // bolt on our laser cannons! | |
} | |
// Describe how a ship should draw to the screen. | |
draw() { | |
fill("white"); | |
noStroke(); | |
triangle(this.x, this.y, this.x - 10, this.y + 30, this.x + 10, this.y + 30); | |
for(let i = 0; i < this.bullets.length; i++) { | |
this.bullets[i].update(); | |
this.bullets[i].draw(); | |
} | |
} | |
update() { | |
if(keyIsDown(RIGHT_ARROW)) { | |
// What goes here?? | |
this.x += 5; | |
} | |
if(keyIsDown(LEFT_ARROW)) { | |
// What needs to change here? | |
this.x -= 5; | |
} | |
if(keyIsDown(UP_ARROW)) { | |
// What needs to change here? | |
this.y -= 5; | |
} | |
if(keyIsDown(DOWN_ARROW)) { | |
// What needs to change here? | |
this.y += 5; | |
} | |
} | |
fire() { | |
// we now have method where we can decribe the fire behavior. | |
console.log(this.bullets); | |
this.bullets.push(new Bullet(this.x, this.y)); | |
} | |
} | |
class Bullet { | |
constructor(x, y) { | |
this.x = x; | |
this.y = y; | |
} | |
update() { | |
this.y -= 5; | |
} | |
draw() { | |
stroke("red"); | |
line(this.x, this.y, this.x, this.y - 10); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment