網站首頁 編程語言 正文
PyGame 是專為游戲開發而設計的 Python 庫。PyGame 建立在SDL庫之上,因此它提供了用 Python 開發游戲的全部功能。Pygame 有很多模塊來執行它的操作,在使用這些模塊之前,必須先對它們進行初始化。所有模塊都可以單獨初始化或一次初始化一個。這篇文章描述了如何一次初始化所有導入的模塊。
使用的方法:
- 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() 方法創建一個顯示表面對象。
- 使用 pygame 的 font.Font() 方法創建一個 Font 對象。
- 使用pygame字體對象的render()方法,創建一個Text表面對象iesurface對象,上面繪制了Text。
- 使用pygame文本表面對象的get_rect()方法為文本表面對象創建一個矩形對象。
- 通過設置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()
運行即可。
原文鏈接:https://blog.csdn.net/qq_39132095/article/details/127957418
相關推薦
- 2022-11-25 Go實現快速生成固定長度的隨機字符串_Golang
- 2022-07-13 Android Canvas - StaticLayout 繪制多行文字
- 2023-01-05 Qt操作SQLite數據庫的教程詳解_C 語言
- 2022-10-03 Docker容器/bin/bash?start.sh無法找到not?found問題解決_docker
- 2022-05-31 C?語言的弱符號與弱引用你了解嗎_C 語言
- 2022-05-12 小程序 onLaunch 與 onLoad 異步問題的解決方案
- 2021-12-09 Linux環境下安裝JDK1.8_Linux
- 2022-05-11 垃圾收集器ParNew&CMS與底層三色標記算法詳解
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支