【发布时间】:2019-11-08 20:56:50
【问题描述】:
我正在尝试在海龟中创建一个石头剪刀布游戏。玩家与电脑对战,他需要通过按“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))
【问题讨论】:
标签: python python-3.x turtle-graphics