PyGame实现初始化导入所有模块方法详解

作者:坚果的博客 时间:2023-05-25 15:14:55 

PyGame 是专为游戏开发而设计的 Python 库。PyGame 建立在SDL库之上,因此它提供了用 Python 开发游戏的全部功能。Pygame 有很多模块来执行它的操作,在使用这些模块之前,必须先对它们进行初始化。所有模块都可以单独初始化或一次初始化一个。这篇文章描述了如何一次初始化所有导入的模块。

使用的方法:

  • pygame.init() – 初始化所有模块。它不带任何参数并返回一个元组 (numpass,numfail),它指示成功初始化的模块数和失败的模块数。

  • pygame.get_init() – 此方法用于检查 pygame 模块是否已初始化。

**示例 1:**此示例初始化所有 pygame 模块并打印成功初始化的模块数。

# importing the library
import pygame
# initializing all the imported
# pygame modules
(numpass,numfail) = pygame.init()
# printing the number of modules
# initialized successfully
print('Number of modules initialized successfully:',
     numpass)

PyGame实现初始化导入所有模块方法详解

**示例 2:**此示例使用 pygame.get_init() 函数来检查 pygame 模块是否已初始化。

# importing the library
import pygame
# initializing the modules
pygame.init()
# checking the initialization
is_initialized = pygame.get_init()
# printing the result
print('Is pygame modules initialized:',
is_initialized)

PyGame实现初始化导入所有模块方法详解

最后给大家附上

在 pygame 窗口上显示文本有 7 个基本步骤:

  • 使用 pygame 的 display.set_mode() 方法创建一个显示表面对象。

  • 使用 pygame 的 font.Font() 方法创建一个 Font 对象。

  • 使用pygame字体对象的render()方法,创建一个Text表面对象iesurface对象,上面绘制了Text。

  • 使用pygame文本表面对象的get_rect()方法为文本表面对象创建一个矩形对象。

  • 通过设置pygame矩形对象的center属性的值来设置矩形对象的位置。

  • 使用 pygame 显示表面对象的 blit() 方法将文本表面对象复制到显示表面对象。

  • 使用 pygame 的 display.update() 方法在 pygame 窗口上显示显示表面对象。

# import pygame module in this program
import pygame
# activate the pygame library
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
# define the RGB value for white,
# green, blue colour .
white = (255, 255, 255)
green = (0, 255, 0)
blue = (0, 0, 128)
# assigning values to X and Y variable
X = 400
Y = 400
# create the display surface object
# of specific dimension..e(X, Y).
display_surface = pygame.display.set_mode((X, Y))
# set the pygame window name
pygame.display.set_caption('坚果show')
# create a font object.
# 1st parameter is the font file
# which is present in pygame.
# 2nd parameter is size of the font
font = pygame.font.Font('freesansbold.ttf', 32)
# create a text surface object,
# on which text is drawn on it.
text = font.render('坚果', True, green, blue)
# create a rectangular object for the
# text surface object
textRect = text.get_rect()
# set the center of the rectangular object.
textRect.center = (X // 2, Y // 2)
# infinite loop
while True:
# completely fill the surface object
# with white color
display_surface.fill(white)
# copying the text surface object
# to the display surface object
# at the center coordinate.
display_surface.blit(text, textRect)
# iterate over the list of Event objects
# that was returned by pygame.event.get() method.
for event in pygame.event.get():
# if event object type is QUIT
# then quitting the pygame
# and program both.
if event.type == pygame.QUIT:
# deactivates the pygame library
pygame.quit()
# quit the program.
quit()
# Draws the surface object to the screen.
pygame.display.update()

运行即可。

PyGame实现初始化导入所有模块方法详解

来源:https://blog.csdn.net/qq_39132095/article/details/127957418

标签:PyGame,初始化,导入模块
0
投稿

猜你喜欢

  • php截取字符串函数分享

    2023-11-14 10:53:21
  • python3发送邮件需要经过代理服务器的示例代码

    2023-07-27 01:00:44
  • CSS浏览器兼容问题整理(IE6.0、IE7.0 与FireFox)

    2008-10-27 13:45:00
  • Python字典“键”和“值”的排序5种方法

    2022-01-13 04:45:42
  • python 制作简单的音乐播放器

    2022-09-12 14:30:44
  • python实现机器人卡牌

    2023-05-29 17:27:38
  • 必须会的SQL语句(二) 创建表、修改表结构、删除表

    2024-01-19 16:51:16
  • python批量导出导入MySQL用户的方法

    2024-01-29 02:58:42
  • python编程开发之类型转换convert实例分析

    2023-03-24 05:48:06
  • 返回页面顶部top按钮通过锚点实现(自写)

    2024-04-10 10:47:23
  • mysql 查询表中平均分最低的班级

    2024-01-22 05:23:45
  • python中列表添加元素的几种方式(+、append()、extend())

    2022-07-17 19:59:42
  • 在Python中进行自动化单元测试的教程

    2023-07-16 04:12:30
  • SQL处理多级分类,查询结果呈树形结构

    2012-08-21 10:50:12
  • 关于TypeScript开发的6六个实用小技巧分享

    2024-04-16 08:59:26
  • 特殊字符、常规符号及其代码对照表

    2010-08-24 18:13:00
  • Linux上安装Python的PIL和Pillow库处理图片的实例教程

    2021-01-17 15:22:50
  • JavaScript实现隐藏省略文字效果的方法

    2024-03-18 20:35:29
  • python+opencv打开摄像头,保存视频、拍照功能的实现方法

    2021-06-12 15:16:02
  • Python基于lxml模块解析html获取页面内所有叶子节点xpath路径功能示例

    2022-11-13 00:12:24
  • asp之家 网络编程 m.aspxhome.com