У меня не работает код на питон, который должен работать как игра виселица. Код:
import random
import tkinter as tk
from tkinter import *
root = tk.Tk()
root.title("Hangman")
root.geometry("900x800")
word_list = ['banans', 'cisiņa']
guessed_word = ""
error_count = 0
burti=[]
x="AĀBCČDEĒFGĢHIĪJKĶLĻMNŅOPRSŠTUŪVZŽ"
for i in range(33):
burti.append(Button(root, text = x[i], fg = "white", bg="blue", width=3, font="Arial 20 bold"))
burti[i].place(x=550+70*(i%5), y=100+70*(i//5))
burti[i].bind(' ')
hangman_parts = ['head', 'body', 'left arm', 'right arm', 'left leg', 'right leg', 'stick_bg', 'stick_sm', 'rope', 'rope_oval']
def prepare_new_word():
global guessed_word
guessed_word = random.choice(word_list)
word_label.config(text=' '.join(['_' for i in range(len(guessed_word))]))
global error_count
error_count = 0
canvas.delete("all")
def check_letter():
letter = input_field.get().lower()
global error_count
new_guess = ''
old_guess = word_label['text']
found = False
for i in range(len(guessed_word)):
if letter == guessed_word[i]:
new_guess += letter
found = True
else:
new_guess += old_guess[i]
word_label.config(text=new_guess)
if new_guess == guessed_word:
print("You win!")
elif not found:
error_count += 1
if error_count == 10:
print("You lose!")
else:
draw_hangman()
input_field.delete(0, tk.END)
def draw_hangman():
if error_count >= len(hangman_parts):
return
part = hangman_parts[error_count]
if part == 'head':
canvas.create_oval(50, 50, 100, 100)
elif part == 'body':
canvas.create_line(75, 100, 75, 200)
elif part == 'left arm':
canvas.create_line(75, 125, 50, 150)
elif part == 'right arm':
canvas.create_line(75, 125, 100, 150)
elif part == 'left leg':
canvas.create_line(75, 200, 50, 225)
elif part == 'right leg':
canvas.create_line(75, 200, 100, 225)
elif part == 'stick_bg':
canvas.create_line(150, 225, 150, 20)
elif part == 'stick_sm':
canvas.create_line(150, 20, 100, 20)
elif part == 'rope':
canvas.create_oval(45, 125, 55, 135)
elif part == 'rope_oval':
canvas.create_oval(95, 125, 105, 135)
def handle_guess():
check_letter()
draw_hangman()
canvas = tk.Canvas(root, width=200, height=250)
canvas.pack()
word_label = tk.Label(root, text='')
word_label.pack()
input_field = tk.Entry(root)
input_field.pack()
prepare_new_word()
new_game_button = tk.Button(root, text='Новая игра', command=prepare_new_word)
new_game_button.pack()
guess_button = tk.Button(root, text='Угадать', command=handle_guess)
guess_button.pack()
root.mainloop()
Просьба убрать поле для ввода буквы слева, а также кнопку "Угадать" и сделать так, чтобы при нажатии на любую кнопку с буквой слева (не обращайте внимание на раскладу) нажатая буква проверялась на наличие слова, если эта буква есть, то чёрточка должна на неё заменятся, а если нет необходимо, чтобы рисовалась одна из частей виселицы.
Заранее благодарен за помощь
Answers & Comments
import random
import tkinter as tk
from tkinter import *
root = tk.Tk()
root.title("Hangman")
root.geometry("900x800")
word_list = ['banans', 'cisiņa']
guessed_word = ""
error_count = 0
burti=[]
x="AĀBCČDEĒFGĢHIĪJKĶLĻMNŅOPRSŠTUŪVZŽ"
for i in range(33):
burti.append(Button(root, text = x[i], fg = "white", bg="blue", width=3, font="Arial 20 bold"))
burti[i].place(x=550+70*(i%5), y=100+70*(i//5))
burti[i].bind('<Button-1>', lambda event, letter=x[i]: check_letter(letter))
hangman_parts = ['head', 'body', 'left arm', 'right arm', 'left leg', 'right leg', 'stick_bg', 'stick_sm', 'rope', 'rope_oval']
def prepare_new_word():
global guessed_word
guessed_word = random.choice(word_list)
word_label.config(text=' '.join(['_' for i in range(len(guessed_word))]))
global error_count
error_count = 0
canvas.delete("all")
def check_letter(letter):
global error_count
new_guess = ''
old_guess = word_label['text']
found = False
for i in range(len(guessed_word)):
if letter.lower() == guessed_word[i]:
new_guess += letter.lower()
found = True
else:
new_guess += old_guess[i]
word_label.config(text=new_guess)
if new_guess == guessed_word:
print("You win!")
elif not found:
error_count += 1
if error_count == 10:
print("You lose!")
else:
draw_hangman()
def draw_hangman():
if error_count >= len(hangman_parts):
return
part = hangman_parts[error_count]
if part == 'head':
canvas.create_oval(50, 50, 100, 100)
elif part == 'body':
canvas.create_line(75, 100, 75, 200)
elif part == 'left arm':
canvas.create_line(75, 125, 50, 150)
elif part == 'right arm':
canvas.create_line(75, 125, 100, 150)
elif part == 'left leg':
canvas.create_line(75, 200, 50, 225)
elif part == 'right leg':
canvas.create_line(75, 200, 100, 225)
elif part == 'stick_bg':
canvas.create_line(150, 225, 150, 20)
elif part == 'stick_sm':
canvas.create_line(150, 20, 100, 20)
elif part == 'rope':
canvas.create_oval(45, 125, 55, 135)
elif part == 'rope_oval':
canvas.create_oval(95, 125, 105, 135)
canvas = tk.Canvas(root, width=200, height=250)
canvas.pack()
word_label = tk.Label(root, text='')
word_label.pack()
prepare_new_word()
new_game_button = tk.Button(root, text='Новая игра', command=prepare_new_word)
new_game_button.pack()
root.mainloop()
Вот держи, все сделал