-
-
Save Elmuti/c8f176410de8d88a9000 to your computer and use it in GitHub Desktop.
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 = 64; | |
y = 500; | |
Speed = 250; | |
Texture = nil; | |
} | |
isAlive = true | |
score = 0 | |
highscore = 0 | |
ammo = 50 | |
health = 100 | |
-- Timers | |
canShoot = true | |
canShootTimerMax = 0.2 | |
canShootTimer = canShootTimerMax | |
createEnemyTimerMax = 0.4 | |
createEnemyTimer = createEnemyTimerMax | |
-- Image Storage | |
bulletTex = nil | |
enemyImg = nil | |
ammoImg = nil | |
-- Entity Storage | |
bullets = {} -- array of current bullets being drawn and updated | |
--e_bullets = {} -- array of current enemy bullets being drawn and updated | |
enemies = {} | |
width, height = love.window.getDimensions( ) | |
-- 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 | |
function flipCoin() | |
local bools = {true, false} | |
return bools[math.random(1,#bools)] | |
end | |
function love.load(arg) | |
love.window.setTitle("Wagnerian Airplane Simulator") | |
collisionSnd = love.audio.newSource("assets/collision.ogg", "static") | |
weaponSnd = love.audio.newSource("assets/weapon.ogg", "static") | |
ammoSnd = love.audio.newSource("assets/ammo.wav", "static") | |
hpSnd = love.audio.newSource("assets/health.wav", "static") | |
player.Texture = love.graphics.newImage("assets/player.png") | |
playerImg = love.graphics.newImage("assets/player.png") | |
hurtTex = love.graphics.newImage("assets/playerhurt.png") | |
bulletTex = love.graphics.newImage('assets/bullet.png') | |
enemyImg = love.graphics.newImage('assets/enemy.png') | |
ammoImg = love.graphics.newImage("assets/ammo.png") | |
healthImg = love.graphics.newImage("assets/health.png") | |
skyImg = love.graphics.newImage("assets/sky.png") | |
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.Texture:getWidth()) then | |
player.x = player.x + (player.Speed * dt) | |
end | |
end | |
if love.keyboard.isDown(' ', 'rctrl', 'lctrl', 'ctrl') and canShoot and isAlive and ammo > 0 then | |
-- Create some bullets | |
ammo = ammo - 1 | |
weaponSnd:stop() | |
weaponSnd:play() | |
newBullet = { | |
x = player.x + (player.Texture:getWidth()/2), | |
y = player.y, | |
img = bulletTex | |
} | |
table.insert(bullets, newBullet) | |
canShoot = false | |
canShootTimer = canShootTimerMax | |
end | |
-- Time out how far apart our shots can be. | |
canShootTimer = canShootTimer - (1 * dt) | |
if canShootTimer < 0 then | |
canShoot = true | |
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(10, love.graphics.getWidth() - 10) | |
local ranNum = math.random(1, 100) | |
if ranNum < 3 then | |
local isHealth = flipCoin() | |
if isHealth then | |
newEnemy = { x = randomNumber, y = -64, img = healthImg, isAmmo = false, isHealth = true} | |
else | |
newEnemy = { x = randomNumber, y = -64, img = ammoImg, isAmmo = true, isHealth = false} | |
end | |
else | |
newEnemy = { x = randomNumber, y = -64, img = enemyImg, isAmmo = false, isHealth = false} | |
end | |
table.insert(enemies, newEnemy) | |
end | |
-- update the positions of enemies | |
for i, enemy in ipairs(enemies) do | |
enemy.y = enemy.y + (100 * dt) | |
if enemy.y > 850 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 | |
collisionSnd:stop() | |
collisionSnd:play() | |
table.remove(bullets, j) | |
table.remove(enemies, i) | |
score = score + 10 | |
end | |
end | |
if CheckCollision(enemy.x, enemy.y, enemy.img:getWidth(), enemy.img:getHeight(), player.x, player.y, player.Texture:getWidth(), player.Texture:getHeight()) | |
and isAlive then | |
if enemy.isHealth then | |
--acquired health! | |
table.remove(enemies, i) | |
hpSnd:stop() | |
hpSnd:play() | |
health = 100 | |
player.Texture = playerImg | |
elseif enemy.isAmmo then | |
--acquired ammunition! | |
table.remove(enemies, i) | |
ammoSnd:stop() | |
ammoSnd:play() | |
ammo = ammo + 50 | |
else | |
if health == 100 then | |
health = 50 | |
player.Texture = hurtTex | |
collisionSnd:stop() | |
collisionSnd:play() | |
table.remove(enemies, i) | |
else | |
health = 0 | |
collisionSnd:stop() | |
collisionSnd:play() | |
table.remove(enemies, i) | |
isAlive = false | |
canShoot = false | |
end | |
end | |
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 = 64 | |
player.y = 500 | |
-- reset our game state | |
if score >= highscore then | |
highscore = score | |
end | |
score = 0 | |
ammo = 50 | |
health = 100 | |
isAlive = true | |
canShoot = true | |
end | |
end | |
function love.draw(dt) | |
love.graphics.print("Highscore: "..tostring(highscore).."\nScore: "..tostring(score).."\nAmmo: "..tostring(ammo).."\nHealth: "..tostring(health).."\nResolution: "..width.."x"..height) | |
love.graphics.draw(skyImg, 0, 0) | |
if isAlive then | |
love.graphics.draw(player.Texture, player.x, player.y) | |
else | |
if score >= highscore then | |
love.graphics.print("NEW HIGHSCORE: "..tostring(score).."\nPress 'R' to restart", love.graphics:getWidth()/2-50, love.graphics:getHeight()/2-10) | |
else | |
love.graphics.print("SCORE: "..tostring(score).."\nPress 'R' to restart", love.graphics:getWidth()/2-50, love.graphics:getHeight()/2-10) | |
end | |
end | |
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 | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment