# Importing the Pygame library for building games and multimedia applications. import pygame # Importing the sys module to exit the program. import sys # Importing constants from pygame.locals module. from pygame.locals import * # Initializing all Pygame modules. pygame.init() # Creating a window surface with a size of 300x300 pixels. display = pygame.display.set_mode((300, 300)) # Creating a clock object to control the frame rate. FPS_CLOCK = pygame.time.Clock() # Defining a class for the player. class Player: def __init__(self): # Creating a rectangle for the player's representation on the display. self.rect = pygame.draw.rect(display, (255), (100,100,100,100)) # Creating an instance of the Player class. player = Player() # Main game loop that runs indefinitely until the program is exited. while True: # Iterating through each event that has occurred. for event in pygame.event.get(): # If the event is the user closing the window... if event.type == QUIT: # ...quit Pygame and exit the program. pygame.quit() sys.exit() # If the event is a mouse button being pressed down... if event.type == pygame.MOUSEBUTTONDOWN: # ...and if the mouse click occurs on the player's rectangle... if player.rect.collidepoint(pygame.mouse.get_pos()): # ...print a message indicating the mouse clicked on the player. print("Mouse Clicked on Player") # If the event is a mouse button being released... if event.type == pygame.MOUSEBUTTONUP: # ...and if the mouse release occurs on the player's rectangle... if player.rect.collidepoint(pygame.mouse.get_pos()): # ...print a message indicating the mouse button released on the player. print("Mouse Button Released on Player") # Updating the display to show any changes. pygame.display.update() # Controlling the frame rate to limit the speed of the game to 30 frames per second. FPS_CLOCK.tick(30)