作者:海擁
主頁:https://haiyong.blog.csdn.net/

Pygame是一組跨平臺的 Python 模塊,專為撰寫視頻游戲而設計,它包括旨在與 Python 編程語言一起使用的計算機圖形和聲音庫,您可以使用 pygame 創建不同型別的游戲,包括街機游戲、平臺游戲等等,
使用的影像:


你可以控制玩家的移動,為此,首先使用 pygame 的 display.set_mode() 方法創建一個顯示物件,并使用 pygame 的 image.load() 方法添加玩家的精靈,set_mode() 函式用于初始化顯示表面或視窗,size 引數是一對代表寬度和高度的數字,flags 引數是附加選項的集合,depth 引數表示用于顏色的位數,
語法:
set_mode(size=(0, 0), flags=0, depth=0, display=0, vsync=0)
創建一個變數來存盤玩家的速度,設定玩家的初始坐標,現在,根據鍵盤事件(即鍵狀態改變時發生的事件)更改播放器的 x 和 y 坐標,
blit(surface,surfacerect) 函式用于在螢屏上繪制影像,
語法:
blit(surface, surfacerect)
為了從佇列中收集所有事件,使用事件模塊的 get() 函式,然后我們使用 for 回圈迭代所有事件,
語法:
get(eventtype=None)
使用顯示模塊的 update() 函式更新螢屏,
語法:
update(rectangle=None)
下面是實作
示例:玩家移動程式
# 匯入 pygame 模塊
import pygame
from pygame.locals import *
# 啟動 pygame 并授予使用 pygame 功能的權限
pygame.init()
# 創建特定尺寸的顯示面物件
window = pygame.display.set_mode((600, 600))
# 在視窗中添加標題
pygame.display.set_caption('Player Movement')
# 添加玩家精靈
image = pygame.image.load(r'haiyong.png')
# 將玩家的初始坐標存盤在兩個變數中,即 x 和 y
x = 100
y = 100
# 創建一個變數來存盤玩家移動的速度
velocity = 12
# 創建無限回圈
run = True
while run:
# 用白色填充背景
window.fill((255, 255, 255))
# 在 x 和 y 坐標處顯示玩家精靈
window.blit(image, (x, y))
# 迭代 pygame.event.get() 方法回傳的 Event 物件串列,
for event in pygame.event.get():
# 如果事件型別為 QUIT,則關閉視窗和程式
if event.type == pygame.QUIT:
run = False
pygame.quit()
quit()
# 如果事件型別為 KEYDOWN,即按下鍵盤按鈕,則檢查事件鍵
if event.type == pygame.KEYDOWN:
# 如果按下的按鈕是左箭頭鍵,則減小 x 坐標
if event.key == pygame.K_LEFT:
x -= velocity
# 如果按下的按鈕是右箭頭鍵,則增加 x 坐標
if event.key == pygame.K_RIGHT:
x += velocity
# 如果按下按鈕是向上箭頭鍵,則減小 y 坐標
if event.key == pygame.K_UP:
y -= velocity
# 如果按下按鈕是向下箭頭鍵,則增加 y 坐標
if event.key == pygame.K_DOWN:
y += velocity
# 將表面物件繪制到螢屏上
pygame.display.update()
輸出:

玩家也可以連續移動,為此,除了要進行一些更改外,其他一切都保持不變,在這里,我們創建了一個新的時鐘物件來使用 clock() 控制游戲的幀速率,
語法
Clock()
創建一個新變數(名為 key_pressed_is)來存盤用戶按下的鍵,為此,我們使用 key 模塊的 get_pressed() 函式,
語法
get_pressed()
它回傳一個布林值序列,表示鍵盤上每個鍵的狀態,
示例:連續移動玩家
# 匯入 pygame 模塊
import pygame
from pygame.locals import *
# 啟動 pygame 并授予使用 pygame 功能的權限
pygame.init()
# 創建特定尺寸的顯示面物件
window = pygame.display.set_mode((600, 600))
# 在視窗中添加標題
pygame.display.set_caption('玩家移動')
# 初始化時鐘 時鐘用于跟蹤和控制游戲的幀速率
clock = pygame.time.Clock()
# 添加玩家精靈
image = pygame.image.load(r'haiyong.png')
# 將玩家的初始坐標存盤在兩個變數中,即 x 和 y
x = 100
y = 100
# 創建一個變數來存盤玩家移動的速度
velocity = 12
# 創建無限回圈
run = True
while run:
# 將幀速率設定為 60 fps
clock.tick(60)
# 用白色填充背景
window.fill((255, 255, 255))
# 在 x 和 y 坐標處顯示玩家精靈
window.blit(image, (x, y))
# 迭代 pygame.event.get() 方法回傳的 Event 物件串列,
for event in pygame.event.get():
# 如果事件型別為 QUIT,則關閉視窗和程式
if event.type == pygame.QUIT:
run = False
pygame.quit()
quit()
# 使用 key.get_pressed() 方法將按下的鍵存盤在新變數中
key_pressed_is = pygame.key.get_pressed()
# 改變玩家坐標
if key_pressed_is[K_LEFT]:
x -= 8
if key_pressed_is[K_RIGHT]:
x += 8
if key_pressed_is[K_UP]:
y -= 8
if key_pressed_is[K_DOWN]:
y += 8
# 將表面物件繪制到螢屏上
pygame.display.update()
輸出:

翻轉玩家精靈
您可以使用 pygame 的轉換模塊的 flip() 函式輕松翻轉任何精靈,例如,如果我們想在玩家改變移動方向時翻轉精靈,那么我們可以使用下面的代碼
window.blit(pygame.transform.flip(image, False, True), (x,y))
flip() 函式用于水平、垂直翻轉表面物件, 或兩者, 這個函式有三個引數:
- 要翻轉的影像
- 進行水平翻轉的布林值
- 進行垂直翻轉的布林值
下面是實作,
示例:翻轉播放器影像
輸出:

我們還可以通過創建精靈串列輕松更新玩家精靈,
image = [pygame.image.load(r’haiyong.png’),
pygame.image.load(r’haiyong2.png’)]
示例:更新精靈
# 匯入 pygame 模塊
import pygame
from pygame.locals import *
# 啟動 pygame 并授予使用 pygame 功能的權限
pygame.init()
# 創建特定尺寸的顯示面物件
window = pygame.display.set_mode((600, 600))
# 在視窗中添加標題
pygame.display.set_caption('玩家切換')
# 初始化時鐘 時鐘用于跟蹤和控制游戲的幀速率
clock = pygame.time.Clock()
# 創建一個變數來檢查運動方向
# 每當玩家改變方向時,我們就會改變它的值
direction = True
# 在串列中添加玩家精靈
image = [pygame.image.load(r'haiyong.png'),
pygame.image.load(r'haiyong2.png')]
# 將玩家的初始坐標存盤在兩個變數中,即 x 和 y
x = 100
y = 100
# 創建一個變數來存盤玩家移動的速度
velocity = 12
# 創建無限回圈
run = True
while run:
# 將幀速率設定為 60 fps
clock.tick(60)
# 用淺灰藍色填充背景
window.fill((176, 224, 230))
# 在 x 和 y 坐標處顯示玩家精靈
# 如果玩家改變方向,則改變玩家精靈
if direction == True:
window.blit(image[0], (x, y))
if direction == False:
window.blit(image[1], (x, y))
# 迭代 pygame.event.get() 方法回傳的 Event 物件串列,
for event in pygame.event.get():
# 如果事件型別為 QUIT,則關閉視窗和程式
if event.type == pygame.QUIT:
run = False
pygame.quit()
quit()
# 改變方向變數的值
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
direction = True
elif event.key == pygame.K_LEFT:
direction = False
# 使用 key.get_pressed() 方法將按下的鍵存盤在新變數中
key_pressed_is = pygame.key.get_pressed()
# 改變玩家坐標
if key_pressed_is[K_LEFT]:
x -= 5
if key_pressed_is[K_RIGHT]:
x += 5
if key_pressed_is[K_UP]:
y -= 5
if key_pressed_is[K_DOWN]:
y += 5
# 將表面物件繪制到螢屏上
pygame.display.update()
換張圖試試

更多和Python相關的文章
??【python入門專案】使用 Python 生成二維碼??
??【python入門專案】使用 Tkinter 的 日歷 GUI 應用程式??
??【python入門專案】將學妹的照片轉換為鉛筆素描 ??
?? 手把手教你使用 Python 制作貪吃蛇游戲 ??
?? 使用 python 的單人AI 掃雷游戲 ??
我已經寫了很長一段時間的技術博客,并且主要通過CSDN發表,這是我的一篇 Web 回應式可過濾的游戲+工具展示頁面教程,我喜歡通過文章分享技術與快樂,您可以訪問我的博客: https://haiyong.blog.csdn.net 以了解更多資訊,希望你們會喜歡!😊
💌 歡迎大家在評論區提出意見和建議!💌
如果你真的從這篇文章中學到了一些新東西,喜歡它,收藏它并與你的小伙伴分享,🤗最后,不要忘了?或📑支持一下哦,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/298378.html
標籤:python
上一篇:Python學習總結
