game additions
-
- Prole
- Posts: 8
- Joined: Thu Feb 29, 2024 11:28 am
game additions
i
Last edited by uknowntgyallyonski on Mon Mar 11, 2024 10:35 am, edited 2 times in total.
Re: snake game additions
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.
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
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".
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".
-
- Prole
- Posts: 8
- Joined: Thu Feb 29, 2024 11:28 am
Re: snake game additions
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
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
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
Who is online
Users browsing this forum: No registered users and 3 guests