Animation using love 2d not working

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
RishB
Prole
Posts: 2
Joined: Wed Apr 12, 2023 2:51 pm

Animation using love 2d not working

Post by RishB »

Hello, I'm new to lua and love 2d and I have a problem. I'm really sorry if I am being stupid.

I'm trying to animate a pacman eating a box. I want the pacman's mouth to close and open. But my code is not working. It doesn't show any syntax errors but it doesn't work.

This is the code.

Code: Select all

_G.love = require("love")

function love.load()
    love.graphics.setBackgroundColor(0.74,1,1)

    
    _G.pacman = {
        x = 200,
        y = 270,
        -- "rad" is radius
        rad = 60,
        angle1 = 1,
        angle2 = 5.5,
        eatenbox = false,
        completearc = false,
        eye = {
            x = 190,
            y = 240,
            -- Here also "rad" means radius!
            rad = 10
        },
        movespeed = 2.5,
        stopmovingx = 900
    }

    _G.food = {
        exists = true,
        x = 650
    }
end

function love.update(dt)
    if pacman.x <= pacman.stopmovingx then
        pacman.x = pacman.x + pacman.movespeed
        pacman.eye.x = pacman.eye.x + pacman.movespeed
    else
        pacman.x = 200
        pacman.eye.x = 190
        pacman.eatenbox = false
        food.exists = true
    end

    if not pacman.completearc then
        pacman.angle1 = pacman.angle1 - 0.1
        pacman.angle2 = pacman.angle2 + 0.1

        if pacman.angle1 == 0 and pacman.angle2 == 6 then
            pacman.completearc = true
        end
    
    elseif pacman.completearc then
        pacman.angle1 = pacman.angle1 + 0.1
        pacman.angle2 = pacman.angle2 - 0.1

        if pacman.angle1 == 1 and pacman.angle2 == 5.5 then
            pacman.completearc = false
        end
    end

    if pacman.x == food.x + 30 then
        pacman.eatenbox = true
    end

    if pacman.eatenbox then
        food.exists = false
    end

end

function love.draw()
    if food.exists then
        love.graphics.setColor(1,0,0)
        love.graphics.rectangle("fill",food.x,235,75,75)
    end

    love.graphics.setColor(0.95,1,0)
    love.graphics.arc("fill", pacman.x, pacman.y, pacman.rad, pacman.angle1, pacman.angle2)

    love.graphics.setColor(0,0,0)
    love.graphics.circle("fill", pacman.eye.x, pacman.eye.y, pacman.eye.rad)

    love.graphics.print("Angle1 = " .. pacman.angle1 .. " Angle2 = " .. pacman.angle2)
end
I'm trying to see if the angle1 and angle2 variables are fully closed or not. If they are, I want to open them. But the angles are closing and are not opening again. I even printed both the angles, but they are never reversing.

Code: Select all

love.graphics.print("Angle1 = " .. pacman.angle1 .. " Angle2 = " .. pacman.angle2)
Can anyone help me? Again, I'm new to lua, so I don't know if I'm being stupid here.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Animation using love 2d not working

Post by zorg »

Don't use == equality when you're working with not-whole numbers; those are not guaranteed to be equal due to the nature of how floating point numbers work; replace those with < > <= or >= as appropriate.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
RishB
Prole
Posts: 2
Joined: Wed Apr 12, 2023 2:51 pm

I got it to work

Post by RishB »

Thanks I got it to work
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 6 guests