zoukankan      html  css  js  c++  java
  • Python 面向对象的案例:植物大战僵尸

      以植物大战僵尸为例,来练习在类中的各种调用

    import random
    
    
    class PlantsVSZombies:
        """
        植物大战僵尸
        """
        top_score = 0   # 默认最高分数
    
        def __init__(self, playser_name):
            """
            玩家有什么特征
            :param playser_name: 玩家名字
            :score : 分数,保存到这里
            """
            self.playser_name = playser_name
            self.score = []     # 分数保存到这里
    
        def start_game(self):
            """玩家的用户名"""
            print(f"
    [{self.playser_name}]开始游戏。。。。。")
            # 计算分数
            self.handle_score()
            print("Game Over 游戏结束")
    
        def handle_score(self):
            """计算分数"""
            self.score.append(random.randint(0, 100))     # random方法随机生成
            PlantsVSZombies.top_score = max(self.score)
    
        @classmethod   # (译:克拉斯.蛮色的)
        def display_top_score(cls):
            """游戏最高分"""
            print(f"游戏最高分为:{cls.top_score}")
    
        @staticmethod   # (译:四大题的.蛮色的)
        def display_help():
            """游戏帮助信息"""
            print("帮助信息:组织僵尸吃掉脑子")
    
    
    # 创建对象,类名(属性值)
    keyou = PlantsVSZombies("键盘")
    # 显示帮助信息,对象.静态方法
    keyou.display_help()
    # 开始游戏10次, i 没有用上用 _ 代替
    for _ in range(10):
        keyou.start_game()
    
    # 获取游戏最高分,对象.类方法
    keyou.display_top_score()

    *******请大家尊重原创,如要转载,请注明出处:转载自:https://www.cnblogs.com/shouhu/,谢谢!!******* 

  • 相关阅读:
    bzoj1084
    bzoj1088 [SCOI2005]扫雷
    [LUOGU] 1892 团伙
    [普及组] 2017 成绩
    [LUOGU] P2661 信息传递
    [LUOGU] P1339 [USACO09OCT]热浪Heat Wave
    [LUOGU] P1828 香甜的黄油 Sweet Butter
    [模板] 单源最短路径
    [LUOGU] 1717 钓鱼
    [UVA] 704 Colour Hash
  • 原文地址:https://www.cnblogs.com/shouhu/p/12743680.html
Copyright © 2011-2022 走看看