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

View File

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

View File

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