Pygame-menu dice rolling GUI. Stuck on displaying returned integer values on screen?

120 views Asked by At

I recently wrote a command line program for random dice rolls for the various types of D&D dice, and I want to turn it into a basic GUI next.

I've got the below image worked out using Pygame-Menu library, and I also have functions which return a random dice roll for each button.

But how do I get the result of the dice rolls displayed on a surface on the screen?
I tried reading the library documentation but I didn't see anything on this topic that I understood.
I also tried to blit a text rect surface onto the screen but it didn't seem to work either.

Is anyone familiar with this particular Pygame library who can assist?

Pygame-Menu "Dungeon Dice" GUI

import random
import pygame
import pygame_menu

screen_width = 320
screen_height = 520

done = False
White = (255,255,255)
Grey = (179,179,179)
Black = (17,17,17)
Green = (30,215,96)


pygame.init()
pygame.display.set_caption('Dungeon Dice')
FPS = 60
screen = pygame.display.set_mode((screen_width, screen_height))
width = screen.get_width()
height = screen.get_height()
rollsound = pygame.mixer.Sound("roll.mp3")
mouse = pygame.mouse.get_pos()
font = pygame.font.SysFont('Calibri', 48)
screen.fill(Black)


def rolld20():
    d20output = random.randint(1,20)
    pygame.mixer.Sound.play(rollsound)
    return(d20output)

def rolld12():
    d12output = random.randint(1,12)
    pygame.mixer.Sound.play(rollsound)
    return(d12output)

def rolld10():
    d10output = random.randint(1,10)
    pygame.mixer.Sound.play(rollsound)
    return(d10output)

def rolld8():
    d8output = random.randint(1,8)
    pygame.mixer.Sound.play(rollsound)
    return(d8output)

def rolld6():
    d6output = random.randint(1,6)
    pygame.mixer.Sound.play(rollsound)
    return(d6output)

def rolld4():
    d4output = random.randint(1,4)
    pygame.mixer.Sound.play(rollsound)
    return(d4output)

def rolld100():
    d100output = random.randint(1,100)
    pygame.mixer.Sound.play(rollsound)
    return(d100output)

menu = pygame_menu.Menu('Dungeon Dice', screen_width, screen_height, theme=pygame_menu.themes.THEME_DEFAULT)

menu.add.button('D4', rolld4)
menu.add.button('D6', rolld6)
menu.add.button('D8', rolld8)
menu.add.button('D10', rolld10)
menu.add.button('D12', rolld12)
menu.add.button('D20', rolld20)
menu.add.button('D100', rolld100)
menu.add.button('Quit', pygame_menu.events.EXIT)

menu.mainloop(screen)
1

There are 1 answers

0
RufusVS On

When you use the menu.mainloop() it handles all the pygame events which limits your options. Looking at the pygame_menu docs I created a main loop that allowed processing other pygame events. I also took some liberties with your die setup and roll function. This code will wait for the sound to complete before displaying the value.

import random
import pygame
import pygame_menu
import time

screen_width = 320
screen_height = 520

done = False
White = (255,255,255)
Grey = (179,179,179)
Black = (17,17,17)
Green = (30,215,96)


pygame.init()
pygame.display.set_caption('Dungeon Dice')
FPS = 60
screen = pygame.display.set_mode((screen_width, screen_height))
width = screen.get_width()
height = screen.get_height()
rollsound = pygame.mixer.Sound("roll.mp3")
mouse = pygame.mouse.get_pos()
font = pygame.font.SysFont('Calibri', 48)
screen.fill(Black)

SONG_END = pygame.USEREVENT + 1

def roll(faces):
    global value
    result_output.set_title(f'Rolling D{faces}...')
    value = random.randint(1,faces)
    channel = pygame.mixer.Sound.play(rollsound)
    channel.set_endevent(SONG_END)
    
def roll_done():
    global value
    result_output.set_title(str(value))
    
menu = pygame_menu.Menu('Dungeon Dice', screen_width, screen_height, theme=pygame_menu.themes.THEME_DEFAULT)

for die_size in [4,6,8,10,12,20,100]:
    menu.add.button(f'D{die_size}',lambda faces=die_size: roll(faces))   
    
menu.add.button('Quit', pygame_menu.events.EXIT)

result_output = menu.add.label('Result',label_id='result')

# menu.mainloop(screen)  # removed this
# new main loop follows:
while True:
    events = pygame.event.get()    
    for event in events:
        if event.type == pygame.QUIT:
            exit()
        elif event.type == SONG_END:
            roll_done()
    if menu.is_enabled():
        menu.update(events)
        menu.draw(screen)
    pygame.display.update()