Created
November 13, 2024 18:30
-
-
Save nickgs/1774a56b9d48f3fb8795a18f4e94416d to your computer and use it in GitHub Desktop.
Shipshooter w/ Test Laser
This file contains 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
/** | |
Let's define our global variables. | |
These values will be available within any | |
function or method. | |
**/ | |
var ship; | |
var laser; | |
/****************** | |
Our Laser Blueprint | |
*******************/ | |
class Laser { | |
constructor(x, y) { | |
// initial values | |
this.x = x | |
this.y = y | |
} | |
draw() { | |
fill("red") | |
rect(this.x, this.y, 5, 50) | |
} | |
move() { | |
this.y = this.y - 10 | |
} | |
} | |
/****************** | |
Our Ship Blueprint | |
*******************/ | |
class Ship { | |
constructor() { | |
this.x = 10 | |
this.y = 10 | |
this.shipSize = 15; | |
this.shipSpeed = 5; | |
} | |
move() { | |
// Check which key is pressed. | |
if(keyIsDown(UP_ARROW) || keyIsDown(87)) { | |
this.y = this.y - this.shipSpeed; | |
} | |
if(keyIsDown(DOWN_ARROW)) { | |
this.y = this.y + this.shipSpeed; | |
} | |
if(keyIsDown(LEFT_ARROW)) { | |
this.x = this.x - this.shipSpeed; | |
} | |
if(keyIsDown(RIGHT_ARROW)) { | |
this.x = this.x + this.shipSpeed; | |
} | |
} | |
draw() { | |
// Let's draw our ship. | |
fill("white"); | |
noStroke(); | |
triangle(this.x, this.y, this.x-this.shipSize,this.y+40, this.x+this.shipSize, this.y+40); | |
} | |
} | |
/** | |
Let's define our p5js functions | |
**/ | |
// function preload() { | |
// shipImg = loadImage("ship.png") | |
// laserImg = loadImage("laser.png") | |
// } | |
function setup() { | |
createCanvas(400, 400); | |
ship = new Ship(); | |
laser = new Laser(200, 400); | |
} | |
function draw() { | |
background("black"); | |
ship.move(); | |
ship.draw(); | |
laser.move(); | |
laser.draw(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment