python植物大战僵尸代码

二话不说,直接上代码。

import pygame
import random
import sys
#1 配置图片地址
#IMAGE_PATH = 'grassland.png'
#1 设置页面宽高
scrrr_width=800
scrrr_height =560
#1 创建控制游戏结束的状态
GAMEOVER = False
#4 图片加载报错处理
LOG = '文件:{}中的方法:{}出错'.format(__file__,__name__)
#3 创建地图类
class Map():
    #3 存储两张不同颜色的图片名称
    map_names_list = ['**************', '*******************']
    #3 初始化地图
    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
    #3 加载地图
    def load_map(self):
         MainGame.window.blit(self.image,self.position)
#4 植物类
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)
#5 向日葵类
class Sunflower(Plant):
    def __init__(self,x,y):
        super(Sunflower, self).__init__()
        self.image = pygame.image.load('**************')
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.price = 50
        self.hp = 100
        #5 时间计数器
        self.time_count = 0

    #5 新增功能:生成阳光
    def produce_money(self):
        self.time_count += 1
        if self.time_count == 25:
            MainGame.money += 10
            self.time_count = 0
    #5 向日葵加入到窗口中
    def display_sunflower(self):
        MainGame.window.blit(self.image,self.rect)
#6 豌豆射手类
class PeaShooter(Plant):
    def __init__(self,x,y):
        super(PeaShooter, self).__init__()
        # self.image 为一个 surface
        self.image = pygame.image.load('**************')
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.price = 50
        self.hp = 200
        #6 发射计数器
        self.shot_count = 0

    #6 增加射击方法
    def shot(self):
        #6 记录是否应该射击
        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 =
Python植物大战僵尸游戏是一个基于Python语言的简单游戏,它的规则和经典的植物大战僵尸游戏类似。在游戏中,玩家需要通过种植各种不同的植物来抵御来袭的僵尸。 关于Python植物大战僵尸代码的介绍,我不清楚您具体想了解哪方面的内容,以下是我简单介绍一下游戏的结构和一些代码实现。 该游戏的代码主要分为三个部分:界面设计、游戏逻辑和游戏控制。其中,界面设计包括各种元素的图片、声音和布局;游戏逻辑包括植物僵尸和子弹的属性、移动和攻击方式;游戏控制包括游戏开始、结束、暂停和保存等功能。 以下是该游戏的一些代码实现: 1. 植物的创建和属性设置: ```python class Plant: def __init__(self, x, y, plant_type): self.x = x self.y = y self.plant_type = plant_type self.health = self.max_health = PLANT_HEALTH[plant_type] self.damage = PLANT_DAMAGE[plant_type] self.attack_speed = PLANT_ATTACK_SPEED[plant_type] self.image = pygame.image.load(PLANT_IMAGE[plant_type]).convert_alpha() ``` 2. 僵尸的创建和属性设置: ```python class Zombie: def __init__(self, x, y, zombie_type): self.x = x self.y = y self.zombie_type = zombie_type self.health = self.max_health = ZOMBIE_HEALTH[zombie_type] self.damage = ZOMBIE_DAMAGE[zombie_type] self.speed = ZOMBIE_SPEED[zombie_type] self.image = pygame.image.load(ZOMBIE_IMAGE[zombie_type]).convert_alpha() ``` 3. 游戏逻辑中判断子弹是否碰撞到僵尸: ```python def check_bullet_zombie_collision(bullets, zombies): for bullet in bullets: for zombie in zombies: if bullet.x + bullet.width > zombie.x and \ bullet.x < zombie.x + zombie.width and \ bullet.y + bullet.height > zombie.y and \ bullet.y < zombie.y + zombie.height: zombies.remove(zombie) bullets.remove(bullet) return True return False ``` 以上只是该游戏代码的一部分,如果您对其他方面还有疑问或者需要更加详细的介绍,请告诉我具体想了解哪方面的内容,我会尽力给您解答。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值