Main Loop and Handling Clicks
Python3
while True : music() if change = = False : display_surface.fill(white) display_surface.blit(image, ( 0 , 0 )) for event in pygame.event.get(): if event. type = = pygame.QUIT: pygame.quit() pygame.display.update() clicked = False if event. type = = pygame.MOUSEBUTTONDOWN: # Check if door 1 is pressed. if (event.pos[ 0 ] > = 71 and event.pos[ 0 ] < = 203 and event.pos[ 1 ] > = 387 and event.pos[ 1 ] < = 632 ): user = 1 clicked = True music() # Check if door 2 is pressed. elif (event.pos[ 0 ] > = 353 and event.pos[ 0 ] < = 485 and event.pos[ 1 ] > = 386 and event.pos[ 1 ] < = 635 ): user = 2 clicked = True music() # Check if door 3 is pressed. elif (event.pos[ 0 ] > = 938 and event.pos[ 0 ] < = 1100 and event.pos[ 1 ] > = 387 and event.pos[ 1 ] < = 633 ): user = 3 # print("Clicked on door 3.") clicked = True music() |
Every game contains an infinite loop (while True). It is checked if the type of event registered is QUIT, i.e. the user has closed the Pygame window. In that case, the program terminates. pygame.MOUSEBUTTONDOWN refers to the event of left-clicking a mouse. The if conditions are used to determine the door that has been pressed by the user (based on the coordinates) and the door number is assigned to the variable, user. For example, if the X coordinate (event.pos[0]) of the point where the click has occurred is between 71 and 203, and the Y coordinate (event.pos[1]) is between 387 and 633, register the click has a click on door 1. The clicked Boolean is set to True whenever one of the three doors is clicked.
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