Python學習模組 Pygame寫遊戲

2021-10-06 16:15:41 字數 1052 閱讀 8211

第乙個例項程式:

建立 main_game.py

一張背景:001.jpg

一張跟隨滑鼠!

# 初始化pygame,為使用硬體做準備

pygame.init()

# 建立乙個視窗

screen = pygame.display.set_mode((640, 480), 0, 32)

# 設定視窗標題

pygame.display.set_caption("hello,world!")

# 載入並轉換

background = pygame.image.load(background_image_filename)

mouse_cursor = pygame.image.load(mouse_image_filename)

# 遊戲主迴圈

while true:

for event in pygame.event.get():

if event.type == quit:

# 接收到退出時間後退出程式

exit()

# 將背景圖畫上去

screen.blit(background, (0, 0))

# 獲得滑鼠位置

x, y = pygame.mouse.get_pos()

# 計算游標左上角位置

x -= mouse_cursor.get_width() / 2

y -= mouse_cursor.get_height() / 2

# 將游標畫上去

screen.blit(mouse_cursor, (x, y))

# 重新整理畫面

pygame.display.update()

python學習(模組)

模組搜尋路徑 匯入乙個叫 spam 的模組時,直譯器先在當前目錄中搜尋名為 spam.py 的檔案,然後在環境變數 pythonpath 指琮的目錄列表中搜尋,然後是環境變數 path 中的路徑列表。如果 pythonpath 沒有設定,或者檔案沒有找到,接下來搜尋安裝目錄,在 unix 中,通常是...

python 模組學習

一 from django.contrib.auth.hashers import make password 通過函式名即可發現,主要有兩個函式,分別是建立密碼和驗證 用法ps 123456 dj ps make password ps,none,pbkdf2 sha256 建立django密碼,...

python學習 模組

time import time time.ctime 獲取當前時間的字串 time.localtime 返回值是strcut time型別的物件 st time.localtime st.tm yday 現在是今年第幾天 st.tm hour 現在是今天第幾個小時 st.tm mon 現在是今年的...