Skip to content
Advertisement

Pyglet game movement lagging behind

So, my snake makes a continuous movement, but if I press any key it goes back in time and lags back and forward. Here is a video: https://youtu.be/KCesu5bGiS8

My guess would be to update the key input faster, but when I do that everything updates faster, so the snake goes faster etc.

Code (as requested in text form) here:

JavaScript

Advertisement

Answer

Converting all my comments into a answer instead. It won’t solve your problem completely. But due to lack of time, I’ll leave something useful at least that almost solves it.

The reason for these are a couple. One of them is that you use a scheduler to render stuff instead of using the built-in on_draw event. Can’t say for sure, but a good guess is that the graphical buffer gets updated/flipped automatically in on_draw while you’re doing your drawing and stuff in a side-chained render function. So moving all the rendering stuff into on_draw makes sense.

Another issue is that you don’t trigger on the actual key press, but instead you need to time the key press to each tick in the scheduler you got going – which you also have mushed into the rendering function. Essentially you’re doing eventhandling+rendering+updating+IO in one cluster*** of a function, heh. Instead, you should rely on on_key_press for keyboard events.

Lastly, you’re doing math operations all over the place – any of which might be half way done when you’re doing the actual rendering. That’s why you might get ghosting or odd artifacts (some things aren’t completely done updating positions etc).

But here is a almost working example of a few steps taken to get closer to what you want. If no one else (including you) haven’t solved this by a few days I’ll go ahead and re-write most of your code and point you in a few good directions (batches being one of them).

JavaScript

I’ll leave you not only with this ish-working code, but a good advice.
Stop asking so many questions, and start learn ways to debug why things are happening the way they do. You’re shooting in the dark right now, asking questions hoping someone will solve the problems for you so you can focus on the fun stuff – which is the game development itself.
But what you’ll have a lot of use for later in life – is finding ways to debug, probe, understand and pinpoint why things are or aren’t happening the way you want.

Put some print("moo") here and there, print some values, add logging/debugging all over the place until you get wiser. It’s not always efficient, but it got me to this point with your code.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement