我正在制作一个 Lua Love 2d 游戏,我想使用 love.keypressed 函数来进行冲刺。但有一个错误

问题描述 投票:0回答:1

我正在制作一个 Lua Love 2d 游戏,我想使用 love.keypressed 函数来进行冲刺。但有一个错误。 我希望我的函数只检测一次,这样我就可以进行破折号。在此处输入图像描述

function love.load()
    player = {}
    player.x = 400
    player.y = 500
    player.speedY = 3
    player.speedX = 4
    player.sprite = love.graphics.newImage('sprites/Spaceship.PNG')
    player.lives = 5
    player.points = 0

    background = love.graphics.newImage('sprites/SpaceBackground.png')

    asteroid = {}
    asteroid.x = {200, 460, 630}
    asteroid.y = {0, 0, 0}
    asteroid.speedY = 4
    asteroid.sprite = love.graphics.newImage('sprites/Asteroid.png')   
    asteroid.number = 3
    asteroid.missed = 1 

    bullet = {}
    bullet.x = {}
    bullet.y = {}
    bullet.speed = 11
    bullet.sprite = love.graphics.newImage('sprites/bullet.png')
    bullet.number = 0


    heart = {}
    heart.x = {200}
    heart.y = {-200}
    heart.speed = 4
    heart.sprite = love.graphics.newImage('sprites/Heart.png')
    heart.number = 1

    whenIshotLast = 0
    updates = 0
    updates2 = 0
    updates3 = 0
    didAnewAsteroidSpawnSoon = 0
    updatesFromLastDashF = 0
    didWePressF = 0
end

function areColliding(x1, y1, sx1, sy1, x2, y2, sx2, sy2)
    if x2 <= x1 + sx1 and x1 <= x2 + sx2 and y2 <= y1 + sy1 and y1 <= y2 + sy2 then
        return true
    else 
        return false
    end
end

function love.update(dt)

    if(player.lives > 0) then
        
        updates = updates + 1
        updates2 = updates2 + 1
        updates3 = updates3 + 1
        
        if updates3 > 200 then
            player.points = player.points + 1
            updates3 = 0
        end
        if(didWePressF == 1) then
            updatesFromLastDashF = updatesFromLastDashF + 1
        end
        -- Player movement controls
        if love.keyboard.isDown("right") or love.keyboard.isDown("d") then
            player.x = player.x + player.speedX
        end
        if love.keyboard.isDown("left") or love.keyboard.isDown("a") then
            player.x = player.x - player.speedX
        end
        if love.keyboard.isDown("down") or love.keyboard.isDown("s") then
            player.y = player.y + player.speedY
        end
        if love.keyboard.isDown("up") or love.keyboard.isDown("w") then
            player.y = player.y - player.speedY
        end
        if(love.keypressed("up", false) or love.keypressed("w", false)) then
            didWePressF = 1
            if(updatesFromLastDashF < 30) then
                player.y = player.y - 50

                didWePressF = 0
                updatesFromLastDashF = 0
            end
        end
        if(player.y > 525) then
            player.y = 525
        end
        if(player.y < 0)then
            player.y = 0
        end
        if(player.x < 0) then
            player.x = 720
        end

        if(player.x > 720) then
            player.x = 0    
        end

        -- Reset shooting cooldown
        if updates > 70 then
            updates = 0
            whenIshotLast = 0
        end
        if updates2 > 400 then
            updates2 = 0
            didAnewAsteroidSpawnSoon = 0
        end

        
        

        -- Shooting bullets
        if love.keyboard.isDown("space") and whenIshotLast < 1 then
            bullet.number = bullet.number + 1
            bullet.x[bullet.number] = player.x + 20
            bullet.y[bullet.number] = player.y
            whenIshotLast = whenIshotLast + 1
        end

        -- Update asteroid positions
        for i = 1, asteroid.number, 1 do
            asteroid.y[i] = asteroid.y[i] + asteroid.speedY
            if asteroid.y[i] > 600 then
                asteroid.y[i] = -500
                asteroid.x[i] = math.random(0, 720)
                asteroid.missed = asteroid.missed + 1
            end
        end

        for i = 1, heart.number, 1 do
            heart.y[i] = heart.y[i] + heart.speed
            if(heart.y[i] > 600) then
                heart.y[i] = -1200
                heart.x[i] = math.random(0, 720)
            end
        end

        -- Check for collisions between asteroids and the player
        for i = 1, heart.number, 1 do
            if areColliding(heart.x[i], heart.y[i], 3 * 32, 3 * 32, player.x, player.y, 0.6 * 150, 0.6 * 120) then
                heart.y[i] = -1200
                heart.x[i] = math.random(0, 720)
                player.lives = player.lives + 1
            end
        end

        for i = 1, asteroid.number, 1 do
            if areColliding(asteroid.x[i], asteroid.y[i], 0.5 * 150, 0.5 * 150, player.x, player.y, 0.6 * 150, 0.6 * 120) then
                asteroid.y[i] = -500
                asteroid.x[i] = math.random(0, 720)
                player.lives = player.lives - 1
            end
        end

        for n = 1, heart.number, 1 do
            for i = bullet.number, 1, -1 do
                if areColliding(bullet.x[i], bullet.y[i], 32, 32, heart.x[n], heart.y[n], 3 * 32, 3 * 32) then
                    heart.y[n] = -1200
                    heart.x[n] = math.random(0, 720)
                    table.remove(bullet.y, i)
                    table.remove(bullet.x, i)
                    bullet.number = bullet.number - 1
                    player.points = player.points + 2
                    player.lives = player.lives + 1
                end
            end
        end

        -- Check for collisions between bullets and asteroids
        for n = 1, asteroid.number, 1 do
            for i = bullet.number, 1, -1 do
                if areColliding(bullet.x[i], bullet.y[i], 32, 32, asteroid.x[n], asteroid.y[n], 0.5 * 150, 0.5 * 150) then
                    asteroid.y[n] = -500
                    asteroid.x[n] = math.random(0, 720)
                    table.remove(bullet.y, i)
                    table.remove(bullet.x, i)
                    bullet.number = bullet.number - 1
                    player.points = player.points + 3
                end
            end
        end

        -- Update bullet positions and remove off-screen bullets
    for i = bullet.number, 1, -1 do
        bullet.y[i] = bullet.y[i] - bullet.speed
        if bullet.y[i] < 0 then
            table.remove(bullet.y, i)
            table.remove(bullet.x, i)
            bullet.number = bullet.number - 1
        end
        
    end
    if(asteroid.missed % 4 == 0 and didAnewAsteroidSpawnSoon == 0 and asteroid.number < 19) then
        asteroid.number = asteroid.number + 1
        asteroid.y[asteroid.number] = -500
        asteroid.x[asteroid.number] = math.random(0, 720)
        asteroid.missed = 0
        didAnewAsteroidSpawnSoon = 1
    end
    
end
    
end

function love.draw()
    love.graphics.draw(background, 0, 0, 0, 1, 1.2)
    love.graphics.draw(player.sprite, player.x, player.y, 0, 0.6, 0.6)

    for i = 1, asteroid.number, 1 do
        love.graphics.draw(asteroid.sprite, asteroid.x[i], asteroid.y[i], 0, 0.5, 0.5)
    end
    for i = 1, bullet.number, 1 do
        love.graphics.draw(bullet.sprite, bullet.x[i], bullet.y[i], 0, 1, 1)    
    end
    for i = 1, heart.number, 1 do
        love.graphics.draw(heart.sprite, heart.x[i], heart.y[i], 0, 3, 3)    
    end

    love.graphics.print(player.lives, 25, 570, 0, 2, 2)

-- 19
    love.graphics.print(player.points, 740, 570, 0, 2, 2)
    love.graphics.print(asteroid.number, 740, 0, 0, 2, 2)
    if(player.lives == 0) then
        love.graphics.print("Loser", 240, 200, 0, 10, 10)
    end

end

你能告诉我如何使用love.keypressed功能吗?在此处输入图像描述 [在此处输入图像描述](https://i.sstatic.net/z1jllHg5.png) 你有什么建议吗?

lua
1个回答
0
投票

love.keypressed 实际上不会返回任何内容,根据此 它是一个回调函数。每次按下任意键都会触发。


-- old 
if(love.keypressed("up",false) or love.keypressed("w",false))then
    didWePressF = 1
    if(updatesFromLastDashF<30)then
        player.y = player.y-50
        didWePressF = 0
        updatesFromLastDashF = 0
    end
end

--new

function love.keypressed(key,code,isrepeat){
    -- key:character of the key, code:scancode of the key,isrepeat:if the key is held down
    if(not isrepeat) then
        --only runs if the keys arent being held 
        if(code=="up" or code=="w")then
            didWePressF = 1
            if(updatesFromLastDashF<30)then
                player.y = player.y-50
                didWePressF = 0
                updatesFromLastDashF = 0
            end
        end
    end
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.