Possible Duplicate:
How would i change the width of the snake in my “snake game”?
Im using python and pygame:
How can i change the width of the “worm” in my game? So far the snake is only 1 pixel wide how can i change the width to be 3 or 4 pixels wide?
class Worm:
def __init__(self, surface):
self.surface = surface
self.x = surface.get_width()/2
self.y = surface.get_height()/2
self.length = 5
self.grow_to = 50
self.vx = 0
self.vy = -1
self.body = []
self.crashed = False
self.color = (255, 255, 0)
def move(self):
"""Move the worm"""
self.x += self.vx
self.y += self.vy
if (self.x, self.y) in self.body:
self.crashed = True
self.body.insert(0, (self.x, self.y))
if (self.grow_to > self.length):
self.length += 1
if len(self.body) > self.length:
self.body.pop()
def draw(self):
for x, y in self.body:
self.surface.set_at((x, y), self.color)
def position (self):
return self.x, self.y
def eat(self):
self.grow_to += 25
NEW CODE____________________________ But this causes the snake to grow continuesly
class Worm:
def __init__(self, surface):
self.surface = surface
self.x = surface.get_width()/2
self.y = surface.get_height()/2
self.length = 5
self.grow_to = 50
self.vx = 0
self.vy = -1
self.body = []
self.crashed = False
self.color = (255, 255, 0)
def move(self):
"""Move the worm"""
self.x += self.vx
self.y += self.vy
if (self.x, self.y) in self.body:
self.crashed = True
self.body.insert(0, (self.x, self.y))
self.body.insert(0, (self.x, self.y+1))
self.body.insert(0, (self.x, self.y-1))
self.body.insert(0, (self.x+1, self.y))
self.body.insert(0, (self.x-1, self.y))
if (self.grow_to > self.length):
self.length += 1
if len(self.body) > self.length:
self.body.pop()
def draw(self):
for x, y in self.body:
self.surface.set_at((x, y), self.color)
for x, y in self.body:
self.surface.set_at((x, y), self.color)
def position (self):
return self.x, self.y
return self.x, self.y+1
return self.x, self.y-1
return self.x+1, self.y
return self.x-1, self.y
def eat(self):
self.grow_to += 25
Grow it thicker with
draw() will automatically take in the new pixels in body. Nice game, I hacked it on Nokia PyS60 some time ago.