python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > PyGame初始化导入模块

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

作者:坚果的博客

pygame安装是为了开发小游戏,在下新手在经过许多尝试后,为大家避雷,给大家分享一个简单有效的方法,下面这篇文章主要给大家介绍了关于Python中Pygame的详细安装过程的相关资料,需要的朋友可以参考下

PyGame 是专为游戏开发而设计的 Python 库。PyGame 建立在SDL库之上,因此它提供了用 Python 开发游戏的全部功能。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 个基本步骤:

# 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实现初始化导入模块方法详解的文章就介绍到这了,更多相关PyGame初始化导入模块内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文