python脚本编写实战(二)

git

  1. 使用pycharm的 github,最方便
  2. 如果pycharm的github不可行,报错或者链接不上,就直接在github创建一个自己的仓库。然后根据提示,一步一步操作,即可

课间作业

一个回合制游戏,每个角色都有hp 和power,hp代表血量,power代表攻击力,hp的初始值为1000,power的初始值为200。
定义一个fight方法:
final_hp = hp-enemy_power
enemy_final_hp = enemy_hp - power
两个hp进行对比,血量剩余多的人获胜

class game:
    def __init__(self):
        self.hp = 1000
        self.power =200

    def fight(self,enemy_hp,enemy_power):
        final_hp = self.hp - enemy_power
        enemy_final_hp = enemy_hp - self.power

        if final_hp >enemy_final_hp:
            print('I win')
        else:
            print('enemy win')
a = game()
a.fight(500,400)
class Game:
    def __init__(self):
        self.hp = 1000
        self.power = 200

    def fight(self, enemy_power, enemy_hp):
        final_hp = self.hp - enemy_power
        enemy_final_hp = enemy_hp - self.power
        if final_hp > enemy_final_hp:
            print("我赢了")
        elif final_hp < enemy_final_hp:
            print("敌人赢了")
        else:
            print("平手")


game = Game()
game.fight(200, 1000)

课间作业

程序代码

"""
一个回合制游戏,每个角色都有hp 和power,hp代表血量,power代表攻击力,hp的初始值为1000,power的初始值为200。
定义一个fight方法:
final_hp = hp-enemy_power
enemy_final_hp = enemy_hp - power
两个hp进行对比,血量剩余多的人获胜
"""


class Game:
    def __init__(self):
        self.hp = 1000
        self.power = 200

    def fight(self, enemy_hp, enemy_power):
        final_hp = self.hp - enemy_power
        enemy_final_hp = enemy_hp - self.power
        if final_hp <= enemy_final_hp:
            print("你输了")
        else:
            print("恭喜,你赢了")


game = Game()
game.fight(1000, 150)

代码执行

game

课间作业:

class Game():
    def __init__(self,hp,power):
        self.hp = hp
        self.power = power
        self.enemy_power = 200
        self.enemy_hp = 1000
    def fight(self):
        final_hp = self.hp-self.enemy_power
        enemy_final_hp = self.enemy_hp - self.power
        if final_hp > enemy_final_hp:
            print("我赢了")
        else:
            print("敌人赢了")

myGame = Game(1000,300)
myGame.fight()
class Game:
    def __init__(self):
        self.hp=1000
        self.power=200

    def fight(self,enemy_hp,enemy_power):
        final_hp=self.hp-enemy_power
        enemy_power_hp=enemy_hp-self.power
        if(final_hp>enemy_hp):
            print("Victory")
        elif(final_hp<enemy_hp):
            print("Defeat")
        else:
            print("Peace Ending!")

game=Game()
game.fight(800,200)
class game:
    def __init__(self):
        self.hp = 1000
        self.power = 200

    def fight(self,enemy_hp,enemy_power):
        final_hp = self.hp - enemy_power
        enemy_final_hp = enemy_hp - self.power
        if final_hp > enemy_final_hp:
            print("我赢了")
        else:
            print("敌人赢了")

pk1 = game()
pk1.fight(100,19)
class Game:

    def __init__(self):

        self.hp=1000

        self.power=200

    def fihgt(self,enemy_power,enemy_hp):

        final_hp = self.hp-enemy_power

        enemy_final_hp = enemy_hp - self.power

        if final_hp>enemy_final_hp:

            print("你赢了")

        elif final_hp<enemy_final_hp:

            print("敌人赢了")

        else:

            print("平手")

game=Game()

game.fihgt(100,1100)
class Game:
    def __init__(self):
        self.hp = 1000
        self.power = 200

    def fight(self, enemy_power, enemy_hp):
        final_hp = self.hp - enemy_power
        enemy_final_hp = enemy_hp - self.power
        if final_hp > enemy_final_hp:
            print("我赢了")
        else:
            print("敌人赢了")
        

