PyGame實現初始化導入所有模塊方法詳解
PyGame 是專為游戲開發(fā)而設計的 Python 庫。PyGame 建立在SDL庫之上,因此它提供了用 Python 開發(fā)游戲的全部功能。Pygame 有很多模塊來執(zhí)行它的操作,在使用這些模塊之前,必須先對它們進行初始化。所有模塊都可以單獨初始化或一次初始化一個。這篇文章描述了如何一次初始化所有導入的模塊。
使用的方法:
- 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() 方法創(chuàng)建一個顯示表面對象。
- 使用 pygame 的 font.Font() 方法創(chuàng)建一個 Font 對象。
- 使用pygame字體對象的render()方法,創(chuàng)建一個Text表面對象iesurface對象,上面繪制了Text。
- 使用pygame文本表面對象的get_rect()方法為文本表面對象創(chuàng)建一個矩形對象。
- 通過設置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()
運行即可。
到此這篇關于PyGame實現初始化導入模塊方法詳解的文章就介紹到這了,更多相關PyGame初始化導入模塊內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
用Python和WordCloud繪制詞云的實現方法(內附讓字體清晰的秘笈)
這篇文章主要介紹了用Python和WordCloud繪制詞云的實現方法(內附讓字體清晰的秘笈),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-01-01Python數據可視化實踐之使用Matplotlib繪制圖表
數據可視化是數據分析的重要環(huán)節(jié),通過將數據轉化為圖形,可以更直觀地展示數據特征和規(guī)律。Python中的Matplotlib庫是一個強大的數據可視化工具,本文將帶您了解Matplotlib的基本使用方法,以及如何繪制常見的圖表2023-05-05