我正在構建一個相對簡單的程序來測試碰撞檢測,這一切都工作正常,除了一件事情,我試圖使背景顏色隨機變化,唯一的問題是它似乎完全忽略了這樣做的功能;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
功能被忽略,任何想法爲什麼?
噢,重做東西會更明智一些,所以這些都不是全局變量。 :-) – Nathan 2009-02-26 18:27:01