我想停止这个'如果声明'从重复

时间:2017-03-06 15:20:56

标签: if-statement printing click python-3.4

每当我点击广场一次,它就会打印文字"你已经打开了一个胸部!"很多次,但我希望程序每次点击只打印一次,即使我按住鼠标按钮。我可以调整或添加什么?

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()

还请给我调整后的代码;非常感谢。提前谢谢!

1 个答案:

答案 0 :(得分:0)

尝试在打印行后面将pressed1的值更改为0, pressed1 = 0