my_game = Game()
my_game.fight(300,1100)
class Game():
    def __init__(self):
        self.hp = 1000
        self.power = 200
    def fight(self,enemy_power,enemy_hp):
        final_hp = self.hp - enemy_power
        enemy_final_hp = enemy_hp - self.power
        if final_hp > enemy_final_hp:
            print("我赢了!")
        elif final_hp == enemy_final_hp:
            print("平手")
        else:
            print("我输了")
game = Game()
game.fight(900,90)

课间作业提交

#定义Game类
class Game:
    # 使用构造函数定义Game类的两个变量hp和power并设置初始值,注意init中的变量需要加上self才能够被其他类调用,申明为实例变量
    def __init__(self):
        self.hp = 1000
        self.power = 200

    # 定义fight方法,并为enemy_hp和enemy_power传参
    def fight(self,enemy_hp,enemy_power):
        final_hp = self.hp-enemy_power
        enemy_final_hp = enemy_hp - self.power
        # 使用判断语句来进行实现“血量剩余多的人获胜”的对比功能
        if final_hp>enemy_final_hp:
            print("我赢了")
        elif final_hp<enemy_final_hp:
            print("我输了,敌人赢了")
        else:
            print("打成平局")
# 对类进行初始化
game=Game()
# 调用类的fight方法,并传两个参数
game.fight(1000,200)
执行结果:

image

class Game:

    def __init__(self):
        self.hp = 1000
        self.power = 200

    def fight(self,enemy_hp,enemy_power):
        final_hp = self.hp - enemy_power
        enemy_final_hp= enemy_hp-self.power
        if final_hp>enemy_final_hp:
            print("我赢了")
        else:
            print("敌人赢了")

game = Game()
game.fight(1000,199)
class GAME:
#函数构造,定义我的血量和我的攻击力
def __init__(self):
    self.hp = 1000
    self.power = 2000
#定义打斗方法
def fight(self,enemy_power,enemy_hp):
    final_hp = self.hp - enemy_power
    enemy_final_hp = enemy_hp - self.power
    #比较我和敌人的最终生命
    if final_hp > enemy_final_hp:
        print("winner")
    else:
        print("lose")
#实例化

g = GAME()
#调用打斗方法,并且赋值 敌人的生命和攻击力
g.fight(800,1000)

#定义一个游戏类
class Game:

#初始化变量
def __init__(self):
    self.hp = 1000
    self.power = 200

# 定义一个fight方法,传入对手的攻击力enemy_power、对手的血量enemy_hp两个参数
def fight(self, enemy_power, enemy_hp):
    #对2个hp进行计算
    #我的最终血量
    final_hp = self.hp - enemy_power
    #对手的最终血量
    enemy_final_hp = enemy_hp - self.power
    #两个hp进行对比,血量剩余多的人获胜
    if final_hp > enemy_final_hp:
        print("我赢了")
    elif final_hp == enemy_final_hp:
        print("平局")
    else:
        print("对手赢了")

#实例化类
gm = Game()
gm.fight(800, 300)

  • 尝试自己写一下课间作业
class TurnBasedGame:
    # 定义一个回合制游戏
    def __init__(self, hp, power):
        # 初始化,需要传入的参数,为了更加灵活,加入hp和power方便需要传参
        self.hp = hp
        self.power = power
        # 在init内加self,声明实例变量,才能被类内其他方法调用

    def fight(self, enemy_hp, enemy_power):
        # 由于init已经传入自身血量和攻击力,可以直接调用,对手血量和攻击力不知道,需要传参
        final_hp = self.hp - enemy_power
        # 自身剩余血量 = 我方血量 - 对方攻击力
        enemy_final_hp = enemy_hp - self.power
        # 敌方剩余血量 = 对方血量 - 我方攻击力
        if final_hp > enemy_final_hp:
            # 我方剩余血量 > 敌方剩余血量时我赢
            print("你赢了")
        elif final_hp == enemy_final_hp:
            # 血量相同时平局比较合适
            print("平局")
            # 剩余情况都是对方赢
        else:
            print("你输了")


game = TurnBasedGame(1000, 200)
game.fight(1000, 199)
关闭