10 Get Start PyGame
PyGame is a Free and Open Source Python programming library for making multimedia application like games.
PyGame is highly portable and runs on nearly every platform and operating system. Millions of people have downloaded pygame itself, which is a whole lot of bits flying across the webs.
10.1 Install PIP and PyGame
10.1.1 update PIP
PIP is a package manager for Python packages, or modules if you like.
If you have Python version 3.4 or later, PIP is included by default.
You may need update your pip version,
Run below command in command line:
python.exe -m pip install --upgrade pip
or
py -m pip install --upgrade pip

Note
The package is installed to particular version of python you are using.
Make sure you which version of python you are using:
py -V
py is the short command of Python in some Windows system, if you didn’t have py command, you could create a Windows alias by run doskey py=python
If you have multiple version of Python installed in your system, python and py may point to different version of Python.
If you use Mac, you will need use python3 other than use py or python in windows.
10.1.2 install pyGame
-
Use the Command to install PyGame
py -m pip install -U pygame --user
-
Check your pygame version
pip show pygame -
Test if you pygame is installed
py -m pygame.examples.aliens
-
More pygame.examples
Note If install pygame failed, you may need install a old version of python 3.10.x
10.2 Your fist PyGame Code
10.2.1 Drawing a dot on Screen with PyGame
# ShowDot.py
# this script is show how to initial a pygame and draw a circle on tha page
import pygame
# initial the game
pygame.init()
screen = pygame.display.set_mode([800, 600])
# boolean flag to use control game run or quit
keep_going = True
# some data defined for game to use
GREEN = (0, 255, 0) # RGB color triplet for GREEN
radius = 50
# only when keep_going is true, the game will running
while keep_going:
# bellow code is for quit the game
for event in pygame.event.get():
if event.type == pygame.QUIT:
keep_going = False
# below code is for draw a circle in the screen
pygame.draw.circle(screen, GREEN, (100, 100), radius)
# refresh the screen displa
pygame.display.update()
pygame.quit()

10.2.2 Load a image on Screen with PyGame
# ShowPic.py
# this script is demo how to load a image to the pygame screen
import pygame # Setup
pygame.init()
screen = pygame.display.set_mode([800, 600])
keep_going = True
# define the picture path and name, it in same location of the script
pic = pygame.image.load("images/CrazySmile.bmp")
while keep_going: # Game loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
keep_going = False
# blit() method will load the image from hard disk. position will be (x,y)
screen.blit(pic, (100, 100))
pygame.display.update()
pygame.quit() # Exit

10.2.3 Make the image bounce onScreen with PyGame!
- if not refill background

Ref
- Video Link of this chapter: https://youtu.be/OvMw2ZgtqGI
- Source Code of this chapter: https://github.com/stoneskin/mlcccCoding/tree/master/2018-02-04/python/07-pyGame-1
- pygame tutorials on PyGame: https://www.pygame.org/wiki/tutorials
- Game of PyGames: