[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Training Pong MVC



Hi

Thanks for the answers. I was only wondering if threading could be added. But your right saying that one should first try other way before using threading.

I think that all events get dispatch to all listners (and not the events are in a weakref list but the listners are). The simplest extension would be a dict {eventtype:[listners in weakref list], ....} and only dispatch to the entries in the list for that event (ok, makes registering listners more complicatet because they also have to say which eventtypes they want to receive and for hirachical dispatching it gets even more complicatet...).

If you use a event list, you have to be carefull wehn going trough not to delete from this list at the same time. It also can get more entries wehn processing the events (supposte a listner gets an event an trough an event that get on the event list...). Ok seams to make things more complicatet.

One more question: do you use pygame.sprite for drawing your sprites in your game with so many sprites? because I'm writing a (faster) sprite engine (only redrawing dirty sprites) you can use with minimal changes to existing code using pygame.sprite . You can find me in IRC #pygame on freenode.net for more information.

~DR0ID

Okay...

First of all, I don't know anything beyond some vague stab at calculating
framerates. Honestly, I haven't gone beyond using pygame.time.wait(),
because it did what it was supposed to do. The way I started to measure
things in terms of some sort of framerate was just Pixels Per Tick. Even
then, I really don't think I know what I'm talking about in terms of timing
:D

The event manager you see in the code is more or less ripped from the MVC
tutorial from the link posted a few mails back. I'm not entirely certain why
it's there, but I assume it may be to do with something further along in the
original author's tutorial (where he starts jamming in network code). You'll
also notice another "useless" variable in the CpuController called
self.newevent. It's safe to say you can probably take both of these
variables out and it'll still run fine.

The one difference between the pong code and the code in the original
tutorial is that in my code, there are technically two TickEvents going on.
The first one regulates the state of the game, updating display, etc, and
then there's PongTickEvent, which was created specifically to control the
motion of the ball, so I could toggle a variable to pause the game. If I had
toggled the TickEvent to an "off" state, I would no long have any control
over input. In a very tenuous way, this acted like a thread. I do stress the
tenuous part.

In terms of *real* threading, I'm not sure how Python handles threading
(I've only been using python for a couple of weeks), but I think if it's
anything like I'm used to, you could launch each controller in a separate
thread, and bottleneck them through the event manager. Although I'm sure
this could lead to situations where some threads may become starved, and a
trivial per-tick action gains priority over something massively important,
such as redrawing the screen. Pygame is good enough as it is (as far as I
can see) that it doesn't really required threading. I'm working on an
isometric game with a world built out of roughly 2500 sprites, which are all
actively listening to see if the mouse is over it, couple that with (at last
count) 100 larger sprites roaming the "surface" of the world and two
parallax scrolling background images. I was initially concerned that I was
going to get a lot of slowdown, and thought threading was the answer,
however, it turned out that I just had to use a flag on my surfaces to speed
them up. I never once saw the manager get choked up with events, and since
all events had to go through it to perform their duty, everything ran at
exactly the same speed.

So yes, threading is a very definite idiom you could apply, but I would
explore all possible avenues of operation before heading down to threading
town :)

Lastly, I think I know what you mean. You're worried that an event,
regardless of its final destination, is going to pass through each
controller until it hits the right spot (if it has one). The events
themselves are already in a weakref dictionary, so I *think* that answers
part of your question... If you're concerned about efficiency, remember that
you're already saving a whole bunch of CPU cycles from just moving off the
basic while true loop. However, I'm kind of interested now to see the
lifetime of an event, so I'll modify my code a little here to see if the
event really does hit each listener in turn before getting to where it needs
to go.

Sorry for waffling.

-----Original Message-----
From: owner-pygame-users@xxxxxxxx [mailto:owner-pygame-users@xxxxxxxx]On
Behalf Of DR0ID
Sent: Wednesday, April 26, 2006 14:54
To: pygame-users@xxxxxxxx
Subject: Re: [pygame] Training Pong MVC


Hi

Thanks for your tutorial. I'm fascinated by the MVC model, because it seems tu run very fast. I wonder, what framerates this game has but dont know how to messure it. I tried following:

class RedrawController:
def __init__(self, eventManager):
self.eventManager = eventManager
self.eventManager.RegisterListener(self)
self._clock = pygame.time.Clock()
def Notify(self, event):
"""
Redraw all updated surfaces
"""
if isinstance(event, RedrawEvent):
game.pong_static_group.draw(game.screen)
dirty_rects = game.pong_dynamic_group.draw(game.screen)
pygame.display.update(dirty_rects)
self._clock.tick()
pygame.display.set_caption(str(self._clock.get_fps()))



and in the CpuController.Run() I commentet out the two lines after the while keyword.
I do not know if this is the correct framerate. (putting the clock into the CPUController.Run() is not the right place because not all deliverd events are RedrawEvents).



Some questions:

In the EventManager there ist a self.eventQueue = [] which is not used and gave me the idea and question:
Is it easy to run a controller or any other component in a different thread that stuffs the eventmanager's eventqueue? (should be threadsafe + EventManager has to change a bit for that) (ok running every component in a different thread should be possible or is it not?)


In your EventManager all registered listners get all events, also if they do not use them all. Would it not be a bit slow if there are many objects and many events to dispatch? Would it be better to only notify a object if it has registered for the event type to dispatch? (ok then you have to check befor dispatching and makes things a bit more complicatet perhaps using a dict like {eventtype:[listners], ....} (also a weakref list?) )

In a complexer application there can be many more different events, right?


~DR0ID





Chris Ashurst schrieb:
Just to confirm: Yes, that's the tutorial.

It's a little confusing if you follow his example, but as long as you walk
away with the concept of having an event manager that listens for events
sent by controllers, you will also walk away with the fact that you won't
have to use the usual "while 1:" method of looping.

In fact, I would like to share my MVC Pong program, as I think it's been
simplified and commented enough to explain what's going on. It's at
http://telestatic.net/python/pong.py

I'm not claiming that it's the ultimate superior last-word in pygame
programming, or even of the MVC model, but it gets the message across :)


~Chris

-----Original Message-----
From: owner-pygame-users@xxxxxxxx [mailto:owner-pygame-users@xxxxxxxx]On
Behalf Of Laura Creighton
Sent: Tuesday, April 25, 2006 00:53
To: pygame-users@xxxxxxxx
Cc: lac@xxxxxxxxxx
Subject: Re: [pygame] Training



In a message of Mon, 24 Apr 2006 22:34:06 EDT, "R. Alan Monroe" writes:
As far as my personal extra 0.02 goes, I found the tutorial for using t
he
MVC model with Pygame incredibly useful.
URL?

Alan
I am not the original poster, but I suspect he or she means:
http://sjbrown.ezide.com/games/writing-games.html

Laura




CONFIDENTIAL NOTICE: This email including any attachments, contains confidential information belonging to the sender. It may also be privileged or otherwise protected by work product immunity or other legal rules. This information is intended only for the use of the individual or entity named above. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution or the taking of any action in reliance on the contents of this emailed information is strictly prohibited. If you have received this email in error, please immediately notify us by reply email of the error and then delete this email immediately.







CONFIDENTIAL NOTICE: This email including any attachments, contains confidential information belonging to the sender. It may also be privileged or otherwise protected by work product immunity or other legal rules. This information is intended only for the use of the individual or entity named above. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution or the taking of any action in reliance on the contents of this emailed information is strictly prohibited. If you have received this email in error, please immediately notify us by reply email of the error and then delete this email immediately.