gpt4 book ai didi

python - 点击几下后 Pygame 窗口随机无响应

转载 作者:行者123 更新时间:2023-12-05 03:26:46 25 4
gpt4 key购买 nike

我正在尝试制作类似于 https://aimtrainer.io 的游戏作为个人项目,我的 pygame 窗口一直没有响应。我的程序将启动,但在 1-15 圈点击后它停止工作。如果我单击一个圆圈,它只会没有响应,窗口的其他部分都很好。我不确定该怎么做,欢迎任何建议。

from random import randint
import pygame, math

WIDTH, HEIGHT = 750, 750
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Click the circles!")

BLACK = (25, 25, 25)
RED = (163, 0, 0)

RADIUS = 40
CIRCLE_COUNT = 5
targetList = []

FPS = 60

class Target:

def __init__(self, x, y):
self.x = x
self.y = y

def drawTarget(self):
pygame.draw.circle(WINDOW, RED, (self.x, self.y), RADIUS)

def inTarget(self, posx, posy):
sqx = (posx - self.x) ** 2
sqy = (posy - self.y) ** 2
if math.sqrt(sqx + sqy) < RADIUS:
return True

def targetsDontIntersect(self, secondTarget):
return math.sqrt((self.x - secondTarget.x) ** 2 + (self.y - secondTarget.y) ** 2) > RADIUS * 2 #True if they don't intersect

def __str__(self) -> str:
return "X is {} and Y is {}".format(self.x, self.y)

def addTarget():

intersection = False
while True:
t = Target(randint(RADIUS, WIDTH - RADIUS), randint(RADIUS, HEIGHT - RADIUS))
for t2 in targetList:
if not t.targetsDontIntersect(t2):
intersection = True

if not intersection:
targetList.append(t)
break

def drawWindow():
WINDOW.fill(BLACK)

if len(targetList) < CIRCLE_COUNT:
addTarget()

for target in targetList:
target.drawTarget()
pygame.display.update()

# Add targets so that they don't intersect
def addTargets():
targetList.append(Target(randint(RADIUS, WIDTH - RADIUS), randint(RADIUS, HEIGHT - RADIUS)))

while len(targetList) < CIRCLE_COUNT:
intersection = False
t = Target(randint(RADIUS, WIDTH - RADIUS), randint(RADIUS, HEIGHT - RADIUS))

for t2 in targetList:
if (not t.targetsDontIntersect(t2)):
intersection = True
break

if not intersection:
targetList.append(t)

def main():
global targetList
clock = pygame.time.Clock()

addTargets()
drawWindow()

while True:
clock.tick(FPS)

for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.MOUSEBUTTONUP:
x, y = pygame.mouse.get_pos()
targetList = [num for num in targetList if not num.inTarget(x, y)] #List of circles that weren't clicked
drawWindow()

if __name__ == "__main__":
main()

最佳答案

intersection = False 必须在循环开始时设置,而不是在循环之前设置,否则一旦 2 个圆相交将导致无限循环:

def addTarget():

# intersection = False <-- DELETE
while True:
intersection = False # <-- INSERT

t = Target(randint(RADIUS, WIDTH - RADIUS), randint(RADIUS, HEIGHT - RADIUS))
for t2 in targetList:
if not t.targetsDontIntersect(t2):
intersection = True
if not intersection:
targetList.append(t)
break

关于python - 点击几下后 Pygame 窗口随机无响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71657700/

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