Created
November 14, 2015 08:45
-
-
Save peterhellberg/ac9f10c9559bdcc7296a to your computer and use it in GitHub Desktop.
Scrolling Shooter from the tutorial “Your First Love2d Game in 200 Lines”
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
-- Configuration | |
function love.conf(t) | |
t.title = "Scrolling Shooter" | |
t.version = "0.9.2" | |
t.window.width = 480 | |
t.window.height = 700 | |
t.console = true | |
end |
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
debug = true | |
player = { x = 200, y = 610, speed = 150, img = nil } | |
isAlive = true | |
score = 0 | |
-- Timers | |
-- We declare these here so we don't have to edit them multiple places | |
canShoot = true | |
canShootTimerMax = 0.2 | |
canShootTimer = canShootTimerMax | |
-- Image Storage | |
bulletImg = nil | |
-- Entity Storage | |
bullets = {} -- array of current bullets being drawn and updated | |
--More timers | |
createEnemyTimerMax = 0.4 | |
createEnemyTimer = createEnemyTimerMax | |
-- More images | |
enemyImg = nil -- Like other images we'll pull this in during out love.load function | |
-- More storage | |
enemies = {} -- array of current enemies on screen | |
function love.load(arg) | |
player.img = love.graphics.newImage('assets/plane.png') | |
bulletImg = love.graphics.newImage('assets/bullet.png') | |
enemyImg = love.graphics.newImage('assets/enemy.png') | |
gunSound = love.audio.newSource('assets/gun-sound.wav', 'static') | |
end | |
function love.update(dt) | |
if love.keyboard.isDown('escape') then | |
love.event.push('quit') | |
end | |
if love.keyboard.isDown('left','a') then | |
if player.x > 0 then -- binds us to the map | |
player.x = player.x - (player.speed*dt) | |
end | |
elseif love.keyboard.isDown('right','d') then | |
if player.x < (love.graphics.getWidth() - player.img:getWidth()) then | |
player.x = player.x + (player.speed*dt) | |
end | |
end | |
-- Time out how far apart our shots can be. | |
canShootTimer = canShootTimer - (1 * dt) | |
if canShootTimer < 0 then | |
canShoot = true | |
end | |
if love.keyboard.isDown(' ', 'rctrl', 'lctrl', 'ctrl') and canShoot then | |
-- Create some bullets | |
newBullet = { x = player.x + (player.img:getWidth()/2), y = player.y, img = bulletImg } | |
table.insert(bullets, newBullet) | |
gunSound:play() | |
canShoot = false | |
canShootTimer = canShootTimerMax | |
end | |
-- Vertical movement | |
if love.keyboard.isDown('up', 'w') then | |
if player.y > (love.graphics.getHeight() / 2) then | |
player.y = player.y - (player.speed*dt) | |
end | |
elseif love.keyboard.isDown('down', 's') then | |
if player.y < (love.graphics.getHeight() - 55) then | |
player.y = player.y + (player.speed*dt) | |
end | |
end | |
-- update the positions of bullets | |
for i, bullet in ipairs(bullets) do | |
bullet.y = bullet.y - (250 * dt) | |
if bullet.y < 0 then -- remove bullets when they pass off the screen | |
table.remove(bullets, i) | |
end | |
end | |
-- Time out enemy creation | |
createEnemyTimer = createEnemyTimer - (1 * dt) | |
if createEnemyTimer < 0 then | |
createEnemyTimer = createEnemyTimerMax | |
-- Create an enemy | |
randomNumber = math.random(80, love.graphics.getWidth() - 80) | |
newEnemy = { x = randomNumber, y = -10, img = enemyImg } | |
table.insert(enemies, newEnemy) | |
end | |
-- update the positions of enemies | |
for i, enemy in ipairs(enemies) do | |
enemy.y = enemy.y + (200 * dt) | |
if enemy.y > 700 then -- remove enemies when they pass off the screen | |
table.remove(enemies, i) | |
end | |
end | |
-- run our collision detection | |
-- Since there will be fewer enemies on screen than bullets we'll loop them first | |
-- Also, we need to see if the enemies hit our player | |
for i, enemy in ipairs(enemies) do | |
for j, bullet in ipairs(bullets) do | |
if CheckCollision(enemy.x, enemy.y, enemy.img:getWidth(), enemy.img:getHeight(), bullet.x, bullet.y, bullet.img:getWidth(), bullet.img:getHeight()) then | |
table.remove(bullets, j) | |
table.remove(enemies, i) | |
score = score + 1 | |
end | |
end | |
if CheckCollision(enemy.x, enemy.y, enemy.img:getWidth(), enemy.img:getHeight(), player.x, player.y, player.img:getWidth(), player.img:getHeight()) | |
and isAlive then | |
table.remove(enemies, i) | |
isAlive = false | |
end | |
end | |
if not isAlive and love.keyboard.isDown('r') then | |
-- remove all our bullets and enemies from screen | |
bullets = {} | |
enemies = {} | |
-- reset timers | |
canShootTimer = canShootTimerMax | |
createEnemyTimer = createEnemyTimerMax | |
-- move player back to default position | |
player.x = 200 | |
player.y = 610 | |
-- reset our game state | |
score = 0 | |
isAlive = true | |
end | |
end | |
function love.draw(dt) | |
for i, bullet in ipairs(bullets) do | |
love.graphics.draw(bullet.img, bullet.x, bullet.y) | |
end | |
for i, enemy in ipairs(enemies) do | |
love.graphics.draw(enemy.img, enemy.x, enemy.y) | |
end | |
if isAlive then | |
love.graphics.draw(player.img, player.x, player.y) | |
else | |
love.graphics.print("Press 'R' to restart", love.graphics:getWidth()/2-50, love.graphics:getHeight()/2-10) | |
end | |
end | |
-- Collision detection taken function from http://love2d.org/wiki/BoundingBox.lua | |
-- Returns true if two boxes overlap, false if they don't | |
-- x1,y1 are the left-top coords of the first box, while w1,h1 are its width and height | |
-- x2,y2,w2 & h2 are the same, but for the second box | |
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2) | |
return x1 < x2+w2 and | |
x2 < x1+w1 and | |
y1 < y2+h2 and | |
y2 < y1+h1 | |
end |
Author
peterhellberg
commented
Nov 14, 2015
Nice thx
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment