實戰專案–>飛機大戰·上

pygame 快速入門

一、使用pygame創建圖形視窗


例:
import pygame
pygame.init()
# 撰寫游戲代碼
print("游戲代碼...")
pygame.quit()


例:
import pygame
hero_rect = pygame.Rect(100, 500, 120, 125)
print("英雄的原點: %d %d" % (hero_rect.x, hero_rect.y))
print("英雄的尺寸: %d %d" % (hero_rect.width, hero_rect.height))
print("%d %d " % hero_rect.size)

例:
import pygame
pygame.init()
# 創建游戲視窗
screen = pygame.display.set_mode((480, 700))
pygame.quit()

二、理解 影像 并實作影像繪制

例:
import pygame
pygame.init()
# 創建游戲視窗
screen = pygame.display.set_mode((480, 700))
# 繪制背景影像
# 1>加載影像資料
bg = pygame.image.load("./images/background.png")
# 2>bilt 繪制影像
screen.blit(bg, (0, 0))
# 3>update 更新螢屏顯示
pygame.display.update()
while True:
pygame.event.get(1000) # 事件監聽
pass
pygame.quit()

例:
import pygame
pygame.init()
# 創建游戲視窗
screen = pygame.display.set_mode((480, 700))
#繪制背景影像
# 1>加載影像資料
bg = pygame.image.load("./images/background.png")
# 2>bilt 繪制影像
screen.blit(bg, (0, 0))
# 3>update 更新螢屏顯示
pygame.display.update()
# 繪制英雄的飛機,注意順序不要被覆寫
hero = pygame.image.load("./images/me1.png")
screen.blit(hero, (180, 500))
pygame.display.update()
while True:
pygame.event.get(1000)
pass
pygame.quit()

例:
import pygame
pygame.init()
# 創建游戲視窗
screen = pygame.display.set_mode((480, 700))
# 繪制背景影像
# 1>加載影像資料
bg = pygame.image.load("./images/background.png")
screen.blit(bg, (0, 0))
# 繪制英雄的飛機,注意順序不要被覆寫
hero = pygame.image.load("./images/me1.png")
screen.blit(hero, (180, 500))
# 可以在所有作業完成后,統一呼叫update方法
pygame.display.update()
while True:
pygame.event.get(1000)
pass
pygame.quit()
三、理解 游戲回圈 和 游戲時鐘

每一次呼叫 update 產生的效果,叫做幀,


例:
import pygame
pygame.init()
# 創建游戲視窗
screen = pygame.display.set_mode((480, 700))
# 繪制背景影像
bg = pygame.image.load("./images/background.png")
screen.blit(bg, (0, 0))
hero = pygame.image.load("./images/me1.png")
screen.blit(hero, (180, 500))
pygame.display.update()
# 創建時鐘物件
clock = pygame.time.Clock()
# 游戲回圈 -> 意味著游戲正式開始
while True:
# pygame.event.get(1000)
clock.tick(60) # 每秒 while 回圈內部代碼執行頻率
pygame.quit()

部分:
clock = pygame.time.Clock()
# 1.定義rect記錄飛機的初始位置
hero_rect = pygame.Rect(180, 500, 102, 126)
while True:
# pygame.event.get(1000)
clock.tick(60)
# 2.修改飛機的位置
hero_rect.y -= 1
# 3.呼叫 bilt 方法繪制影像
screen.blit(bg, (0, 0))
screen.blit(hero, hero_rect)
# 4.呼叫 update 方法更新顯示
pygame.display.update()

部分:
while True:
clock.tick(60)
# 捕獲事件
event_list = pygame.event.get()
if len(event_list) > 0:
print(event_list)
hero_rect.y -= 3
if hero_rect.y <= -126:
hero_rect.y = 700
screen.blit(bg, (0, 0))
screen.blit(hero, hero_rect)
pygame.display.update()

部分:
clock.tick(60)
# 監聽事件
for event in pygame.event.get():
# 判斷事件型別是否是退出事件
if event.type == pygame.QUIT:
print("游戲退出...")
# quit 卸載所有模塊
pygame.quit()
exit()
hero_rect.y -= 3
if hero_rect.y <= -126:
hero_rect.y = 700
screen.blit(bg, (0, 0))
screen.blit(hero, hero_rect)
pygame.display.update()
四、理解 精靈 和 精靈組

注意:仍然需要呼叫 pygame.display.update() 才能在螢屏上看到最終結果,

部分:
plane_sprites(在下篇會用于專案的python檔案)模塊代碼:
import pygame
class GameSprite(pygame.sprite.Sprite):
"""飛機大戰游戲精靈"""
def __init__(self, image_name, speed=1):
# 呼叫父類的初始化方法
super().__init__()
# 定義物件屬性
self.image = pygame.image.load(image_name) # 把指定名稱的影像,加載到影像屬性中
self.rect = self.image.get_rect()
self.speed = speed
def update(self):
# 在螢屏的垂直方向上移動
self.rect.y += self.speed
# self.rect.x += self.speed # 也可以加橫向

部分:
演練精靈代碼:
import pygame
from plane_sprites import *
pygame.init()
# 創建游戲視窗
screen = pygame.display.set_mode((480, 700))
bg = pygame.image.load("./images/background.png")
screen.blit(bg, (0, 0))
hero = pygame.image.load("./images/me1.png")
screen.blit(hero, (180, 500))
pygame.display.update()
# 創建時鐘物件
clock = pygame.time.Clock()
# 1.定義rect記錄飛機的初始位置
hero_rect = pygame.Rect(180, 500, 102, 126)
# 創建敵機的精靈
enemy = GameSprite("./images/enemy1.png")
enemy1 = GameSprite("./images/enemy1.png", 2)
# 創建敵機的精靈組
enemy_group = pygame.sprite.Group(enemy, enemy1)
while True:
clock.tick(60)
# 監聽事件
for event in pygame.event.get():
# 判斷事件型別是否是退出事件
if event.type == pygame.QUIT:
print("游戲退出...")
# quit 卸載所有模塊
pygame.quit()
# exit()
exit()
hero_rect.y -= 3
if hero_rect.y <= -126:
hero_rect.y = 700
screen.blit(bg, (0, 0))
screen.blit(hero, hero_rect)
# 讓精靈組呼叫uptate方法 - 讓組中所有精靈更新位置
enemy_group.update()
# 讓精靈組呼叫draw方法 - 在screen上繪制所有精靈
enemy_group.draw(screen)
pygame.display.update()
pygame.quit()
附帶B站找到的"飛機大戰素材"地址:
https://pan.baidu.com/s/1pMM0beb
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/265397.html
標籤:python
