Archive for movement

Solid Map Objects

Posted in code with tags , , , , on August 3, 2008 by Tau

The last thing left on my list before being happy with the movement drawing was the characters interaction with the map objects. Previously, the Character Sprite was drawn after the Map Objects and so would run straight over them. Not Good. So what I’ve done is added another attribute to the input file “Solid”, if this is the case, and if a movement in a given direction would cause the character’s Rect would collide with the Map Object’s Rect, the movement function isn’t called. Thus making the object appear solid.

canmove = True
if movekey[K_LEFT]:
for image in self.thismap.grids:
if image.getSolid():
if image.rect.collidepoint(self.char.rect.centerx – 10,            self.char.rect.centery):
canmove = False
if canmove:
self.char.move(“left”)
self.Scrolling(“left”)
self.drawMap()

Next, I think, I’d like to develop the Character. It may just be a matter of an arbitrary event that gives +10 HP or whatever, and stores for further use.

I’ve also renamed the main file from gametest to nsf0.1, and will keep versions from now on. I’m tempted to use CVS, but it’s just another thing to learn, and my learning list is a little full right now.

How I do things (part 1)

Posted in code with tags , on July 29, 2008 by Tau

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.