【发布时间】: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