我的代码没有显示错误,但它没有做任何事情

时间:2013-11-26 05:11:54

标签: python-2.7

我正在为我的计算机编程课程制作一个Rock,Paper,Scissors游戏,我已经设法将代码写下来,但它没有做任何事情。我将它与我的朋友进行了比较,它类似,但他的作品和我的作品却没有。这是我的代码。

from graphics import *
import random

win = GraphWin("Rock,Paper,Scissors Visual",500,350)
playerScore = 0
cpuScore = 0

#Basic UI
playerLabel = Text(Point(100,50),"Player")
playerLabel.draw(win)
playerPoints = Text(Point(150,50),playerScore)
playerPoints.draw(win)
cpuLabel = Text(Point(400,50),"CPU")
cpuLabel.draw(win)
cpuPoints = Text(Point(350,50),cpuScore)
cpuPoints.draw(win)


#Text
textLine1 = "You're playing Rock, Paper, Scissors."
textLine2 = "Click on your first choice."
text1 = Text(Point(250,100),textLine1)
text1.draw(win)
text2 = Text(Point(250,120),textLine2)
text2.draw(win)


#Positioning Rock, Paper, and Scissors, aswell as drawing them.
rock = Image(Point(90,250),"rockhue.gif")
rock.draw(win)
rockContainer = Rectangle(Point(26,300),Point(154,200))
rockContainer.draw(win)
paper = Image(Point(220,250),"paperhue.gif")
paper.draw(win)
paperContainer = Rectangle(Point(180,300),Point(260,200))
paperContainer.draw(win)
scissors = Image(Point(390,250),"scissorshue.gif")
scissors.draw(win)
scissorsContainer = Rectangle(Point(286,300),Point(494,200))
scissorsContainer.draw(win)


def playerChoice():
    playerClick = win.getMouse()
    playerClickX = playerClick.getX()
    playerClickY = playerClick.getY()
#Rock region
    if 26 < playerClickX < 154 and 300 < playerClickY < 200:
        return "1"

#Paper region
    elif 180 < playerClickX < 260 and 300 < playerClickY < 200:
        return "2"

#Scissors region
    elif 286 < playerClickX < 494 and 300 < playerClickY < 200:
        return "3"


#Randomly generated choice for computer.
def cpuChoice():
    choice = random.randint(1,3)
    if choice==1:
        return "1" 
    elif choice==2:
        return "2"
    elif choice==3:
        return "3"

#Decides winner.
def theWinner(playerChoice, cpuChoice):
    if playerChoice == cpuChoice:
        return "0"
    elif playerChoice == "1": #rock
        if cpuChoice == "2":
            return "2"
        if cpuChoice == "3":
            return "1"
    elif playerChoice == "2": #paper
        if cpuChoice == "1":
            return "1"
        if cpuChoice == "3":
            return "2"
    elif playerChoice == "3": #scissors
        if cpuChoice == "1":
            return "2"
        if cpuChoice == "2":
            return "1"

#Changes text and adds score.
def textChange(theWinner):
    if theWinner == "0":
        text1.undraw(win)
        text2.undraw(win)
        textLine1 = "You and the Computer had the same Choice."
        textLine2 = "It's a Tie, no one gains any points."
        text1 = Text(Point(250,100),textLine1)
        text1.draw(win)
        text2 = Text(Point(250,120),textLine2)
        text2.draw(win)
    elif theWinner == "1":
        text1.undraw(win)
        text2.undraw(win)
        textLine1 = "You out picked the Computer."
        textLine2 = "You win, and also gain 1 point."
        text1 = Text(Point(250,100),textLine1)
        text1.draw(win)
        text2 = Text(Point(250,120),textLine2)
        text2.draw(win)
        playerScore = playerScore + 1
        playerPoints.undraw(win)
        playerPoints = Text(Point(150,50),playerScore)
        playerPoints.draw(win)
    elif theWinner == "2":
        text1.undraw(win)
        text2.undraw(win)
        textLine1 = "The Computer out picked you."
        textLine2 = "You lose, and the computer gains 1 point."
        text1 = Text(Point(250,100),textLine1)
        text1.draw(win)
        text2 = Text(Point(250,120),textLine2)
        text2.draw(win)
        cpuScore = cpuScore + 1
        cpuPoints.undraw(win)
        cpuPoints = Text(Point(350,50),cpuScore)
        cpuPoints.draw(win)

#Groups up all functions into a game
def theGame():
    while True:
        playerChoice()
        cpuChoice()
        theWinner(playerChoice, cpuChoice)
        textChange(theWinner)

theGame()

请感谢您的回复。

1 个答案:

答案 0 :(得分:0)

一些明显的错误:

theGame() playerChoice()您对cpuChoice()theWinner()textChange(theWinner(playerChoice(), cpuChoice())) 的回复没有做任何事情;你调用函数,忽略返回值,然后将函数本身传递给下一个函数。尝试实际传递其返回值:

cpuChoice()

def cpuChoice(): return random.randint(1,3) 似乎不必要地笨拙;为什么不重构它,简单地说:

playerChoice()

最后,300 < playerClickY < 200 中的不等式是不正确的;例如

True

永远不会是{{1}},因为任何大于300的数字都不会同时小于200.