0% found this document useful (0 votes)
6 views

Code 1.0

game code

Uploaded by

art.2160500
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
6 views

Code 1.0

game code

Uploaded by

art.2160500
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 6
H Bre Pecceaarscs Ue Ur Ce) Cece R ce yea at eee DSU Sema aSCLe Nenu") clock = pygame.time.Clock() Ce ec SCL ).convert_alpha() Ce eT ee + 80) Boose se a ar D Po UN SEt(t OODD Peccun Stites ts aa cee) Soe erect ) convert alpha) Sete ear LL eta Senora TCG) ersaec ae creme Re acCt oC cee) Exit rect = Exitimg.get rect(toplefts(100, 300)) Sa Canady Creo Ca ur et pr aD UE LLL ae} rae re ee ee a ee mT acta raeco tsuneo print ("Start button clicked!") Peer aerice a eye) Lif Exit rect .collidepoint event.pos) c Pecans ) Pre Cr em) Poeun Cee Mcrae) screen.blit(Exit img, Exit_rect) Preciarestes Ones Cee RSC SCNT) oes tts} Ese) exit() 1. Importing Modules: * pygame: This module is imported to provide access to functions and classes for creating games and multimedia applications in Python. sys: The exit function from this module is imported to allow for a clean exit from the program. 2. Initializing Pygame: pygame.init(): This function initializes all the Pygame modules and prepares them for use. It's essential to call this before using any other Pygame functions. 3. Setting Up the Window: screen = pygame.display.set_mode((800, 600) ) : This creates a display window with a resolution of 800 x 600 pixels and assigns it to the variable screen. pygame.display.set_caption("Start Menu "): This sets the title of the window to "Start Menu". 4, Initializing a Clock: clock = pygame.time.Clock(): This creates a clock object that will be used to control the frame rate of the game. 5. Loading Images and Font: bgimage = pygame. image. load('menu_img. jpg').convert_alphe This loads the background image file named "menu_img,jpg" and converts it to a format that supports alpha transparency. menu_font = pygame. font.Font("Pixeltype.ttf", 5): This creates a font object using the "Pixeltype.ttf" font file with a size of 50 pixels. surfacefont = menu_font.render('Frozen letters', True, ‘white'): This renders the text "Frozen letters" onto a surface using the specified font, anti-aliasing, and white color. 5. Loading Images and Font: bgimage = pygame. image. load('menu_img.jpg').convert_alpha This loads the background image file named “menu_img.jpg" and converts it to a format that supports alpha transparency. menu_font = pygame. font.Font("Pixeltype.ttf", 50): This creates a font object using the "Pixeltype.ttf" font file with a size of 50 pixels. surfacefont = menu_font.render('Frozen letters', True, ‘white'): This renders the text “Frozen letters" onto a surface using the specified font, anti-aliasing, and white color. 6. Displaying Background and Text: screen.blit(bgimage, (0, ®)): This draws the background image onto the screen at position (0, 0). * screen.blit(surfacefont, (100, 100)): This draws the text surface onto the screen at position (100, 100). 7. Loading Start and Exit Buttons: * Start_img = pygame. image. load("start_png.png") .convert_alph Loads the Start button image. e Exit_img = pygame. image. load("exit_png.png") .convert_alpho Loads the Exit button image. * Start_rect = Start_img.get_rect(topleft= (100, 200)): Gets the rectangular area of the Start button image and positions it at (100, 200). © Exit_rect = Exit_img.get_rect(topleft=(100, 300) ) : Does the same for the Exit button. +, In the code you provided, defining game states relies on two key aspects: 1. State Variables: * START_MENU = @ : This line assigns the value O to the variable START_MENU . This value represents the state where the player is presented with the start menu. ‘© GAME_PLAYING = 1 : Similar to the first line, this assigns the value 1 to the variable GAME_PLAYING . This value represents the state where the player is actively engaged in the game itself. These variables act as symbolic identifiers for each state. Using numbers instead of descriptive names keeps the code concise, but you could use clearer namos like STATE_START_MENU and STATE GAME PLAYING for better readability. 2. Current State Variable: * current_stote = START_MENU : This line initializes the current_stote variable to the value of START_MENU . This means the game starts in the start menu state, represented by the value O. This variable tracks the current active state of the game. As the game progresses and the player interacts with elements, the value of this variable will change to reflect the new state, like switching to GAME_PLAYING by clicking the start button. 1. while running: : This line starts the main loop that keeps the game running. The loop continues as long as the running variable is True. 2. for event in pygame.event.get():: This loop iterates through all the events that have occurred since the last iteration. Events can be various user interactions, like mouse clicks, key presses, or window actions. 3. Event Handling: - if event.type == pygame.QUIT: : This checks if the user has clicked the close button on the window. If so, it sets the running flag to False, which will break the main loop and terminate the game. - elif event.type == pygame.MOUSEBUTTONDOWN and current_state == START_MENU: : This checks if the user has clicked the mouse button while in the start menu state. - if Start_rect.collidepoint(event.pos):: This checks if the mouse click happened within the rectangle defined by the Start button image. If so, it prints a message and changes the current_state to GAME _PLAYING, indicating the transition to the game playing state. - elif Exit_rect.collidepoint(event.pos) :: This checks if the mouse click happened within the rectangle defined by the Exit button image. If so, it prints a message, sets the running flag to False, and terminates the game. 4. State-Based Rendering: - if current_state == START_MENU: : This checks if the game is currently in the start menu state. If so, it displays the background image, the text "Frozen letters", and the Start and Exit buttons on the screen. - elif current_state == GAME_PLAYING: : This checks if the game is in the game playing state. However, the code currently doesn't have any functionality for this state. It's a placeholder for where you would add the logic for your actual game. 5. Display Update and Frame Rate Control: - pygame.display.update() : This updates the display to show any changes made to the screen since the last iteration. - clock. tick(6Q) : This limits the frame rate of the game to 60 frames per second. This ensures smooth animation and prevents the game from running too fast or too slow.

You might also like