首页 > 解决方案 > 当我尝试在 Pygame 中使用鼠标事件和按键事件时,它不起作用

问题描述

您好,我想在 Pygame 中寻求帮助。我正在尝试制作类似于 osu 的非常非常 aliexpress 版本的东西,而我的问题是让我们说有两个不同的圆圈,一个是红色的,一个是蓝色的

x = random.randint(100, 650)
y = random.randint(100, 650)
circle_list = [circlered, circleblue]
circle = random.choice(circle_list)
window.blit(circle,(x,y)) 

这就是我画圆的方式

现在我想知道是否在右圆圈中按下了右键对于红色圆圈,键是 K_x 对于蓝色圆圈,键是 K_y

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_x and incircle == True and circle == circlered:
                window.blit(background, (0,0))
                x = random.randint(100, 650)
                y = random.randint(100, 650)
                circle_list = [circlered, circleblue]
                circle = random.choice(circle_list)
                window.blit(circle,(x,y)) 
                incircle = False

    if event.key == pygame.K_c and incircle == True and circle == circleblue:
                window.blit(background, (0,0))
                x = random.randint(100, 650)
                y = random.randint(100, 650)
                circle_list = [circlered, circleblue]
                circle = random.choice(circle_list)
                window.blit(circle,(x,y)) 
                incircle = False

顺便说一句,“incircle”是让程序知道光标是否在圆圈中:D

好的,这里的问题是我可以按 x 或 c 并且它仍然可以完成任务可能不会有时我可以在红色圆圈上按 c 有时我可以在蓝色圆圈上按 x

如果您需要查看整个代码: https ://paste.pythondiscord.com/oyipamataf.apache

每一个帮助都非常感谢!

标签: python-3.xpygame

解决方案


推荐阅读