200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > python植物大战僵尸辅助_【python面向对象实战练习】植物大战僵尸

python植物大战僵尸辅助_【python面向对象实战练习】植物大战僵尸

时间:2021-04-29 16:42:51

相关推荐

python植物大战僵尸辅助_【python面向对象实战练习】植物大战僵尸

import random

class PlantsVSZombies:

"""

植物大战僵尸

"""

# 类属性

top_score = 0

# 实例属性,好比是菜,

# 加入玩家的属性:姓名,分数,玩家特有的属性

def __init__(self, playser_name):

self.playser_name = playser_name

self.score = []

# 实例方法,获取实例属性,需要实例方法

def start_game(self):

print("{}开始游戏...".format(self.playser_name))

# 计算分数

self.handle_score()

# 打印游戏结束

print("Game Over !")

# 实例方法:对菜进行加工

# 获取实例属性,计算分数的方法,处理分数

def handle_score(self):

# 将玩家独有的分数添加到实例属性score列表当中

self.score.append(random.randint(0, 100))

# 将实例属性赋值给类方法,将分数的最大值赋值给类属性top_score当中

PlantsVSZombies.top_score = max(self.score)

# 类方法,对类属性进行加工

@classmethod

def display_top_score(cls):

print("游戏最高分:{}".format(cls.top_score))

# 设置静态方法,打印帮助信息

@staticmethod

def display_help():

print("帮助信息:阻止僵尸进入房间,吃掉脑子!")

# 创建hc对象,给对象赋值实例属性name

hc = PlantsVSZombies("帅哥")

# 调用静态方法,帮助信息

hc.display_help()

# 设置玩游戏的次数

for _ in range(10):

hc.start_game()

# 打印最高分

hc.display_top_score()

标签:实战,实例,python,self,面向对象,score,hc,top,属性

来源: /python-test001/p/12443413.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。