我正在尝试创建一个石头剪刀游戏,允许用户点击“摇滚”纸张或“剪刀”按钮进行游戏。当用户点击按钮时,它应该告诉用户他们选择了什么,计算机选择了什么,以及游戏结果是什么。
而不是发生这种情况,我只是点击时没有做任何事情的按钮。它告诉我他们是未定义的,但我定义了它们所以我不知道该怎么做。
from tkinter import *
from random import randint
def main():
computer = randint(1,3)
results = show_message(player_number, computer)
print(results)
def comp():
computer = randint(1,3)
return computer
def show_message(player_number, computer):
if player_number == 1 and computer == 1:
str_var.set("player chose rock, computer chose rock: tie")
elif player_number == 2 and computer == 2:
str_var.set("player chose paper, computer chose rock: tie")
elif player_number == 3 and computer == 3:
str_var.set("player chose scissors, computer chose scissors: tie")
elif player_number == 1 and computer == 2:
str_var.set("player chose rock, computer chose paper: computer wins")
elif player_number == 1 and computer == 3:
str_var.set("player chose rock, computer chose scissors: player wins")
elif player_number == 2 and computer == 1:
str_var.set("player chose paper, computer chose rock: player wins")
elif player_number == 2 and computer == 3:
str_var.set("player chose paper, computer chose scissors: computer wins")
elif player_number == 3 and computer == 1:
str_var.set("player chose scissors, computer chose rock: computer wins")
else:
str_var.set("player chose scissors, computer chose paper: player wins")
#functions to set message
def rock_clicked(event=None):
player_number = 1
computer = randint(1,3)
show_message(player_number, computer)
def paper_clicked(event=None):
player_number = 2
computer = randint(1,3)
show_message(player_number, computer)
def scissors_clicked(event=None):
player_number = 3
computer = randint(1,3)
show_message(player_number, computer)
root = Tk()
root.title("MY FIRST GAME")
root.geometry("300x150")
#variables
str_var = StringVar()
str_var.set("")
player_number = IntVar()
#make top frame
top_frame = Frame(root)
top_frame.pack(side=TOP)
top_label = Label(top_frame, text="ROCK PAPER SCISSORS", bg="blue")
top_label.pack()
#make bottom frame
bottom_frame = Frame(root)
bottom_frame.pack(side=BOTTOM)
button_rock = Button(top_frame, text="rock")
button_rock.bind("<Button-1>", rock_clicked)
button_paper = Button(top_frame, text="paper")
button_paper.bind("<Button-1>", paper_clicked)
button_scissors = Button(top_frame, text="scissors")
button_scissors.bind("<Button-1>", scissors_clicked)
button_rock.pack(side=LEFT, padx=10, pady=30)
button_paper.pack(side=LEFT, padx=10, pady=30)
button_scissors.pack(side=LEFT, padx=10, pady=30)
Label(top_frame, textvariable=str_var).pack(side=BOTTOM, padx=10, pady=40)
main()
root.mainloop()