説明
今回作成したのは、以前他の方からアドバイスを受けて改良した戦闘ゲームです。
2対2のコマンドラインで動作するものとなっており、通常攻撃はハンドガン(aキー)、防御は(bキー)、特殊攻撃は(cキー)となっております。また、xキーでコマンドキャンセルを行えます。
また、戦闘を行う相手は数字キーで定め、ここでは0キーでコマンドキャンセルを行います。
ソースコード
armed_squad.py
import character
import random
import time
#タイトル
def title():
print("-----------------")
print("<<<ARMED SQUAD>>>")
print("-----------------")
#オープニング
def beginning():
global p1
global p2
global e1
global e2
p1 = character.Player1()
p2 = character.Player2()
e1 = character.Enemy1()
e2 = character.Enemy2()
time.sleep(2)
print("~STORY~\nアームドスクワッドは、犯罪組織から人質を救出すべく、\n占拠されたビルに潜入した!")
time.sleep(1)
print(f"{p1.name}、出動!")
time.sleep(1)
print(f"{p2.name}、出動!")
time.sleep(1)
print(f"司令官:犯人はビル4Fに立てこもっている! 直ちに攻撃せよ!")
time.sleep(1)
#線を引く
def line():
print("ーーーーーーーーーーーーーーーーーーーー")
#攻撃フェイズ開始表示
def start():
print("ATTACK PHASE!!")
global damage
damage=0
#ステータス表示
def status():
print("\n",str(p1.name)," HP:[",(p1.hp),"] 弾:[",(p1.shoot),"]\n",str(p2.name)," HP:[",(p2.hp),"] 弾:[",(p2.shoot),"]\n",str(e1.name)," HP[",(e1.hp),"]\n",str(e2.name)," HP[",(e2.hp),"]",sep='')
#プレイヤー死亡表示
def p1_dead():
print("バーナードは倒れた!")
p1.hp = 0
line()
time.sleep(1)
def p2_dead():
print("マイキーは倒れた!")
p2.hp = 0
line()
time.sleep(1)
def e1_dead():
print("ベネット撃退!")
e1.hp = 0
line()
time.sleep(1)
def e2_dead():
print("ゴードン撃退!")
e2.hp = 0
line()
time.sleep(1)
def e1_die():
print(f"{e1.name}は既に死亡している")
line()
def e2_die():
print(f"{e2.name}は既に死亡している")
line()
def rand_guard():
global per
global add
per = random.uniform(0.01,1.00)
add = random.randint(20,30)
#攻撃コマンド(action->バーナード,action2->マイキー)
def do():
global action
global action2
global cursor
global cursor2
if p1.hp > 0:
action = input("バーナード(a:ハンドガン,b:防御,c:特殊武器,x:キャンセル)>>>")
if action == "x":
return do()
cursor=input("バーナードの攻撃対象(0:コマンドキャンセル,1:ベネット,2:ゴードン)>>>")
if cursor == "0":
return do()
if p2.hp > 0:
action2=input("マイキー(a:ハンドガン,b:防御,c:特殊武器,x:キャンセル)>>>")
if action2 == "x":
return do()
cursor2=input("マイキーの攻撃対象(0:コマンドキャンセル,1:ベネット,2:ゴードン)>>>")
if cursor2 == "0":
return do()
if action == "a":
if cursor == "1":
#ガード確率
guard = random.uniform(0.00,1.00)
if p1.hp > 0:
line()
print(f"{p1.name}の攻撃!")
if e1.hp <= 0:
e1_die()
elif e1.hp > 0 and guard < 0.20:
print(f"{e1.name}は防御した!")
damage = int(p1.ap/4-e1.gp/4)
print(f"{e1.name}は{damage}のダメージ")
e1.hp -= damage
line()
time.sleep(1)
elif e1.hp > 0:
add = (random.randint(10,20))
damage = int(p1.ap/2-e1.gp/4+add)
print(f"{e1.name}は{damage}のダメージ")
e1.hp -= damage
line()
time.sleep(1)
if e1.hp > 0:
add = (random.randint(10,20))
print(f"{e1.name}の攻撃!")
damage = int(e1.ap/2-p1.gp/4+add)
print(f"{p1.name}は{damage}のダメージ")
p1.hp -= damage
line()
time.sleep(1)
if p1.hp <= 0:
p1_dead()
elif e1.shoot > 0 and e1.hp > 0:
add = (random.randint(20,30))
print(f"{e1.name}は{e1.w_name}で攻撃!")
e1.shoot -= 1
damage = int(e1.ap/2-p1.gp/4+add)
print(f"{p1.name}は{damage}のダメージ")
p1.hp -= damage
line()
time.sleep(1)
if p1.hp <= 0:
p1_dead()
elif e1.hp <= 0:
e1_dead()
if e1.hp <= 0 and e2.hp <= 0:
print("全員撃退!")
line()
time.sleep(1)
elif cursor=="2":
#ガード確率
guard = random.uniform(0.00,1.00)
if p1.hp > 0:
line()
print(f"{p1.name}の攻撃!")
if e2.hp <= 0:
e2_die()
elif e2.hp > 0 and guard < 0.20:
print(f"{e2.name}は防御した!")
damage = int(p1.ap/4-e2.gp/4)
print(f"{e2.name}は{damage}のダメージ")
e2.hp-=damage
line()
time.sleep(1)
elif e2.hp > 0:
add = (random.randint(10,20))
damage = int(p1.ap/2-e2.gp/4+add)
print(f"{e2.name}は{damage}のダメージ")
e2.hp-=damage
line()
time.sleep(1)
if e2.hp > 0:
add=(random.randint(10,20))
print(f"{e2.name}の攻撃!")
damage = int(e2.ap/2-p1.gp/4+add)
print(f"{p1.name}は{damage}のダメージ")
p1.hp-=damage
line()
time.sleep(1)
if p1.hp <= 0:
p1_dead()
elif e2.shoot > 0 and e2.hp > 0:
add = (random.randint(30,45))
e2.shoot -= 1
print(f"{e2.name}は{e2.w_name}で攻撃!")
damage = int(e2.ap/2-p1.gp/4+add)
print(f"{p1.name}は{damage}のダメージ")
p1.hp-=damage
line()
time.sleep(1)
if p1.hp <= 0:
p1_dead()
elif e2.hp <= 0:
e2_dead()
if e1.hp <= 0 and e2.hp <= 0:
print("全員撃退!")
line()
time.sleep(1)
if action2 == "a":
if cursor2 == "1":
if p1.hp > 0:
#ガード確率
guard = random.uniform(0.00,1.00)
line()
print(f"{p2.name}の攻撃!")
if e1.hp <= 0:
e1_die()
elif e1.hp > 0 and guard < 0.20:
print(f"{e1.name}は防御した!")
damage = int(p2.ap/4-e1.gp/4)
print(f"{e1.name}は{damage}のダメージ")
e1.hp-=damage
line()
time.sleep(1)
elif e1.hp > 0:
add = (random.randint(10,20))
damage = int(p2.ap/2-e1.gp/4+add)
print(f"{e1.name}は{damage}のダメージ")
e1.hp-=damage
line()
time.sleep(1)
if e1.hp >= 1:
add=(random.randint(10,20))
print(f"{e1.name}の攻撃!")
damage = int(e1.ap/2-p2.gp/4+add)
print(f"{p2.name}は{damage}のダメージ")
p2.hp-=damage
line()
time.sleep(1)
if p2.hp <= 0:
p2_dead()
elif e1.shoot > 0 and e1.hp > 0:
add = (random.randint(20,30))
e1.shoot -= 1
print(f"{e1.name}は{e1.w_name}で攻撃!")
damage = int(e1.ap/2-p2.gp/4+add)
print(f"{p2.name}は{damage}のダメージ")
p2.hp -= damage
line()
time.sleep(1)
if p2.hp <= 0:
p2_dead()
elif e1.hp <= 0:
e1_dead()
if e1.hp <= 0 and e2.hp <= 0:
print("全員撃退!")
line()
time.sleep(1)
elif cursor2 == "2":
if p2.hp >= 0:
#ガード確率
guard = random.uniform(0.01,1.00)
line()
print(f"{p2.name}の攻撃!")
if e2.hp <= 0:
e2_die()
elif e2.hp > 0 and guard < 0.20:
print(f"{e2.name}は防御した!")
damage = int(p2.ap/4-e2.gp/4)
print(f"{e2.name}は{damage}のダメージ")
e2.hp-=damage
line()
time.sleep(1)
elif e2.hp > 0:
add = (random.randint(10,20))
damage = int(p2.ap/2-e2.gp/4+add)
print(f"{e2.name}は{damage}のダメージ")
e2.hp-=damage
line()
time.sleep(1)
if e2.hp > 0:
add = (random.randint(10,20))
print(f"{e2.name}の攻撃!")
damage = int(e2.ap/2-p2.gp/4+add)
print(f"{p2.name}は{damage}のダメージ")
p2.hp -= damage
line()
time.sleep(1)
if p2.hp <= 0:
print("マイキーは倒れた!")
p2.hp = 0
line()
time.sleep(1)
elif e2.shoot > 0 and e2.hp > 0:
add = (random.randint(20,30))
e2.shoot -= 1
print(f"{e2.name}は{e2.w_name}で攻撃!")
damage = int(e2.ap/2-p2.gp/4+add)
print(f"{p2.name}は{damage}のダメージ")
p2.hp-=damage
line()
time.sleep(1)
if p2.hp <= 0:
p2_dead()
elif e2.hp <= 0:
e2_dead()
if e1.hp <= 0 and e2.hp <= 0:
print("全員撃退!")
line()
time.sleep(1)
if action == "b":
if cursor == "1":
rand_guard()
line()
print(f"{p1.name}は防御した!")
time.sleep(1)
if e1.hp <= 0:
e1_die()
elif e1.hp > 0:
print(f"{e1.name}の攻撃!")
damage = int(e1.ap/4-p1.gp/4+add)
p1.hp -= damage
print(f"{p1.name}は{damage}のダメージ")
line()
time.sleep(1)
if p1.hp <= 0:
p1_dead()
else:
print(f"{e1.name}は{e1.w_name}で攻撃!")
if e1.shoot <= 0:
print("弾切れです!")
e1.shoot = 0
elif per >= 0.30 and e1.shoot > 0:
add = (random.randint(20,30))
damage = int(e1.ap/4-p1.gp/4+add)
print(f"{p1.name}は{damage}のダメージ")
line()
time.sleep(1)
if p1.hp <= 0:
p1_dead()
elif cursor == "2":
rand_guard()
line()
print(f"{p1.name}は防御した!")
time.sleep(1)
if e2.hp <= 0:
e2_die()
elif e2.hp > 0:
print(f"{e2.name}の攻撃!")
damage = int(e2.ap/4-p1.gp/4+add)
p2.hp -= damage
print(f"{p1.name}は{damage}のダメージ")
line()
time.sleep(1)
if e2.hp <= 0:
e2_dead()
else:
print(f"{e2.name}は{e2.w_name}で攻撃!")
if e2.shoot <= 0:
print("弾切れです!")
e2.shoot = 0
elif per >= 0.30 and e2.shoot > 0:
add = (random.randint(20,30))
damage = int(e1.ap/4-p1.gp/4+add)
print(f"{p1.name}は{damage}のダメージ")
line()
time.sleep(1)
if p1.hp <= 0:
p1_dead()
if action2 == "b":
if cursor2== "1":
rand_guard()
line()
print(f"{p2.name}は防御した!")
time.sleep(1)
if e1.hp <= 0:
e1_die()
elif e1.hp > 0:
print(f"{e1.name}の攻撃!")
damage = int(e1.ap/4-p1.gp/4+add)
p1.hp -= damage
print(f"{p2.name}は{damage}のダメージ")
line()
time.sleep(1)
if p2.hp <= 0:
e1_dead()
else:
print(f"{e1.name}は{e1.w_name}で攻撃!")
if e1.shoot <= 0:
print("弾切れです!")
e1.shoot = 0
elif per >= 0.30 and e1.shoot > 0:
add = (random.randint(30,45))
damage = int(e1.ap/4-p2.gp/4+add)
print(f"{p2.name}は{damage}のダメージ")
line()
time.sleep(1)
if p2.hp <= 0:
p2_dead()
elif cursor2== "2":
rand_guard()
line()
print(f"{p2.name}は防御した!")
time.sleep(1)
if e2.hp <= 0:
e2_die()
elif e2.hp > 0:
print(f"{e2.name}の攻撃!")
damage = int(e2.ap/4-p2.gp/4+add)
p2.hp -= damage
print(f"{p2.name}は{damage}のダメージ")
line()
time.sleep(1)
if p2.hp <= 0:
p2_dead()
elif e2.hp <= 0:
print(f"{e2.name}は死亡している")
line()
else:
if e2.shoot <= 0:
print("弾切れです!")
e2.shoot = 0
elif per >= 0.30 and e2.shoot > 0:
add = (random.randint(30,45))
print(f"{e2.name}は{e2.w_name}で攻撃!")
damage = int(e2.ap/4-p2.gp/4+add)
print(f"{p2.name}は{damage}のダメージ")
line()
time.sleep(1)
if p2.hp <= 0:
p2_dead()
if action == "c":
if cursor == "1":
if p1.hp > 0 and p1.shoot > 0:
guard = random.uniform(0.00,1.00)
line()
print(f"{p1.name}は{p1.w_name}で攻撃!")
if e1.hp <= 0:
e1_die()
elif e1.hp > 0 and guard < 0.25:
print(f"{e1.name}は防御した!")
damage = int(p1.ap/2-e1.gp/4)
p1.shoot -= 1
print(f"{e1.name}は{damage}のダメージ")
e1.hp-=damage
line()
time.sleep(1)
if e1.hp <= 0:
e1_dead()
elif p1.shoot <= 0:
print("弾切れです!")
p1.shoot = 0
line()
elif p1.shoot > 0:
add = (random.randint(30,45))
p1.shoot -= 1
damage = int(p1.ap/2-e1.gp/4+add)
e1.hp -= damage
print(f"{e1.name}は{damage}のダメージ")
line()
time.sleep(1)
if e1.hp <= 0:
e1_dead()
if e1.hp > 0:
add = (random.randint(30,45))
print(f"{e1.name}の攻撃!")
damage = int(e1.ap/2-p1.gp/4+add)
print(f"{p1.name}は{damage}のダメージ")
p1.hp-=damage
line()
time.sleep(1)
if p1.hp <= 0:
p1_dead()
elif e1.shoot > 0 and e1.hp > 0:
add = (random.randint(30,45))
print(f"{e1.name}は{e1.w_name}で攻撃!")
if e1.shoot <= 0:
print("弾切れです!")
e1.shoot = 0
else:
damage = int(e1.ap/2-p1.gp/4+add)
print(f"{p1.name}は{damage}のダメージ")
p1.hp-=damage
line()
time.sleep(1)
if p1.hp <= 0:
p1_dead()
if e1.hp <= 0 and e2.hp <= 0:
print("全員撃退!")
line()
elif cursor== "2":
if p1.hp > 0 and p1.shoot > 0:
guard = random.uniform(0.00,1.00)
line()
print(f"{p1.name}は{p1.w_name}で攻撃!")
if e2.hp <= 0:
e2_die()
elif e2.hp > 0 and guard < 0.30:
print(f"{e2.name}は防御した!")
p1.shoot -= 1
damage = int(p1.ap/2-e2.gp/4)
print(f"{e2.name}は{damage}のダメージ")
e2.hp -= damage
line()
time.sleep(1)
if e2.hp < 0:
e2_dead()
elif p1.shoot <= 0:
print("弾切れです!")
p1.shoot = 0
elif p1.shoot > 0:
add = (random.randint(30,45))
p1.shoot -= 1
damage = int(p1.ap/2-e2.gp/4+add)
e2.hp-=damage
print(f"{e2.name}は{damage}のダメージ")
line()
time.sleep(1)
if e2.hp < 0:
e2_dead()
if e2.hp > 0:
add = (random.randint(30,45))
print(f"{e2.name}の攻撃!")
damage = int(e2.ap/2-p1.gp/4+add)
print(f"{p1.name}は{damage}のダメージ")
p1.hp-=damage
line()
time.sleep(1)
elif e2.shoot <= 0:
print("弾切れです!")
e2.shoot = 0
elif e2.shoot > 0 and e2.hp > 0:
e2.shoot -= 1
add = (random.randint(30,45))
print(f"{e2.name}は{e2.w_name}で攻撃!")
damage = int(e2.ap/2-p1.gp/4+add)
print(f"{p1.name}は{damage}のダメージ")
p1.hp -= damage
line()
time.sleep(1)
if p1.hp <= 0:
p1_dead()
if e1.hp <= 0 and e2.hp <= 0:
print("全員撃退!")
line()
if action2 == "c":
if cursor2=="1":
add = random.randint(30,45)
if p2.hp > 0 and p2.shoot > 0:
guard = random.uniform(0.00,1.00)
line()
print(f"{p2.name}は{p2.w_name}で攻撃!")
if e1.hp <= 0:
e1_die()
elif p2.shoot <= 0:
print("弾切れです!")
p2.shoot = 0
elif e1.hp > 0 and guard < 0.30:
print(f"{e1.name}は防御した!")
p2.shoot -= 1
damage = int(p2.ap/2-e1.gp/4)
print(f"{e1.name}は{damage}のダメージ")
e1.hp-=damage
line()
time.sleep(1)
if e1.hp <= 0:
e1_dead()
elif p2.shoot <= 0:
print("弾切れです!")
p2.shoot = 0
elif p1.shoot > 0:
p2.shoot -= 1
damage = int(p2.ap/2-e1.gp/4+add)
e1.hp -= damage
print(f"{e1.name}は{damage}のダメージ")
line()
time.sleep(1)
if e1.hp <= 0:
e1_dead()
if e1.hp > 0:
if e1.shoot <= 0:
print("弾切れです!")
e1.shoot = 0
elif e1.shoot > 0 and e1.hp > 0:
print(f"{e1.name}は{e1.w_name}で攻撃!")
e1.shoot -= 1
damage = int(e1.ap/2-p2.gp/4+add)
print(f"{p2.name}は{damage}のダメージ")
p2.hp -= damage
line()
time.sleep(1)
if p2.hp <= 0:
p2_dead()
if e1.hp <= 0 and e2.hp <= 0:
print("全員撃退!")
line()
elif cursor2=="2":
add = random.randint(20,30)
if p2.hp > 0 and p2.shoot > 0:
guard = random.uniform(0.00,1.00)
line()
print(f"{p2.name}は{p2.w_name}で攻撃!")
if e2.hp <= 0:
e2_die()
elif p2.shoot <= 0:
print("弾切れです!")
p2.shoot = 0
elif p2.shoot > 0:
add = (random.randint(30,45))
p2.shoot -= 1
damage = int(p2.ap/2-e2.gp/4+add)
print(f"{e2.name}は{damage}のダメージ")
e2.hp -= damage
line()
time.sleep(1)
if e2.hp <= 0:
e2_dead()
if e2.hp > 0:
print(f"{e2.name}の攻撃!")
add = (random.randint(30,45))
damage = int(e2.ap/2-p2.gp/4+add)
print(f"{p2.name}は{damage}のダメージ")
p2.hp -= damage
line()
if p2.hp <= 0:
p2_dead()
time.sleep(1)
elif e2.hp > 0 and guard < 0.30:
print(f"{e2.name}は防御した!")
damage = int(p2.ap/2-e2.gp/4)
print(f"{e2.name}は{damage}のダメージ")
e2.hp -= damage
line()
time.sleep(1)
elif e2.shoot > 0 or e2.hp > 0:
e2.shoot-=1
if e2.shoot <= 0:
print("弾切れです!")
e2.shoot = 0
elif e2.shoot > 0 and e2.hp > 0:
add = (random.randint(35,40))
print(f"{e2.name}は{e2.w_name}で攻撃!")
damage = int(e2.ap/2-p2.gp/4+add)
print(f"{p2.name}は{damage}のダメージ")
p2.hp -= damage
line()
time.sleep(1)
if p2.hp <= 0:
p2_dead()
if e1.hp <= 0 and e2.hp <= 0:
print("全員撃退!")
line()
if p1.hp <= 0:
p1.hp == 0
if p2.hp <= 0:
p2.hp == 0
if e1.hp <= 0:
e1.hp == 0
if e2.hp <= 0:
e2.hp == 0
elif action!={"a","b","c"} or action2!={"a","b","c"} or cursor!={1,2,3} or cursor2!={1,2,3}:
time.sleep(1)
print("指定のキーを入力してください")
def main():
title()
beginning()
while True:
status()
start()
do()
#グッドエンド
if e1.hp <= 0 and e2.hp <= 0:
print(f"{p1.name}:人質救出完了!\n犯罪組織の連中は撃退した!\n至急ビルから脱出する!")
print("GAME CLEAR!!")
break
#ゲームオーバー時
elif p1.hp <= 0 and p2.hp <= 0:
time.sleep(1)
print("司令官:こちら本部! 応答せよ! 応答せよ! 返事がない……。")
print("GAME OVER")
break
main()
character.py
class Character():
def __init__(self,name,hp,w_name,ap,gp,shoot,act,status):
self.name=name
self.hp=hp
self.w_name=w_name
self.ap=ap
self.gp=gp
self.shoot=shoot
self.act=act
self.status=status
class Player1(Character):
def __init__(self):
self.name="バーナード"
self.hp=200
self.w_name="サブマシンガン"
self.ap=75
self.gp=100
self.shoot=3
self.act=None
self.status=None
class Player2(Character):
def __init__(self):
self.name="マイキー"
self.hp=200
self.w_name="アサルトライフル"
self.ap=70
self.gp=90
self.shoot=3
self.act=None
self.status=None
class Enemy1(Character):
def __init__(self):
self.name="ベネット"
self.hp=200
self.w_name="二丁拳銃"
self.ap=60
self.gp=50
self.shoot=2
self.act=None
self.status=None
class Enemy2(Character):
def __init__(self):
self.name="ゴードン"
self.hp=200
self.w_name="ショットガン"
self.ap=70
self.gp=60
self.shoot=2
self.act=None
self.status=None
作成してみた感想
今回作ってみて分かったこととしては、Pythonの基礎中の基礎の事も自分でも分かっていないんだなと自覚しました。if文及びelif文の優先順位も、絶妙によく分かっていなかったので、勉強し直そうと思います。