Anomalyy
Anomalyy

Reputation: 9

Why is my flappy bird game only loading images?

I am new to lua and tried making a flappy bird game. I got the game to run and got no errors. But when the game runs only the background and bird png files are shown on screen. The background image is to the left and almost off screen, and the bird png is in the middle of the screen. I don't see the pipe image anywhere on screen. There is also no movement no matter what keys I press. How can get the images where they should be and get the game to run properly?

Below is my code:

`

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

` I tried running the game only for the background and bird pngs to be the only images on the screen with no movement.

type here

Upvotes: 0

Views: 67

Answers (1)

Meskyy076
Meskyy076

Reputation: 11

It's because the gameState is "start". You should make a button which starts the game and changes that from "start" to "play".

For example:

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

Upvotes: 1

Related Questions