1, A way to pause all activity in the game when a button is pressed
2, A way to reset a game once the player has lost.
Since this is likely to need user coding, this is the code I am trying to implement it with:
Code: Select all
function love.load()
hero = {} --A table for the hero
hero.x = 80
hero.y = 300
hero.width=15
hero.height=15
hero.speed = 300
score = 0
enemies = {} --A table for the enemies
gameover = ("false")
hero.shots = {}
for i=0,5 do
enemy = {}
enemy.width=40
enemy.height=20
enemy.x = math.random(800,1000)
enemy.y = math.random(150,450)
enemy.speed = 20
table.insert (enemies, enemy)
end
end
function love.update(dt)
if gameover ~= ("true") then
if love.keyboard.isDown("left") then
hero.x = hero.x - hero.speed*dt
elseif love.keyboard.isDown("up") then
hero.y = hero.y - hero.speed*dt
elseif love.keyboard.isDown("down") then
hero.y = hero.y + hero.speed*dt
elseif love.keyboard.isDown("right") then
hero.x = hero.x + hero.speed*dt
end
for i,v in ipairs(enemies) do
--let them move
v.x = v.x - (enemy.speed+0.3*score)*dt
--stop the game when they reach the edge
if v.x <= 0 then
gameover = ("true")
end
end
local remEnemy = {}
local remShot = {}
--update those shots
for i,v in ipairs(hero.shots) do
--move them across
v.x = v.x + 100*dt
-- Remove non-visible shots
if v.x < 800 then
table.insert(remShot, i)
end
-- check for collision with enemies
for ii,vv in ipairs(enemies) do
if CheckCollision(v.x,v.y,2,5,vv.x,vv.y,vv.width,vv.height) then
-- mark that enemy for removal
table.insert(remEnemy, ii)
--generate a new one
i=i+1
enemy = {}
enemy.width=40
enemy.height=20
enemy.x = math.random(800,1000)
enemy.y = math.random(150,450)
enemy.speed = 20
table.insert (enemies, enemy)
-- mark the shot to be removed
table.insert(remShot, i)
end
end
end
-- remove the marked enemies
for i,v in ipairs(remEnemy) do
table.remove(enemies, v)
score = math.floor(score + 5 + enemy.speed/1000)
end
--remove the shot
for i,v in ipairs(remShot) do
table.remove(hero.shots, v)
end
--create new enemy
end
end
function love.draw()
--let's draw some ground
love.graphics.setColor(255,255,255,255)
love.graphics.rectangle("fill", 0, 500, 800, 100)
--let's draw some sky
love.graphics.setColor(255,255,255,255)
love.graphics.rectangle("fill", 0, 0, 800, 100)
--how to play the game
love.graphics.setColor(0,0,0,255)
love.graphics.print("Stop the bad guys from crossing the screen!", 10, 550)
love.graphics.print("Arrow keys to move", 10, 565)
love.graphics.print("Press space when on top of an enemy to kill it", 10, 580)
--let's draw our hero
love.graphics.setColor(255,255,255,255)
love.graphics.rectangle("fill", hero.x, hero.y, hero.width, hero.height)
-- let's draw some enemies
love.graphics.setColor(255,255,255,255)
for i,v in ipairs(enemies) do
love.graphics.rectangle("fill", v.x, v.y, v.width, v.height)
end
-- What are the scores
love.graphics.setColor(0,0,0,255)
love.graphics.print("Score:", 10, 10)
love.graphics.print(score, 100, 10)
-- let's draw our shots
love.graphics.setColor(255,255,255,255)
for i,v in ipairs(hero.shots) do
love.graphics.rectangle("fill", v.x, v.y, 2, 2)
end
if gameover == ("true") then
love.graphics.print("YOU LOSE!", 370, 295)
end
end
function shoot()
local shot = {}
shot.y = hero.y + hero.height/2
shot.x = hero.x
table.insert(hero.shots, shot)
end
function love.keyreleased(key)
if (key == " ") then
shoot()
end
if(key == "f") then
love.graphics.toggleFullscreen()
end
end
function love.keypressed(k)
if k == 'escape' then
love.event.push('q') --quit the game
end
end
function CheckCollision(box1x, box1y, box1w, box1h, box2x, box2y, box2w, box2h)
if box1x > box2x + box2w - 1 or -- Is box1 on the right side of box2?
box1y > box2y + box2h - 1 or -- Is box1 under box2?
box2x > box1x + box1w - 1 or -- Is box2 on the right side of box1?
box2y > box1y + box1h - 1 -- Is b2 under b1?
then
return false -- No collision. Yay!
else
return true -- Yes collision. Ouch!
end
end
It is all you need to run my game too, so if you want you can download it and run it, give me some feedback of my first game so far . Thanks to anyone who helps.