| | |
just for fun
![]() |
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
I'm re-creating the classic PacMan for the fun of it and needed a way to translate back and forth between directions and dx, dy for a sprite. Here was a fun solution that avoided cumbersome if...elif...elif...elif... chains. Any thoughts on readability, efficiency? Specifically, if this gets called by 4 monsters and 1 pacman every clock-tick = 1/60 sec, am I likely to run into trouble?
Jeff
Jeff
Python Syntax (Toggle Plain Text)
def get_direction(dx,dy): dirs = {'dx == 0 and dy < 0': 'up', # in actual program, maps to self.UP == 0 'dx < 0 and dy == 0': 'left', 'dx == 0 and dy > 0': 'down', 'dx > 0 and dy == 0': 'right'} for d in dirs: if eval(d): return dirs[d] return None
Last edited by jrcagle; Feb 13th, 2007 at 3:36 pm.
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
I think you're right. The problem I was having was a disconnect between the direction and the dx, dy. The better solution is, IMO,
Jeff
Python Syntax (Toggle Plain Text)
def get_direction(self): return self._direction def set_direction(self, value): self._direction = value dx,dy = {UP: (0,-1), LEFT: (-1,0), DOWN: (0,1), RIGHT:(1,0)} [self._direction] direction = property(get_direction, set_direction)
Jeff
Last edited by jrcagle; Feb 14th, 2007 at 12:00 am.
![]() |
Similar Threads
- Fun Snowplow Game (Geeks' Lounge)
- http://fun-zone.uni.cc for low price (Websites for Sale)
- Cryptography Fun (Python)
- Guitar FUN (Geeks' Lounge)
- a fun RTS for linux (*nix Software)
- Fun with Forms (PHP)
Other Threads in the Python Forum
- Previous Thread: Tkinter Spreadsheet
- Next Thread: wxPython: wx.RadioButtons SetValue has no affect in Linux
| Thread Tools | Search this Thread |
address alarm anydbm app beginner cipher cmd conversion coordinates curves cx-freeze data decimals development dictionary directory dynamic error excel feet file float format function generator getvalue halp handling homework http images import input ip itunes java keycontrol leftmouse line linux list lists loan loop maintain maze millimeter module mouse number numbers output parsing path port prime programming push py2exe pygame pymailer python queue random recursion recursive schedule screensaverloopinactive script scrolledtext searchingfile slicenotation split sqlite ssh string strings sudokusolver text threading time tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 variable variables ventrilo vigenere web webservice wikipedia wxpython xlwt






