Page 1 of 1

game additions

Posted: Mon Mar 04, 2024 12:34 pm
by uknowntgyallyonski
i

Re: snake game additions

Posted: Tue Mar 05, 2024 1:03 am
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.

Re: snake game additions

Posted: Tue Mar 05, 2024 3:32 am
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".

Re: snake game additions

Posted: Tue Mar 05, 2024 3:13 pm
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

Re: snake game additions

Posted: Tue Mar 05, 2024 7:30 pm
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