日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

PyGame實(shí)現(xiàn)初始化導(dǎo)入所有模塊方法詳解_python

作者:堅果的博客 ? 更新時間: 2022-12-21 編程語言

PyGame 是專為游戲開發(fā)而設(shè)計的 Python 庫。PyGame 建立在SDL庫之上,因此它提供了用 Python 開發(fā)游戲的全部功能。Pygame 有很多模塊來執(zhí)行它的操作,在使用這些模塊之前,必須先對它們進(jìn)行初始化。所有模塊都可以單獨(dú)初始化或一次初始化一個。這篇文章描述了如何一次初始化所有導(dǎo)入的模塊。

使用的方法:

  • pygame.init() – 初始化所有模塊。它不帶任何參數(shù)并返回一個元組 (numpass,numfail),它指示成功初始化的模塊數(shù)和失敗的模塊數(shù)。
  • pygame.get_init() – 此方法用于檢查 pygame 模塊是否已初始化。

**示例 1:**此示例初始化所有 pygame 模塊并打印成功初始化的模塊數(shù)。

# 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() 函數(shù)來檢查 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() 方法創(chuàng)建一個顯示表面對象。
  • 使用 pygame 的 font.Font() 方法創(chuàng)建一個 Font 對象。
  • 使用pygame字體對象的render()方法,創(chuàng)建一個Text表面對象iesurface對象,上面繪制了Text。
  • 使用pygame文本表面對象的get_rect()方法為文本表面對象創(chuàng)建一個矩形對象。
  • 通過設(shè)置pygame矩形對象的center屬性的值來設(shè)置矩形對象的位置。
  • 使用 pygame 顯示表面對象的 blit() 方法將文本表面對象復(fù)制到顯示表面對象。
  • 使用 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()

運(yùn)行即可。

原文鏈接:https://blog.csdn.net/qq_39132095/article/details/127957418

欄目分類
最近更新