Why won't the sprites in my group show up? (PyGame)

Isis theCodeMaiden at gmail.com
Sat Mar 18 19:58:45 CET 2006


Hi all,
I am writing a snake game, but all is not going well...

Two problems here. First of all, the little circles I use for my sprite
images only show up as quadrants.
Second of all, only the Head sprite for my snake shows up at all. Why?

Here are the class definitions:

! /usr/bin/python

import pygame as PG

seg_size = 20

class SnakeSegment(PG.sprite.Sprite):


	def __init__(self, pos):
		PG.sprite.Sprite.__init__(self)
		self.image = PG.Surface([seg_size,seg_size])
		self.rect = self.image.get_rect()
		self.rect.topleft = pos

#		self.direction = 0


class SnakeHead(SnakeSegment):

	def __init__(self,pos,spd = 10):
		PG.sprite.Sprite.__init__(self)
		SnakeSegment.__init__(self,pos)
		PG.draw.circle(self.image,(255,0,0),self.rect.center,4)
		self.speed = spd # governs speed of whole snake

	def update(self):
		self.rect.left += self.speed

class SnakeBody(SnakeSegment):
	def __init__(self,pos,prev):
		PG.sprite.Sprite.__init__(self)
		SnakeSegment.__init__(self,pos)
		PG.draw.circle(self.image,(0,255,0),self.rect.center,4)

		self.previous=prev
	def update(self):
		self.rect.center = self.previous.rect.center

class SnakeTail(SnakeSegment):
	def __init__(self,pos,prev):
		PG.sprite.Sprite.__init__(self)
		SnakeSegment.__init__(self,pos)
		PG.draw.circle(self.image,(0,0,255),self.rect.center,4)

		self.previous=prev

	def update(self):
		self.rect.center = self.previous.rect.center

class Snake(PG.sprite.OrderedUpdates):

	def __init__(self, pos, length = 6):
		PG.sprite.OrderedUpdates.__init__(self)
		self.head = SnakeHead(pos)
		self.segs = length - 2
		self.position = pos


		self.add(self.head)

		prev = self.head

		posx, posy = pos

		for x in range(self.segs):
			posx += seg_size
			prev = (SnakeBody((posx,posy),prev))
			self.add(prev)
#			print prev.rect.center
		posx += seg_size
		self.add(SnakeTail((posx, posy),prev))
		self._spritelist.reverse() # we need tail first, head last

Whew!
And here is my little test program.

#!/usr/bin/python

from snake import *
#import pygame as PG
DBUF = PG.DOUBLEBUF
FSCR = PG.FULLSCREEN
screenx = 640
screeny = 480

def main():
	PG.init()

	screen = PG.display.set_mode((screenx, screeny)) #python likes to make
pointers.

	q = Snake((100,100))
	q.draw(screen)
	q.update()
	PG.display.flip()
	while 1:
		PG.event.pump()
		keyinput = PG.key.get_pressed()
		if keyinput[PG.K_ESCAPE] or PG.event.peek(PG.QUIT):
			break

if __name__ == '__main__':
	main()

Whoop, there it is. What have I done wrong?




More information about the Python-list mailing list