可能重複:
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
NEW CODE____________________________但是這將導致蛇增長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
你最近怎麼畫蛇? Pygame,我假設,但你使用'pygame.draw.rect(self.image,color,[xpos,ypos,width,height])'...? – Singular1ty
沒有即時通訊使用'self.surface.set_at((x,y),self.color)' – enrique2334
'Surface.set_at'是爲放置一個彩色像素而設計的,所以根據蛇面對的方式,運行'表面。 set_at'兩次或三次,每次將1個像素添加到x值。 – Singular1ty