How I do things (part 1)

A brief tour of how I’m doing those gamey things, for you to compare with your own code, as inspiration for new code, or to mock cruelly.

Moving the Character: Within the mainloop, I write:

pygame.event.pump()
movekey = pygame.key.get_pressed()
if movekey[K_LEFT]:
self.char.move(“left”)
if movekey[K_RIGHT]:
self.char.move(“right”)
if movekey[K_UP]:
self.char.move(“up”)
if movekey[K_DOWN]:
self.char.move(“down”)

Now I needn’t use the strings which the following code uses with If loops; I could just pass the movekey on and subject that to If statements like here, but I didn’t when I wrote this, and just haven’t gotten around to changing it. So, within the Character class, we have:

def move(self, key):
xMove = 0
yMove = 0

if (key == “right”):
xMove = self.x_dist
if (key == “left”):
xMove = -self.x_dist
if (key == “up”):
yMove = -self.y_dist
if (key == “down”):
yMove = self.y_dist
self.rect.move_ip(xMove,yMove)
locxnow = self.loc.GetLocationx()
locynow = self.loc.GetLocationy()
locxnow = locxnow + xMove
locynow = locynow + yMove
self.loc.SetPosition(locxnow, locynow)
print self.loc.GetLocationx()
print self.loc.GetLocationy()

This means I have twice as many If statements than I need, and will definitely be changed soon during a mass cleanup. It’s just that so far, once something worked I just kept moving, rather than optimising. But as I approach an plateau (as I have now) it affords me coding time to go back and clean things up.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: