arrested (gaming) development: sprites, pixies and other fantasy beings!

please note in order to see the code and the videos, you’ll need to go to the full post. feedburner doesn’t appear to play nice with my embedded videos. and if you are looking at the full blog post, yes the sourcecode displayer looks a bit naff, soz.

sprites are ludicrously easy to move around in game maker. place them in a room (the screen) and make sure the object sprite reacts to the edges by giving it an action i.e. move in the opposite direction, and badda bing! you have a moving sprite!

i knew vaguely that pygame had a sprite type object, but it was only when linux format did a feature on games development using the module that it all started to make sense.

i took their sprite class and tweaked it slightly to make the sprite transparent based on the color of the bottom left pixel as game maker does. also based on the chimp tutorial i converted the image to speed up blitting (screen drawing).

class Sprite:
	def __init__(self, xpos, ypos, filename):
		self.x = xpos
		self.y = ypos
		self.bitmap = image.load(filename)
		self.bitmap = self.bitmap.convert() # faster blitting apparently...
		self.dy = 1 # going down

		# set transparency based on bottom left pixel
		self.colorkey = self.bitmap.get_at((0,self.bitmap.get_height()-1))
		self.bitmap.set_colorkey(self.colorkey)

	def set_position(self, xpos, ypos):
		self.x = xpos
		self.y = ypos

	def render(self):
		screen.blit(self.bitmap, (self.x, self.y))

the only improvement i made to sprite loading was keeping file handling os neutral by using os.path.join

backdrop = image.load(os.path.join('data', 'Background.bmp'))
backdrop = backdrop.convert() # another tweak
hero = Sprite(20, 200, os.path.join('data','Dragon.gif'))
boss = Sprite(500, 200, os.path.join('data','Boss.gif'))
fireball = Sprite(0, 480, os.path.join('data','Fireball.gif'))

as with all pygames there is an infinite loop that wraps the game ticks (steps in game maker) until the game stops, there’s no point placing sample code, just know that it exists and the rest of the code detailed is looped within it.

next we get to the movement for the hero (yes, the code hasn’t changed much from the lxf samples, i’ll rewrite by the next post to make things a little more clearer). pretty bog standard stuff, once you know how keyboard input works. i also added an event if the user presses escape as this quits the game in game maker.

	for ourevent in event.get():
		if ourevent.type == QUIT:
			quit = 1
		if ourevent.type == KEYDOWN:
			if ourevent.key == K_DOWN and hero.y < 370:
				hero.y += 5
			if ourevent.key == K_UP and hero.y > 10:
				hero.y -= 5
			if ourevent.key == K_ESCAPE:
				quit = 1
			if ourevent.key == K_SPACE:
				fireball.x = hero.x + 100
				fireball.y = hero.y + 10

next up is the boss. (though i get the feeling these actions should be wrapped up in the actual boss sprite class – watch this space.) very basic checks to determine if the sprite is going up (decrement the y position) or down (increment the y position) and when we toggle a flag to change direction when we reach the top or bottom of the screen.

	if boss.dy:
		boss.y += 5
	else:
		boss.y -= 5

	if boss.y > 350:
		boss.dy = 0

	if boss.y < -50:
		boss.dy = 1

finally update the dragon's fireball position, refresh the display.

	fireball.x += 10

	boss.render()
	hero.render()
	fireball.render()

	display.update()
	clock.tick(60)

as a point of reference i will include video posts of how the game should look in game maker and what i have so far been able to reproduce in pygame. so first up is the game maker demo:

and next is the pygame version:

eagle eyed viewers will note that the pygame version doesn't have animated sprites. that's because pygame doesn't do "animated" gifs, but fear not readers we've made this the topic of next week's post!

prev | about | next (animation)


About this entry