2009-02-26 164 views
2

我正在構建一個相對簡單的程序來測試碰撞檢測,這一切都工作正常,除了一件事情,我試圖使背景顏色隨機變化,唯一的問題是它似乎完全忽略了這樣做的功能;pygame函數似乎被忽略

import pygame 
from pygame.locals import * 
import random, math, time, sys 
pygame.init() 

Surface = pygame.display.set_mode((800,600)) 

backgroundr = int(random.random()*255)+1 
backgroundg = int(random.random()*255)+1 
backgroundb = int(random.random()*255)+1 

Circles = [] 
class Circle: 
    def __init__(self): 
     self.radius = int(random.random()*50) + 1 
     self.x = random.randint(self.radius, 800-self.radius) 
     self.y = random.randint(self.radius, 600-self.radius) 
     self.speedx = 0.5*(random.random()+1.0) 
     self.speedy = 0.5*(random.random()+1.0) 
     self.r = int(random.random()*255)+1 
     self.g = int(random.random()*255)+1 
     self.b = int(random.random()*255)+1 
##  self.mass = math.sqrt(self.radius) 

for x in range(int(random.random()*30) + 1): 
    Circles.append(Circle()) 

def CircleCollide(C1,C2): 
    C1Speed = math.sqrt((C1.speedx**2)+(C1.speedy**2)) 
    XDiff = -(C1.x-C2.x) 
    YDiff = -(C1.y-C2.y) 
    if XDiff > 0: 
     if YDiff > 0: 
      Angle = math.degrees(math.atan(YDiff/XDiff)) 
      XSpeed = -C1Speed*math.cos(math.radians(Angle)) 
      YSpeed = -C1Speed*math.sin(math.radians(Angle)) 
     elif YDiff < 0: 
      Angle = math.degrees(math.atan(YDiff/XDiff)) 
      XSpeed = -C1Speed*math.cos(math.radians(Angle)) 
      YSpeed = -C1Speed*math.sin(math.radians(Angle)) 
    elif XDiff < 0: 
     if YDiff > 0: 
      Angle = 180 + math.degrees(math.atan(YDiff/XDiff)) 
      XSpeed = -C1Speed*math.cos(math.radians(Angle)) 
      YSpeed = -C1Speed*math.sin(math.radians(Angle)) 
     elif YDiff < 0: 
      Angle = -180 + math.degrees(math.atan(YDiff/XDiff)) 
      XSpeed = -C1Speed*math.cos(math.radians(Angle)) 
      YSpeed = -C1Speed*math.sin(math.radians(Angle)) 
    elif XDiff == 0: 
     if YDiff > 0: 
      Angle = -90 
     else: 
      Angle = 90 
     XSpeed = C1Speed*math.cos(math.radians(Angle)) 
     YSpeed = C1Speed*math.sin(math.radians(Angle)) 
    elif YDiff == 0: 
     if XDiff < 0: 
      Angle = 0 
     else: 
      Angle = 180 
     XSpeed = C1Speed*math.cos(math.radians(Angle)) 
     YSpeed = C1Speed*math.sin(math.radians(Angle)) 
    C1.speedx = XSpeed 
    C1.speedy = YSpeed 
    C1.r = int(random.random()*255)+1 
    C1.g = int(random.random()*255)+1 
    C1.b = int(random.random()*255)+1 
    C2.r = int(random.random()*255)+1 
    C2.g = int(random.random()*255)+1 
    C2.b = int(random.random()*255)+1 

def ColourCheck(): 
    checknumber = int(random.random()*50)+1 
    if checknumber == 50: 
     backgroundr = int(random.random()*255)+1 
     backgroundg = int(random.random()*255)+1 
     backgroundb = int(random.random()*255)+1 

def Move(): 
    for Circle in Circles: 
     Circle.x += Circle.speedx 
     Circle.y += Circle.speedy 
def CollisionDetect(): 
    for Circle in Circles: 
     if Circle.x < Circle.radius or Circle.x > 800-Circle.radius: 
      Circle.speedx *= -1 
      Circle.r = int(random.random()*255)+1 
      Circle.g = int(random.random()*255)+1 
      Circle.b = int(random.random()*255)+1 
     if Circle.y < Circle.radius or Circle.y > 600-Circle.radius: 
      Circle.speedy *= -1 
      Circle.r = int(random.random()*255)+1 
      Circle.g = int(random.random()*255)+1 
      Circle.b = int(random.random()*255)+1 
    for Circle in Circles: 
     for Circle2 in Circles: 
      if Circle != Circle2: 
       if math.sqrt( ((Circle.x-Circle2.x)**2) + ((Circle.y-Circle2.y)**2) ) <= (Circle.radius+Circle2.radius): 
        CircleCollide(Circle,Circle2) 
def Draw(): 
    Surface.fill((backgroundr,backgroundg,backgroundb)) 
    for Circle in Circles: 
     pygame.draw.circle(Surface,(Circle.r,Circle.g,Circle.b),(int(Circle.x),int(600-Circle.y)),Circle.radius) 
    pygame.display.flip() 

def GetInput(): 
    keystate = pygame.key.get_pressed() 
    for event in pygame.event.get(): 
     if event.type == QUIT or keystate[K_ESCAPE]: 
      pygame.quit(); sys.exit() 

def main(): 
    while True: 
     ColourCheck() 
     GetInput() 
     Move() 
     CollisionDetect() 
     Draw() 
if __name__ == '__main__': main() 

這是ColourCheck功能被忽略,任何想法爲什麼?

回答

6

我相信backgroundr,backgroundg和backgroundb是你的ColourCheck()函數的局部變量。

如果你決定使用全局變量,試試這個在您的文件的頂部:

global backgroundr; 
global backgroundg; 
global backgroundb; 
backgroundr = int(random.random()*255)+1 
backgroundg = int(random.random()*255)+1 
backgroundb = int(random.random()*255)+1 

,這在你的函數:

def ColourCheck(): 
    global backgroundr; 
    global backgroundg; 
    global backgroundb; 
    checknumber = int(random.random()*50)+1 
    if checknumber == 50: 
     backgroundr = int(random.random()*255)+1 
     backgroundg = int(random.random()*255)+1 
     backgroundb = int(random.random()*255)+1 
+0

噢,重做東西會更明智一些,所以這些都不是全局變量。 :-) – Nathan 2009-02-26 18:27:01

0

Move()CollisionDetect()Draw()所有請參閱Circles,但不要將其聲明爲全局。嘗試在每個函數的開頭添加一個global Circles行。另外,我建議將變量更改爲小寫;不僅初始上限通常表示Python中的類,而且實際上在Circle變量和Circle類之間產生(無關緊要)衝突。

例如:

circles = [] 

# ... 

for x in range(int(random.random()*30) + 1): 
    circles.append(Circle()) 

# ... 

def Move(): 
    global circles 

    for circle in circles: 
     circle.x += circle.speedx 
     circle.y += circle.speedy 

# ... 

編輯:

而且彌敦道指出,您的backgroundX變量也需要ColorCheck()Draw()被宣佈全球。

你可能會考慮將所有這些函數包裝到一個Game類(或類似的)中,以避免使用這麼多的全局變量。