Updating Image Based on the User’s Input
Python3
def show_car(car, state): my_font = pygame.font.SysFont( "latoblack" , 26 ) display_surface = pygame.display.set_mode((X, Y)) car1 = pygame.image.load( 'car_1.jpg' ) car2 = pygame.image.load( 'car_2.jpg' ) car3 = pygame.image.load( 'car_3.jpg' ) if car = = 1 : display_surface.blit(car1, ( 0 , 0 )) pygame.display.update() elif car = = 2 : display_surface.blit(car2, ( 0 , 0 )) pygame.display.update() elif car = = 3 : display_surface.blit(car3, ( 0 , 0 )) pygame.display.update() if state = = 1 : the_text = my_font.render( "You won by switching!" , True , ( 231 , 0 , 10 )) display_surface.blit(the_text, ( 350 , 180 )) pygame.display.update() elif state = = 2 : the_text = my_font.render( "You could've won by staying!" , True , ( 231 , 0 , 0 )) display_surface.blit(the_text, ( 350 , 180 )) pygame.display.update() elif state = = 3 : the_text = my_font.render( "You won by staying!" , True , ( 231 , 0 , 0 )) display_surface.blit(the_text, ( 350 , 180 )) pygame.display.update() elif state = = 4 : the_text = my_font.render( "You could've won by switching!" , True , ( 231 , 0 , 0 )) display_surface.blit(the_text, ( 350 , 180 )) pygame.display.update() def draw_rect(): pygame.draw.rect(display_surface, ( 20 , 24 , 11 ), ( 300 , 220 , 300 , 40 ), 1 ) pygame.display.update() pygame.draw.rect(display_surface, ( 14 , 2 , 200 ), ( 300 , 260 , 300 , 40 ), 1 ) pygame.display.update() |
The show_car function may seem a bit cumbersome, but its functionality is fairly trivial. It aims to display the image of the appropriate orientation, i.e., if the car is behind door 3, the image corresponding to that orientation (car at position 3) would be displayed. All the images are available (along with the names used in the code) at the end of this article. The state parameter tells the outcome of the simulation.
There are four possible outcomes:
Result | Decision |
Won | Stay |
Won | Switch |
Lost | Stay |
Lost | Switch |
It is necessary to use pygame.display.update() for any display changes to take place. The font and font size can be altered by changing the parameters in the my_font variable. The first argument of display_surface.blit is the image that is to be rendered, and the second argument is a tuple denoting the X and Y coordinates. my_font has an attribute render used for displayed text. The first argument is the text that is to be displayed, and the third argument is a tuple containing RGB values for specifying the color of the text. display_surface.blit is used to render the text specified by render onto the canvas. The first argument is the specifications of the text, and the second argument has the X and Y coordinates. In order to find the coordinates, use:
Python3
x, y = pygame.mouse.get_pos() |
This will create a crosshair and the coordinates of the points clicked would be known.
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.