Making Imports and Generating Configuration
Python3
import pygame import random pygame.init() white = ( 255 , 255 , 255 ) X = 1200 Y = 650 doors = random.sample( range ( 1 , 4 ), 3 ) goat1 = doors[ 0 ] goat2 = doors[ 1 ] goats = [goat1, goat2] car = doors[ 2 ] |
Only two external modules are needed for this simulation pygame and random. At the beginning of every Pygame program, it is necessary to initialize Pygame by writing pygame.init(). The X and Y values refer to the dimensions of the window in which the simulation would run. These values can be altered based on the desired resolution and the images used. The list, doors, have values between 1 and 3 (inclusive). The order of these values would jumbled each time the program is run. This is done in order to assign the doors behind which there will be goats. According to the problem, there are a total of three doors behind two of these doors, there would be goats, and behind the third door, there would be a car. The variables goat1 and goat2 hold the door numbers behind which there would be goats. These numbers are then added to a list called goats. The third number is assigned to the variable, car.
Monty Hall Problem’s Simulation Using Pygame
In this article, we are going to see how to create Monty Hall games using Pygame in Python. Monty Hall was a game show of the American television game show Let’s Make a Deal.
Suppose you’re on a game show, and you’re given the choice of three doors, Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what’s behind the doors, opens another door, say No. 3, which has a goat. He then says to you, “Do you want to pick door No. 2?” Is it to your advantage to switch your choice?
Demo of what the end product would look like:
Please ensure that the images and the audio used are present in the same folder as the Python file. The images and the audio can be downloaded from here.
Contact Us