python - Discord.py hangman game - problem with bot recognizing positions of guessed letters
问题描述
Im trying to create a hangman game in discord.py. Now i have been trying to figure out how to continue. The bot simply chooses random word from a list and then sends embed message with instructions and image of hangman (the image below is just for example, there will be only ground at the beginning of course) and finally number of underscores depending on lenght of the word. (wanted to do it with for loop but it doesnt work because the message wouldnt be in one line). Now i dont know how to continue. For example the word is beer and someone types e so the outcome should be that the bot sends another message but this time with e letters (__ e e __) but i dont know how to make the bot recognize where are the positions of the guessed letter and then how to send it.
words = ["car","beer","patient",......]
@bot.command()
@commands.cooldown(1, 60.0, commands.BucketType.guild)
async def hangman(msg):
if msg.channel.id != channel:
return
guessed_letters = []
word = str(random.choice(words))
word_len = len(word)
e = discord.Embed(color=3447003)
e.set_image(url="https://i.imgur.com/nYgwk8M.png")
await msg.send("**HANGMAN** (Guess by sending letters)",embed=e)
if word_len == 3:
await msg.send("**__** **__** **__**")
if word_len == 4:
await msg.send("**__** **__** **__** **__**")
if word_len == 5:
await msg.send("**__** **__** **__** **__** **__**")
if word_len == 6:
await msg.send("**__** **__** **__** **__** **__** **__**")
if word_len == 7:
await msg.send("**__** **__** **__** **__** **__** **__** **__**")
if word_len == 8:
await msg.send("**__** **__** **__** **__** **__** **__** **__** **__**")
if word_len == 9:
await msg.send("**__** **__** **__** **__** **__** **__** **__** **__** **__**")
def checkhangman(message):
return message.content in ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
guess = await bot.wait_for("message", check=checkhangman)
if str(guess) in word:
guessed_letters.append(str(guess))
#For example someone types e and the word is beer, so the outcome will be:
await msg.send("**__** **e** **e** **__**")
...
#And then the game continues
else:
#Here if the guess is not in the word, the bot will send another image of hangman and the game continues
e = discord.Embed(color=3447003)
e.set_image(url="https://i.imgur.com/nYgwk8M.png")
...
解决方案
This isn't related to the question, but those if-statements
for the length hurt me a bit. You don't have to hardcode every single possible length in there, you're better off just joining a list of them with a space.
I'm not sure what you mean with
doesn't work because message wouldn't be in one line
because you can just add them to a string without newlines & then it'll be one line.
underscores = "**" + " ".join(["__" for _ in range(word_len)]) + "**"
In order to get the letters in there, you can just iterate over your word and add an underscore if the current letter has not been guessed yet. If the letter has been guessed, you can show the letter.
word = "beer" # The current word
guessed_letter = "e" # Currently guessed letter
guessed = ["a", "c", "d"] # List of all guessed letters
l = [] # List of characters to join back into a string
guessed.append(guessed_letter) # Add guessed letter to the list of letters
for letter in word: # Check every letter in the word to see if it has been guessed
if letter in guessed:
l.append(letter) # Show the letter if it has been guessed
else:
l.append("\_\_") # Otherwise show an underscore, EDIT: escape them
# "l" now contains the string that should be sent back
underscores = "**" + " ".join(l) + "**"
await msg.send(underscores)
You can also use string += string
instead of string.join()
, but then you'll have to remove an extra space that was added at the end. No big deal, but I prefer using join
to make my life easier.
EDIT:
__a__ is markdown syntax to underline something on Discord, so you have to escape the underscores using backslashes.
推荐阅读
- c# - 我在 Windows 窗体 C# 中打印收据时遇到问题
- xaml - WPF Style.Triggers 的 UWP 等效项
- amcharts - 强制更改标签文本颜色
- amazon-web-services - AWS ECS 容器状态已停止(退出代码 12)但运行状况良好
- ruby - Ruby 继承和类型
- python - 将多标签列转换为pyspark中的多列?
- pyodbc - 调用 iSeries 存储过程的 pyodbc 错误
- ios - 架构 x86_64 颤动的未定义符号
- hadoop - 如何从开源 Hadoop 构建 deb/rpm 存储库或由 ambari 安装的公开可用的 HDP 源代码
- rabbitmq - Spring AMQP RabbitListener 与 2 个 Rabbit Clusters 连接