Pygame入门 2022 (1)

视频链接:www.youtube.com/watch?v=AY9MnQ4x3zk B站搬运地址:www.bilibili.com/video/BV1Vh411q7z1 代码及素材:github.com/clear-code-projects/UltimatePygameIntro

Pygame入门 2022 (1)

Pygame入门 2022 (1)
intro
Pygame入门 2022 (1)

Pygame入门 2022 (1)
开始学Pygame前,需要知道函数和类的基础概念。 你可以去Pygame官网www.pygame.org 查看相关文档。
Pygame入门 2022 (1)

Pygame入门 2022 (1)
游戏是如何运行的 How games work
Pygame入门 2022 (1)

Pygame入门 2022 (1)
Pygame入门 2022 (1)
Pygame入门 2022 (1)

Pygame入门 2022 (1)
电影原理:每秒播放24帧(frame)图片,你的眼睛就会将其视为动态图。电影利用这种视觉效果,通过快速播放图片实现动态的效果。
Pygame入门 2022 (1)

Pygame入门 2022 (1)
游戏的原理类似于电影,在电影中,你可以通过每秒播放许多帧的图片来实现动态效果。与电影不同的是,你需要接收玩家的输入来改变游戏屏幕。
[En]

The principle of a game is similar to that of a movie, where you can achieve dynamic effects by playing pictures of many frames per second. Unlike movies, you need to receive player input to change the game screen.

Pygame入门 2022 (1)

Pygame入门 2022 (1)
Pygame入门 2022 (1)
Pygame入门 2022 (1)

Pygame入门 2022 (1)
What Pygame does
Pygame入门 2022 (1)

Pygame入门 2022 (1)
Pygame 能为你做什么? 1.绘制图像(以及播放声音) 2.检查玩家输入(内置函数input()会阻塞进程,对游戏来说是无用的) 3.提供有用的工具,如碰撞检测(collisions),文本、计时器。
Pygame入门 2022 (1)

Pygame入门 2022 (1)
Pygame提供了简单的游戏开发工具,但是没有复杂的功能,如果你想要开发3A大作,像GTA、战地等游戏,应该去学习更复杂的引擎(虚幻,Unity等)。
Pygame入门 2022 (1)

Pygame入门 2022 (1)
开始用Pygame Creating a blank window (+ clock)
Pygame入门 2022 (1)

Pygame入门 2022 (1)
Pygame不是内置模块,需要安装: pip install pygame
Pygame入门 2022 (1)

Pygame入门 2022 (1)
然后新建文件,输入
Pygame入门 2022 (1)

Pygame入门 2022 (1)
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
import pygame
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)

Pygame入门 2022 (1)

Pygame入门 2022 (1)
运行后,出现版本信息,说明安装成功。
[En]

After running, the version information appears, then the installation is successful.

Pygame入门 2022 (1)

Pygame入门 2022 (1)
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
pygame 2.1.2 (SDL 2.0.18, Python 3.8.13)
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
Hello from the pygame community. https://www.pygame.org/contribute.html
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)

Pygame入门 2022 (1)

Pygame入门 2022 (1)

空窗口

Pygame入门 2022 (1)

Pygame入门 2022 (1)
从一个空白窗口开始。 我们需要初始化pygame,设置窗口。 然后在一个循环中,不断检查输入,更新显示。
Pygame入门 2022 (1)

Pygame入门 2022 (1)
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
导入相关包
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)

![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
初始化pygame
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
设置窗口
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)

![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
while True:
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
    事件循环
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
    做一些处理
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
    更新显示
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)

Pygame入门 2022 (1)

Pygame入门 2022 (1)
用Python实现:
Pygame入门 2022 (1)

Pygame入门 2022 (1)
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
import pygame
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
from sys import  exit
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)

![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
初始化 引擎
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
pygame.init()
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
设置屏幕  宽度800,高度400
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
screen = pygame.display.set_mode((800,400))
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
设置标题
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
pygame.display.set_caption('Runner')
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)

![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
while True:
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
    # 事件循环
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
    for event in pygame.event.get():
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
        # 如果点击退出,则关闭程序
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
        if event.type == pygame.QUIT:
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
            pygame.quit()
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
            exit()
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
    # 绘图,更新
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
    pygame.display.update()
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)

Pygame入门 2022 (1)

Pygame入门 2022 (1)
跑步会得到一个空的黑色窗口。
[En]

Running will get an empty black window.

Pygame入门 2022 (1)

Pygame入门 2022 (1)

帧数问题:

Pygame入门 2022 (1)

Pygame入门 2022 (1)
Pygame入门 2022 (1)
Pygame入门 2022 (1)

Pygame入门 2022 (1)
在代码中,它是通过时钟实现的
[En]

In the code, it is implemented through the clock

Pygame入门 2022 (1)

Pygame入门 2022 (1)
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
...
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)

![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
clock = pygame.time.Clock()
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)

![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
while True:
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
    ...
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)

![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
    pygame.display.update()
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
    clock.tick(60) # 不超过60 fps
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)

Pygame入门 2022 (1)

Pygame入门 2022 (1)
通过表面显示图像 display image by surface
Pygame入门 2022 (1)

Pygame入门 2022 (1)
Pygame入门 2022 (1)
Pygame入门 2022 (1)

