2013-11-26 8 views
0

Я делаю игру «Рок, бумага, ножницы» для своего класса компьютерного программирования, и мне удалось записать код, но это не так, ничего не делай. Я сравнивал его с моими друзьями, и это похоже, но его работы и мои нет. Вот мой код.Мой код не показывает ошибки, но это ничего не делает

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() 

Пожалуйста, благодарите вас за любой ответ.

ответ

0

Несколько очевидных ошибок:

В theGame() вы ничего не делаете с доходов от playerChoice(), cpuChoice() или theWinner(); вы вызываете функцию, игнорируете возвращаемое значение, затем передаете самим функциям следующей функции. Попробуйте фактически передавая их возвращаемые значения по:

textChange(theWinner(playerChoice(), cpuChoice())) 

cpuChoice() кажется излишне неуклюжим; почему бы не реорганизовывать его, просто:

def cpuChoice(): 
    return random.randint(1,3) 

Наконец, неравенство в playerChoice() неверны; например

300 < playerClickY < 200 

никогда не может быть True, так как любое число больше, чем 300 никогда не будет одновременно меньше 200.

Смежные вопросы