python - 检测元素是否已经被点击 pygame
问题描述
我正在 pygame 中制作一个简单的“查找对象”游戏。每当单击一个元素时,它都会将分数增加 1。我正在使用元素的坐标来检查它是否已被单击。这是代码:
import pygame
pygame.init()
pygame.mixer.init()
screen_width = 905
screen_height = 390
screen = pygame.display.set_mode([screen_width, screen_height])
pygame.display.set_caption("Game")
clock = pygame.time.Clock()
done = False
score = 0
first = pygame.image.load("1st.png").convert()
current_level_no = 1
if current_level_no ==1:
screen.blit(first, [0, 0])
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.MOUSEBUTTONDOWN:
x = event.pos[0]
y = event.pos[1]
if current_level_no == 1:
if x > 766 and x < 787:
if y > 311 and y < 339:
pygame.draw.ellipse (screen, LIME, [766, 311, 21, 28], 3)
score +=1
if x > 60 and x < 135:
if y > 365 and y < 380:
pygame.draw.ellipse (screen, LIME, [60, 365, 71, 15], 3)
score +=1
if x > 798 and x < 843:
if y > 150 and y < 171:
pygame.draw.ellipse (screen, LIME, [798, 150, 45, 25], 3)
score +=1
if x > 61 and x < 102:
if y > 115 and y < 141:
pygame.draw.ellipse (screen, LIME, [61, 115, 44, 36], 3)
score +=1
if x > 357 and x < 394:
if y > 151 and y < 170:
pygame.draw.ellipse (screen, LIME, [357, 151, 44, 22], 3)
score +=1
if x > 256 and x < 288:
if y > 209 and y < 230:
pygame.draw.ellipse (screen, LIME, [256, 209, 37, 25], 3)
score +=1
clock.tick(60)
pygame.display.flip()
pygame.quit()
我想知道如何检测元素是否已被用户单击。谢谢。
解决方案
推荐阅读
- c# - 将不在查询中与日期范围过滤器相结合产生零结果
- reactjs - 如何用 Jest 覆盖 mapDispatchToProps 中的行?
- centos - qemu-sparc 与 qemu-system-sparc
- java - 使用条件 Bean 自动装配构造函数
- c# - 使用 Roslyn 获取程序集属性
- javascript - 添加新列表项时,我创建的切换功能不起作用
- java - 如何使用正则表达式java在两个字符串中查找匹配的字符
- javascript - 打字稿中的类接口
- php - 如何写一个条件,以便 sql 语句提取 2 小时前已解决的订单?
- python - 如何根据多列中的单个值从多索引中进行选择?