我正在构建一个相对简单的程序来测试碰撞检测,除了一件事之外,它现在一切正常,我试图使背景颜色随机变化,唯一的问题是它似乎完全跳过执行此操作的功能;
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
函数被忽略,任何想法为什么?
答案 0 :(得分: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
答案 1 :(得分: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
# ...
修改强>
正如Nathan所说,您的backgroundX
变量也需要在ColorCheck()
和Draw()
中声明为全局。
您可以考虑将所有这些函数包装到Game
类(或某些类)中,以避免使用这么多全局变量。