lundi 6 mars 2017

I want to stop this 'if statement' from repeating

Whenever I click on the square once, it prints the text "You have opened a chest!" numerous times, but I want the program to only print it once per click even if I hold the mouse button in. What do I adjust or add in?

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

Please also give me the adjusted code; that would be highly appreciated. Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire