Pygame - Python游戲編程入門(3)


前言

  在上一節我們完成了對玩家飛機的基本操作,這一節我們就來創造出敵人了(°∀°)ノ~目標有三個,第一個是在屏幕上繪制出敵機,第二個是判斷子彈是否擊中了敵人,第三個是對被擊中的敵人作后續的處理。明白方向后就可以開始了!

 

正片開始~

  1. 繪制敵機

  隨機是游戲中一個很重要的元素,不可預測的機制為游戲帶來了更豐富的體驗。這次我們要在程序中加入隨機數,兩行代碼:  

# 導入random庫中的randint函數
from random import randint
# 返回一個整數N, a<=N<=b
N = randint(a, b)

  這樣我們就可以使得敵機每次出現的位置變得不可預測了~(。・ω・。)

  跟之前的風格類似,我們把敵機封裝成類,主要是為了能夠更方便地使用碰撞檢測的功能。

 1 # 敵人類
 2 class Enemy(pygame.sprite.Sprite):
 3     def __init__(self, enemy_surface, enemy_init_pos):
 4         pygame.sprite.Sprite.__init__(self)            
 5         self.image = enemy_surface
 6         self.rect = self.image.get_rect()
 7         self.rect.topleft = enemy_init_pos
 8         self.speed = 2
 9 
10     def update(self):
11         self.rect.top += self.speed
12         if self.rect.top > SCREEN_HEIGHT:
13             self.kill()

  依然是超出屏幕區域自動銷毀對象,最后就是創建敵人對象並在屏幕上繪制出來:

 1 ...
 2 # enemy1圖片 **********************************************************
 3 enemy1_surface = shoot_img.subsurface(pygame.Rect(534, 612, 57, 43))
 4 # ********************************************************************
 5 ...
 6 
 7 # 事件循環(main loop)
 8 while True:
 9 
10     ...
11 
12     # 產生敵機 *****************************************************
13     if ticks % 30 == 0:
14         enemy = Enemy(enemy1_surface, [randint(0, SCREEN_WIDTH - enemy1_surface.get_width()), -enemy1_surface.get_height()])
15         enemy_group.add(enemy)
16     # 控制敵機
17     enemy_group.update()
18     # 繪制敵機
19     enemy_group.draw(screen)
20     # ************************************************************
21         
22     ...

  導入圖片資源當然是必不可少的啦;我們使用ticks控制敵人產生的頻率,每30ticks產生一架新敵機,然后將敵機對象加入一個group,統一操作,每一tick更新一次全體enemy的位置。現在繪制的任務就完成啦~看一下效果:

  雖然enemy繪制出來了,但是現在出現了兩個問題;第一,子彈無法擊中敵人;第二,敵人無法擊毀玩家飛機。下面我們先來解決第一個問題。

 

  2. 我們的子彈擊穿了敵人的鎧甲!

  說了那么久,終於說到了“碰撞檢測”,游戲中的碰撞檢測應用范圍很廣,不過在pygame中,碰撞檢測(collide)的機制其實很簡單,就是判斷sprite1.rect與sprite2.rect是否重疊。那么在pygame.sprite中,我們可以看到一些函數名中包含collide的函數,這些函數一般是用於檢測碰撞的,我們可以大體分為sprite與sprite的碰撞檢測,sprite與group的碰撞檢測,group與group的碰撞檢測。回歸問題,子彈是一個group,敵人是一個group,那我們在游戲中檢測子彈是否擊中了敵人很明顯需要用group與group的碰撞檢測了~

  pygame.sprite.groupcollide()——檢測兩個group之間所有sprite的碰撞

  groupcollide(group1, group2, dokill1, dokill2, collided = None) -> Sprite_dict

  group1——精靈組1

  group2——精靈組2

  dokill1——是否殺死發生碰撞時group1中的精靈對象

  dokill2——是否殺死發生碰撞時group2中的精靈對象

  collided——可選參數,可自定義一個回調函數,參數為兩個精靈對象,用於自定義兩個精靈是否發生碰撞,返回bool值;若忽略此參數,則默認碰撞條件為兩個精靈的rect發生重疊

  返回一個包含所有group1中與group2發生碰撞的精靈字典(dict)

  

  現在,我們只需要在程序中加入兩行代碼:

...

# 創建擊毀敵人組
enemy1_down_group = pygame.sprite.Group()

# 事件循環(main loop)
while True:

    ...

    # 檢測敵機與子彈的碰撞 *******************************************
    enemy1_down_group.add(pygame.sprite.groupcollide(enemy1_group, hero.bullets1, True, True))
    
  ...

 

  創建一個包含被擊毀的敵人的group,然后在每一tick中檢測一次是否發生碰撞,再將被擊毀的敵人加入這個group,方便后續對墜毀敵機的動畫渲染;這樣第二部分也完成啦~

 

  3. 華麗的墜毀(`・ω・´)

  現在我們已經實現子彈與敵機的碰撞檢測了,但憑空消失是在不咋的,我們來一個華麗一點的爆炸!(°∀°)ノ

  首先導入enemy1爆炸的資源圖片

1 enemy1_down_surface = []
2 enemy1_down_surface.append(shoot_img.subsurface(pygame.Rect(267, 347, 57, 43)))
3 enemy1_down_surface.append(shoot_img.subsurface(pygame.Rect(873, 697, 57, 43)))
4 enemy1_down_surface.append(shoot_img.subsurface(pygame.Rect(267, 296, 57, 43)))
5 enemy1_down_surface.append(shoot_img.subsurface(pygame.Rect(930, 697, 57, 43)))

 

  然后就是控制爆炸圖片切換的速度了,在主循環中加入:

1 for enemy1_down in enemy1_down_group:
2         screen.blit(enemy1_down_surface[enemy1_down.down_index], enemy1_down.rect)
3         if ticks % (ANIMATE_CYCLE//2) == 0:
4             if enemy1_down.down_index < 3:
5                 enemy1_down.down_index += 1
6             else:
7                 enemy1_down_group.remove(enemy1_down)

 

  當index超出圖片下標,判斷為爆炸效果演示完畢,銷毀墜毀的enemy1精靈。

  這樣爆炸效果就出來啦~~

  這一節任務完成!~附上完整代碼:

  1 # -*- coding = utf-8 -*-
  2 """
  3 @author: Will Wu
  4 """
  5 
  6 import pygame                   # 導入pygame庫
  7 from pygame.locals import *     # 導入pygame庫中的一些常量
  8 from sys import exit            # 導入sys庫中的exit函數
  9 from random import randint
 10 
 11 # 定義窗口的分辨率
 12 SCREEN_WIDTH = 480
 13 SCREEN_HEIGHT = 640
 14 
 15 # 子彈類
 16 class Bullet(pygame.sprite.Sprite):
 17 
 18     def __init__(self, bullet_surface, bullet_init_pos):
 19         pygame.sprite.Sprite.__init__(self)            
 20         self.image = bullet_surface
 21         self.rect = self.image.get_rect()
 22         self.rect.topleft = bullet_init_pos
 23         self.speed = 8
 24 
 25     # 控制子彈移動
 26     def update(self):
 27         self.rect.top -= self.speed
 28         if self.rect.bottom < 0:
 29             self.kill()
 30             
 31 
 32 # 玩家類
 33 class Hero(pygame.sprite.Sprite):
 34     
 35     def __init__(self, hero_surface, hero_init_pos):
 36         pygame.sprite.Sprite.__init__(self)            
 37         self.image = hero_surface
 38         self.rect = self.image.get_rect()
 39         self.rect.topleft = hero_init_pos
 40         self.speed = 6
 41 
 42         # 子彈1的Group
 43         self.bullets1 = pygame.sprite.Group()
 44 
 45     # 控制射擊行為
 46     def single_shoot(self, bullet1_surface):
 47         bullet1 = Bullet(bullet1_surface, self.rect.midtop)
 48         self.bullets1.add(bullet1)
 49 
 50     # 控制飛機移動
 51     def move(self, offset):
 52         x = self.rect.left + offset[pygame.K_RIGHT] - offset[pygame.K_LEFT]
 53         y = self.rect.top + offset[pygame.K_DOWN] - offset[pygame.K_UP]
 54         if x < 0:
 55             self.rect.left = 0
 56         elif x > SCREEN_WIDTH - self.rect.width:
 57             self.rect.left = SCREEN_WIDTH - self.rect.width
 58         else:
 59             self.rect.left = x
 60             
 61         if y < 0:
 62             self.rect.top = 0
 63         elif y > SCREEN_HEIGHT - self.rect.height:
 64             self.rect.top = SCREEN_HEIGHT - self.rect.height
 65         else:
 66             self.rect.top = y
 67 
 68 # 敵人類
 69 class Enemy(pygame.sprite.Sprite):
 70     def __init__(self, enemy_surface, enemy_init_pos):
 71         pygame.sprite.Sprite.__init__(self)            
 72         self.image = enemy_surface
 73         self.rect = self.image.get_rect()
 74         self.rect.topleft = enemy_init_pos
 75         self.speed = 2
 76 
 77         # 爆炸動畫畫面索引
 78         self.down_index = 0
 79 
 80     def update(self):
 81         self.rect.top += self.speed
 82         if self.rect.top > SCREEN_HEIGHT:
 83             self.kill()
 84         
 85 ###########################################################################
 86 
 87 # 定義畫面幀率
 88 FRAME_RATE = 60
 89 
 90 # 定義動畫周期(幀數)
 91 ANIMATE_CYCLE = 30
 92 
 93 ticks = 0
 94 clock = pygame.time.Clock()
 95 offset = {pygame.K_LEFT:0, pygame.K_RIGHT:0, pygame.K_UP:0, pygame.K_DOWN:0}
 96 
 97           
 98 # 初始化游戲
 99 pygame.init()                   # 初始化pygame
100 screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])     # 初始化窗口
101 pygame.display.set_caption('This is my first pygame-program')       # 設置窗口標題
102 
103 # 載入背景圖
104 background = pygame.image.load('resources/image/background.png')
105 
106 # 載入資源圖片
107 shoot_img = pygame.image.load('resources/image/shoot.png')
108 
109 # 用subsurface剪切讀入的圖片
110 # Hero圖片
111 hero_surface = []
112 hero_surface.append(shoot_img.subsurface(pygame.Rect(0, 99, 102, 126)))
113 hero_surface.append(shoot_img.subsurface(pygame.Rect(165, 360, 102, 126)))
114 #hero_surface.append(shoot_img.subsurface(pygame.Rect(165, 234, 102, 126)))     
115 #hero_surface.append(shoot_img.subsurface(pygame.Rect(330, 624, 102, 126)))
116 #hero_surface.append(shoot_img.subsurface(pygame.Rect(330, 498, 102, 126)))
117 #hero_surface.append(shoot_img.subsurface(pygame.Rect(432, 624, 102, 126)))
118 hero_pos = [200, 500]
119 
120 # bullet1圖片
121 bullet1_surface = shoot_img.subsurface(pygame.Rect(1004, 987, 9, 21))
122 
123 # enemy1圖片 **********************************************************
124 enemy1_surface = shoot_img.subsurface(pygame.Rect(534, 612, 57, 43))
125 enemy1_down_surface = []
126 enemy1_down_surface.append(shoot_img.subsurface(pygame.Rect(267, 347, 57, 43)))
127 enemy1_down_surface.append(shoot_img.subsurface(pygame.Rect(873, 697, 57, 43)))
128 enemy1_down_surface.append(shoot_img.subsurface(pygame.Rect(267, 296, 57, 43)))
129 enemy1_down_surface.append(shoot_img.subsurface(pygame.Rect(930, 697, 57, 43)))
130 # ********************************************************************
131 
132 # 創建玩家
133 hero = Hero(hero_surface[0], hero_pos)
134 
135 # 創建敵人組
136 enemy1_group = pygame.sprite.Group()
137 
138 # 創建擊毀敵人組
139 enemy1_down_group = pygame.sprite.Group()
140 
141 # 事件循環(main loop)
142 while True:
143 
144     # 控制游戲最大幀率
145     clock.tick(FRAME_RATE)
146 
147     # 繪制背景
148     screen.blit(background, (0, 0))
149 
150     # 改變飛機圖片制造動畫
151     if ticks >= ANIMATE_CYCLE:
152         ticks = 0
153     hero.image = hero_surface[ticks//(ANIMATE_CYCLE//2)]
154 
155     # 射擊
156     if ticks % 10 == 0:
157         hero.single_shoot(bullet1_surface)
158     # 控制子彈
159     hero.bullets1.update()
160     # 繪制子彈
161     hero.bullets1.draw(screen)
162 
163     # 產生敵機 *****************************************************
164     if ticks % 30 == 0:
165         enemy = Enemy(enemy1_surface, [randint(0, SCREEN_WIDTH - enemy1_surface.get_width()), -enemy1_surface.get_height()])
166         enemy1_group.add(enemy)
167     # 控制敵機
168     enemy1_group.update()
169     # 繪制敵機
170     enemy1_group.draw(screen)
171     # ************************************************************
172 
173     # 檢測敵機與子彈的碰撞 *******************************************
174     enemy1_down_group.add(pygame.sprite.groupcollide(enemy1_group, hero.bullets1, True, True))
175     
176     for enemy1_down in enemy1_down_group:
177         screen.blit(enemy1_down_surface[enemy1_down.down_index], enemy1_down.rect)
178         if ticks % (ANIMATE_CYCLE//2) == 0:
179             if enemy1_down.down_index < 3:
180                 enemy1_down.down_index += 1
181             else:
182                 enemy1_down_group.remove(enemy1_down)
183     # ************************************************************
184         
185     # 繪制飛機
186     screen.blit(hero.image, hero.rect)
187     ticks += 1 # python已略去自增運算符
188 
189     # 更新屏幕
190     pygame.display.update()                                         
191     
192     # 處理游戲退出
193     # 從消息隊列中循環取
194     for event in pygame.event.get():
195         if event.type == pygame.QUIT:
196             pygame.quit()
197             exit()
198 
199         # ※ Python中沒有switch-case 多用字典類型替代
200         # 控制方向       
201         if event.type == pygame.KEYDOWN:
202             if event.key in offset:
203                 offset[event.key] = hero.speed
204         elif event.type == pygame.KEYUP:
205             if event.key in offset:
206                 offset[event.key] = 0
207 
208     # 移動飛機
209     hero.move(offset)
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM