game additions

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
uknowntgyallyonski
Prole
Posts: 8
Joined: Thu Feb 29, 2024 11:28 am

game additions

Post by uknowntgyallyonski »

i
Last edited by uknowntgyallyonski on Mon Mar 11, 2024 10:35 am, edited 2 times in total.
RNavega
Party member
Posts: 251
Joined: Sun Aug 16, 2020 1:28 pm

Re: snake game additions

Post by RNavega »

Hi, could you please edit your post to put the code between the [code] [/code] tags? This way it preserves the indentation that you were working on, helping people to understand your code more clearly.

As for the movement, it happens inside love.update(), right after the "local headX, headY" line. The head moves to a new cell on every single update, which usually happens at 60 FPS. If you want to lower the rate with which the snek is updated, you need to rate-limit it: make a timer variable that starts at zero, and inside love.update it *accumulates* "dt" (which is the fraction of time, in seconds, since the last call to love.update). Right after accumulating it, you also test to see if that timer variable has passed a certain amount (like 1.0 for a full second), and, if it has, then you: A) reset the timer (set its value back to zero) and B) perform the movement that was already happening on each frame.
RNavega
Party member
Posts: 251
Joined: Sun Aug 16, 2020 1:28 pm

Re: snake game additions

Post by RNavega »

Hm, actually, what I said about "resetting the timer to zero" after the waiting time passes is incorrect.

It probably won't make a perceptual difference, but the accurate thing to do is to reset it to "its current value, minus the waiting time", because it might slightly pass that waiting time and you don't want to lose that extra time, whatever it is.

So instead of...
"if myTimer >= waitingTime then myTimer = 0.0"
The logic should be...
"if myTimer >= waitingTime then myTimer = myTimer - waitingTime".
uknowntgyallyonski
Prole
Posts: 8
Joined: Thu Feb 29, 2024 11:28 am

Re: snake game additions

Post by uknowntgyallyonski »

I fixed the speeds of the snakes thank you, would you have an idea on how i can change the pixels of the snake to an actual image of the snake
RNavega
Party member
Posts: 251
Joined: Sun Aug 16, 2020 1:28 pm

Re: snake game additions

Post by RNavega »

Instead of love.graphics.setColor + love.graphics.rectangle, use a single love.graphics.draw(myImage, ...), with myImage holding the result of love.graphics.newImage() done in the initialization part of your program, outside of any repeating functions.
You'd need 2 images for the snake: the head and the body. You can use the angle parameter of .draw() to rotate the snake head image to point to its direction, while using the same image (so there's no need to have 4 images for the head, one for each direction. Just use the same one, rotated at the draw call).

The wiki has everything you need to know, I suggest devouring it like a hungry snake:
https://love2d.org/wiki/love.graphics.newImage
https://love2d.org/wiki/love.graphics.draw
Post Reply

Who is online

Users browsing this forum: Google [Bot], slime and 48 guests