每当我点击广场一次,它就会打印文字"你已经打开了一个胸部!"很多次,但我希望程序每次点击只打印一次,即使我按住鼠标按钮。我可以调整或添加什么?
import pygame
pygame.init()
pygame.display.set_caption('Crash!')
window = pygame.display.set_mode((300, 300))
running = True
#Draw Once
Rectplace = pygame.draw.rect(window, (255, 0, 0),(100, 100, 100, 100))
pygame.display.update()
#Main Loop
while running:
#mouse position and button clicking
pos = pygame.mouse.get_pos()
(pressed1,pressed2,pressed3) = pygame.mouse.get_pressed()
#if statement
if Rectplace.collidepoint(pos) and pressed1 == 1:
print("You have opened a chest!")
#Quit pygame
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
还请给我调整后的代码;非常感谢。提前谢谢!
答案 0 :(得分:0)
尝试在打印行后面将pressed1的值更改为0, pressed1 = 0