2014-02-11 58 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]