Python program to create a dice game using Turtle
We have seen and played many dice games link snake-ladder, poker, etc. Have you ever wonder how to build them? In this article, we will build a simple dice game in Python using Turtle. We will use the turtle module in Python to implement our dice game.
Modules Used
1. Turtle module
The turtle module is pre-installed in Python. To import the turtle module copy the following command
import turtle
2. Random module
Random module helps to generate random values. To import the random module copy the following command
import random
Dice Game in Python Using Turtle
Step 1: Import the required modules
import turtle import random
Step 2: Create a screen
Turtle.Screen() creates a window that used to draw. The bgcolor() is used to set the color of the turtle window.
s = turtle.Screen() s.bgcolor("lightgreen")
Step 3: Create players
turtle.Turtle() return an object that helps to draw. The color() function used to set the color of the object. The shape() used to return the shape of the object as per requirements. goto() function set the position of the turtle.
p_one = turtle.Turtle() p_one.color("orange") p_one.shape("turtle") p_one.penup() p_one.pensize(5) p_one.goto(-200, 100)
Similarly, create another player with the same requirements but with different color and position. clone() function used to clone the function of player one.
p_two = p_one.clone() p_two.color("blue") p_two.penup() p_two.goto(-200, -100)
Output
Step 4: Draw the finishing point
Each player will draw his own finish point using his own turtle. circle() function used to draw a circle with a given radius.
p_one.goto(300, 60) p_one.pendown() p_one.circle(40) p_one.penup() p_one.goto(-200, 100) p_two.goto(300, -140) p_two.pendown() p_two.circle(40) p_two.penup() p_two.goto(-200, -100)
Output
Step 5: Create a dice
Create an array dice[] that store the possible outcome of dice
die = [1, 2, 3, 4, 5, 6]
Step 6: Decide the winner
- Firstly check p_one has already reached the finish line. If reached print “player one win” and end the game.
- if p_two has already reached the finish line. If reached print “player two win” and end the game.
- Else, play the game alternatively
- Each player will press enter to roll the dice.
- random.choice() function used get the random values.
- With each possible outcome, each player will move 20*die_outcome
- The same process will repeats until the game ends.
for i in range(20): if p_one.pos() >= (300, 80): print("player one win") break elif p_two.pos() >= (300, -80): print("Player two win") break else: p_one_turn = input("Press enter") die_outcome_one = random.choice(die) print("The result of the die roll is: ") print(die_outcome_one) print("The number of steps will be: ") print(20*die_outcome_one) p_one.fd(20*die_outcome_one) p_two_turn = input("Press enter") die_outcome_two = random.choice(die) print("The result of the die roll is: ") print(die_outcome_two) print("The number of steps will be: ") print(20*die_outcome_two) p_two.fd(20*die_outcome_two)
Output Press enter The result of the die roll is: 3 The number of steps will be: 60 Press enter The result of the die roll is: 1 The number of steps will be: 20 Press enter The result of the die roll is: 2 The number of steps will be: 40 Press enter The result of the die roll is: 3 The number of steps will be: 60 Press enter The result of the die roll is: 4 The number of steps will be: 80 Press enter The result of the die roll is: 2 The number of steps will be: 40 Press enter The result of the die roll is: 4 The number of steps will be: 80 Press enter The result of the die roll is: 3 The number of steps will be: 60 Press enter The result of the die roll is: 5 The number of steps will be: 100 Press enter The result of the die roll is: 2 The number of steps will be: 40 Press enter The result of the die roll is: 6 The number of steps will be: 120 Press enter The result of the die roll is: 2 The number of steps will be: 40 Press enter The result of the die roll is: 6 The number of steps will be: 120 Press enter The result of the die roll is: 2 The number of steps will be: 40 player one win
Complete Source Code
import turtle import random s = turtle.Screen() s.bgcolor("lightgreen") p_one = turtle.Turtle() p_one.color("orange") p_one.shape("turtle") p_one.penup() p_one.pensize(5) p_one.goto(-200, 100) p_two = p_one.clone() p_two.color("blue") p_two.penup() p_two.goto(-200, -100) p_one.goto(300, 60) p_one.pendown() p_one.circle(40) p_one.penup() p_one.goto(-200, 100) p_two.goto(300, -140) p_two.pendown() p_two.circle(40) p_two.penup() p_two.goto(-200, -100) die = [1, 2, 3, 4, 5, 6] for i in range(20): if p_one.pos() >= (300, 80): print("player one winns") break elif p_two.pos() >= (300, -80): print("Player two winner") break else: p_one_turn = input("Press enter") die_outcome_one = random.choice(die) print("The result of the die roll is: ") print(die_outcome_one) print("The number of steps will be: ") print(20*die_outcome_one) p_one.fd(20*die_outcome_one) p_two_turn = input("Press enter") die_outcome_two = random.choice(die) print("The result of the die roll is: ") print(die_outcome_two) print("The number of steps will be: ") print(20*die_outcome_two) p_two.fd(20*die_outcome_two)
Also, read
Leave a Reply