gpt4 book ai didi

python - pygame 函数似乎被忽略

转载 作者:行者123 更新时间:2023-11-28 21:30:58 24 4
gpt4 key购买 nike

我正在构建一个相对简单的程序来测试碰撞检测,目前一切正常,除了一件事,我试图使背景颜色随机变化,唯一的问题是它似乎完全跳过执行此操作的函数;

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 函数被忽略了,知道为什么吗?

最佳答案

我相信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

关于python - pygame 函数似乎被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/591776/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com