Python:将代码迁移到类中

将代码迁移到类中是一种很好的做法,可以提高代码的组织性、可重用性和可维护性。通过将功能封装到类中,我们可以更好地管理状态和行为。

下面我们将前面的战斗系统示例迁移到一个类结构中。

在这里插入图片描述

1、问题背景

我正在开发一个模拟篮球比赛的程序,并希望将代码放入一个类中,以避免代码变得难以编辑。主要原因是,为了考虑加时赛,我需要复制粘贴我的所有代码。我意识到这将非常低效,因此决定寻找一种更好的方法。

2、解决方案

使用Python创建一个名为engine的类,并在其中包含运行比赛模拟的所有代码。
使用两个名为HT和AT的Team对象来分别表示主队和客队。
使用一个名为Player对象来表示比赛中的每个球员。
将代码组织成几个方法,包括:
engine方法:运行比赛模拟。
playGame方法:在主队和客队之间进行比赛。
calculateScore方法:计算比赛分数。
printScore方法:打印比赛分数。

import random

class Team:
    def __init__(self, name, players):
        self.name = name
        self.players = players

class Player:
    def __init__(self, name, stats):
        self.name = name
        self.stats = stats

class Engine:
    def __init__(self):
        self.time = 2880
        self.fga = 0
        self.hts = 0
        self.ats = 0
        self.htn = ""
        self.atn = ""
        self.ht = 0
        self.at = 0

    def engine(self):
        print("1.Miami Heat\n2.San Antonio Spurs\n3.Cleveland Cavaliers")
        self.ht = MIA
        self.htn = "Miami"
        self.at = SAS
        self.atn = "San Antonio"

    def playGame(self):
        while self.time > 0:
            pos = random.randint(0, 4)
            sub = random.randint(4, 24)

            # Home Team
            if pos == 0:
                sht = random.randint(1, 3)
                if sht == 1:
                    chance = random.randint(1, 100)
                    if chance <= num1HC:
                        print(HT[0][14], "hits a three!")
                        self.hts += 3
                        self.fga += 1
                        self.time -= sub
                    else:
                        print(HT[0][14], "misses from deep.")
                        self.fga += 1
                        self.time -= sub
                if sht == 2:
                    chance = random.randint(1, 100)
                    if chance <= num2HC:
                        print(HT[0][14], "nails the long two.")
                        self.time -= sub
                        self.hts += 2
                        self.fga += 1
                    else:
                        print(HT[0][14], "comes up short from mid range.")
                        self.time -= sub
                        self.fga += 1
                if sht == 3:
                    chance = random.randint(1, 100)
                    if chance <= num3HC:
                        print(HT[0][14], "backs down", AT[0][14], "and lays it in for two.")
                        self.time -= sub
                        self.hts += 2
                        self.fga += 1
                    else:
                        print(HT[0][14], "misses from close range.")
                        self.time -= sub
                        self.fga += 1
            if pos == 1:
                sht = random.randint(1, 3)
                if sht == 1:
                    chance = random.randint(1, 100)
                    if chance <= num1HPF:
                        print(HT[1][14], "hits a three!")
                        self.time -= sub
                        self.hts += 3
                        self.fga += 1
                    else:
                        print(HT[1][14], "misses from deep.")
                        self.time -= sub
                        self.fga += 1
                if sht == 2:
                    chance = random.randint(1, 100)
                    if chance <= num2HPF:
                        print(HT[1][14], "nails the long two.")
                        self.time -= sub
                        self.hts += 2
                        self.fga += 1
                    else:
                        print(HT[1][14], "comes up short from mid range.")
                        self.time -= sub
                        self.fga += 1
                if sht == 3:
                    chance = random.randint(1, 100)
                    if chance <= num3HPF:
                        print(HT[1][14], "backs down", AT[1][14], "and lays it in for two.")
                        self.time -= sub
                        self.hts += 2
                        self.fga += 1
                    else:
                        print(HT[1][14], "misses from close range.")
                        self.time -= sub
                        self.fga += 1
            if pos == 2:
                sht = random.randint(1, 3)
                if sht == 1:
                    chance = random.randint(1, 100)
                    if chance <= num1HSF:
                        print(HT[2][14], "hits a three!")
                        self.time -= sub
                        self.hts += 3
                        self.fga += 1
                    else:
                        print(HT[2][14], "misses from deep.")
                        self.time -= sub
                        self.fga += 1
                if sht == 2:
                    chance = random.randint(1, 100)
                    if chance <= num2HSF:
                        print(HT[2][14], "nails the long two.")
                        self.time -= sub
                        self.hts += 2
                        self.fga += 1
                    else:
                        print(HT[2][14], "comes up short from mid range.")
                        self.time -= sub
                        self.fga += 1
                if sht == 3:
                    chance = random.randint(1, 100)
                    if chance <= num3HSF:
                        print(HT[2][14], "drives to the basket and lays it for two.")
                        self.time -= sub
                        self.hts += 2
                        self.fga += 1
                    else:
                        print(HT[2][14], "misses from close range.")
                        self.time -= sub
                        self.fga += 1
            if pos == 3:
                sht = random.randint(1, 3)
                if sht == 1:
                    chance = random.randint(1, 100)
                    if chance <= num1HSG:
                        print(HT[3][14], "hits a three!")
                        self.time -= sub
                        self.hts += 3
                        self.fga += 1
                    else:
                        print(HT[3][14], "misses from deep.")
                        self.time -= sub
                        self.fga += 1
                if sht == 2:
                    chance = random.randint(1, 100)
                    if chance <= num2HSG:
                        print(HT[3][14], "nails the long two.")
                        self.time -= sub
                        self.hts += 2
                        self.fga += 1
                    else:
                        print(HT[3][14], "comes up short from mid range.")
                        self.time -= sub
                        self.fga += 1
                if sht == 3:
                    chance = random.randint(1, 100)
                    if chance <= num3HSG:
                        print(HT[3][14], "drives to the basket and lays it for two.")
                        self.time -= sub
                        self.hts += 2
                        self.fga += 1
                    else:
                        print(HT[3][14], "misses from close range.")
                        self.time -= sub
                        self.fga += 1
            if pos == 4:
                sht = random.randint(1, 3)
                if sht == 1:
                    num = (HT[4][0]) - (AT[4][6] / 2) - 10
                    chance = random.randint(1, 100)
                    if chance <= num1HPG:
                        print(HT[4][14], "hits a three!")
                        self.time -= sub
                        self.fga += 1
                        self.hts += 3
                    else:
                        print(HT[

总结

通过将代码迁移到类结构中,我们实现了:

  • 代码重用:通过将角色和战斗逻辑封装到类中,我们可以更好地重用代码并支持多个角色实例。
  • 代码清晰:类将状态和行为有机地结合起来,使得代码逻辑更易于理解和维护。
  • 扩展性:类结构便于将来扩展更多的功能,如不同种类的角色、特殊攻击等。

这是将代码从简单的函数转变为类结构的一个重要步骤。

猜你喜欢

转载自blog.csdn.net/weixin_44617651/article/details/142940417