Skip to content

Instantly share code, notes, and snippets.

@starzonmyarmz
Last active January 18, 2022 14:11
Show Gist options
  • Save starzonmyarmz/23bdbebf7058d8f849ccf50284ded586 to your computer and use it in GitHub Desktop.
Save starzonmyarmz/23bdbebf7058d8f849ccf50284ded586 to your computer and use it in GitHub Desktop.
Top Down Shooter
function Bullet(x, y) {
this.x = x
this.y = y
this.r = 3
this.update = function() {
this.y -= 3
}
this.render = function() {
fill('red')
noStroke()
circle(this.x, this.y, this.r * 2)
}
}
<!doctype html>
<html>
<head></head>
<body>
<script src="p5.js"></script>
<script src="zombie.js"></script>
<script src="bullet.js"></script>
<script src="player.js"></script>
<script src="script.js"></script>
</body>
</html>
function Player(x, y) {
this.x = x
this.y = y
this.r = 10
this.render = function() {
fill(0)
noStroke()
circle(this.x, this.y, this.r * 2)
}
}
let player
let bullets = []
let zombies = []
let timer = 3000
let change_timer
function setup() {
createCanvas(400, 480)
player = new Player(400 / 2, 400)
change_timer = millis() + 1000
}
function draw() {
background('#efefef')
player.render()
bullets.forEach(function(bullet, index) {
bullet.update()
bullet.render()
if (bullet.y < -50) {
bullets.splice(index, 1)
}
zombies.forEach(function(zombie, index) {
if (dist(zombie.x, zombie.y, bullet.x, bullet.y) <= zombie.r + bullet.r) {
bullets.splice(index, 1)
zombies.splice(index, 1)
}
})
})
zombies.forEach(function(zombie) {
zombie.update()
zombie.render()
if (dist(zombie.x, zombie.y, player.x, player.y) <= zombie.r + player.r) {
console.log('game over')
}
})
if (millis() > change_timer) {
zombies.push(new Zombie(random(50, 350), random(-50, 100)))
change_timer = millis() + timer
}
}
function keyPressed() {
if (keyCode == LEFT_ARROW) {
player.x -= 5
}
if (keyCode == RIGHT_ARROW) {
player.x += 5
}
if (keyCode == 32) {
bullets.push(new Bullet(player.x, player.y))
}
}
function Zombie(x, y) {
this.x = x
this.y = y
this.r = 15
this.update = function() {
this.y += 2
}
this.render = function() {
fill('green')
noStroke()
circle(this.x, this.y, this.r * 2)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment