Dice Roller Game in Python – Roll a Virtual Die

Dice rotates after clicking it 🎲
Random numbers appear during rolling 🔄
Rolling sound plays 🔊
Final number is displayed 🎯
Restart & Sound Toggle buttons

Python Code: Dice Rotates After Click

import tkinter as tk
import random
import time
import pygame

# Initialize pygame for sound
pygame.mixer.init()

# Load dice rolling sound
rolling_sound = pygame.mixer.Sound("roll.mp3")  # Replace with a real dice sound file
sound_enabled = True  # Track if sound is enabled

# Initialize global variables
player_scores = {"Player 1": 0, "Player 2": 0}
current_player = "Player 1"
winning_score = 30

# Dice faces (to simulate rotation)
DICE_FACES = ["⚀", "⚁", "⚂", "⚃", "⚄", "⚅"]

# Function to animate dice rolling
def roll_animation(player):
    if sound_enabled:
        rolling_sound.play()  # Play rolling sound
    
    for _ in range(10):  # Rotate dice numbers quickly
        dice_value = random.randint(1, 6)
        if player == "Player 1":
            dice_label1.config(text=DICE_FACES[dice_value - 1])  # Show face
        else:
            dice_label2.config(text=DICE_FACES[dice_value - 1])
        root.update()
        time.sleep(0.1)  # Small delay for rolling effect

# Function to roll the dice
def roll_dice(player):
    global current_player
    if player != current_player:
        return  # Ignore clicks from the wrong player

    roll_animation(player)  # Rotate dice before showing final value

    final_value = random.randint(1, 6)
    player_scores[player] += final_value

    # Display final dice face
    if player == "Player 1":
        dice_label1.config(text=DICE_FACES[final_value - 1])
    else:
        dice_label2.config(text=DICE_FACES[final_value - 1])

    # Update scorecard
    score_label.config(text=f"Player 1: {player_scores['Player 1']}  |  Player 2: {player_scores['Player 2']}")

    # Check for a winner
    if player_scores[player] >= winning_score:
        result_label.config(text=f"🎉 {player} Wins!")
        disable_game()
        return

    # Switch turn
    current_player = "Player 1" if current_player == "Player 2" else "Player 2"
    turn_label.config(text=f"{current_player}'s Turn")

# Disable game after winning
def disable_game():
    dice_button1.config(state=tk.DISABLED)
    dice_button2.config(state=tk.DISABLED)
    restart_button.config(state=tk.NORMAL)  # Enable restart button

# Restart game
def restart_game():
    global player_scores, current_player
    player_scores = {"Player 1": 0, "Player 2": 0}
    current_player = "Player 1"

    # Reset UI elements
    turn_label.config(text=f"{current_player}'s Turn")
    score_label.config(text="Player 1: 0  |  Player 2: 0")
    dice_label1.config(text="🎲")
    dice_label2.config(text="🎲")
    result_label.config(text="")

    # Enable buttons
    dice_button1.config(state=tk.NORMAL)
    dice_button2.config(state=tk.NORMAL)
    restart_button.config(state=tk.DISABLED)

# Toggle sound on/off
def toggle_sound():
    global sound_enabled
    sound_enabled = not sound_enabled
    sound_button.config(text="🔊 Sound ON" if sound_enabled else "🔇 Sound OFF")

# Create GUI window
root = tk.Tk()
root.title("Two Player Dice Roller with Rotation Effect")
root.geometry("400x400")

# Labels
turn_label = tk.Label(root, text=f"{current_player}'s Turn", font=("Arial", 16))
turn_label.pack(pady=10)

score_label = tk.Label(root, text="Player 1: 0  |  Player 2: 0", font=("Arial", 14))
score_label.pack(pady=10)

# Player 1's Dice
dice_button1 = tk.Button(root, text="🎲 Roll (Player 1)", font=("Arial", 14), command=lambda: roll_dice("Player 1"))
dice_button1.pack(pady=5)

dice_label1 = tk.Label(root, text="🎲", font=("Arial", 40))
dice_label1.pack()

# Player 2's Dice
dice_button2 = tk.Button(root, text="🎲 Roll (Player 2)", font=("Arial", 14), command=lambda: roll_dice("Player 2"))
dice_button2.pack(pady=5)

dice_label2 = tk.Label(root, text="🎲", font=("Arial", 40))
dice_label2.pack()

# Winner Display
result_label = tk.Label(root, text="", font=("Arial", 16), fg="green")
result_label.pack(pady=10)

# Restart Button
restart_button = tk.Button(root, text="🔄 Restart Game", font=("Arial", 14), command=restart_game, state=tk.DISABLED)
restart_button.pack(pady=10)

# Sound Toggle Button
sound_button = tk.Button(root, text="🔊 Sound ON", font=("Arial", 14), command=toggle_sound)
sound_button.pack(pady=5)

# Run the GUI loop
root.mainloop()

How It Works:

  • Click the dice → It spins quickly before stopping 🎲

  • Random numbers appear during spinning 🔄

  • Final dice face appears after rotation stops 🎯

  • Rolling sound plays (if enabled) 🔊

  • Winner is announced when 30 points reached 🎉

  • Mute Button – Click “🔊 Sound ON” to disable dice sound.

  • Restart button activates 🔄 → Click to reset the game.

Files Needed:

🎲 Dice Rolling Sound (roll.mp3)

Get a free dice rolling sound from:

Pixabay

Videvo

Freesound