Loading all Valid Configurations (Images) and Generating Position of Goats
Python3
if clicked: image1 = pygame.image.load( 'goat_1.jpg' ) image2 = pygame.image.load( 'goat_2.jpg' ) image3 = pygame.image.load( 'goat_3.jpg' ) image4 = pygame.image.load( 'car_1.jpg' ) image5 = pygame.image.load( 'car_2.jpg' ) image6 = pygame.image.load( 'car_3.jpg' ) wr = random.randint( 0 , 1 ) if (goats[ 0 ] = = user): g = goats[ 1 ] elif (goats[ 1 ] = = user): g = goats[ 0 ] else : g = goats[wr] if g = = 1 : change = True display_surface.blit(image1, ( 0 , 0 )) pygame.display.update() elif g = = 2 : change = True display_surface.blit(image2, ( 0 , 0 )) pygame.display.update() elif g = = 3 : change = True display_surface.blit(image3, ( 0 , 0 )) pygame.display.update() print (u "There is a goat behind door {}" . format (g)) my_font = pygame.font.SysFont( "mvboli" , 26 ) the_text = my_font.render( "Do you want to:" , True , ( 231 , 0 , 0 )) display_surface.blit(the_text, ( 350 , 180 )) the_text2 = my_font.render( "1.Switch" , True , ( 0 , 0 , 190 )) display_surface.blit(the_text2, ( 350 , 220 )) the_text3 = my_font.render( "2.Stay" , True , ( 190 , 0 , 0 )) display_surface.blit(the_text3, ( 350 , 260 )) draw_rect() clicked2 = False print (u "The car is behind door {}" . format (car)) clicked2 = False if event. type = = pygame.MOUSEBUTTONDOWN: # Compare click coordinates with # coordinates where it says 'Switch' and 'Stay'. if (event.pos[ 0 ] > = 299 and event.pos[ 0 ] < = 597 and event.pos[ 1 ] > = 220 and event.pos[ 1 ] < = 260 ): # user2 = 1 means user has chosen to switch. user2 = 1 clicked2 = True elif (event.pos[ 0 ] > = 301 and event.pos[ 0 ] < = 598 and event.pos[ 1 ] > = 259 and event.pos[ 1 ] < = 297 ): user2 = 2 # user2 = 2 means user has chosen to stay. clicked2 = True |
Once a valid click is registered, the six images are loaded. it is checked if the user has clicked on a door that has a goat behind it. As the door selected by the user cannot be opened and the door that can be opened must contain a goat behind it, there is only one possible door that can be opened. Say, goats are behind doors 1 and 2, and the user clicks on door 1, as door 1 cannot be opened, door 2 is the only door that can be opened. The variable, g, stores the valid door that can be opened. If the user did not select a door that has a goat behind it, a goat is randomly assigned using the randint function, and storing either 1 or 0 in the variable, wr.
The variable, user2, stores whether the user chose to stay or switch. The Boolean, clicked2, stores whether the click for determining the choice (stay or switch) has been registered or not.
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.