13

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!

Jerrybibo
  • 1,315
  • 1
  • 21
  • 27
  • Try to remove the two lines under `pygame.time.delay(2000)`. It looks like they're unnecessary and could cause the problem. – skrx May 31 '18 at 09:19
  • @skrx Once I've removed the two lines below the `delay()` method call, I do see the `Hello World!` text getting blitted; however, it only gets displayed for an instant (one frame?) as opposed to the intended 2 seconds. – Jerrybibo May 31 '18 at 09:22
  • That's odd. The execution of the program should be stopped after the `delay` call. BTW, I had trouble to reproduce the bug in the first place. I think I saw it once for a very short time but couldn't reproduce it again. Perhaps it would be better to restructure the program and use one of these [timers](https://stackoverflow.com/questions/30720665/countdown-timer-in-pygame). – skrx May 31 '18 at 09:38

2 Answers2

9

For some reason the program worked for me with the only thing I changed was the font. This is the same as what @Omega0x013 is doing, but you have an unlimited framerate. It works because FPS.tick() returns the amount of time since it was last called, if you don't specify a frame rate, it does not hold the program.

import pygame
import sys
from pygame.locals import *
displayText = False
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(None,30)
    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)

FPS = pygame.time.Clock()
while True:
    wS.fill(BLACK)
    for event in pygame.event.get():
        if event.type == KEYDOWN and event.key == K_SPACE:
            totalTime = 0
            FPS.tick()
            displayText = True

        if event.type == QUIT:
            pygame.quit()
            sys.exit(0)
    if displayText:
        totalTime += FPS.tick()
        create_text(250, 250, "Hello world!", WHITE)
        if totalTime >= 2000:
            displayText = False
    pygame.display.update()
  • Wow, this solution is perfect for my usage. Thanks! (Also, shouldn't it be OK if the `FPS.tick()` call is removed from the event loop?) – Jerrybibo Jul 17 '18 at 02:05
  • 4
    The Fps.Tick() returns the amount of time since it was last called. If I press space then 5 seconds later I press space again and call FPS.tick() it will return 5 seconds, meaning the screen will instantly turn black, by keeping it in the event loop, whenever i press space, the time is reset. –  Jul 17 '18 at 02:28
8

you should try this:

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)
clock = pygame.time.Clock()

showing = False
timer = 0
# 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:
            showing = True
            timer = 0

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

    if showing:
        timer += 1
        create_text(250, 250, "Hello world!", WHITE)

    if timer == 20:
        timer = 0
        showing = False

    pygame.display.update()
    clock.tick(100)

what this does is every time you press space it shows the text for 2s by showing it, counting 2000ms and then not showing it.

Jerrybibo
  • 1,315
  • 1
  • 21
  • 27
Omega0x013
  • 161
  • 7
  • 4
    The indentation is a little off, you might want to fix that. Also, I understand that this is not specified within the question, but `pygame.Clock.tick()` comes with the drawback of a limited framerate. I would lean more towards a solution that does not involve such limitations. – Jerrybibo Jul 16 '18 at 16:56
  • This solution worked if I unindented the last 2 `if` clauses to the same level as the `for event` loop; I upvoted your answer. But I'm also looking for a more elegant solution that doesn't rely on `pygame.Clock.tick()`. – Jerrybibo Jul 16 '18 at 17:02
  • i would agree that there are limitations to pygame.Clock.tick() @Jerrybibo, but it makes the time that it takes constant and measurable for exactly two seconds. – Omega0x013 Jul 16 '18 at 17:04