Pygame入门 2022 (1)
有两种表面:显示表面(display surface)和常规表面(surface)。 显示表面是游戏窗口,所有东西都通过它显示。只有一个。 常规表面可能是一个图像,需要再显示表面上才会显示。可以有很多个。
Pygame入门 2022 (1)

Pygame入门 2022 (1)
就像我们有一块黑板(display surface),可以在黑板上贴很多图片(surface)。
Pygame入门 2022 (1)

Pygame入门 2022 (1)

坐标系

Pygame入门 2022 (1)

Pygame入门 2022 (1)
Pygame入门 2022 (1)
Pygame入门 2022 (1)

Pygame入门 2022 (1)

纯色表面

Pygame入门 2022 (1)

Pygame入门 2022 (1)
现在,让我们在窗口中添加一个surface,显示红色方块。
Pygame入门 2022 (1)

Pygame入门 2022 (1)
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
import pygame
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
from sys import  exit
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)

![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
初始化 引擎
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
pygame.init()
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
设置屏幕  宽度800,高度400
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
screen = pygame.display.set_mode((800,400))
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
设置标题
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
pygame.display.set_caption('Runner')
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
时钟
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
clock = pygame.time.Clock()
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
surface
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
test_surface = pygame.Surface((100,200))
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
test_surface.fill('Red')
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)

![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
while True:
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
    for event in pygame.event.get():
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
        if event.type == pygame.QUIT:
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
            pygame.quit()
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
            exit()
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
    # 绘图,更新
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
    screen.blit(test_surface,(0,0))  # 将test_surface放到screen上。(0,0):放置后test_surface的左上角位于screen的(0,0)处
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
    pygame.display.update()
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
    clock.tick(60) # 不超过60 fps
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)

![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)

Pygame入门 2022 (1)

Pygame入门 2022 (1)

图片表面

Pygame入门 2022 (1)

Pygame入门 2022 (1)
当然,实际我们通常需要显示真正的图像: 只需要用 pygame.image.load 加载我们需要显示的图片即可
Pygame入门 2022 (1)

Pygame入门 2022 (1)
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
test_surface = pygame.image.load('graphics/Sky.png')
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)

![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)

Pygame入门 2022 (1)

Pygame入门 2022 (1)
Pygame入门 2022 (1)
Pygame入门 2022 (1)

Pygame入门 2022 (1)

文字表面

Pygame入门 2022 (1)

Pygame入门 2022 (1)
创建文字表面的三个步骤: 1.创建字体(font) 2.在表面上写文字 3.将表面放到显示表面上
Pygame入门 2022 (1)

Pygame入门 2022 (1)
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
test_font = pygame.font.Font('font/Pixeltype.ttf',50)
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
text_surface = test_font.render("My game",False,'Black')
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)

![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
...
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)

![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
screen.blit(text_surface,(300,50))
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)

Pygame入门 2022 (1)

Pygame入门 2022 (1)
最后,实现的效果如下: [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Tr43MpdL-1660814354825)(assets/my_game.png)] 完整代码:
Pygame入门 2022 (1)

Pygame入门 2022 (1)
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
import pygame
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
from sys import  exit
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)

![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
初始化 引擎
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
pygame.init()
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
设置屏幕
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
screen = pygame.display.set_mode((800,400)) # 宽度800,高度400
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
pygame.display.set_caption('Runner') # 设置标题
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
时钟
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
clock = pygame.time.Clock()
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
surface
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
sky_surface = pygame.image.load('graphics/Sky.png')
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
ground_surface  = pygame.image.load('graphics/ground.png')
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
test_font = pygame.font.Font('font/Pixeltype.ttf',50)
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
text_surface = test_font.render("My game",False,'Black')
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)

![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
while True:
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
    # 获取用户输入
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
    for event in pygame.event.get():
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
        # 用户点击退出,关闭游戏
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
        if event.type == pygame.QUIT:
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
            pygame.quit()
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
            exit()
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
    # 绘图,更新
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
    screen.blit(sky_surface, (0, 0))  # 将test_surface放到screen上。(0,0):放置后test_surface的左上角位于screen的(0,0)处
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
    screen.blit(ground_surface,(0,300))
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
    screen.blit(text_surface,(300,50))
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
    pygame.display.update()
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)
    clock.tick(60) # 不超过60 fps
![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)

![Pygame入门 2022 (1)](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230523/loop)

Pygame入门 2022 (1)

Pygame入门 2022 (1)
小结:
Pygame入门 2022 (1)

Pygame入门 2022 (1)
用到的函数:
Pygame入门 2022 (1)

Pygame入门 2022 (1)
功能 函数 初始化pygame pygame.init() 设置窗口 screen = pygame.display.set_mode((800,400)) 设置标题 pygame.display.set_caption(‘Runner’) 更新显示 pygame.display.update() 设置时钟 clock = pygame.time.Clock() 设置频率 clock.tick(60) # 不超过60 fps 加载图片 sky_surface = pygame.image.load(‘graphics/Sky.png’) 放置表面 screen.blit(sky_surface, (0, 0)) 设置字体 test_font = pygame.font.Font(‘font/Pixeltype.ttf’,50) 用字体生成表面 text_surface = test_font.render(“My game”,False,’Black’)

Original: https://blog.51cto.com/pigeon/5602492
Author: 一只大鸽子
Title: Pygame入门 2022 (1)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/500629/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球