Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] dice roll?
#1
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()
Reply
#2
Your mouse click handling needs to be in the event loop.  Also, don't draw in your event loop.
Don't load images every time you use them, load them once.

Experiment with this:
import random

import pygame as pg
 

x = 30
y = 30
BUTTON = pg.Rect(150, 150, 80, 80)


def load_images():
    images = {}
    numbers = ["one", "two", "three", "four", "five", "six"]
    font = pg.font.Font(None, 72)
    for i,num in enumerate(numbers, start=1):
        ##images[i] = pg.image.load("{}.png".format(num)).convert()
        images[i] = font.render(str(i), 1, pg.Color("tomato"))
    return images
    
 
def main():
    pg.init()
    screen = pg.display.set_mode((300, 300))
    dice_images = load_images()
    
    done = False
    clock = pg.time.Clock()
    roll = None
    while done == False:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            elif event.type == pg.MOUSEBUTTONDOWN:
                mouse_pos = pg.mouse.get_pos()
                if BUTTON.collidepoint(mouse_pos):
                    roll = random.randint(1,6)
                    print(roll)
        screen.fill(pg.Color("white"))
        screen.fill(pg.Color("black"), BUTTON)
        if roll:
            screen.blit(dice_images[roll], (x,y))
        pg.display.flip()
    pg.quit()


if __name__ == "__main__":
    main()
If you change load_images to this it should work for your file names:
def load_images():
    images = {}
    numbers = ["one", "two", "three", "four", "five", "six"]
    for i,num in enumerate(numbers, start=1):
        images[i] = pg.image.load("{}.png".format(num)).convert()
    return images
Here is a template for a simple (organized) pygame program:  
https://gist.github.com/Mekire/f67d83087...0fb2939796
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] another dice roll! mzmingle 3 4,522 Nov-28-2017, 04:47 PM
Last Post: Windspar
  [PyGame] ROLL function Zman350x 4 4,950 Aug-03-2017, 02:55 PM
Last Post: Zman350x

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020