網(wǎng)站首頁 編程語言 正文
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
相關(guān)推薦
- 2022-06-23 muduo源碼分析之TcpServer模塊詳細(xì)介紹_Redis
- 2022-05-24 .NET?Core剪裁器背后的技術(shù)及工作原理介紹_實(shí)用技巧
- 2022-05-26 GitHub配置SSH?Key的完整步驟_其它綜合
- 2022-10-28 MPAndroidChart繪制自定義運(yùn)動數(shù)據(jù)圖表示例詳解_Android
- 2023-07-10 Python使用MongoDB數(shù)據(jù)庫
- 2022-02-01 uniapp 開發(fā)h5 優(yōu)化加載速度
- 2022-05-06 基于python分享極坐標(biāo)下的幾類典型曲線_python
- 2022-08-31 pandas基礎(chǔ)?Series與Dataframe與numpy對二進(jìn)制文件輸入輸出_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支