Reputation: 2346
What I'm trying to do: I'm trying to create turn-based fighting game with discord.py. While the health of both players is more than 0, the message author and the mentioned user would take turns either by using punch, heal, or surrender (self-explanatory).
Problem: I'm unsure where to start when it comes to incorporating the switching of turns. Below I've posted the entirety of the code I used for the message author, but I can't seem to find a way to make the next turn happen once the message author has taken a turn, and vice versa.
@client.command()
async def battle(ctx, user: discord.Member):
aut = 5
ops = 5
user = user.mention
cmam = ctx.message.author.mention
if user == cmam:
await ctx.send(f"You can't fight yourself {cmam} <:BlobWuh:761383059349438464>")
else:
await ctx.send(f'{cmam} vs {user}, who will win?')
while aut > 0 and ops > 0:
await ctx.send(f'{cmam}: `Punch, Heal, Surrender`')
def check(m):
return m.content == 'punch' or m.content == 'heal' or m.content == 'surrender' and m.author == ctx.message.author
response = await client.wait_for('message', check = check)
if "punch" in response.content.lower():
if ops > 0:
dmg = [0, 1, 2]
dmg = (random.choice(dmg))
ops = ops - dmg
await ctx.send(f"{user} is down to **{ops}** health")
if ops <= 0:
await ctx.send(f"**{cmam} has won the battle**")
break
elif ops <= 0:
await ctx.send(f"**{cmam} has won the battle**")
break
if "heal" in response.content.lower():
if aut >= 5:
await ctx.send(f"{cmam} already has **{aut}** health, they lose a turn")
else:
aut = aut + 1
await ctx.send(f"{cmam} now has **{aut}** health")
if "surrender" in response.content.lower():
await ctx.send(f"{cmam} has surrendered with **{aut}** health. {user} has claimed victory with **{ops}** health remaining")
aut = aut - 20
ops = ops - 20
break
Upvotes: 0
Views: 1384
Reputation: 2346
Okay so this is for anyone who stumbles onto this again, it's mostly the same code but there's a turn variable to store the current turn. (Thank you @Ethan M-H for the advice)
@client.command()
async def test(ctx, user: discord.Member):
turn = 0 #adding a turn variable for turn taking
aut = 5
ops = 5
user = user.mention
cmam = ctx.message.author.mention
if user == cmam:
await ctx.send(f"You can't fight yourself {cmam}")
else:
await ctx.send(f"{cmam} vs {user}, who will win?")
while aut > 0 and ops > 0:
if turn == 0:
await ctx.send(f"{cmam}: `test`")
def check(m):
return m.content == "test" and m.author == ctx.message.author
response = await client.wait_for('message', check = check)
if "test" in response.content.lower() and turn == 0:#turn == 0 is here since it doesn't work sometimes
await ctx.send("a nice test")
turn = turn + 1
elif turn == 1:
await ctx.send(f"{user}: `test`")
def check(o):
return o.content == "test" and o.author == discord.Member
response = await client.wait_for('message', check = check)
if "test" in response.content.lower() and turn == 1:#turn == 1 is here since (elif turn == 1) doesn't work sometimes
await ctx.send("the test is strong with this one")
turn = turn - 1
Upvotes: 1