pythonpygamepython-3.5diceself-updating

pygame window won't update image


i'm working on a dice roll program and when the dice is rolling it shows random faces of it and then the final face. but the problem is when the dice is rolling it only shows the first face and doesn't update to the other faces. anybody know why this is happening?

here's the code:

from pygame.locals import *
from random import randint
import pygame
import sys

for x in range(11):
    if x > 1:
        n = randint(1,6)
        print(x,":",n)

pygame.init()

screen = pygame.display.set_mode((500,500),0,32)

one = pygame.image.load("one.png").convert_alpha()
two = pygame.image.load("two.png").convert_alpha()
three = pygame.image.load("three.png").convert_alpha()
four = pygame.image.load("four.png").convert_alpha()
five = pygame.image.load("five.png").convert_alpha()
six = pygame.image.load("six.png").convert_alpha()

while True:
    for evt in pygame.event.get():
        if evt.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    pygame.display.update()

    if n == 1:
        screen.blit(one,(250,250))
    if n == 2:
        screen.blit(two,(250,250))
    if n == 3:
        screen.blit(three,(250,250))
    if n == 4:
        screen.blit(four,(250,250))
    if n == 5:
        screen.blit(five,(250,250))
    if n == 6:
        screen.blit(six,(250,250))

Solution

  • The problem with your code is that this code is running before pygame is setup.

    for x in range(11):
        if x > 1:
            n = randint(1,6)
            print(x,":",n)` 
    

    You could manage this problem in the while statement, only verifying how many dices had you displayed before.

    After that, you have to repaint all your screen, first painting again your background and then the image to display.

    This code already works.

    from pygame.locals import *
    from random import randint
    import pygame
    import sys
    
    pygame.init()
    
    "Set screen"
    screen = pygame.display.set_mode((1000,1000),0,32)
    
    "Load images"
    one = pygame.image.load("one.png").convert_alpha()
    two = pygame.image.load("two.png").convert_alpha()
    three = pygame.image.load("three.png").convert_alpha()
    four = pygame.image.load("four.png").convert_alpha()
    five = pygame.image.load("five.png").convert_alpha()
    six = pygame.image.load("six.png").convert_alpha()
    
    "Fill background"
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((255, 255, 255))
    
    "Setup Counter"
    counterOfDices = 0
    
    while True:
        for evt in pygame.event.get():
            if evt.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
    
        screen.blit(background, (0, 0))
    
        "Verify How many dices had been displayed"
        if counterOfDices < 11:
            n = randint(1,6)
            counterOfDices += 1
    
        if n == 1:
            screen.blit(one,(250,250))
        if n == 2:
            screen.blit(two,(250,250))
        if n == 3:
            screen.blit(three,(250,250))
        if n == 4:
            screen.blit(four,(250,250))
        if n == 5:
            screen.blit(five,(250,250))
        if n == 6:
            screen.blit(six,(250,250))
    
        pygame.time.delay(100)
        pygame.display.update()