Skip to content

Instantly share code, notes, and snippets.

@nickgs
Created May 7, 2020 19:52
Show Gist options
  • Save nickgs/e9b1ea57f2d09d8c5e4580010a2b3d9a to your computer and use it in GitHub Desktop.
Save nickgs/e9b1ea57f2d09d8c5e4580010a2b3d9a to your computer and use it in GitHub Desktop.
let ship;
function setup() {
createCanvas(windowWidth, windowHeight);
background("black");
ship = new Ship(windowWidth / 2, windowHeight / 2);
}
function draw() {
background('black');
ship.draw();
if(keyIsPressed) {
// this code will execute when a key is held down.
if(keyCode === LEFT_ARROW) {
// if the user hits the left arrow, this block of code will execute.
ship.x -= 3;
}
if(keyCode === RIGHT_ARROW) {
// if the user hits the left arrow, this block of code will execute.
ship.x += 3;
}
if(keyCode === UP_ARROW) {
// if the user hits the left arrow, this block of code will execute.
ship.y -= 3;
}
if(keyCode === DOWN_ARROW) {
// if the user hits the left arrow, this block of code will execute.
ship.y += 3;
}
}
}
class Ship {
constructor(x, y) {
this.x = x;
this.y = y;
}
draw() {
fill("white");
triangle(this.x, this.y, this.x - 10, this.y + 30, this.x + 10, this.y + 30);
}
}
class Bullet {
constructor(x, y) {
this.x = x;
this.y = y;
}
draw() {
fill("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