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)
**示例 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 窗口上显示文本有 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()
运行即可。
来源:https://blog.csdn.net/qq_39132095/article/details/127957418
猜你喜欢
- 交换变量 x = 6y = 5x, y = y, xprint x>>> 5print y>>>
- 本文采用vue,同时增加鼠标点击事件和一些页面小优化基本结构新建一个sandBox.vue文件编写功能的基本结构 <div class
- 近期有个需要进行音频转码的小任务需要用到ffmpeg,安装和使用的过程中遇到了很多问题没有办法解决,从网上找了各种教程也是一言难近,本文二哥
- 1. 换源地址# 中科大deb http://mirrors.ustc.edu.cn/kali kali-rolling main non-
- 1、 下载MYSQL5.1.48源码,CMAKE,VS2008 2、 安装CMAKE和VS2008,解压MYSQL5.1.48到D:\mys
- 每次在"万达影城"网上购票总会用到左上角选择城市的功能。如下:今天就在ASP.NET MVC中实现一下。我想最好的方式应
- 本文实例讲述了Python实现计算文件MD5和SHA1的方法。分享给大家供大家参考,具体如下:不多说,直接源码:#file md5impor
- 本文实例讲述了javascript获取select值的方法。分享给大家供大家参考,具体如下:<!DOCTYPE html><
- 1. 原理孔洞指的是被前景像素点或者说感兴趣的像素点包围起来的区域,这个区域是我们不感兴趣的背景区域。数字图像处理的孔洞填充的公式为:I 为
- jQuery表单验证实例 / 包含用户名、密码、住址、邮箱验证如下图别忘了引入jQuery框架!!!话不多说直接先上jQuery部分代码:&
- element-ui el-table组件自定义合计(summary-method)坑项目需要用到表格,带有合计功能的,照搬的element
- 在实际数据分析和建模过程中,我们通常需要从数据库中读取数据,并将其转化为 Pandas dataframe 对象进行进一步处理。而 MySQ
- 1.在python文件下新建python文件,输入文件名后按Enter键生成,比如: one.py .2.简单输入python代码: pri
- Python 是面向对象的语言,所以程序抛出的异常也是类。常见的异常类1.NameError:尝试访问一个没有申明的变量2.ZeroDivi
- 直方图,又称质量分布图,是一种统计报告图,由一系列高度不等的纵条或线段表示数据分布情况。用横轴表示数据类型,纵轴表示分布情况。直方图是数值数
- 引言项目中有一个需求:需要根据学期时间动态的计算出该学期有多少周通过上网查找,找到了一个工具类moment.jsmoment.js是一个Ja
- 本文实例讲述了Python数据类型之Tuple元组。分享给大家供大家参考,具体如下:tuple元组1.概述本质上是一种有序的集合,和列表非常
- 请问如何在Oracle Setver端检测ODBC是否连接好了?首先,在SQLPLUS安装时勾选oracle open client ada
- 目录1)连接请求的变量1、max_connections2、back_log3、wait_timeout和interative_timeou
- 普通关闭我的mysql是自己下载的tar包,自己设定安装目录来安装的。停止mysql服务,说来简单,但不知道的话,还真是挠头。在这和mysq