我是lua和love的新手。我正在尝试制作一个飞扬的小鸟游戏。游戏运行时,小鸟不会跳跃。这只鸟只是漂浮,直到它坠毁并且游戏结束。此外,只显示了我的原始管道,没有生成新管道。当我按下空格键并保持产卵管道时,如何让鸟跳起来? 下面是我的代码:
--sets up game window
function love.load()
love.window.setMode(400, 600)
love.window.setTitle("Flappy Bird")
--load assets
bg= love.graphics.newImage("flappy background.png")
bird = love.graphics.newImage("bird image.png")
pipeImage = love.graphics.newImage("pipe-up.png")
jumpSound = love.audio.newSource("jump sound.wav", "static")
scoreSound = love.audio.newSource("scoring.wav", "static")
gameOverSound = love.audio.newSource("game over.wav", "static")
--initialize game variables
x = 502
jumpHeight = 500
velocity = -300
gapHeight = 150
pipeX = 400
pipeY = love.math.random(100, 400)
scored = false
score = 0
gameover = false
end
--draw game objects
function love.draw()
--Get the dimensions of the window
local windowWidth, windowHeight = love.graphics.getDimensions()
--Calculate the scaling factor for the background image
local sx = windowWidth / bg:getWidth()
local sy = windowHeight / bg:getHeight()
--Draw the scaled background image
love.graphics.draw(bg, 0, 0, 0, sx, sy)
love.graphics.draw(bird, x, y)
love.graphics.draw(pipeImage, pipeX, pipeY - pipeImage:getHeight() - gapHeight)
love.graphics.draw(pipeImage, pipeX, pipeY + gapHeight)
love.graphics.print("Score: " .. score, 10, 10)
end
--adds movement to bird
function love.keypressed(key)
if key == "space" and not gameover then
velocity = -jumpHeight
jumpSound:play() -- play the jump sound effect
elseif key == "r" and gameover then
--reinitialize game variables
x = 50
y = 300
velocity = -120
pipeX = 400
pipeY = love.math.random(100, 400)
scored = false
score = 0
gameover = false
end
end
--checks collison with pipes
function checkCollision(x1, y1, w1, h1, x2, y2, w2, h2)
return x1 < x2 + w2 and
x2 < x1 + w1 and
y1 < y2 + h2 and
y2 < y1 + h1
end
function love.update(dt)
if not gameover then
--update the bird's position and velocity
velocity = velocity + gravity * dt
y = y + velocity * dt
--update the pipe's position
pipeX = pipeX - 200 * dt
--reset the pipe's position if it goes offscreen
if pipeX < -pipeImage:getWidth() then
pipeX = -120
pipeY = love.math.random(100, 400)
scored = false
end
--check for collision with pipes
if checkCollision(x, y, bird:getWidth(), bird:getHeight(), pipeX, pipeY - pipeImage:getHeight() - gapHeight, pipeImage:getWidth(), pipeImage:getHeight()) or
checkCollision(x, y, bird:getWidth(), bird:getHeight(), pipeX, pipeY + gapHeight, pipeImage:getWidth(), pipeImage:getHeight()) or
y < 0 or
y > love.graphics.getHeight() then
gameover = true
gameOverSound:play()
end
--check for scoring
if x > pipeX + pipeImage:getWidth() and not scored then
score = score + 1
scored = true
scoreSound:play()
end
end
end
--adds game over screen
function drawGameOver()
love.graphics.print("Game Over", 100, 100)
love.graphics.print("Score: " .. score, 100, 150)
end
function love.draw()
if not gameover then
--Get the dimensions of the window
local windowWidth, windowHeight = love.graphics.getDimensions()
--Calculate the scaling factor for the background image
local sx = windowWidth / bg:getWidth()
local sy = windowHeight / bg:getHeight()
--Draw the scaled background image
love.graphics.draw(bg, 0, 0, 0, sx, sy)
end
end
y = 300
love.graphics.draw(bird, x, y)
love.graphics.draw(pipeImage, pipeX, pipeY - pipeImage:getHeight() - gapHeight)
love.graphics.draw(pipeImage, pipeX, pipeY + gapHeight)
love.graphics.print("Score: " .. score, 10, 10)
else
drawGameOver()
end
end
--restarts the game
function love.keypressed(key)
if key == "r" and gameover then
--reinitialize game variables
x = 50
y = 300
velocity = 0
pipeX = 400
pipeY = love.math.random(100, 400)
scored = false
score = 0
gameover = false
gravity = 0
我尝试调整跳跃高度、速度和重力变量来解决问题,但无济于事。我还将按键功能合并为一个,认为重启功能在跳转功能之前出现,但这也不起作用。我做错了什么?
看来在你让鸟跳的代码中你做错了什么。
function love.keypressed(key)
if key == "space" and not gameover then
velocity = -jumpHeight
您正在使鸟的速度等于
-jumpHeight
。
你应该尝试用velocity = -jumpHeight
替换y = y + jumpHeight
。
关于你的第二个问题,关于如何保持产卵管道。您可以通过检查
pipeX
值来检测管道是否在屏幕外(在这种情况下很可能是 pipeX < 0
)。如果是这样,您可以像重置游戏时一样重置pipeX
和pipeY
。