The Artima Developer Community
Sponsored Link

Python Buzz Forum
Corvids:

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Rod Hyde

Posts: 23
Nickname: rodhyde
Registered: Aug, 2004

Rod Hyde is a UNIX sysadmin who has had a Road to Damascus experience with Python
Corvids: Posted: Aug 17, 2004 5:50 AM
Reply to this message Reply

This post originated from an RSS feed registered with Python Buzz by Rod Hyde.
Original Post: Corvids:
Feed Title: The landscape.
Feed URL: http://www.bloglines.com/blog/RodHyde/rss
Feed Description: The technical landscape through the eyes of an occasional indie games programmer who now uses Python as his language of choice.
Latest Python Buzz Posts
Latest Python Buzz Posts by Rod Hyde
Latest Posts From The landscape.

Advertisement
Corvids is a simple 2d shooter game written in Python using a SWIG wrapper around the PTK library.

One thing that I have come to appreciate in Python is the ease at which I can implement callbacks and events. For example, I have implemented an event manager which enables functions to be called at some point in the future. The entire code for the event manager module is 35 lines, of which 14 are either comments or blank lines.

In the past (pre-Python) I have implemented event managers either using "Event" types or by using interfaces. The Python way of doing it is so much simpler.

To post an event to be called next tick...


em.post(self.make_mine, self.x, self.y)

To post an event to be called after n ticks...


em.post_after(30 * randint(0, 10), self.make_zapper)

It really is that simple. I can post any function I like without caring about what type it is or which class it belongs to. I think this is an example of Python's so-called "duck typing", ie, if it looks like a duck, walks like a duck and quacks like a duck then to all intents and purposes it is a duck.

What I particularly like about this, is that a function such as "make_zapper" can check if it is allowed to create a zapper, then, if it was unable to create one, post itself to the event manager to be called again after a random number of ticks. It makes it very, very easy to create and manage waves of aliens.

Read: Corvids:

Topic: Astronomy picture of the day: Hoag's Object Previous Topic   Next Topic Topic: Chocolatl

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use