【Python】毕设 仿PVZ 23.3.26

进度

  1. 卡槽选择箭头载入,实现选择植物种植的逻辑
# 箭头以内部类的形式,作为SeedBank的属性存在
class Arrow:
    arrow_animation = []
    animespeed = 0.01
    f = 0

    def __init__(self):
        # 加载箭头动画
        import_assets('assets/SeedBank/arrow', self.arrow_animation)
        self.image = self.arrow_animation[0]

    def animate(self, screen, pos):
        self.f += self.animespeed
        if self.f > len(self.arrow_animation):
            self.f = 0

        self.image = self.arrow_animation[int(self.f)]
        screen.blit(self.image, pos)

# 插曲:建立箭头类的时候,我把import_assets类重新编写成函数,把其放在Setting类中,供所有精灵使用
def import_assets(path, animation_package):
    pygame.display.init()
    for image in os.listdir(path):
        full_path = path + '/' + image
        image_surf = pygame.image.load(full_path).convert_alpha()
        animation_package.append(image_surf)
# 然而,当我在箭头类中调用这个方法时,出现了报错:No video mode has been set
    class Arrow:
        arrow_animation = []
        import_assets(path, animation_package)
# 分析之后,原因可能是和python处理类的顺序有关,类的属性会在游戏运行之前就加载,此时没有游戏屏幕
# 而pygame的逻辑是必须要有屏幕才能加载图片,所以import_assets不能放在外面,只能放在__init__里
  1. 把AI移到游戏管理类里
    这里我调整了整个游戏的逻辑层次:
    在这里插入图片描述其中,存在子类反馈的情况,如太阳花生成阳光,僵尸走到最左结束游戏,使用全局变量解决此问题
    改变了整个逻辑后,选择植物种植的逻辑也要大改:玩家点击屏幕,将信息传到控制系统,然后由控制系统判断:点击的是卡槽,改变卡槽;点击的是地图,改变地图
class GameManager:
    def check_click(self, click_pos):
        # 点击的是卡槽
        if click_pos[1] <= Benchmark[1]:
            (a, b) = self.seedbank.cards_BM
            (x, y) = click_pos
            if a <= x and b <= y <= b + 70:
                self.seedbank.nowpt = (x - a) // 50

        # 点击的是草坪
        else:
            logic_pos = [(click_pos[i] - Benchmark[i]) // Cell[i] for i in range(2)]
            if 0 <= logic_pos[0] <= 8 and 0 <= logic_pos[1] <= 4:
                self.gameMap.deal_events(logic_pos, self.seedbank.nowpt)

效果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值