Multiple Dice Game using ASCII Art in Python

Haseeb Zeeshan
6 min readSep 28, 2021

--

We normally use dice for board games, but do you know that you can make a virtual version that can have as many dices as you want with python?

To start with, let’s make a flowchart about what we want to do to make a random dice

So the pseudocode for making the six-sided random dice is:

Now let’s code it in python, first get the random library we need to make the dice work. Here is what we are going to write in our code:

Import the random library

Make a variable called dice_amount. Now assign an input from the user to the variable. The input() function asks the user a question which they can answer.

To convert a string data type into an integer datatype, we need to use typecasting, the function I’ll be using for that is the int() function.

Now let’s make an array called dice_rolls, to store the dice rolls that we got. To write an array in python, you need to write in the same way as a variable, but put square brackets in front of the equals sign. Arrays are a way you can store multiple values of the same type as shown in the code below:

Now, make a variable that stores a boolean. A boolean is another data type, that can either be True, or False. This boolean will be used to make the dice keep rolling when the user wants as shown here:

The code so far:

The logic for the dice roller:

Start by making a while loop, which only loops when some condition is true, otherwise, it does not loop. In this case, I’ll be using the while loop to throw the same number of dice again, so the user doesn’t need to continuously reload the program and give the values to do so. As shown below:

A while loop

Inside the while loop, make a for loop, which will act as a counter. In this case, we’ll make it loop through the code which will add a random number to our array. As well as that, I’m using the range() function to allow the for loop to act as a counter, without the range() function, it would give an error. This is the loop I wrote:

Inside the for loop, we’ll be assigning the array dice_rolls a random number. To add a value to an array, you need to use the append() function, to add a number to the end of the array, in this case, we’re using the append function to add random values to this array, so we can display them. This is the code for it:

This is the full code we’ve written so far:

To display our output to the user, we need to write another for loop which will loop through the items in the array, however, we are going to use this for loop to loop through the items in the array and make the program give a different output for each one. This is the code for the for loop:

Looping through the array

Use an f-string to put the variable in the middle of the text (interpolation), which will allow the print function to display the text with the variable value.

Then just assign an input function to a variable called check, and make the input function ask if you want to roll again or not. In this case, there is no need for typecasting. This is what I wrote:

Write an if-else conditional, which checks if the user says Yes or No. Based on this, we make the loop equal to True or False and remove everything in the array so that the program won’t output the new random numbers as well as the old random numbers. To separate the old random numbers from the new ones use a print function with dashes in it as shown below.

The whole code that we have written so far is:

Displaying dice with ASCII art:

To make our program visually appealing, we are going to use ASCII art. ASCII art is making art with keyboard characters, to show ASCII art, you need to put your ASCII art inside a print function, print() is how you show an output or text to a user, for ASCII art, we are going to use a special print(). This special print uses 3 double quotes on each side, and you should write your ASCII art in the middle of it. Make sure you do this below the import random like so:

To display different dice based on the random number, we first need to make a conditional, which depending on the number, gives different outputs in the form of dice ASCII art as shown here:

Challenge:

Try to think about what will come after this that allows you to make a different dice ASCII art for each different number.

Hint:

It will use multiple elif conditionals to make it change for each random number

The final code:

Now it’s time to see the final code:

The final code

Final Thoughts:

As you can see, there is a satisfying feeling when you create your own dice using ASCII art. It is hard to imagine that a 3D shape like dice can be made on a console but it is certainly possible. You can be as creative as you want with very limited resources provided by a simple python console.

--

--

Haseeb Zeeshan

I’m an inquisitive person who likes to experiment with Python. My tutorials can be found at https://www.haseebzeeshan.com/