• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

14天学习训练营: 初识Pygame

武飞扬头像
YF云飞
帮助1

目录

学习知识点

PyGame 之第一个 PyGame 程序

导入模块

初始化

1.screen

2. 游戏业务

学习笔记

当 init () 的时候,它在干什么?

 init () 实际上检查了哪些东西呢?

它到底 init 了哪些子模块?

总结


学新通

14天学习训练营导师课程:
李宁《Python Pygame游戏开发入门与实战》
李宁《计算机视觉OpenCV Python项目实战》1
李宁《计算机视觉OpenCV Python项目实战》2
李宁《计算机视觉OpenCV Python项目实战》3

努力是为了在平庸生活中变得更好~
学习有时候是枯燥的,但收获的快乐是加倍的

学习知识点

PyGame 之第一个 PyGame 程序

导入模块

在使用 PyGame 之前,需要先通过 import pygame 导入 PyGame 模块。

  1.  
    #导入PyGame模块
  2.  
    import pygame
  3.  
    #导入sys模块
  4.  
    import sys

初始化

导入完成后要做的就是初始化 PyGame,它完成 PyGame 中所有模块的初始化操作。

  1.  
    #初始化PyGame
  2.  
    pygame.init()

1.screen

screen 从字面意识理解即为屏幕,在 PyGame 中,screen 本质上是一个 Surface 对象。Surface 对象是 PyGame 中重要的部分,可以把它想像成我们要绘制图形、图像的一张白纸,在这张白纸上,我们可以绘制任意的形状、图像、文字等。

screen=pygame.display.set_mode((800,600))

以上代码,是通过 PyGame 中的 display 模块设置主窗口的大小,也就是说 screen 是我们整个程序的主窗口,可以理解为我们最大的画布,我们后面创建的画布(Surface 对象)都将绘制到这个最大的画布上。

pygame.display.set_caption("第一个PyGame程序")

以上代码,是通过 PyGame 中的 display 模块设置主窗口的标题,即标题栏中的内容。


2. 游戏业务

  1.  
    #PyGame业务逻辑
  2.  
    while True:
  3.  
    # 循环获取事件,监听事件状态
  4.  
    for event in pygame.event.get():
  5.  
    # 判断用户是否点了"X"关闭按钮,并执行if代码段
  6.  
    if event.type == pygame.QUIT:
  7.  
    # 卸载所有模块
  8.  
    pygame.quit()
  9.  
    # 终止程序,确保退出程序
  10.  
    sys.exit()

通常在 while 循环内部,我们需要做三件事,分别是获取事件、更新游戏状态、重新绘制屏幕。

通过以上的代码,我们即可以创建一个 PyGame 的应用了。


学习笔记

当 init () 的时候,它在干什么?

init 这个单词在我们用 python 进行面向对象开发的时候是跑不了的。理解 python 的__init__其实就是和这里的 init 作用差不多——做的工作都是初始化。

在解释这个概念时,首先还是以专有名词入手—— 初始化(initialize vt.)。至于其作用,我的解释是这样的:

我们已经知道 python 有一个特殊的 “工具包(模块)” 叫 pygame 了。在我们要动手用它完成我们的想法之前,电脑这个“强迫症”需要我们检查一遍,这个工具包是否完整,能否正常给我们提供帮助。而这个检查的动作,就是 pygame.init()。


 init () 实际上检查了哪些东西呢?

这个其实也不难得到答案。我直接在 shell 执行了这个函数:

  1.  
    >>> import pygame
  2.  
    >>> pygame.init()
  3.  
    (6, 0)

不明所以的,他给了我一个元组 (6,0),我也很不理解,这个 6 和 0 分别代表什么意思。所以查阅了 pygame 的官方文档:

initialize all imported pygame modules

init() -> (numpass, numfail)

Initialize all imported pygame modules. No exceptions will be raised if a module fails, but the total number if successful and failed inits will be returned as a tuple. You can always initialize individual modules manually, but pygame.init() is a convenient way to get everything started. The init() functions for individual modules will raise exceptions when they fail.

You may want to initialize the different modules separately to speed up your program or to not use things your game does not.

It is safe to call this init() more than once: repeated calls will have no effect. This is true even if you have pygame.quit() all the modules.

翻译如下:

初始化所有导入的 pygame 模块。如果模块失败,则不会引发异常,但如果成功且失败的总数将作为元组返回。您可以随时手动初始化单个模块,但 pygame.init () 初始化所有导入的 pygame 模块是一种方便的方法来启动所有内容。各个模块的 init () 函数会在失败时引发异常。

您可能希望单独初始化不同的模块以加速您的程序或不使用您的游戏没有的东西。

不止一次调用此 init () 是安全的:重复调用将不起作用。即使你有 pygame.quit () 所有模块也是如此。


它到底 init 了哪些子模块?

  1.  
    >>> import pygame
  2.  
    >>> pygame.init()
  3.  
    (6, 0)
  4.  
    >>> pygame.display.init()
  5.  
    >>> pygame.font.init()
  6.  
    >>> pygame.joystick.init()
  7.  
    >>> pygame.mixer.init()
  8.  
    >>> pygame.freetype.init()
  9.  
    Traceback (most recent call last):
  10.  
    File "<stdin>", line 1, in <module>
  11.  
    AttributeError: module 'pygame' has no attribute 'freetype'
  12.  
    >>> pygame.midi.init()
  13.  
    Traceback (most recent call last):
  14.  
    File "<stdin>", line 1, in <module>
  15.  
    AttributeError: module 'pygame' has no attribute 'midi'
  16.  
    >>> pygame.cdrom.init()
  17.  
    >>> pygame.scrap.init()
  18.  
    Traceback (most recent call last):
  19.  
    File "<stdin>", line 1, in <module>
  20.  
    pygame.error: No display mode is set
学新通

我把 pygame 官网上面的 doc 里介绍的所有带有 init 的子模块都运行了一遍。其中 midi 和 freetype 这两个模块已经没有了(吐槽一下官方的文档吧,都没了还放着嘛)。最后一个 scrap 初始化是因为没有窗口。这样的话,其实已经有 5 个模块是被初始化了。但是 scrap 在没有窗口的情况下会报错,到底算不算一个 init。还需要之后再仔细看看文档和源码吧。


总结

Pygame 是被设计用来写游戏的 python 模块集合,Pygame 是在优秀的 SDL 库之上开发的功能性包。使用 python 可以导入 pygame 来开发具有全部特性的游戏和多媒体软件,Pygame 是极度轻便的并且可以运行在几乎所有的平台和操作系统上。

之后还需要逐步深入才能有更多的认识,一起加油!

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfjgbkh
系列文章
更多 icon
同类精品
更多 icon
继续加载