首页 > 解决方案 > 检测元素是否已经被点击 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()

我想知道如何检测元素是否已被用户单击。谢谢。

标签: pythonpygame

解决方案


推荐阅读