Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pygame Surface updates non-sequentially

Tags:

People also ask

What does blit mean in pygame?

blit() — blit stands for Block Transfer—and it's going to copy the contents of one Surface onto another Surface . 00:17 The two surfaces in question are the screen that you created and the new Surface . So, . blit() will take that rectangular Surface and put it on top of the screen.

What does Set_mode do in pygame?

This function can be called before pygame. display. set_mode() to create the icon before the display mode is set. If the display has a window title, this function will change the name on the window.

How do I update my pygame display?

update() Function. After you are done calling the drawing functions to make the display Surface object look the way you want, you must call pygame. display. update() to make the display Surface actually appear on the user's monitor.

How is surface defined in pygame?

Creating a surface Creating surfaces in pygame is quite easy. We just have to pass the height and the width with a tuple to pygame. Surface() method. We can use various methods to format our surface as we want.


I am attempting to implement a simple Pygame script that is supposed to:

  1. First, check for when the user presses the Space key;
  2. On Space keypress, display some text; then
  3. Pauses for 2 seconds and then update the screen to its original state.

Note that all of the above events must occur sequentially, and cannot be out of order.

I am encountering problems where the program first pauses, then displays the text only appears for a split second (or not at all) before the screen updates to its original state.

The program seems to have skipped over step 2 and proceeded with the pause in step 3 before displaying text. My code is as follows:

import pygame
import sys
from pygame.locals import *

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

pygame.init()
wS = pygame.display.set_mode((500, 500), 0, 32)


# Method works as intended by itself
def create_text(x, y, phrase, color):
    """
    Create and displays text onto the globally-defined `wS` Surface
    (params in docstring omitted)
    """
    # Assume that the font file exists and is successfully found
    font_obj = pygame.font.Font("./roboto_mono.ttf", 32)
    text_surface_obj = font_obj.render(phrase, True, color)
    text_rect_obj = text_surface_obj.get_rect()
    text_rect_obj.center = (x, y)
    wS.blit(text_surface_obj, text_rect_obj)


while True:
    wS.fill(BLACK)
    for event in pygame.event.get():
        if event.type == KEYDOWN and event.key == K_SPACE:

            # Snippet containing unexpected behavior
            create_text(250, 250, "Hello world!", WHITE)
            pygame.display.update()
            pygame.time.delay(2000)
            # Snippet end

        if event.type == QUIT:
            pygame.quit()
            sys.exit(0)
    pygame.display.update()

Thanks in advance!