植物大戰僵屍游戲的開發(python)


 

  

裝備東西: 搭建好python環境, 四張圖片,(背景圖片,炮彈圖片,僵屍圖片,豌豆圖片),就ok了  沒有安裝pygame的需要進行安裝  pip install pygame

  參考視頻

# 植物大戰僵屍的練習 使用 pygame  模塊進行開發

import pygame
from pygame.locals import *
import random

# 1.創建窗口
# 2.顯示豌豆
# 3.通過鍵盤控制豌豆上下移動
WIDTH = 1200
HEIGHT = 600

# 創建豌豆對象
class Peas:

    def __init__(self):
        self.image_source = pygame.image.load('./res/peas.png')
        self.image_new = pygame.transform.scale(self.image_source, (100, 100))
        self.image_rect = self.image_new.get_rect()
        # 初始化位置
        self.image_rect.top = 280
        self.image_rect.left = 60
        # 設置豌豆是否上下移動
        self.is_move_up = False
        self.is_move_down = False
        # 是否發射炮彈
        self.is_shout = False

    def dispaly(self):
        """ 豌豆顯示在頁面上"""
        mode.blit(self.image_new, self.image_rect)

    def move_up(self):
        if self.image_rect.top > 90:
            self.image_rect.move_ip(0, -6)

    def move_down(self):
        if self.image_rect.bottom < 600:
            self.image_rect.move_ip(0, 6)

    def shout_bullet(self):
        # 創建一個炮彈對象
        bullet = Bullet(self);
        # 保存創建好的炮彈對象
        Bullet.bullet_list.append(bullet)

# 炮彈對象
class Bullet:
    bullet_list = []   # 創建一個類對象
    interval = 0   # 炮彈的間隔

    def __init__(self,peas):
        self.image_source = pygame.image.load('./res/bullet.png')
        self.image_new = pygame.transform.scale(self.image_source, (50, 50))
        self.image_rect = self.image_new.get_rect()
        # 初始化位置(和豌豆有關系)
        self.image_rect.top = peas.image_rect.top
        self.image_rect.left = peas.image_rect.right

    def display(self):
        mode.blit(self.image_new, self.image_rect)

    def move(self):
        self.image_rect.move_ip(10, 0)
        # 如果炮彈越界移除炮彈
        if self.image_rect.left > WIDTH - 100:
            Bullet.bullet_list.remove(self)

        # 如果炮彈碰撞到僵屍,僵屍消失
        for item in Zombie.zombie_list:
            if self.image_rect.colliderect(item.image_rect):  # 碰撞機制
                Bullet.bullet_list.remove(self)
                Zombie.zombie_list.remove(item)
                break

# 僵屍的出現和移動
class Zombie:
    # 保存多個僵屍
    zombie_list = []
    interval = 0  # 僵屍創建頻率

    def __init__(self):
        self.image_source = pygame.image.load('./res/zombie.png')
        self.image_new = pygame.transform.scale(self.image_source, (150, 150))
        self.image_rect = self.image_new.get_rect()
        # 初始化位置
        self.image_rect.top = random.randint(100,HEIGHT - 100)
        self.image_rect.left = WIDTH - 100

    def display(self):
        mode.blit(self.image_new, self.image_rect)

    def move(self):
        self.image_rect.move_ip(-3, 0)
        # 如果炮彈越界移除炮彈
        if self.image_rect.left < 20:
            Zombie.zombie_list.remove(self)

        # 如果炮彈碰撞到僵屍,僵屍消失
        for item in Bullet.bullet_list:
            if self.image_rect.colliderect(item.image_rect):
                Bullet.bullet_list.remove(item)
                Zombie.zombie_list.remove(self)
                break




def key_control():
    '''時間監聽'''
    # 對事件進行處理 它監聽的是一個列表
    for item in pygame.event.get():
        # 事件類型進行判斷
        if item.type == QUIT:
            pygame.quit()
            exit()
        # 按下鍵盤的事件判斷
        if item.type == KEYDOWN:  # 也就是說,只要鍵盤向下,一直是True
            # 判斷具體的鍵
            if item.key == K_UP:
                print("向上移動")
                # 移動
                peas.is_move_up = True
            elif item.key == K_DOWN:
                print("向下移動")
                peas.is_move_down = True
            elif item.key == K_SPACE:
                print("空格鍵按下")
                peas.is_shout = True
        elif item.type == KEYUP:
            # 判斷具體的鍵
            if item.key == K_UP:
                print("向上移動")
                peas.is_move_up = False
            elif item.key == K_DOWN:
                print("向下移動")
                peas.is_move_down = False
            elif item.key == K_SPACE:
                print("空格鍵松開")
                peas.is_shout = False


if __name__ == '__main__':
    # 顯示窗體
    mode = pygame.display.set_mode((WIDTH, HEIGHT))
    # 為了使圖片完全覆蓋窗口,涉及到一個坐標,就是從左上角開始
    # 加載圖片
    bg = pygame.image.load('./res/bg.jpg')
    # 改變圖片大小 生成新的圖片
    bg = pygame.transform.scale(bg,(WIDTH,HEIGHT))
    # 獲取圖片的位置和大小
    bg_rect = bg.get_rect()

    # 創建一個時鍾,優化速度效果
    clock = pygame.time.Clock()
    # 創建豌豆對象
    peas = Peas();
    # 為了方式程序結束,窗口消失,需要寫一個死循環
    while True:
        # 設置背景顏色(顏色的填充)
        mode.fill((0, 0, 0))
        # 圖片和窗口的關聯
        mode.blit(bg, bg_rect)
        # 調用豌豆的顯示方法
        # 顯示豌豆
        peas.dispaly()
        # 事件的調用
        key_control()

        if peas.is_move_up:
            peas.move_up()

        if peas.is_move_down:
            peas.move_down()

        # 發射炮彈
        Bullet.interval += 1
        if peas.is_shout and Bullet.interval >= 20:
            Bullet.interval = 0
            peas.shout_bullet()

        # 顯示所有的炮彈
        for bullet in Bullet.bullet_list:
            bullet.display()
            bullet.move()

        # 創建僵屍對象
        Zombie.interval += 1
        if Zombie.interval >= 20:
            Zombie.interval = 0
            zombie = Zombie();
            Zombie.zombie_list.append(zombie)

        # 顯示所有的僵屍
        for zombie in Zombie.zombie_list:
            zombie.display()
            zombie.move()

        # 動畫變化的幀頻率
        clock.tick(60)
        # 顯示圖片
        pygame.display.update()

 


免責聲明!

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



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