External Exam Download Resources Web Applications Games Recycle Bin

Roll a dice with a keyboard space press

dice3.py

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

dice_images = ["dice1.bmp","dice2.bmp","dice3.bmp",
               "dice4.bmp","dice5.bmp","dice6.bmp"]
import random
dice = pygame.image.load("unknown.bmp")

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   
        dice = pygame.image.load(random.choice(dice_images))
        
  screen.blit(dice, (150, 100))
  
  pygame.display.flip()
pygame.quit()
                    

put image files + python file into same folder: