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



我是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

'我试着只运行游戏的背景和鸟的声音是屏幕上唯一没有移动的图像。

type here

这是因为gameState是"开始"的。你应该制作一个开始游戏的按钮,并将其从"开始"改为";";play" .

例如:

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()
elseif key == "space" and gameState == "start" then
gameState = "play"
end
end

相关内容

  • 没有找到相关文章

最新更新