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
猜你喜欢
- python的数据类型有:数字(int)、浮点(float)、字符串(str),列表(list)、元组(tuple)、字典(dict)、集合
- 导言在前面的教程我们看到了如何使用两个页面(一个主页,用于列出供应商; 一个明细页,用于显示选定供应商提供的产品)创建主/从报表 . 这种两
- 文中用到了BeautifulSoup这个库, 目的是处理html文档分析的, 因为我只是提取了title的关键字,所以可以用正则表达式代替,
- 本文实例为大家分享了opencv转换颜色空间更改图片背景的具体代码,供大家参考,具体内容如下思路:1、将BGR转换为HSV颜色空间 2、设置
- 如下所示:# coding = utf-8import requestsimport jsonhost = "http://47.
- 一、安装cuda1、在英伟达官网下载最新版的cuda驱动https://developer.nvidia.com/zh-cn/cuda-do
- 这两个均是 python 的内建函数,通过读取控制台的输入与用户实现交互。但他们的功能不尽相同。举两个小例子。>>> ra
- 查询语言通过在查询表格中键入单词或短语,然后单击按钮执行查询,就可以在 Web 站点中搜索任意的单词或短语(例如,查询表格示例
- 一、效果图如下二、使用步骤1.创建并配置一个django项目1.1新建一个项目ch3django-admin startproject ch
- 本文实例为大家分享了python画中国国旗的具体代码,供大家参考,具体内容如下# author : momoimport turtle#中国
- #-*- coding: UTF-8 -*-'''Created on 2013-12-5@author: good
- groupby()函数在python的DataFrame中对数据进行分组统计主要使用groupby()函数。1. groupby基本用法1.
- 本文实例讲述了Python实现的银行系统模拟程序。分享给大家供大家参考,具体如下:银行系统模拟程序1、概述使用面向对象思想模拟一个简单的银行
- 背景今天朋友给我发了一个某游戏的兑换码,压缩文件发过来的。结果被加密了。wc?还说叫爸爸就给我密码?男人是这么容易像恶势力低头的?直接给你爆
- tensor内所有元素相乘a = torch.Tensor([1,2,3])print(torch.prod(a))输出 tens
- 本文研究的主要是python PIL实现图片合成的相关内容,具体介绍如下,分享实例代码。在项目中需要将两张图片合在一起。遇到两种情况,一种就
- 只要把下面代码放到index.asp或者default.asp中,只要在首页代码顶部引用call Check_Wap(),这个也是我的工程中
- 本文实例讲述了Python使用re模块正则提取字符串中括号内的内容操作。分享给大家供大家参考,具体如下:直接上代码吧:# -*- codin
- 本文实例讲述了Python功能键的读取方法。分享给大家供大家参考。具体分析如下:先getch一下得到a,如果等于0或者224,就说明是功能键
- asp函数实现把数字格式化为每3个数字时以逗号间隔的数字见下:<%Function Comma(str)If No