单击几下后,pygame窗口随机发出反应症
我正在尝试制作一个类似于 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()
I am trying to make a game similar to https://aimtrainer.io as a personal project and my pygame window keeps going unresponsive. My program will start, but after 1-15 circle clicks it stops working. It only goes unresponsive if I click a circle, other parts of the window are fine. I'm not sure what to do, any advice is appreciated.
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
交叉点= false
必须在循环的开头,而不是在循环之前设置,否则,一旦2个圆圈相交,这将导致无尽的循环:intersection = False
must be set at the beginning of the loop, instead of before the loop, otherwise this will result in an endless loop once 2 circles intersect: