自作ゲームで敵キャラが防御しない……。
解決したいこと
現在Pythonで2対2のコマンドラインで動くテキストゲームを作成しているのですが、どうしても敵キャラが防御をしません。乱数を生成して、50%未満だったら防御をするというプログラムになっているはずなのですが、思うように動いてくれません。
以前作ったゲームでは問題なく防御したのですが……。
肝心のソースコード一覧
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(1)
print(f"{p1.name}、出動!")
time.sleep(1)
print(f"{p2.name}、出動!")
time.sleep(1)
print("犯人はビル4Fに立てこもっている! 直ちに攻撃せよ!")
#線を引く
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='')
#攻撃コマンド(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:
print(f"{e1.name}は既に死亡している")
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)
elif guard < 0.50:
print(f"{e1.name}は防御した!")
damage = int(p1.ap/4-e1.gp/4)
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-p1.gp/4+add)
print(f"{p1.name}は{damage}のダメージ")
p1.hp-=damage
line()
time.sleep(1)
if p1.hp <= 0:
print("バーナードは倒れた!")
p1.hp = 0
line()
time.sleep(1)
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:
print("バーナードは倒れた!")
p1.hp = 0
line()
time.sleep(1)
elif e1.hp <= 0:
print("ベネット撃退!")
e1.hp = 0
line()
time.sleep(1)
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:
print(f"{e2.name}は既に死亡している")
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)
elif guard < 0.50:
print(f"{e2.name}は防御した!")
damage = int(p1.ap/6-e1.gp/4)
print(f"{e2.name}は{damage}のダメージ")
e2.hp-=damage
line()
time.sleep(1)
if e2.hp >= 1:
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:
print("バーナードは倒れた!")
p1.hp = 0
line()
time.sleep(1)
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:
print("バーナードは倒れた!")
p1.hp = 0
line()
time.sleep(1)
elif e2.hp <= 0:
print("ゴードン撃退!")
e2.hp = 0
line()
time.sleep(1)
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:
print(f"{e1.name}は既に死亡している")
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 guard < 0.50:
print(f"{e1.name}は防御した!")
damage = int(p2.ap/4-e1.gp/4)
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:
print("マイキーは倒れた!")
p2.hp = 0
line()
time.sleep(1)
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:
print("マイキーは倒れた!")
p2.hp = 0
line()
time.sleep(1)
elif e1.hp <= 0:
print("ベネット撃退!")
e1.hp = 0
line()
time.sleep(1)
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:
print(f"{e2.name}は既に死亡している")
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)
elif guard < 0.50:
print(f"{e2.name}は防御した!")
damage = int(p2.ap/4-e2.gp/4)
print(f"{e2.name}は{damage}のダメージ")
e2.hp-=damage
line()
time.sleep(1)
if e2.hp >= 1:
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:
print("マイキーは倒れた!")
p2.hp = 0
line()
time.sleep(1)
elif e2.hp <= 0:
print("ゴードン撃退!")
e2.hp = 0
line()
time.sleep(1)
if e1.hp <= 0 and e2.hp <= 0:
print("全員撃退!")
line()
time.sleep(1)
if action == "b":
if cursor == "1":
per=random.uniform(0.01,1.00)
add = (random.randint(20,30))
line()
print(f"{p1.name}は防御した!")
time.sleep(1)
if e1.hp <= 0:
print(f"{e1.name}は死亡している")
line()
elif e1.hp > 0:
print(f"{e1.name}の攻撃!")
damage = int(e1.ap/6-p1.gp/4+add)
p1.hp -= damage
print(f"{p1.name}は{damage}のダメージ")
line()
time.sleep(1)
if p1.hp <= 0:
print("バーナードは倒れた!")
p1.hp = 0
line()
time.sleep(1)
else:
print(f"{e1.name}は{e1.w_name}で攻撃!")
if e1.shoot <= 0:
print("弾切れです!")
e1.shoot = 0
else:
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:
print("バーナードは倒れた!")
p1.hp = 0
line()
time.sleep(1)
elif cursor == "2":
per=random.uniform(0.01,1.00)
add = (random.randint(20,30))
line()
print(f"{p1.name}は防御した!")
time.sleep(1)
if e2.hp <= 0:
print(f"{e2.name}は死亡している")
line()
elif e2.hp > 0:
print(f"{e2.name}の攻撃!")
damage = int(e2.ap/6-p1.gp/4+add)
p2.hp -= damage
print(f"{p1.name}は{damage}のダメージ")
line()
time.sleep(1)
if e2.hp <= 0:
print("ゴードン撃退!")
e2.hp = 0
line()
time.sleep(1)
else:
print(f"{e2.name}は{e2.w_name}で攻撃!")
if e2.shoot<=0:
print("弾切れです!")
e2.shoot = 0
else:
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:
print("バーナードは倒れた!")
p1.hp = 0
line()
time.sleep(1)
if action2 == "b":
if cursor2== "1":
add = (random.randint(20,30))
line()
print(f"{p2.name}は防御した!")
time.sleep(1)
if e1.hp <= 0:
print(f"{e1.name}は死亡している")
line()
elif e1.hp > 0:
print(f"{e1.name}の攻撃!")
damage = int(e1.ap/6-p1.gp/4+add)
p1.hp -= damage
print(f"{p2.name}は{damage}のダメージ")
line()
time.sleep(1)
if p2.hp <= 0:
print("マイキーは倒れた!")
p2.hp = 0
line()
time.sleep(1)
else:
print(f"{e1.name}は{e1.w_name}で攻撃!")
if e1.shoot <= 0:
print("弾切れです!")
e1.shoot = 0
else:
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:
print("マイキーは倒れた!")
p2.hp = 0
line()
time.sleep(1)
elif cursor2== "2":
add = (random.randint(20,30))
line()
print(f"{p2.name}は防御した!")
time.sleep(1)
if e2.hp <= 0:
print(f"{e2.name}は死亡している")
line()
elif e2.hp > 0:
print(f"{e2.name}の攻撃!")
damage = int(e2.ap/6-p2.gp/4+add)
p2.hp -= damage
print(f"{p2.name}は{damage}のダメージ")
line()
time.sleep(1)
if p2.hp <= 0:
print("マイキーは倒れた!")
p2.hp = 0
line()
time.sleep(1)
elif e2.hp <= 0:
print(f"{e2.name}は死亡している")
line()
else:
if e2.shoot <= 0:
print("弾切れです!")
e2.shoot == 0
else:
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:
print("マイキーは倒れた!")
p2.hp == 0
line()
time.sleep(1)
if action == "c":
if cursor == "1":
if {p1.hp > 0, e1.hp >= 0}:
guard = random.uniform(0.00,1.00)
line()
print(f"{p1.name}は{p1.w_name}で攻撃!")
if e1.hp <= 0:
print(f"{e1.name}は死亡している")
line()
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:
print("ベネット撃退!")
e1.hp = 0
line()
time.sleep(1)
elif e1.hp > 0:
print(f"{e1.name}は防御した!")
damage = int(p1.ap/4-e1.gp/4)
p1.shoot -= 1
print(f"{e1.name}は{damage}のダメージ")
e1.hp-=damage
line()
time.sleep(1)
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:
print("バーナードは倒れた!")
p1.hp = 0
line()
time.sleep(1)
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/4-p1.gp/4+add)
print(f"{p1.name}は{damage}のダメージ")
p1.hp-=damage
line()
time.sleep(1)
if p1.hp <= 0:
print("バーナードは倒れた!")
p1.hp = 0
line()
time.sleep(1)
if e1.hp <= 0 and e2.hp <= 0:
print("全員撃退!")
line()
elif cursor== "2":
if {p1.hp > 0, e1.hp > 0}:
guard = random.uniform(0.00,1.00)
line()
print(f"{p1.name}は{p1.w_name}で攻撃!")
if e2.hp <= 0:
print(f"{e2.name}は死亡している")
line()
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:
print("ゴードン撃退!")
e2.hp = 0
line()
time.sleep(1)
elif e2.hp > 0 and guard < 0.30:
print(f"{e2.name}は防御した!")
p1.shoot -= 1
damage = int(p1.ap/4-e2.gp/4)
print(f"{e2.name}は{damage}のダメージ")
e2.hp-=damage
line()
time.sleep(1)
if {e2.shoot > 0 , 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)
if p1.hp <= 0:
print("バーナードは倒れた!")
p1.hp = 0
line()
time.sleep(1)
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, e1.hp > 0}:
guard = random.uniform(0.00,1.00)
line()
print(f"{p2.name}は{p2.w_name}で攻撃!")
if e1.hp <= 0:
print(f"{e1.name}は既に死亡している")
line()
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()
if e1.hp <= 0:
print("ベネット撃退!")
e1.hp = 0
line()
time.sleep(1)
elif e1.hp > 0 and guard < 0.30:
print(f"{e1.name}は防御した!")
p2.shoot -= 1
damage = int(p2.ap/4-e1.gp/4)
print(f"{e1.name}は{damage}のダメージ")
e1.hp-=damage
line()
time.sleep(1)
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}で攻撃!")
damage = int(e1.ap/4-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)
if e1.hp <= 0 and e2.hp <= 0:
print("全員撃退!")
line()
elif cursor2=="2":
add = random.randint(20,30)
if {p2.hp > 0, e2.hp > 0, p2.shoot > 0}:
guard = random.uniform(0.00,1.00)
line()
print(f"{p2.name}は{p2.w_name}で攻撃!")
if e2.hp <= 0:
print(f"{e2.name}は既に死亡している")
line()
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:
print("ゴードン撃退!")
e2.hp = 0
line()
time.sleep(1)
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.shoot <= 0:
print("弾切れです!")
p2.shoot = 0
if p2.hp <= 0:
print("マイキーは倒れた!")
p2.hp = 0
line()
time.sleep(1)
elif e2.hp > 0 and guard < 0.30:
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.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:
print("マイキーは倒れた!")
p2.hp = 0
line()
time.sleep(1)
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}:撃退完了! 至急ビルから脱出する!")
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=300
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=300
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=75
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
0 likes