Play Your Own Dice Rolling Simulator Game With Python Source Code!

04 May 2023 Balmiki Mandal 0 Python

Dice Rolling Simulator Python Game – Source Code Included

Are you looking for a fun way to develop your skills in Python programming? In this short guide, we’ll cover the fundamentals of creating a dice-rolling simulator in Python. This simple game requires no additional libraries or frameworks, and the code can be adapted for more complex projects.

Overview

The goal of this project is to simulate rolling two dice and then display the results. You’ll also write some code that determines if the user has rolled a double, otherwise known as ‘snake eyes’. Finally, you will write some code that displays the current score of the game.

Creating the Program

Let’s begin by coding the main program file. Create a new file on your computer and name it DiceRoller.py. Enter the following code:

import random

def diceRoll():
  //Generate two random numbers
  die1 = random.randint(1,6)
  die2 = random.randint(1,6)
  return die1 + die2

def main():
  //Print a welcome message
  print("Welcome to the Dice Roller simulator!")
  
  // infinite loop until break is called
  while True:
    //Prompt user for input
    prompt = input("Press enter to roll or type 'exit' to quit: ")
    // If user types exit, end the program
    if prompt == 'exit':
      break
  
    //Otherwise, roll the dice
    result = diceRoll()
    print("You rolled a", result)

if __name__ == "__main__":
  main()

Let’s go through what each line of code does. We start by importing the random module from Python’s standard library. This module provides us with functions to generate random numbers which we will use to create the dice rolls. After that, we create two functions. The first, diceRoll(), generates two random numbers between 1 and 6 and returns the sum to the main function. The main function prints a welcome message and then enters a while loop. This loop prompts the user for input and then either ends the program if the user types ‘exit’ or generates a dice roll if the user presses enter.

Adding Double Roll Feature

Next, let’s add a feature to this program that detects if the user has rolled double, otherwise known as snake eyes. Amend the diceRoll() function as follows:

def diceRoll():
  //Generate two random numbers
  die1 = random.randint(1,6)
  die2 = random.randint(1,6)
  
  //Checks if both dice have the same number
  if die1 == die2:
    print("You rolled a double!")
  
  return die1 + die2

This small change adds the ability to detect double rolls. Now, when the user rolls a double, the program will print the message “You rolled a double!”.

Adding a Score Feature

Finally, let’s add a score feature to this program. This will increment the score every time the user rolls a double. Add the following lines of code after the main() function:

def update_score(score):
  score += 1
  print("Current score: %d" % score)
  return score

score = 0

This creates a new function (update_score()) that increments the score and then prints the current score. Finally, we declare a score variable and set it to 0. Now, all we have to do is call the update_score() function when the user rolls a double. Amend the if statement in the diceRoll function as follows:

if die1 == die2:
  print("You rolled a double!")
  score = update_score(score)

Now, when the user rolls a double, their score will be updated and displayed.

Conclusion

In this short guide, we covered the fundamentals of creating a dice-rolling simulator in Python. We looked at how to generate random numbers, detect doubles and keep track of the current score. With a few simple lines of code, you can create a simple game that can be used as a great learning tool for those just getting started with Python programming.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.