可能重复:
How would i change the width of the snake in my “snake game”?
我正在使用python和pygame: 我怎样才能改变游戏中“蠕虫”的宽度?到目前为止蛇只有1像素宽,我怎么能将宽度改为3或4像素宽?
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
新法典_____________________________但这会导致蛇继续生长
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
答案 0 :(得分:2)
用
增长 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))
... # corner cases if they are important
draw()会自动接收正文中的新像素。不错的游戏,我不久前在诺基亚PyS60上攻击了它。
答案 1 :(得分:0)
Surface.set_at
用于放置彩色像素,但是通过操作x
值或y
值,您应该能够通过循环运行该代码并将其打印出来多次。
编辑抱歉,代码应该添加到Y值以使蛇“更厚”......
这是伪代码:
#Snake
for snakeLength in range(3):
if Snake is facing right:
self.surface.set_at((x, y+snakeLength), self.color)
这是我这样做的想法,但它是未经考验的抱歉。我经常使用精灵。
修改: Pygame Docs,“一次获取和设置一个像素通常太慢,无法在游戏或实时情况下使用。”< / p>