一 GitHub
python-pea-zombie:https://github.com/yihonglei/Python-Study(一個文件處理)
PlayPeaZombie:https://github.com/yihonglei/PlayPeaZombie(多個文件抽象分類)
二 項目運行
1、python3 + pygame模塊;
2、空格鍵發送炮彈,上下鍵移動炮彈;
三 python-pea-zombie源碼
1、python-pea-zombie代碼運行環境python3 + pygame模塊;
2、github下載後直接運行即可,下面是源碼,圖片在python-pea-zombie的material目錄下。
"""
植物大戰殭屍代碼未拆分簡潔版,可直接運行。
主要功能:
1、主體窗口顯示;
2、豌豆顯示,以及上下移動;
3、炮彈顯示,以及移動;
4、殭屍顯示,以及移動;
5、炮彈碰到殭屍或殭屍碰到炮彈,二者均從頁面上消失;
6、當殭屍碰到豌豆時,遊戲結束;
"""
import pygame
import random
from pygame.locals import *
# 窗體的寬度和高度
WIDTH = 1200
HEIGHT = 600
# 豌豆對象
class Pea:
"""
初始化豌豆屬性
"""
def __init__(self):
# 加載一個豌豆圖片
self.image = pygame.image.load("./material/image/pea.gif")
# 獲取到豌豆圖片的大小和位置
self.image_rect = self.image.get_rect()
# 設置豌豆顯示的位置
self.image_rect.top = 285
self.image_rect.left = 30
# 判斷豌豆是否向上移動
self.is_move_up = False
# 判斷豌豆是否向下移動
self.is_move_down = False
# 判斷豌豆是否射炮彈
self.is_shout = False
"""
豌豆顯示
"""
def display(self):
# 設置豌豆背景圖片
screen.blit(self.image, self.image_rect)
"""
豌豆向上移動
"""
def move_up(self):
# 如果越界,則不能移動,否則,可以移動
if self.image_rect.top > 30:
self.image_rect.move_ip(0, -10)
# 如果向上移動的時候,殭屍和豌豆碰撞,遊戲結束
for z in Zombie.zombie_list:
if self.image_rect.colliderect(z.image_rect):
pygame.quit()
exit()
"""
豌豆向下移動
"""
def move_down(self):
# 如果越界,則不能移動,否則可以移動
if self.image_rect.bottom < 600:
self.image_rect.move_ip(0, 10)
# 如果向下移動的時候,殭屍和豌豆碰撞,遊戲結束
for z in Zombie.zombie_list:
if self.image_rect.colliderect(z.image_rect):
pygame.quit()
exit()
"""
炮彈發射
"""
def bullet_shout(self):
# 創建一個炮彈
bullet = Bullet(self)
Bullet.bullet_list.append(bullet)
# 炮彈對象
class Bullet:
# 所有的炮彈信息
bullet_list = []
# 炮彈創建間隔時間
interval = 0
"""
初始化炮彈屬性
"""
def __init__(self, pea):
self.image = pygame.image.load("./material/image/bullet.gif")
self.image_rect = self.image.get_rect()
self.image_rect.top = pea.image_rect.top
self.image_rect.left = pea.image_rect.right
"""
炮彈顯示
"""
def display(self):
# 設置炮彈背景圖片
screen.blit(self.image, self.image_rect)
"""
炮彈移動
"""
def move(self):
# 移動炮彈
self.image_rect.move_ip(8, 0)
# 如果炮彈越界,刪除炮彈
if self.image_rect.right > WIDTH - 20:
Bullet.bullet_list.remove(self)
# 如果殭屍和炮彈碰撞,則殭屍和炮彈刪除
for z in Zombie.zombie_list:
if self.image_rect.colliderect(z.image_rect):
Zombie.zombie_list.remove(z)
Bullet.bullet_list.remove(self)
break
# 殭屍對象
class Zombie:
# 所有的殭屍信息
zombie_list = []
# 創建殭屍間隔
interval = 0
def __init__(self):
# 加載一個殭屍圖片
self.image = pygame.image.load("./material/image/zombie.gif")
# 改變圖片大小
self.image = pygame.transform.scale(self.image, (70, 70))
# 獲取到殭屍圖片的大小和位置
self.image_rect = self.image.get_rect()
# 設置殭屍顯示的位置
self.image_rect.top = random.randint(10, HEIGHT - 70)
self.image_rect.left = WIDTH
# 顯示炮彈的方法
def display(self):
screen.blit(self.image, self.image_rect)
# 移動殭屍
def move(self):
# 移動殭屍
self.image_rect.move_ip(-2, 0)
# 如果殭屍越界,刪除殭屍
if self.image_rect.left < 0:
Zombie.zombie_list.remove(self)
# 如果豌豆和殭屍碰撞,則遊戲結束
if self.image_rect.colliderect(pea.image_rect):
pygame.quit()
exit()
# 如果炮彈和殭屍碰撞,則炮彈和殭屍都要消失
for b in Bullet.bullet_list:
if self.image_rect.colliderect(b.image_rect):
Bullet.bullet_list.remove(b)
Zombie.zombie_list.remove(self)
break
# 事件監聽處理
def key_control():
for event in pygame.event.get():
# 事件類型判斷
if event.type == QUIT:
# 退出遊戲
pygame.quit()
# 退出系統
exit()
# 按下事件類型的監聽
elif event.type == KEYDOWN:
# 判斷具體的鍵
if event.key == K_UP:
print("向上移動")
pea.is_move_up = True
elif event.key == K_DOWN:
print("向下移動")
pea.is_move_down = True
elif event.key == K_SPACE:
print("空格事件")
pea.is_shout = True
# 鍵盤松開事件監聽
elif event.type == KEYUP:
# 判斷具體的鍵
if event.key == K_UP:
print("向上移動放鬆")
pea.is_move_up = False
elif event.key == K_DOWN:
print("向下移動放鬆")
pea.is_move_down = False
elif event.key == K_SPACE:
print("空格事件放鬆")
pea.is_shout = False
if __name__ == '__main__':
"""
1、顯示窗體
"""
# 顯示窗體
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# 背景圖片
background_image = pygame.image.load("./material/image/background.png")
# 設置背景圖片大小
scale_background_image = pygame.transform.scale(background_image, (WIDTH, HEIGHT))
# 獲取圖片的位置和大小
scale_background_image_rect = scale_background_image.get_rect()
# 創建一個時鐘,優化運行速度的效果
clock = pygame.time.Clock()
"""
2、顯示豌豆
"""
# 創建豌豆對象
pea = Pea()
# 創建炮彈對象
bullet = Bullet(pea)
while True:
# 填充背景色爲黑色,因爲豌豆在拖動時會留下拖動痕跡
screen.fill((0, 0, 0))
# 設置窗體的背景圖片
screen.blit(scale_background_image, scale_background_image_rect)
# 設置豌豆背景圖片
pea.display()
"""
3、對事件處理,豌豆移動
"""
key_control()
# 豌豆上下移動
if pea.is_move_up:
pea.move_up()
if pea.is_move_down:
pea.move_down()
# 每隔一段時間創建炮彈
Bullet.interval += 1
if pea.is_shout and Bullet.interval >= 15:
Bullet.interval = 0
pea.bullet_shout()
# 每隔一段時間創建殭屍
Zombie.interval += 1
if Zombie.interval >= 15:
Zombie.interval = 0
Zombie.zombie_list.append(Zombie())
# 顯示所有的炮彈
for bullet in Bullet.bullet_list:
# 炮彈顯示
bullet.display()
# 炮彈移動
bullet.move()
# 顯示所有的殭屍
for zombie in Zombie.zombie_list:
# 殭屍顯示
zombie.display()
# 殭屍移動
zombie.move()
# 更新圖片
pygame.display.update()
# 幀頻率
clock.tick(60)