Reputation: 9
I am new to lua and love. I am attempting to make a flappy bird game. When the game runs, the bird does not jump. the bird just floats until it crashes and the game ends. Also, only my original pipes are shown, no new pipes are being spawned. How do i get the bird to jump when i hit the space key and keep spawning pipes? below is my code:
--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
I tried adjusting the jumpheight, velocity, and gravity variables to fix the issue but to no avvail. I also combined the keypressed functions into one thinking the restart function was coming before the jump function, but that did not work either. What am i doing wrong?
Upvotes: 0
Views: 71
Reputation: 620
It seems that in your code to make the bird jump you did something wrong.
function love.keypressed(key)
if key == "space" and not gameover then
velocity = -jumpHeight
You are making the velocity of the bird equal to -jumpHeight
.
You should try replacing velocity = -jumpHeight
with y = y + jumpHeight
.
Regarding your second question on how to keep spawning pipes. You could detect if the pipe is off screen by checking the pipeX
value (In this case most likely when pipeX < 0
). If it is you can reset pipeX
and pipeY
like you do when you reset the game.
Upvotes: 0