简体   繁体   中英

pygame time management on event

Sorry for the novel answer. I wants to make a grid and, when I click on square, then pass to WHITE one second and, then, return to BLACK. For me, the logical form is this, but there are something that I don't understand: (the pygame.time.delay(1000) don't work)

import pygame

# Pygame screen
# Cuadrados
# Añadir tiempo al click

# colores

BLACK    = (   0,   0,   0)
WHITE    = ( 255, 255, 255)
GREEN    = (   0, 255,   0)
RED      = ( 255,   0,   0)

# iniciar pygame

pygame.init()

# caracteristicas de la ventana

size = (260,260)
screen = pygame.display.set_mode(size)

pygame.display.set_caption("Pantalla")

# definir array de cuadro (10x10)

width = 20
height = 20
margin = 5

grid = [[0 for x in range(10)] for y in range(10)]


# control de procesos

done = False

clock = pygame.time.Clock()

# loop principal

while not done:

    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            done = True

        # evento de click

        elif event.type == pygame.MOUSEBUTTONDOWN:
            column = pos[0] // (width + margin)
            row = pos[1] // (height + margin)
            # print
            print("Click ", pos, "Coordenadas: ", row, column)
            grid[row][column] = 1
            pygame.time.delay(1000) 
            grid[row][column] = 0


    # lógica de click
    pos = pygame.mouse.get_pos()
    x = pos[0]
    y = pos[1]

    #color de fondo

    screen.fill(WHITE)

    # dibujar cuadro

    for row in range(10):
        for column in range(10):
            if grid[row][column] == 1:
                color = WHITE
            else:
                color = BLACK
            pygame.draw.rect(screen, color, [margin + (margin + width) * column, margin + (margin + height) * row, width, height])



    # escribir todo

    pygame.display.flip()

    clock.tick(60)

# finalizar

pygame.quit()

pygame.time.delay(1000) IS working! This causes the program to do absolutely nothing for a full second; this includes not updating the screen, because the screen will not update without pygame.display.flip() being called, which won't happen because the program isn't even doing anything for a full second.

Here is an example of a program that waits a full second the correct way:

import pygame, sys
pygame.init()
screen = pygame.display.set_mode([500,500])
clock = pygame.time.Clock()

fps = 60
delay = 0

while 1:
    clock.tick(fps)
    delay -= 1
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            delay = fps #a one second delay
    if delay <= 0:
        screen.fill([0,0,0])
    else:
        screen.fill([255,255,255])
    pygame.display.flip()

Notice the only delay here is the frame rate limiter (clock.tick). Since delay is set to fps , and fps is "frames per second ", setting delay to fps equals a full second delay.

I have not been able to test this code, but what it should do is flash white for one second when you click the screen. You should be able to apply the same concept in your own code.

If you need further clarification (or my code isn't working), please notify me in the comments, I would be more than happy to help you further!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM