为什么我的 flappy bird 游戏只加载图像?

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

我是 lua 的新手,尝试制作一个 flappy bird 游戏。我让游戏运行并且没有错误。但是当游戏运行时,屏幕上只显示背景和鸟 png 文件。背景图像在左侧,几乎在屏幕之外,而 png 鸟在屏幕中间。我在屏幕上的任何地方都看不到管道图像。无论我按什么键,也没有任何动作。怎样才能让图像出现在它们应该出现的位置并让游戏正常运行?

下面是我的代码:

`

-- Constants
WINDOW_WIDTH = 480
WINDOW_HEIGHT = 640
GRAVITY = 900
JUMP_VELOCITY = -300
PIPE_GAP = 120
PIPE_SPEED = 60

-- Variables
local bird
local pipes = {}
local lastPipeY = -PIPE_GAP / 2
local score = 0
local gameState = "start"
local background = love.graphics.newImage("flappy background.png")
local pipeImage = love.graphics.newImage("pipe-up.png")
local birdImage = love.graphics.newImage("bird image.png")
local font = love.graphics.newFont(50)


-- Load assets
function love.load()
    love.window.setTitle("Snappy Bird")
    love.window.setMode(WINDOW_WIDTH, WINDOW_HEIGHT)
    love.graphics.setFont(font)

    bird = {
        x = WINDOW_WIDTH / 2 - birdImage:getWidth() / 2,
        y = WINDOW_HEIGHT / 2 - birdImage:getHeight() / 2,
        vy = 0,
        image = birdImage
    }

    -- Load audio
    jumpSound = love.audio.newSource("jump sound.wav", "static")
    scoreSound = love.audio.newSource("scoring.wav", "static")
    gameOverSound = love.audio.newSource("game over.wav", "static")
end

-- Update game state
function love.update(dt)
  
    if gameState == "play" then
        -- Update bird
        bird.vy = bird.vy + GRAVITY * dt
        bird.y = bird.y + bird.vy * dt

        -- Update pipes
        for i, pipe in ipairs(pipes) do
            pipe.x = pipe.x - PIPE_SPEED * dt

            -- Remove pipes that are off screen
            if pipe.x + pipeImage:getWidth() < 0 then
                table.remove(pipes, i)
            end

            -- Check for collision
            if checkCollision(pipe.x, pipe.y, pipeImage:getWidth(), pipeImage:getHeight(), bird.x, bird.y, birdImage:getWidth(), birdImage:getHeight()) then
                gameState = "over"
                gameOverSound:play()
            end

            -- Check for score
            if pipe.x + pipeImage:getWidth() < bird.x and not pipe.scored then
                pipe.scored = true
                score = score + 1
                scoreSound:play()
            end
        end

        -- Spawn new pipes
        if pipes[#pipes].x < WINDOW_WIDTH - PIPE_GAP then
            local pipeY = lastPipeY + love.math.random(-80, 80)
            if pipeY < 0 then pipeY = 0 end
            if pipeY > WINDOW_HEIGHT - PIPE_GAP then pipeY = WINDOW_HEIGHT - PIPE_GAP end
            table.insert(pipes, {x = WINDOW_WIDTH, y = pipeY, scored = false})
            lastPipeY = pipeY
        end

        -- Check for game over
        if bird.y > WINDOW_HEIGHT or bird.y + birdImage:getHeight() < 0 then
            gameState = "over"
            gameOverSound:play()
        end
    end
end

-- Draw game elements
function love.draw()
    love.graphics.draw(background, 0, 0)

    -- Draw pipes
    for _, pipe in ipairs(pipes) do
        love.graphics.draw(pipeImage, pipe.x, pipe.y)
    end

    -- Draw bird
    love.graphics.draw(bird.image, bird.x, bird.y)

    -- Draw score
    love.graphics.print("Score: " .. score, 10, 10)

-- Draw game over message
if gameState == "over" then
    love.graphics.print("Game Over", WINDOW_WIDTH / 2 - font:getWidth("Game Over") / 2, WINDOW_HEIGHT / 2 - font:getHeight() / 2)
    love.graphics.print("Press R to restart", WINDOW_WIDTH / 2 - font:getWidth("Press R to restart") / 2, WINDOW_HEIGHT / 2 + font:getHeight() / 2)
end

end

-- Handle keyboard input
function love.keypressed(key)
if key == "escape" then
love.event.quit()
elseif key == "space" and gameState == "play" then
bird.vy = JUMP_VELOCITY
bird.y = bird.y + bird.vy * dt
jumpSound:play()
elseif key == "r" and gameState == "over" then
reset()
end
end

-- Handle mouse input
function love.mousepressed(x, y, button)
if button == 1 and gameState == "play" then
bird.vy = JUMP_VELOCITY

jumpSound:play()
elseif button == 1 and gameState == "over" then
reset()
end
end

-- Reset game state
function reset()
bird.y = WINDOW_HEIGHT / 2 - birdImage:getHeight() / 2
bird.vy = 0
pipes = {}
lastPipeY = -PIPE_GAP / 2
score = 0
gameState = "play"
end

-- Check for collision
function checkCollision(ax, ay, aw, ah, bx, by, bw, bh)
return ax < bx + bw and ay < by + bh and bx < ax + aw and by < ay + ah
end

` 我尝试只运行游戏,因为背景和鸟类 png 是屏幕上唯一没有移动的图像。

type here
lua game-development love2d
© www.soinside.com 2019 - 2024. All rights reserved.