Python. PyGame library

12.5.1.1 connect the PyGame library and its modules; 
12.5.1.2 create the game window; 
12.5.1.3 code the game loop of the application. 

Python. PyGame library

PyGame lets you:

  • Draw graphic shapes
  • Display bitmaps
  • Animate objects
  • Keyboard and mouse interaction
  • Play sound
  • Object Collision Detection

Installing the PyGame library

To install the library, you need to run the pip install <Module name> command:

Win + R --> cmd -->

c:\.....>pip install pygame

Connecting and Initializing a PyGame module in the program 

import pygame # module connection

pygame.init() # Pygame module initialization

 

Surface creation (Application window)

size = width, height = 400, 300 # size is tuple of two values
screen = pygame.display.set_mode(size) # screen size setting
# screen — surface, canvas on which you can draw

!!! We have created a game window, but have not made a correct exit from the game !!!

Game loop

running = True # game loop boolean
while running: # start the game loop
     for event in pygame.event.get(): # loop catches events
         if event.type == pygame.QUIT: # handle the exit button click event
             running = False # stop the game loop
    
    #drawing and changing the properties of objects
    #...
    pygame.display.flip() # refresh the screen
pygame.quit() # close the window

 


Questions:

1. Describe how to install the PyGame library.

2. Explain how to use modules, methods, and functions of the PyGame library in the program.

3. Explain each keyword in the line "pygame.display.set_mode(size)".

4. Define using an infinity loop in the game.


Exercises:

Ex. 1. Determine the order of lines of code


Tasks:

 

Tasks on Stepik.org course "Python Programming for NIS"

Task 1. "Create game window"

Write a program to create a game window. Size of window 600*400 pixels. Ensure that the window closes correctly.

Категория: Algorithms | Добавил: bzfar77 (01.11.2022)
Просмотров: 6101 | Теги: Surface, Window, PyGame, Python | Рейтинг: 5.0/2
Всего комментариев: 0
avatar