【问题标题】:Click detection issue with pygamepygame的点击检测问题
【发布时间】:2021-11-09 09:11:52
【问题描述】:
import pygame
from pygame.constants import K_DOWN
import time
from time import sleep
 
pygame.init()
screen = pygame.display.set_mode((300, 300))
tickNumber = int(0)
done = False
boxOneSymbol = 0
boxTwoSymbol = 0
boxThreeSymbol = 0
boxFourSymbol = 0
boxFiveSymbol = 0
boxSixSymbol = 0
boxSevenSymbol = 0
boxNineSymbol = 0
smallBoxColor = (154, 154, 154)
playerTurn = 0
FPS_CLOCK = pygame.time.Clock()
#mouse_presses = pygame.mouse.get_pressed()
left, middle, right = pygame.mouse.get_pressed()
 

font = pygame.font.SysFont('Roboto', 13)
 

 
while not done:
        for event in pygame.event.get():
                if event.type == pygame.QUIT:
                        done = True
                if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                    is_blue = not is_blue

        pressed = pygame.key.get_pressed()

        
        pygame.draw.rect(screen, (0, 128, 255), pygame.Rect(10, 10, 280, 280))  
        

        #if is_blue: color = (0, 128, 255)
        #else: color = (255, 100, 0)
        pygame.draw.rect(screen, smallBoxColor, pygame.Rect(30, 30, 60, 60))
        pygame.draw.rect(screen, smallBoxColor, pygame.Rect(30, 120, 60, 60))
        pygame.draw.rect(screen, smallBoxColor, pygame.Rect(30, 215, 60, 60))
        
        pygame.draw.rect(screen, smallBoxColor, pygame.Rect(120, 215, 60, 60))
        pygame.draw.rect(screen, smallBoxColor, pygame.Rect(120, 120, 60, 60))
        pygame.draw.rect(screen, smallBoxColor, pygame.Rect(120, 30, 60, 60))

        pygame.draw.rect(screen, smallBoxColor, pygame.Rect(215, 215, 60, 60))
        pygame.draw.rect(screen, smallBoxColor, pygame.Rect(215, 120, 60, 60))
        pygame.draw.rect(screen, smallBoxColor, pygame.Rect(215, 30, 60, 60))


        textsurface = font.render('Au joueur 1!', False, (0, 0, 0))
        
        
 
        #if event.type == pygame.MOUSEBUTTONDOWN:
            #mouse_presses = pygame.mouse.get_pressed()
        #if mouse_presses[0]:
            #print("Left Mouse key was clicked")

        left, middle, right = pygame.mouse.get_pressed()
 
        if left:
            print("Left Mouse Key is being pressed")

        pygame.display.update()
        FPS_CLOCK.tick(30)
        tickNumber = (tickNumber) + (1)
        #print(int(tickNumber), 'Ticks')
        pygame.display.flip()

我正在用 pygame 制作井字游戏,我需要检测玩家何时点击某处,所以我在 https://coderslegacy.com/ 上找到了这段小代码:

       left, middle, right = pygame.mouse.get_pressed()

           if left:
              print("Left Mouse Key is being pressed") 

检测左键单击,但它始终检测到最多 4 次单击而不是仅一次,因为我的时钟在我单击时刷新了 4 次,我该如何解决这个问题?

【问题讨论】:

    标签: python pygame tic-tac-toe


    【解决方案1】:

    一种方法是使用标志并添加条件:

    clicked = False
    
    while not done:
        left, middle, right = pygame.mouse.get_pressed()
    
        if left and not clicked:
            clicked = True
            print('clicked')
        if not left:
            clicked = False
    

    您也可以使用事件,因为按住按钮时不会再生成...DOWN 事件:

    for event in pygame.event.get():
        if event.type = pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                print('clicked')
    

    【讨论】:

      猜你喜欢
      • 2021-09-06
      • 1970-01-01
      相关资源
      最近更新 更多