Animation using love 2d not working
Posted: Wed Apr 12, 2023 3:12 pm
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.
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.
Can anyone help me? Again, I'm new to lua, so I don't know if I'm being stupid here.
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
Code: Select all
love.graphics.print("Angle1 = " .. pacman.angle1 .. " Angle2 = " .. pacman.angle2)