0

So, I'm trying to make a game, and I'm working on the basics, I'm trying to figure out why is when I press the 0 key, it prints "a", but the image on the screen doesn't change. This is the same when pressing one too.

import pygame
from pygame.display import mode_ok

pygame.init()
size = width, height = 1000, 800
speed = [2, 2]
black = 1, 2, 0
screen = pygame.display.set_mode(size)
mode_ok(size, flags=1, depth=5, display=0)
DISPLAYSURF = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)

pygame.display.set_caption('Game')
# image database
pic = pygame.image.load(r"intro_ball.gif")
pic2 = pygame.image.load("poeple.png")
pic3 = pygame.image.load("frame_04_delay-0.1s.gif")
pic4 = pygame.image.load("frame_17_delay-0.1s.gif")
screen.blit(pygame.transform.scale(pic, (1000, 800)), (0, 0))
screen.blit(pic2, (500, 400))
pygame.display.flip()


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_0:
                screen.blit(pic3, (0, 0))
                print("a")
            if event.key == pygame.K_1:
                screen.blit(pygame.transform.scale(pic4, (1000, 800)), (0, 0))
                print("B")
    pygame.event.pump()


If anyone can help, that would be greatly appreciated.

1 Answers1

0

The event loop runs only once when the event occurs. When an image is drawn in the event loop, it is only visible for a brief moment. If you want to draw the pictures permanently, you need to draw them in the application loop.
Set the current picture to a current_pic variable. Change the variable when the event occurs. Continuously draw the picture indicated by the current_pic variable in the application loop:

current_pic = pygame.transform.scale(pic, (1000, 800))

clock = pygame.time.Clock()
run = True
while True:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_0:
                current_pic = pic3
                print("a")
            if event.key == pygame.K_1:
                current_pic = pygame.transform.scale(pic4, (1000, 800))
                print("B")
    
    screen.blit(current_pic, (0, 0))
    screen.blit(pic2, (500, 400))
    pygame.display.flip()

pygame.quit()

The typical PyGame application loop has to:

Rabbid76
  • 202,892
  • 27
  • 131
  • 174