Python 2 - P5 - Pygame - p1
Python 2 - P5 - Pygame - p1
PYGAME
TS. NGUYỄN HOÀNG LONG
BỘ MÔN TIN HỌC TRẮC ĐỊA
0916666384
nguyenhoanglong@humg.edu.vn
1
2 PYGAME
• The Pygame library is probably the most
well known python library when it comes
to making 2D games.
• It’s not the most advanced or high level
library, but it’s simple and easy to learn
• The Pygame framework includes several
modules with functions for drawing
graphics, playing sounds, handling mouse
input, and other things that developers
need while developing games in Python.
3
CREATE A PYGAME FILE
4
RUN THE PYGAME
5
THE GAME LOOP
• Every game that exists has what we call a
“Game Loop”.
• The Game Loop is where all the game
events occur, update and get drawn to the
screen.
• Once the initial setup and initialization of
variables is out of the way, the Game Loop
begins where the program keeps looping
over and over until an event of type QUIT
occurs
6
EVENT OBJECTS
• An “Event” occurs when the user performs a
specific action, such as clicking his mouse or
pressing a keyboard button. Pygame records each
and every event that occurs.
• We can find out which events have happened by
calling the pygame.event.get() function, which
returns a list of pygame.event.Event objects
• One of the many attributes (or properties) held by
event objects is type. The type attribute tells us
what kind of event the object represents.
• If you take a look at the example shown earlier,
you’ll see we used event.type == QUIT to
determine whether the game was to be closed or
not.
7
CHANGE THE TITLE
8
CHANGE THE TITLE
9
DISPLAY UPDATE
10
DISPLAY UPDATE
11
FRAME PER SECOND (FPS)
12
FRAME PER SECOND (FPS)
13
CONTROL FPS
14
CREATE CHARACTERS
15
CREATE CHARACTERS
16
SURFACE.blit()
#example
WIN.blit(SUPERMAN_IMAGE, (200,
50))
17
RESIZE CHARACTER
18
PRACTISE 5-1
19