Python 面向对象的案例:植物大战僵尸

时间:2020-04-21
本文章向大家介绍Python 面向对象的案例:植物大战僵尸,主要包括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"\n[{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/,谢谢!!******* 

原文地址:https://www.cnblogs.com/shouhu/p/12743680.html