我正在尝试在乌龟中创建剪刀石头布游戏。玩家在电脑前玩游戏,他需要通过按“ r”,“ p”或“ s”来捡石头或剪刀。在将键链接到返回选择的功能时,我发现了问题。
我正在使用一些形状和文字来向播放器提供信息。它是如何工作的,我创建了3个返回石头或剪刀的函数,并且应该使用.listen()和onkeypress()+一些if语句将它们链接到键“ r”,“ p”,“ s”。
我收到错误:
Exception in Tkinter callback Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 1705, in __call__ return self.func(*args) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/turtle.py", line 701, in eventfun fun() TypeError: 'str' object is not callable Exception in Tkinter callback Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 1705, in __call__ return self.func(*args) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/turtle.py", line 701, in eventfun fun()
我的代码:
import turtle
import random
#Creating the screen
wn = turtle.Screen()
wn.title('Rock Paper Scissors')
wn.bgcolor('black')
wn.setup(800, 600)
wn.tracer(0)
#Shape 1
rock = turtle.Turtle()
rock.shape('square')
rock.goto(170,100)
rock.speed(0)
rock.color('#964B00')
rock.shapesize(stretch_wid=5, stretch_len=5)
rock.penup()
#Shape 2
paper = turtle.Turtle()
paper.shape('square')
paper.goto(-170,100)
paper.speed(0)
paper.color('white')
paper.shapesize(stretch_wid=5, stretch_len=5)
paper.penup()
#Shape 3
scissors = turtle.Turtle()
scissors.shape('square')
scissors.goto(0,-170)
scissors.speed(0)
scissors.color('blue')
scissors.shapesize(stretch_wid=5, stretch_len=5)
scissors.penup()
#Top text, winner.
pen = turtle.Turtle()
pen.speed(0)
pen.color('white')
pen.penup()
pen.hideturtle()
pen.goto(0, 240)
pen.write("Who wins: ", align="center", font=("Courier", 24))
#Rock choice.
pen1 = turtle.Turtle()
pen1.speed(0)
pen1.color('white')
pen1.penup()
pen1.hideturtle()
pen1.goto(170, 152)
pen1.write('Rock[R]', align="center", font=("Courier", 16))
#Papper choice
pen2 = turtle.Turtle()
pen2.speed(0)
pen2.color('white')
pen2.penup()
pen2.hideturtle()
pen2.goto(-168, 152)
pen2.write('Paper[P]', align="center", font=("Courier", 16))
#Scissors choice
pen3 = turtle.Turtle()
pen3.speed(0)
pen3.color('white')
pen3.penup()
pen3.hideturtle()
pen3.goto(0, -118)
pen3.write('Scissors[S]', align="center", font=("Courier", 16))
#Player choosing the rock,paper,scissors by pressing specific keys.
def rockfn():
player_pick = 'rock'
return player_pick.title()
def paperfn():
player_pick = 'paper'
return player_pick.title()
def scissorsfn():
player_pick = 'scissors'
return player_pick.title()
#Computer random choice
r = 'Rock'
p = 'Paper'
s = 'Scissors'
choice = [r, p, s]
computer_choice = random.choice(choice)
#Keys for player choice.
wn.listen()
wn.onkeypress(rockfn, 'r')
wn.onkeypress(paperfn, 'p')
wn.onkeypress(scissorsfn, 's')
#Main Loop
while True:
wn.update()
if rockfn() or paperfn() or scissorsfn() == computer_choice:
pen.clear()
pen.write("It's a draw", align="center", font=("Courier", 24))
if wn.onkeypress(rockfn(), 'r'):
if computer_choice == 'paper'.title():
pen.clear()
pen.write("Computer picked {}, you lost.".format(p.title()), align="center", font=("Courier", 24))
else:
pen.clear()
pen.write('Computer picked {}, you win.'.format(s.title()), align="center", font=("Courier", 24))
if wn.onkeypress(paperfn(), 'p'):
if computer_choice == 'scissors'.title():
pen.clear()
pen.write('Computer picked {}, you lost.'.format(s.title()), align="center", font=("Courier", 24))
else:
pen.clear()
pen.write('Computer picked {}, you win.'.format(r.title()), align="center", font=("Courier", 24))
if wn.onkeypress(scissorsfn(), 's'):
if computer_choice == 'rock'.title():
pen.clear()
pen.write('Computer picked {}, you lost.'.format(r.title()), align="center", font=("Courier", 24))
else:
pen.clear()
pen.write('Computer picked {}, you win.'.format(p.title()), align="center", font=("Courier", 24))
我正在尝试在乌龟中创建剪刀石头布游戏。玩家在电脑前玩游戏,他需要通过按“ r”,“ p”或“ s”来捡石头或剪刀。我在链接键时发现问题...
在onkeypress
中,您需要指定功能名称。您已包括括号,因此您正在calling
与scissorsfn()
相对于scissorsfn
的第一个参数的问题无关,此代码将不起作用。 wn.onkeypress()
始终返回wn.onkeypress()
,因此永远不会执行此代码: