After learning the introduction to python game development of MOOC, the main content is as follows:
1. Developed with pygame
2. First, a minimum development framework is given, and gradually filled in on the basis of the minimum development framework
(1) Minimum development framework
# Unit PYG02: Pygame Hello World Game
import pygame,sys
pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Pygame游戏之旅")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.update()
Part 1: Introducing pygame and sys
Part 2: Initialization and Setup
1. Initialize the internal module of pygame
2. Initialize the display window
3. Set the window title
Part Three: Get Events and Respond Step by Step
Part 4: Refresh the screen
Part of the code explained:
import pygame,sys
sys.exit()
in:
• sys is Python's standard library
• sys provides manipulation of Python runtime environment variables
• sys.exit() is used to exit to end the game and exit
pygame.init()
• Initialize the creation and variable setting of each functional module in Pygame, call by default
pygame.display.set_mode(size)
• Initialize the display window, the first parameter size is a binary tuple, representing the width and height of the window respectively
pygame.display.set_caption(title)
• Set the title content of the display window, the parameter title is a string type
while True:
• Infinite loop until the end of the Python runtime exits
pygame.event.get()
• Take an event from Pygame's event queue and remove the event from the queue, eg: keyboard press is an event.
event.type
pygame.QUIT
• Obtain event types and respond to them one by one;
• pygame.QUIT is the exit event constant defined in Pygame
pygame.display.update()
•Update the display window, and redraw all the default windows
3. According to the functional requirements, the code is expanded to show the expanded squash game
# Unit PYG02: Pygame Wall Ball Game version 3 操控型
import pygame,sys
pygame.init()
size = width, height = 600, 400
speed = [1,1]
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load("PYG02-ball.gif")
ballrect = ball.get_rect()
fps = 300
fclock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0]) - 1)*int(speed[0]/abs(speed[0]))
elif event.key == pygame.K_RIGHT:
speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
elif event.key == pygame.K_UP:
speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1
elif event.key == pygame.K_DOWN:
speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1]) - 1)*int(speed[1]/abs(speed[1]))
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = - speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = - speed[1]
screen.fill(BLACK)
screen.blit(ball, ballrect)
pygame.display.update()
fclock.tick(fps)
Part of the code explained:
fps = 300 # Frames per Second frame rate parameter per second
fclock = pygame.time.Clock()
#pygame.time.Clock() Create a Clock object for operating time
clock.tick(framerate)
#Control the frame rate, that is, the window refresh rate, for example: clock.tick(100) means 100 frame refreshes per second The static image displayed in each video is called a frame
pygame.image.load(filename)
Load the image under the filename path into the game, support 13 commonly used image formats such as JPG, PNG, GIF (non-animation)
ball = pygame.image.load("PYG02 ‐ball.gif")
ballrect = ball.get_rect()
Surface object: Pygame uses the internally defined Surface object to represent all loaded images,
Rect object: where the .get_rect() method returns a rectangular Rect object that covers the image
Rect object Rect object has some important properties, for example: top, bottom, left, right represent the width of top, bottom, left and right, height represents the width and height
ballrect.move(x,y)
Move the rectangle by an offset (x, y), that is, move x pixels in the horizontal axis direction and y pixels in the vertical axis direction, xy is an integer
pygame.KEYDOWN
Pygame's event definition for keyboard tapping, each key of the keyboard corresponds to a specific definition
pygame.K_UP
pygame.K_LEFT
pygame.K_DOWN
pygame.K_RIGHT
screen.blit(src, dest)
draws one image on another image, that is, draws src to the dest position. The drawing of the squash is guided by the Rect object.
screen.fill(color)
shows that the window background is filled with the color color, using the RGB color system.
Since the squash ball is constantly moving, the original position will be filled with white by default after the movement , so the background color needs to be refreshed constantly
The summary is as follows:
1. Build the structure according to the minimum framework structure, import the library, establish the main screen, establish a loop to respond to events, and refresh the screen to achieve output.
2. Fill in the event response as needed to realize the game function
3. Use the surface object to generate in-game elements, and use blit to bind to the main screen to display the output
4. Use the rect object to operate on game elements
5. event.type receives input to realize mouse and keyboard control
6. You can operate the clock object time, control the refresh frequency and other operations
7. It is necessary to prepare a large number of PNG format images for the generation of game elements
Since this is the time to write this kind of blog, I don’t know how to write it, so I will write it here! Hope it's useful to those who see it!