1

As i mention, i'm super new in pygame and need help with this. I'm just testing the pygame commands, and want to show up a white screen with a message whenever i initialize the game and press any key, but apparently, it's not working. Here's my code:

# pygame template
import pygame
# import random

WIDTH = 400
HEIGHT = 500
FPS = 60
TITLE = 'My Game'
FONT_NAME = 'SNAKE/DinoTopia.ttf'

# define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# initialize pygame and create window
pygame.init()
pygame.mixer.init()
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(TITLE)
CLOCK = pygame.time.Clock()

# classes


class Main:
    def __init__(self):
        self.running = False
        self.game_font = pygame.font.match_font(FONT_NAME)

    def show_screen(self):
        if self.running is True:
            SCREEN.fill(WHITE)
            self.draw_text('HELLO', 50, WIDTH/2, HEIGHT/2, BLUE)
            pygame.display.flip()
            self.wait_for_key()

    def wait_for_key(self):
        for key in pygame.event.get():
            if key.type == pygame.KEYUP:
                self.running = True
                self.show_screen()

    def draw_text(self, text, size, x, y, color):
        font = pygame.font.Font(self.game_font, size)
        text_surface = font.render(text, True, color)
        text_rect = text_surface.get_rect()
        text_rect.center = (x, y)
        SCREEN.blit(text_surface, text_rect)


g = Main()
# game loop
running = True

while running:
# keep loop running at the right speed
    CLOCK.tick(FPS)
# process input
    for event in pygame.event.get():
    # check for closing window
        if event.type == pygame.QUIT:
            running = False

# update

# draw/ render
SCREEN.fill(BLACK)
g.wait_for_key()
# *after* drawing everything, flip the display
pygame.display.flip()

pygame.quit()

i know something may be wrong with the wait_for_key function but i can't see what it is, so a little help would be nice! thanks in advance!

ronny
  • 41
  • 7

1 Answers1

1

show screen function is only called when a key is up. A single frame. You have to call it every frame. Also don't call pygame.event.get and pygame.display.flip more than once.

# pygame template
import pygame
# import random

WIDTH = 400
HEIGHT = 500
FPS = 60
TITLE = 'My Game'
FONT_NAME = 'SNAKE/DinoTopia.ttf'

# define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# initialize pygame and create window
pygame.init()
pygame.mixer.init()
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(TITLE)
CLOCK = pygame.time.Clock()

# classes


class Main:
    def __init__(self):
        self.running = False
        self.game_font = pygame.font.match_font(FONT_NAME)

    def show_screen(self):
        if self.running:
            SCREEN.fill(WHITE)
            self.draw_text('HELLO', 50, WIDTH/2, HEIGHT/2, BLUE)

    def wait_for_key(self, events):
        for event in events:
            if event.type == pygame.KEYUP:
                self.running = True

    def draw_text(self, text, size, x, y, color):
        font = pygame.font.Font(self.game_font, size)
        text_surface = font.render(text, True, color)
        text_rect = text_surface.get_rect()
        text_rect.center = (x, y)
        SCREEN.blit(text_surface, text_rect)


g = Main()
# game loop
running = True

while running:
# keep loop running at the right speed
    CLOCK.tick(FPS)
# process input
    events = pygame.event.get()
    for event in events:
    # check for closing window
        if event.type == pygame.QUIT:
            running = False
# update

    # draw/ render
    SCREEN.fill(BLACK)
    g.wait_for_key(events)
    g.show_screen()
    # *after* drawing everything, flip the display
    pygame.display.flip()

pygame.quit()