Last active
May 6, 2019 18:16
-
-
Save nickgs/ca92c4ab8c59a32aa88b0ac111ffd060 to your computer and use it in GitHub Desktop.
Our ship shooter game
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
var ship; // the games ship. | |
var enemies = []; | |
function setup() { | |
createCanvas(800, 600); | |
ship = new Ship(100, 100); | |
} | |
function draw() { | |
background(255,255,255, 90); | |
if(frameCount % 100 == 0) { | |
enemies.push(new Enemy(random(600), -10)); | |
} | |
for(var i = 0; i < enemies.length; i++) { | |
enemies[i].update(); | |
enemies[i].draw(); | |
for(var j = 0; j < ship.blaster.length; j++) { | |
// Code here is going to be execute for each Ray in the blaster | |
var distance = dist(enemies[i].p.x, enemies[i].p.y,ship.blaster[j].p.x, ship.blaster[j].p.y); | |
if(distance < 20) { | |
// This code will execute when the distance < 20. | |
enemies[i].live = false; | |
} | |
} | |
} | |
ship.update(); | |
ship.draw(); | |
} | |
/** | |
* Our Ship. | |
*/ | |
class Ship { | |
constructor(x, y) { | |
this.p = createVector(x, y); | |
this.v = createVector(0, 0); | |
this.size = 50; | |
this.blaster = []; | |
} | |
update() { | |
if(keyIsPressed === true) { | |
if(keyCode == RIGHT_ARROW) { | |
this.v.x += 1; | |
} | |
if(keyCode == LEFT_ARROW) { | |
this.v.x -= 1; | |
} | |
if(keyCode == UP_ARROW) { | |
this.v.y -= 1; | |
} | |
if(keyCode == DOWN_ARROW) { | |
this.v.y += 1; | |
} | |
if(keyCode == 32) { | |
this.blaster.push(new Ray(this.p.x, this.p.y)); | |
} | |
} | |
// Add the velocity to the position. | |
// Some simple phyics. | |
this.p.x += this.v.x; | |
this.p.y += this.v.y; | |
// Lets test our boundaries. | |
if(this.p.x >= width) { | |
this.p.x = 10; | |
} | |
if(this.p.x <= 0) { | |
this.p.x = width; | |
} | |
if(this.p.y >= height) { | |
this.p.y = 10; | |
} | |
if(this.p.y <= 0) { | |
this.p.y = height; | |
} | |
// decelerate our velocity by a fixed (de)cceratation. | |
this.v.x *= .9; | |
this.v.y *= .9; | |
for(var i = 0; i < this.blaster.length; i++) { | |
this.blaster[i].update(); | |
} | |
} | |
draw() { | |
fill("black"); | |
noStroke(); | |
triangle(this.p.x, | |
this.p.y, | |
this.p.x + this.size * .4, | |
this.p.y + this.size, | |
this.p.x - this.size * .4, | |
this.p.y + this.size ) | |
for(var i = 0; i < this.blaster.length; i++) { | |
this.blaster[i].draw(); | |
} | |
} | |
} | |
// End our Ship. | |
class Ray { | |
constructor(x, y) { | |
this.p = createVector(x, y); | |
this.v = createVector(0, 0); | |
this.len = 10; | |
} | |
update() { | |
this.p.y--; | |
} | |
draw() { | |
stroke("red"); | |
line(this.p.x, | |
this.p.y, | |
this.p.x, | |
this.p.y - this.len); | |
} | |
} | |
// Lets describe our Enemy. | |
class Enemy { | |
constructor(x, y) { | |
this.p = createVector(x, y); | |
this.live = true; | |
} | |
update() { | |
this.p.y++; | |
} | |
draw() { | |
fill("blue"); | |
if(this.live == false) { | |
fill("red"); | |
} | |
square(this.p.x, this.p.y, 20); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment