I am making a Lua Love 2d game and I wanted to use the love.keypressed function to make a dash a move. But there is an eror

  Kiến thức lập trình

I am making a Lua Love 2d game and I wanted to use the love.keypressed function to make a dash a move. But there is an eror.
I want my function to detect only once so I can make my dash.enter image description here

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

Can you tell me how am I supposed to use the love.keypressed function?enter image description here
[enter image description here](https://i.sstatic.net/z1jllHg5.png)
Do you have any suggestions?

New contributor

Емил Славчев is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT