Pygame遊戲框架

1.1、安裝pygame

pip install pygame

驗證是否安裝成功。
在IDLE裏輸入以下命令,如果輸出pygame的版本信息,則說明安裝成功

import pygame
pygame.ver

1.2、Pygame常用模塊

模塊名 功能
pygame.cdrom 訪問光驅
pygame.cursors 加載光驅
pygame.display 訪問顯示設備
pygame.draw 繪製形狀、線和點
pygame.event 管理事件
pygame.font 使用字體
pygame.image 加載和存儲圖片
pygame.joystick 使用遊戲手柄或者類似的東西
pygame.key 讀取鍵盤按鍵
pygame. mixer 聲音
pygame.mouse 鼠標
pygame.movie 播放視頻
pygame.overlay 訪問高級視頻疊加
pygame.rect 管理矩形區域
pygame.sndarray 操作聲音數據
pygame.sprite 操作移動圖像
pygame.surface 管理圖像和屏幕
pygame.surfarray 管理點陣圖像數據
pygame.time 管理時間和幀信息
pygame.transform 縮放和移動圖片

利用display和event模塊創建一個Pygame窗口,代碼如下

# -*- coding: utf-8 -*-
import sys
import pygame

pygame.init()   # 初始化Pygame
size = width, height = 320, 240  # 設置窗口
screen = pygame.display.set_mode(size)  # 顯示窗口

# 執行死循環,確保窗口一直顯示
while True:
    # 檢查事件
    for event in pygame.event.get():  # 遍歷所有事件
        if event.type == pygame.QUIT:   # 如果單擊關閉窗口,則退出
            sys.exit()
pygame.quit()   # 退出Pygame

下面對代碼進行一些解釋,
pygame.event.get()能獲取時間的隊列,然後利用一個死循環,再根據type屬性判斷事件的類型,如event.type == pygame.QUIT表示,檢測到關閉Pygame窗口的事件,pygame.KEYDOWN表示鍵盤按下事件等等

display模塊的常用方法

方法名 功能
pygame.display.init 初始化display模塊
pygame.display.quit 退出diaplay模塊
pygame.display.set_mode 初始化一個準備顯示的界面
pygame.display.set_init 如果display模塊以及初始化,則返回True
pygame.display.get_syrface 獲取當前的Surface模塊
pygame.display.flip 更新整個待顯示的Surface對象到屏幕上
pygame.display.update 更新部分內容顯示到屏幕上,如果沒有參數,則與flip方法的功能相同

1.3、製作小球跳躍遊戲

1.31、添加給窗口添加小球

我們先準一張ball.jpg圖片,然後加載該圖片,代碼如下

# -*- coding: utf-8 -*-
import sys, pygame
pygame.init()   # 初始化 Pygame
size = width, height = 640, 480  # 設置窗口
screen = pygame.display.set_mode(size)      # 顯示窗口
color = (255, 255, 255)  # 設置顏色

ball = pygame.image.load("ball.png")  # 加載圖片
ballrect = ball.get_rect()  # 獲取矩形框

# 執行死循環
while True:
    # 檢查事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:  # 如果單擊關閉窗口,則退出
            sys.exit()

    screen.fill(color)   # 填充顏色
    screen.blit(ball, ballrect)   # 將圖片畫到窗口上面去
    pygame.display.flip()         # 更新全部顯示

pygame.quit()     # 退出Pygame
Surface 對象的常用方法名 功能
pygame.Surface.blit 將一幅畫像畫到另一幅圖像像
pygame.Surface.convert 轉換圖像的像素格式
pygame.Surface.convert_alpha 轉換圖像的像素格式,包含Alpha通道的轉換
pygame.Surface.fill 使用顏色填充Surface
pygame.Surface.get_rect 獲取Surface的矩形區域

1.32、讓小球動起來

通過ball.get_rect()方法的返回值bllrectt的一個move()方法讓矩形移動。
move(x,y) x表示X軸移動的距離,y表示Y軸移動的距離。窗口左上角座標爲(0,0)

加個while循環,讓小球不斷的移動起來

# -*- coding=utf8 -*-
import sys
import pygame
pygame.init()  # 初始化Pygame
size = width, height = 1000, 650  # 設置窗口
screen = pygame.display.set_mode(size)  # 顯示窗口
color = (255, 255, 255)  # 設置顏色

ball = pygame.image.load("ball.jpg")  # 加載圖片
ballrect = ball.get_rect()  # 獲取矩形區域

speed = [5,5]  # 設置移動的X軸、Y軸的距離
# 執行死循環,確保窗口一直顯示
while True:
    # 檢查事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:    # 如果單擊關閉窗口,則退出
            sys.exit()

    ballrect = ballrect.move(speed)  # 移動小球
    screen.fill(color)  # 填充小球
    screen.blit(ball, ballrect)  # 將圖片畫到窗口上
    pygame.display.flip()     # 更新全部顯示

pygame.quit()  # 退出Pygame

1.33、增加碰撞檢測的功能

在1.32中我們發現小球在窗口中一閃而過,這時我們就需要添加碰撞檢測功能了

# -*- coding=utf8 -*-
import sys
import pygame
pygame.init()  # 初始化Pygame
size = width, height = 1000, 650  # 設置窗口
screen = pygame.display.set_mode(size)  # 顯示窗口
color = (255, 255, 255)  # 設置顏色

ball = pygame.image.load("ball.jpg")  # 加載圖片
ballrect = ball.get_rect()  # 獲取矩形區域

speed = [2, 2]  # 設置移動的X軸、Y軸的距離
# 執行死循環,確保窗口一直顯示
while True:
    # 檢查事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:    # 如果單擊關閉窗口,則退出
            sys.exit()

    ballrect = ballrect.move(speed)  # 移動小球
    # 碰到左右邊緣
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = -speed[0]

    # 碰到上下邊緣
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = -speed[1]

    screen.fill(color)  # 填充小球
    screen.blit(ball, ballrect)  # 將圖片畫到窗口上
    pygame.display.flip()     # 更新全部顯示

pygame.quit()  # 退出Pygame

1.34、設置時鐘,控制運行時間

# -*- coding=utf8 -*-
import sys
import pygame
pygame.init()  # 初始化Pygame
size = width, height = 1000, 650  # 設置窗口
screen = pygame.display.set_mode(size)  # 顯示窗口
color = (255, 255, 255)  # 設置顏色

ball = pygame.image.load("ball.jpg")  # 加載圖片
ballrect = ball.get_rect()  # 獲取矩形區域

speed = [5, 5]  # 設置移動的X軸、Y軸的距離
clock = pygame.time.Clock()   # 設置時鐘
# 執行死循環,確保窗口一直顯示
while True:
    # 檢查事件
    clock.tick(60)       # 每秒執行60次
    for event in pygame.event.get():
        if event.type == pygame.QUIT:    # 如果單擊關閉窗口,則退出
            sys.exit()

    ballrect = ballrect.move(speed)  # 移動小球
    # 碰到左右邊緣
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = -speed[0]

    # 碰到上下邊緣
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = -speed[1]

    screen.fill(color)  # 填充小球
    screen.blit(ball, ballrect)  # 將圖片畫到窗口上
    pygame.display.flip()     # 更新全部顯示

pygame.quit()  # 退出Pygame
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章