added delay for each player

This commit is contained in:
Simon Kellet 2024-04-21 13:51:07 +01:00
parent c6e3d95791
commit 6915ac4ca2
3 changed files with 43 additions and 27 deletions

View File

@ -1,24 +1,30 @@
function keyPressed(key)
if EnableKeyPress == false then
return --return early
end
local bulletImg = "/assets/bullet.png"
if EnableKeyPress1 == true then
if key == "space" then
local bullet = Bullet(UserPlayer1.x + UserPlayer1.width / 2, UserPlayer1.y, 1, bulletImg, 500)
local bullet = Bullet(UserPlayer1.x + UserPlayer1.width / 2, UserPlayer1.y, 1, BulletImg, 500)
GameSounds.shoot1:play()
table.insert(Bullets1, bullet)
elseif key == "return" then
local bullet = Bullet(UserPlayer2.x + UserPlayer2.width / 2, UserPlayer2.y, 2, bulletImg, 500)
GameSounds.shoot2:play()
table.insert(Bullets2, bullet)
elseif key == "escape" then
love.event.quit()
elseif key == "r" then
love.load()
KeyPressTime1 = KeyDelay1
EnableKeyPress1 = false
end
end
-- KeyPressTime now equals a delay
KeyPressTime = KeyDelay
EnableKeyPress = false
if EnableKeyPress2 == true then
if key == "return" then
local bullet = Bullet(UserPlayer2.x + UserPlayer2.width / 2, UserPlayer2.y, 2, BulletImg, 500)
GameSounds.shoot2:play()
table.insert(Bullets2, bullet)
KeyPressTime2 = KeyDelay2
EnableKeyPress2 = false
end
end
if key == "escape" then
love.event.quit()
end
if key == "r" then
love.load()
end
end

View File

@ -19,9 +19,13 @@ function love.load()
ScrnHeight = love.graphics.getHeight()
ScrnWidth = love.graphics.getWidth()
EnableKeyPress = true
KeyPressTime = 0
KeyDelay = 0.5
EnableKeyPress1 = true
KeyPressTime1 = 0
KeyDelay1 = 0.5
EnableKeyPress2 = true
KeyPressTime2 = 0
KeyDelay2 = 0.5
GameSounds = {}
--static, loaded into memory all time
@ -35,10 +39,11 @@ function love.load()
GameSounds.bg:setVolume(0.5)
GameSounds.bg:play()
local player1Img = "/assets/player1.png"
local player2Img = "/assets/player2.png"
UserPlayer1 = Player(400, ScrnHeight - 30, 1, 3, player1Img, 300)
UserPlayer2 = Player(200, 0, 2, 3, player2Img, 300)
Player1Img = "/assets/player1.png"
Player2Img = "/assets/player2.png"
BulletImg = "/assets/bullet.png"
UserPlayer1 = Player(400, ScrnHeight - 30, 1, 3, Player1Img, 300)
UserPlayer2 = Player(200, 0, 2, 3, Player2Img, 300)
Bullets1 = {}
Bullets2 = {}

View File

@ -8,9 +8,14 @@ end
function updateGame(dt)
--add delay between key presses
KeyPressTime = math.max(0, KeyPressTime - dt)
if KeyPressTime <= 0 then
EnableKeyPress = true
KeyPressTime1 = math.max(0, KeyPressTime1 - dt)
if KeyPressTime1 <= 0 then
EnableKeyPress1 = true
end
KeyPressTime2 = math.max(0, KeyPressTime2 - dt)
if KeyPressTime2 <= 0 then
EnableKeyPress2 = true
end
TimeToPlaceWall = TimeToPlaceWall - dt