2014-02-11 62 views
-2

我是Python新手(記住這一點),並且正在刺探初學者書中的示例之一。其中有一個程序在屏幕上繪製隨機圓圈。但是,儘管將字符複製爲字符,但circle.py的第19行仍存在語法錯誤。有人可以花時間爲我查看代碼嗎? (順便說一句,對於一個初學者,我的Python OK理解。我得到函數,變量和麪向對象編程,但我不知道以後太多了。)看不到語法錯誤

1  import pygame, random 
2 
3  class Circle: 
4   _minimum=100;_maximum=255 
5   _colour=None 
6   _properties=[] 
7  
8  def __init__(self,screen,width,height): 
9   self.random_colour() 
10   self.draw_circle(screen,width,height) 
11  
12  def draw_circle(self, screen, width, height): 
13   x=random.randint(1,width) 
14   y=random.randint(1,height) 
15   size=random.randint(1,5) 
16   self.properties=[x,y,size] 
17   pygame.draw.circle(screen,self._colour,(x,y),size) 
18  
19  def random_colour(self) 
20   red=random.randint(self._minimum,self._maximum) 
21   green=random.randint(self._minimum,self._maximum) 
22   blue=random.randint(self._minimum,self._maximum) 
23   self._colour=[red,green,blue] 
24   
25  def clear_circle(self,screen): 
26   pygame.draw_circle(screen,(0,0,0),(self._properties[0],self._properties[1],self._properties[2] 

感謝您的幫助,在所有。

回答

0
for n in range(100): 
    clock.tick(45) 
    circles.append(circle.Circle(screen,WIDTH,HEIGHT)) 
    pygame.display.update() 

clock.tick(1) 

for c in circles: 
     clock.tick(45) 
     c.clear_circle(screen) 
     pygame.display.update() 

您的第二條if語句被錯誤地縮進。將你的縮進校正到4個空格,你的語法錯誤將會消失。

你的縮排在班上也是錯誤的。確保示波器正確縮進,因爲這對蟒蛇很重要

+3

沒有義務在塊之間進行一致的縮進。 – njzk2

+0

njzk2是對的,這不是一個錯誤。當然,每個縮進級別使用一致的空間量是一個好主意,但是Python僅僅相對於前一行和下一行查看縮進級別。 – Onyxite

+0

同意。在每個塊內,縮進必須一致,但是兩個不同塊之間的縮進可能不同。 Python可能不會抱怨,但其他開發人員會。 – SethMMorton

5
def random_colour(self) # missing a colon 
    red=random.randint(self._minimum,self._maximum) 
    green=random.randint(self._minimum,self._maximum) 
    blue=random.randint(self._minimum,self._maximum) 
    self._colour=[red,green,blue]