| | |
just for fun
![]() |
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 149
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: 149
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 |
alarm anydbm app assignment beginner bluetooth character cipher cmd conversion coordinates corners curves customdialog cx-freeze data decimals definedlines development directory events excel exe feet file float format function generator getvalue gnu halp handling homework http ideas input ip itunes keycontrol leftmouse line linux list lists loan loop maintain maze millimeter module mouse number numbers output parsing path prime programming push py2exe pygame pymailer python queue random rational raw_input recursion recursive schedule screensaverloopinactive script searchingfile slicenotation sqlite ssh string strings sudokusolver text time tlapse tooltip tuple type ubuntu unicode url urllib urllib2 variable ventrilo vigenere web webservice wikipedia wxpython xlib xlwt






