【python游戏】努力制造阳光,让植物有力量对抗僵尸吧~

本文介绍如何使用Python和Pycharm制作《植物大战僵尸》的简化版和复刻版。从游戏素材、代码展示到效果展示,详细讲解了游戏的各个组成部分,包括植物、僵尸的移动和攻击、碰撞检测以及游戏循环等关键逻辑。通过实例代码,带领读者体验游戏开发的乐趣。
摘要由CSDN通过智能技术生成

前言

大家早好、午好、晚好吖 ❤ ~欢迎光临本文章

晃着脑袋生产阳光的向日葵,突突突吐着子弹的豌豆射手!​

行动迟缓种类丰富的僵尸……

印象最深的是“僵尸吃掉了你的脑子!”

还有疯狂的戴夫,无一不唤醒着我们的童年记忆​

山民们闯到哪一关了?解锁了哪些植物?

在今天,就让我们来上一个大工程,制作植物大战僵尸小游戏里的冒险模式~

(有一个简单版和复刻版嘚~)

软件安装

  • Python 3.8 / 编译器

  • Pycharm 2021.2版本 / 代码编辑器

简单版

游戏素材

代码展示

引入需要的模块

import pygame
import random

配置图片地址

IMAGE_PATH = 'imgs/'

设置页面宽高

scrrr_width = 800
scrrr_height = 560

创建控制游戏结束的状态

GAMEOVER = False

图片加载报错处理

LOG = '文件:{
   }中的方法:{
   }出错'.format(__file__, __name__)

创建地图类


class Map():

存储两张不同颜色的图片名称

    map_names_list = [IMAGE_PATH + 'map1.png', IMAGE_PATH + 'map2.png']

初始化地图

    def __init__(self, x, y, img_index):
        self.image = pygame.image.load(Map.map_names_list[img_index])
        self.position = (x, y)
        # 是否能够种植
        self.can_grow = True

加载地图

    def load_map(self):
        MainGame.window.blit(self.image, self.position)

植物类

class Plant(pygame.sprite.Sprite):
    def __init__(self):
        super(Plant, self).__init__()
        self.live = True

加载图片

    def load_image(self):
        if hasattr(self, 'image') and hasattr(self, 'rect'):
            MainGame.window.blit(self.image, self.rect)
        else:
            print(LOG)

向日葵类

class Sunflower(Plant):
    def __init__(self, x, y):
        super(Sunflower, self).__init__()
        self.image = pygame.image.load('imgs/sunflower.png')
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.price = 50
        self.hp = 100

时间计数器

        self.time_count = 0

新增功能:生成阳光

    def produce_money(self):
        self.time_count += 1
        if self.time_count == 25:
            MainGame.money += 5
            self.time_count = 0

向日葵加入到窗口中

    def display_sunflower(self):
        MainGame.window.blit(self.image, self.rect)

豌豆射手类

class PeaShooter(Plant):
    def __init__(self, x, y):
        super(PeaShooter, self).__init__()
        # self.image 为一个 surface
        self.image = pygame.image.load('imgs/peashooter.png')
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.price = 50
        self.hp = 200

发射计数器

        self.shot_count = 0

增加射击方法

    def shot(self):

记录是否应该射击

        should_fire = False
        for zombie in MainGame.zombie_list:
            if zombie.rect.y == self.rect.y and zombie.rect.x < 800 and zombie.rect.x > self.rect.x:
                should_fire = True

如果活着

        if self.live and should_fire:
            self.shot_count += 1

计数器到25发射一次

            if self.shot_count == 25:

基于当前豌豆射手的位置,创建子弹

                peabullet = PeaBullet(self)

将子弹存储到子弹列表中

                MainGame.peabullet_list.append(peabullet)
                self.shot_count = 0

将豌豆射手加入到窗口中的方法

    def display_peashooter(self):
        MainGame.window.blit(self.image, self.rect)

豌豆子弹类


class PeaBullet(pygame.sprite.Sprite):
    def __init__(self, peashooter):
        self.live = True
        self.image = pygame.image.load('imgs/peabullet.png')
        self.damage = 50
        self.speed = 10
        self.rect = self.image.get_rect()
        self.rect.x = peashooter.rect.x + 60
        self.rect.y = peashooter.rect.y + 15

    def move_bullet(self):

在屏幕范围内,实现往右移动

        if self.rect.x < scrrr_width:
            self.rect.x += self.speed
        else:
            self.live = False

新增,子弹与僵尸的碰撞

    def hit_zombie(self):
        for zombie in MainGame.zombie_list:
            if pygame.sprite.collide_rect(self, zombie):
                # 打中僵尸之后,修改子弹的状态,
                self.live = False
                # 僵尸掉血
                zombie.hp -= self.damage
                if zombie.hp <= 0:
                    zombie.live = False
                    self.nextLevel()

闯关方法

    def nextLevel(self):
        MainGame.score += 20
        MainGame.remnant_score -= 20
        for i in range(1, 100):
            if MainGame.score == 100 * i and MainGame.remnant_score == 0:
                MainGame.remnant_score = 100 * i
                MainGame.shaoguan += 1
                MainGame.produce_zombie += 50

    def display_peabullet(self):
        MainGame.window.blit(self.image, self.rect)

僵尸类

class Zombie(pygame.sprite.Sprite):
    def __init__(self, x, y):
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值