571.21K
Категория: ПрограммированиеПрограммирование

PyGame module overview. Game window. II - term

1.

PyGame module
overview. Game
window.
II - term

2.

II term
topics

3.

PyGame module overview. Game window.
• 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.

4.

What’s PyGame?
Pygame is a "game library", a set of tools that help
programmers create games. These include:
• Graphics and animation
• Sound (including music)
• Controls (mouse, keyboard, gamepad and so on)

5.

Game cycle
At the heart of every game is a cycle, which is commonly called the "game cycle". It runs over and over again, doing
everything to make the game work. Each cycle in the game is called a frame.
A lot of things happen in each frame, but they can be divided into three categories:
1.Input processing (events)
We are talking about everything that happens outside the game — the events to which it should react. These can be
keystrokes on the keyboard, mouse clicks, and so on.
2. Updating the game
Changing everything that needs to change within one frame. If the character is in the air, gravity should pull him down. If
two objects meet at high speed, they should explode.
3.Rendering (drawing)
In this step, everything is displayed on the screen: backgrounds, characters, menus. Everything the player needs to see
appears on the screen in the right place.
Time
Another important aspect of the game cycle is the speed of its operation. Many people are probably familiar with the
term FPS, which stands for Frames Per Second (or frames per second). It indicates how many times the cycle should
be repeated in one second. It is important that the game is not too slow or fast. It is also important that the game does
not work at different speeds on different PCs. If a character needs 10 seconds to cross the screen, these 10 seconds
should be the same for all computers.

6.

Installing Pygame
When installing PyGame, follow the exact instructions:
• Check if Python is installed
• Check the PIP installation
• Install Pygame
• Check if PyGame is working or no
1) To check if Pyhton is installed type ‘py’ or ‘python’ on command line
(terminal), to open command line press WIN+R button and type ‘cmd’
command
2) To check PIP installation type ‘pip --version’ on PyCharm terminal
3)To install pygame on command line type ‘pip install pygame’
How to solve common errors when installing PIP:
https://okdk.ru/kak-dobavit-python-v-peremennuju-windows-path/
https://www.jetbrains.com/help/pycharm/installing-uninstalling-and-upgradingpackages.html#interpreter-settings

7.

What is PIP?
PIP is a package manager for Python packages, or modules if you like.
• Note: If you have Python version 3.4 or later, PIP is included by default.
What is a Package?
A package contains all the files you need for a module.
Modules are Python code libraries you can include in your project.
Check if PIP is Installed:
Navigate your command line to the location of Python's script directory, and type the
following:
Check PIP version:
C:\Users\Your Name\AppData\Local\Programs\Python\Python3632\Scripts>pip --version
Install PIP
If you do not have PIP installed, you can download and install it from this page:
https://pypi.org/project/pip/

8.

The main modules of the Pygame package
MODULE
PURPOSE
pygame.cdrom
Access and management of CD drives
pygame.cursors
Loads cursor images
pygame.display
Access to the display
pygame.draw
Draws shapes, lines and dots
pygame.event
Managing external events
pygame.font
Uses system fonts
pygame.image
Loads and saves the image
pygame.joystick
Uses joysticks and similar devices
pygame.key
Reads keystrokes from the keyboard
pygame.mixer
Downloads and plays ringtones
pygame.mouse
Controls the mouse
pygame.movie
Playing video files
pygame.music
Works with music and audio streaming
pygame.surface
Manages images and screen
pygame.time
pygame module for time and frame rate management
pygame.transform
Resizing and moving images

9.

https://www.youtube.com/watch?v=AY9MnQ4x3zk

10.

Connecting and Initializing a PyGame module in the
program
• import pygame # module connection
• pygame.init() # Pygame module initialization

11.

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

12.

13.

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

14.

https://learningapps.org/view30656768

15.

Try to compile code below and explain every line of code:
import pygame
import random
WIDTH = 360
HEIGHT = 480
FPS = 30
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("My Game")
clock = pygame.time.Clock()
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()

16.

Resources for self-evaluation
https://younglinux.info/pygame/pygame
https://waksoft.susu.ru/2019/04/24/pygame-shpargalka-dlja-ispolzovanija/
https://www.8host.com/blog/ustanovka-pygame-i-sozdanie-shablona-dlyarazrabotki-igr-v-python-3/
https://www.youtube.com/watch?v=evjpa3v22-U
https://waksoft.susu.ru/2019/04/24/pygame-shpargalka-dlja-ispolzovanija/
https://younglinux.info/pygame/framework
https://it.wikireading.ru/hCmfFxqBO6
https://www.youtube.com/watch?v=mEX0R1NOk7Q
English     Русский Правила