By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,186 Members | 1,037 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,186 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.cent er,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.cent er,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.cent er,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?

Mar 18 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Isis wrote:
Hi all,
I am writing a snake game, but all is not going well...


Chances are good that you'll get access to many more potential
respondents by posting the question to the Pygame mailing list.
Mar 18 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.