External Exam Download Resources Web Applications Games Recycle Bin

Roll two dice

dice4.py

import pygame, random
pygame.init()
screen = pygame.display.set_mode((400, 300))
done = False

dice_images = ["unknown.bmp", #index 0, dice 1-6:
               "dice1.bmp","dice2.bmp","dice3.bmp",
               "dice4.bmp","dice5.bmp","dice6.bmp"]
dice1 = pygame.image.load(dice_images[0]) #unknown.bmp
dice2 = pygame.image.load(dice_images[0])

while not done:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      done = True
    if event.type == pygame.KEYDOWN: #pressed once
      if event.key == pygame.K_SPACE: #space key
        dice1_value = random.randint(1,6)
        dice2_value = random.randint(1,6)
        dice1 = pygame.image.load(dice_images[dice1_value])
        dice2 = pygame.image.load(dice_images[dice2_value])
        
  screen.blit(dice1, (100, 100))
  screen.blit(dice2, (200, 100))
  pygame.display.flip()
  
pygame.quit()
                    

put image files + python file into same folder: