PyGame實(shí)現(xiàn)初始化導(dǎo)入所有模塊方法詳解
PyGame 是專為游戲開發(fā)而設(shè)計(jì)的 Python 庫。PyGame 建立在SDL庫之上,因此它提供了用 Python 開發(fā)游戲的全部功能。Pygame 有很多模塊來執(zhí)行它的操作,在使用這些模塊之前,必須先對它們進(jìn)行初始化。所有模塊都可以單獨(dú)初始化或一次初始化一個(gè)。這篇文章描述了如何一次初始化所有導(dǎo)入的模塊。
使用的方法:
- pygame.init() – 初始化所有模塊。它不帶任何參數(shù)并返回一個(gè)元組 (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 個(gè)基本步驟:
- 使用 pygame 的 display.set_mode() 方法創(chuàng)建一個(gè)顯示表面對象。
- 使用 pygame 的 font.Font() 方法創(chuàng)建一個(gè) Font 對象。
- 使用pygame字體對象的render()方法,創(chuàng)建一個(gè)Text表面對象iesurface對象,上面繪制了Text。
- 使用pygame文本表面對象的get_rect()方法為文本表面對象創(chuàng)建一個(gè)矩形對象。
- 通過設(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('堅(jiān)果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('堅(jiān)果', 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)行即可。

到此這篇關(guān)于PyGame實(shí)現(xiàn)初始化導(dǎo)入模塊方法詳解的文章就介紹到這了,更多相關(guān)PyGame初始化導(dǎo)入模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python?replace?空格數(shù)據(jù)處理的實(shí)現(xiàn)
本文主要介紹了python?replace?空格數(shù)據(jù)處理的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
用Python和WordCloud繪制詞云的實(shí)現(xiàn)方法(內(nèi)附讓字體清晰的秘笈)
這篇文章主要介紹了用Python和WordCloud繪制詞云的實(shí)現(xiàn)方法(內(nèi)附讓字體清晰的秘笈),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
使用Python實(shí)現(xiàn)炫酷的數(shù)據(jù)動(dòng)態(tài)圖大全
數(shù)據(jù)可視化是通過圖形、圖表、地圖等可視元素將數(shù)據(jù)呈現(xiàn)出來,以便更容易理解、分析和解釋,它是將抽象的數(shù)據(jù)轉(zhuǎn)化為直觀形象的過程,本文給大家介紹了使用Python實(shí)現(xiàn)炫酷的數(shù)據(jù)動(dòng)態(tài)圖大全,需要的朋友可以參考下2024-06-06
基于python requests庫中的代理實(shí)例講解
今天小編就為大家分享一篇基于python requests庫中的代理實(shí)例講解,具有很好的參考價(jià)值。希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
Python數(shù)據(jù)可視化實(shí)踐之使用Matplotlib繪制圖表
數(shù)據(jù)可視化是數(shù)據(jù)分析的重要環(huán)節(jié),通過將數(shù)據(jù)轉(zhuǎn)化為圖形,可以更直觀地展示數(shù)據(jù)特征和規(guī)律。Python中的Matplotlib庫是一個(gè)強(qiáng)大的數(shù)據(jù)可視化工具,本文將帶您了解Matplotlib的基本使用方法,以及如何繪制常見的圖表2023-05-05

