Nov-27-2017, 11:38 AM
For homework I've been told to write a pygame program where you can click to roll a dice. I've tried making it so you just click a button and it displays a dice side, but that isn't working. It prints the number into the shell but it does not show the dice on the pygame window - any help?
import pygame, random pygame.init() screen = pygame.display.set_mode((300, 300)) done = False clock = pygame.time.Clock() x = 30 y = 30 BLACK = 0, 0, 0 button = pygame.Rect(150, 150, 80, 80) def pickNumber(): diceroll = random.randint(1, 6) if diceroll == 1: dice = pygame.image.load("one.png") elif diceroll == 2: dice = pygame.image.load("two.png") elif diceroll == 3: dice = pygame.image.load("three.png") elif diceroll == 4: dice = pygame.image.load("four.png") elif diceroll == 5: dice = pygame.image.load("five.png") elif diceroll == 6: dice = pygame.image.load("six.png") return (dice, diceroll) while done == False: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True if event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = pygame.mouse.get_pos() if button.collidepoint(mouse_pos): dice, diceroll = pickNumber() screen.blit(dice, (150, 150)) print(diceroll) pygame.draw.rect(screen, BLACK, button) pygame.display.flip() screen.fill((255, 255, 255)) pygame.quit()