Re: Hi guys, I have a problem with my pyglet program. Any help is appreciated.

34 views
Skip to first unread message

Crispin Wellington

unread,
Mar 17, 2013, 12:48:51 AM3/17/13
to pyglet...@googlegroups.com
Just looking over it quickly, I think it's your use of window.push_handlers inside the move function. Push handlers will push more handlers onto a handler stack. So first time its called you have one handler. When move is called a second time, now you have two handlers, both the same, so the function will be called twice. Then 3 handlers. Then 4 handlers. They just stack up and up causing the speed to increase more and more with each keypress event being called multiple times.

Remember that if you "return True" after your handler (your move(dt) function) the event wont propagate up the handler stack. And even though this might cause the behavoir to go away, this is NOT a solution, it's just hiding the symptom, because you are still building a massive handler stack with ongoing tank movement, which will eventually slow the game to a crawl and eventually cause memory to run out.

Hope this helps

Regards

Crispin

On 17/03/13 06:13, Pranav Salunke wrote:
Hey Guys,
I made a simple prototype for my own testing of Pyglet and I found one weird bug in my code which I could not nail no matter how many times I go through the same. The bug is that the speed of the moving sprite gradually increases and the sprite gets faster and faster as the number of clicks increase. Is there any suggestion as what may be causing this. Thanks a lot.

 
I have attached the Tar Ball containing the code.

Regards,
Pranav
--
You received this message because you are subscribed to the Google Groups "pyglet-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyglet-users...@googlegroups.com.
To post to this group, send email to pyglet...@googlegroups.com.
Visit this group at http://groups.google.com/group/pyglet-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

claudio canepa

unread,
Mar 17, 2013, 2:57:29 AM3/17/13
to pyglet...@googlegroups.com
On Sat, Mar 16, 2013 at 7:13 PM, Pranav Salunke <pps.p...@gmail.com> wrote:
Hey Guys,
I made a simple prototype for my own testing of Pyglet and I found one weird bug in my code which I could not nail no matter how many times I go through the same. The bug is that the speed of the moving sprite gradually increases and the sprite gets faster and faster as the number of clicks increase. Is there any suggestion as what may be causing this. Thanks a lot.

 
I have attached the Tar Ball containing the code.

Regards,
Pranav
  


1. clock.schedule_interval(move, 1.0 / 6.0) instructs pyglet to call move each 1/60 sec ; that is repeat the call at 1/60, 2/60, 3/60,...

2. Every time a key is pressed the code in update will add a new request to call move with a 1/60 interval (pyglet don't cares it is the same function).
So, after pressing k times a key, move will be called k  times after each 1/60

This problem is easy: move the line
    pyglet.clock.schedule_interval(move, 1.0/60.0)
from the current location in update to just before the line
    pyglet.app.run()

Then you can eliminate the function update from the code

Theres another problem with the code: each time move is called, it tells pyglet to record keypresses in KEYMAP.
So, after k calls to move, each keypress will be sent k times to KEYMAP.
That is a waste of CPU and memory.

Solution to this is easy: move the line
   mainWindow.push_handlers(KEYMAP)
from move to just before the line
    pyglet.app.run()


Notice that  

@window.event
def on_key_press(symbol, modifiers):

a) eliminates all previously registered handlers for on_key_press events
b) registers the function as a handler for on_key_press events.

So, if you want to use

 @window.event
def on_key_press(symbol, modifiers):

and 

mainWindow.push_handlers(KEYMAP)

the later must  be after the first in the code, or else the KEYMAP handler will be discarded.


Pranav Salunke

unread,
Mar 17, 2013, 10:04:15 AM3/17/13
to pyglet...@googlegroups.com
Thanks for going through my code. Yes, I would not use Return as its running away from my problems. I'm a student and I need to learn and not workaround if possible. I think Claudio has given some pointers on how to fix it as I guessed that the problem is with clock.schedule.... but I didn realise its the position of it. I'll try it out and give feedback. Also Once my game is finished I will send you guys the link.
Thanks again.

Pranav Salunke

unread,
Mar 17, 2013, 10:05:30 AM3/17/13
to pyglet...@googlegroups.com
Yes Ill move the position of the clock schedule line and also I want to remap my keys every time as I will have different functions for the keys - in case of main menu, level's of the game. If theres a better way that this please do tell me.

Thanks a lot :)
Reply all
Reply to author
Forward
0 new messages