2011-10-24 26 views
0

可能重複:
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 
+0

你最近怎麼畫蛇? Pygame,我假設,但你使用'pygame.draw.rect(self.image,color,[xpos,ypos,width,height])'...? – Singular1ty

+0

沒有即時通訊使用'self.surface.set_at((x,y),self.color)' – enrique2334

+0

'Surface.set_at'是爲放置一個彩色像素而設計的,所以根據蛇面對的方式,運行'表面。 set_at'兩次或三次,每次將1個像素添加到x值。 – Singular1ty

回答

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()會自動獲取正文中的新像素。不錯的遊戲,我前段時間在Nokia PyS60上黑了。

+0

現在,當我提出你的建議時,蛇的移動速度比它刪除它的「尾巴」的速度快,因此它的持續增長速度將會持續。 – enrique2334

+0

你是對的,它應該只在y方向上移動,而在x方向移動時,反之亦然。 – Risto

+0

你是什麼意思? – enrique2334

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,「獲取和設置像素一次一個一般速度太慢,遊戲或實時環境中使用。」