繁体   English   中英

为什么c_score变量和p_score变量在分数上仅增加1时会无休止地增加?

[英]Why does the c_score variable and p_score variable increase unstoppably when it should just increase by one in score?

下面的用Python编写的代码*在codekulptor中编译,并没有使score()函数中的全局变量c_score和p_score仅增加1,而是无限地增加它。

*忽略任何可能引起的格式化错误,代码可以很好地编译,只是功能无法按预期工作。

import random 
import simplegui #graphic library

'''Following variables are all global variables
which are used in various capacities throughout the program. '''

message = "Welcome to Stone, Paper, Scissors"
c_message = "Computer is waiting for you to begin"
w_message = "The game hasn't begun yet"
number = 0
c_number = 0
c_score = 0
p_score = 0

'''First of the many helper functions, simulates computer
choice and prints relevant message in GUI window'''

def comp_choice():
   global c_number  #Calling global variable
   global c_message
   c_number = random.randrange(1,4) #'random' function
   if c_number == 1:
     c_message = "Computer chose Rock"
   elif c_number == 2:
     c_message = "Computer chose Paper"
   elif c_number == 3:
     c_message = "Computer chose scissors"
   return c_message
def score():
  global c_score
  global p_score
  global number
  global c_number
  difference = number - c_number
  if difference == -1 or difference == 2:
    c_score += 1
  elif difference == 1 or difference == -2:
    p_score += 1
  return str(p_score) + '      ' + str(c_score)


#Functions which code user choice buttons 
def rock():
  global message #calls for global variable
  global number
  number = 1
  message = "You chose Rock"
  '''This function call here is used to stimulate each
round, the one following this applies the logic
of the original game to determine and give results.'''
  comp_choice() 
  game_logic()

def paper():
  global number
  global message
  number = 2
  message = "You chose Paper"
  comp_choice()
  game_logic()

def scissors():
  global number
  global message
  number = 3
  message = "You chose Scissors"
  comp_choice()
  game_logic()

# Heart of the game
def game_logic():
  global number
  global c_number
  global w_message
  difference = number - c_number
  if difference == -1 or difference == 2:
     w_message = "Computer Wins!"
  elif difference == 1 or difference == -2:
     w_message = "Player Wins!"
  else:
     w_message = 'Player and Computer Tie'


def draw(canvas):
   canvas.draw_text("Player's choice: %s" % message, [0,100], 30, "Red")
   canvas.draw_text("Computer's choice: %s" % c_message, [0,125], 30, "Green")
   canvas.draw_text(w_message, [0,150], 30, "Black")
   canvas.draw_text(score(), [0,75], 30, "Black")

'''Following code registers buttons and their functions, it also creates the playing window.'''   
frame = simplegui.create_frame("Home", 700, 200)
frame.set_canvas_background('White')
button1 = frame.add_button( "Rock", rock, 100)
button2 = frame.add_button( "Paper", paper, 100)
button3 = frame.add_button("Scissors", scissors, 100)
frame.set_draw_handler(draw)
frame.start()

问题不在于您的score功能 那“正确”地工作。 问题是您每次必须绘制GUI时都要调用score()

就像调用w_message一样,而不是调用score() game_logic()函数设置一个score字符串。

作为附带说明,您应该重新考虑您的代码结构。 使用global如果你应该只有真正被使用; 否则,您应该使用参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM