Open In App

Dice game using Turtle in Python

Last Updated : 05 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Dice Roll Game using python turtle

The simple python program implements the rolling of dice using the turtle library and random module of python. The turtle library is used to draw graphics and animations on the screen, while the random library is used to generate random numbers and random selections that can be used to control the behavior of the turtle and add a random element to the graphics and animations. 

import turtle

import random

The program creates a simulation of a dice roll that allows the user to roll the dice by pressing the spacebar and shows the result of the roll by displaying the dots on the screen in the pattern of the number rolled. The code imports the turtle and random modules, set up a turtle screen and defines a function called click(). The function generates a random integer between 1 and 6 and based on the value of that integer, it creates and positions turtle objects to draw a representation of a dice roll with red circles for the dots on the dice. The code is also printing the number on the dice output screen.

The Algorithm:

  1. Import the turtle and random libraries.
  2. Create a turtle screen with a specific width, height, background color, and title.
  3. Define the dot_positions list, which contains the positions and colors of the dots for each number that can be rolled on a die.
  4. Create a list of 7 turtle objects and assigns them to the variable dot.
  5. Define the click() function which takes no arguments.
  6. Inside the function, generate a random number between 1 and 6 using the random.randint() function and store it in the variable num.
  7. Print the number rolled on the screen along with a message using an f-string.
  8. Iterate over the dot list, and update the position and color of each dot according to the dot_positions list for the number that was rolled.
  9. Bind the click() function to the ‘space’ key press event using turtle.listen() and turtle.onkeypress() functions.
  10. Call the turtle mainloop() function to start the turtle animation and event processing.
  11. Wait for the user to press the spacebar, and when they do, the click() function is called, updating the dots on the screen with the dots for the number that was rolled.

Python3




# import modules
import turtle
import random
  
# creating the screen
d = turtle.Screen()
d.setup(width=300, height=300)
d.bgcolor('white')
d.title('Dice Play')
d.tracer(1)
  
# creating the dots
dot_positions = [[(0, 0, 'red'), (-100, 100, 'white'), (-100, 0, 'white'), (-100, -100, 'white'), (100, 100, 'white'),
                  (100, 0, 'white'), (100, -100, 'white')],
  
                 [(0, 0, 'white'), (-100, 100, 'red'), (-100, 0, 'white'), (-100, -100, 'white'), (100, 100, 'white'), (100, 0, 'white'),
                  (100, -100, 'red')],
  
                 [(0, 0, 'red'), (-100, 100, 'red'), (-100, 0, 'white'), (-100, -100, 'white'), (100, 100, 'white'), (100, 0, 'white'),
                  (100, -100, 'red')],
  
                 [(0, 0, 'white'), (-100, 100, 'red'), (-100, 0, 'white'), (-100, -100, 'red'), (100, 100, 'red'), (100, 0, 'white'), (100, -
                                                                                                                                       100, 'red')],
  
                 [(0, 0, 'red'), (-100, 100, 'red'), (-100, 0, 'white'), (-100, -100, 'red'), (100, 100, 'red'), (100, 0, 'white'), (100, -
                                                                                                                                     100, 'red')],
  
                 [(0, 0, 'white'), (-100, 100, 'red'), (-100, 0, 'red'), (-100, -100, 'red'), (100, 100, 'red'), (100, 0, 'red'), (100, -
                                                                                                                                   100, 'red')]]
  
  
dot = [turtle.Turtle() for _ in range(7)]
  
  
def click():
  # generating random number
    num = random.randint(1, 6)
  
    print(f" The number rolled on dice is {num}\n ")
    for i in range(7):
        dot[i].shape('circle')
        dot[i].color(dot_positions[num-1][i][2])
        dot[i].penup()
        dot[i].goto(dot_positions[num-1][i][0], dot_positions[num-1][i][1])
        dot[i].dot()
  
  
d.listen()
d.onkeypress(click, 'space')
d.mainloop()


Output:

It also prints the number rolled on the screen along with a message using an f-string.

Dice number on the console

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads