这是我为高中制作的数学程序的代码。我现在很挣扎,因为我不知道我做错了什么,它说 sum1
未定义。如果有人可以花时间检查我的代码并对其进行整理,我将非常感激。
我的错误在这一行:(第 130 行)
message_to_screen("什么是:"+ str(sum1) + "+ "+ str(sum2),
黑色的,
-100,
“中”)
如果我需要澄清任何事情,请告诉我。
谢谢!
# Below I am importing the modules that I will need
import pygame
import random
import time
from pygame.locals import *
# This initiates pygame
pygame.init()
# These are my defined colours
white = (255,255,255)
lightGrey = (200,200,200)
black = (0,0,0)
grey = (100,100,100)
red = (255,0,0)
yellow = (200,200,0)
green = (34,177,76)
# This sets the display width and height
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Major League Mathematics')
clock = pygame.time.Clock()
FPS = 30
easyvalues = list(range(10, 100 + 1))
mediumvalues = list(range(100, 1000 + 1))
hardvalues = list(range(1000, 10000 + 1))
# These are my set sizes for my message_to_screen definition
smallfont = pygame.font.SysFont("Arial", 30)
medfont = pygame.font.SysFont("Arial", 50)
largefont = pygame.font.SysFont("Arial", 100)
menufont = pygame.font.SysFont("Arial", 80)
# The next 3 definitions define how text is displayed
def text_objects(text,color,size):
# This is an 'if' statement
if size == "small":
textSurface = smallfont.render(text, True, color)
elif size == "medium":
textSurface = medfont.render(text, True, color)
elif size == "large":
textSurface = largefont.render(text, True, color)
elif size == "menu":
textSurface = menufont.render(text, True, color)
return textSurface, textSurface.get_rect()
def text_to_button(msg, color, buttonx, buttony, buttonwidth, buttonheight, size = "medium"):
textSurf, textRect = text_objects(msg,color,size)
textRect.center = ((buttonx + (buttonwidth / 2)), buttony + (buttonheight / 2))
gameDisplay.blit(textSurf, textRect)
def message_to_screen(msg,color,y_displace = 0,size = "small"):
textSurf, textRect = text_objects(msg,color,size)
textRect.center = (display_width / 2), (display_height / 2) + y_displace
gameDisplay.blit(textSurf, textRect)
def button(text, color, x, y, width, height, inactive_color, active_color, value = None):
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + width > cur[0] > x and y + height > cur[1] > y:
pygame.draw.rect(gameDisplay, active_color, (x, y, width, height))
if click[0] == 1 and value != None:
if value == "easy":
easyvalues = list(range(10, 100 + 1))
sum1 = random.choice(easyvalues)
sum2 = random.choice(easyvalues)
gameLoop()
if value == "medium":
mediumvalues = list(range(100, 1000 + 1))
sum1 = random.choice(mediumvalues)
sum2 = random.choice(mediumvalues)
gameLoop()
if value == "hard":
hardvalues = list(range(1000, 10000 + 1))
sum1 = random.choice(hardvalues)
sum2 = random.choice(hardvalues)
gameLoop()
else:
easyvalues = list(range(10, 100 + 1))
sum1 = random.choice(easyvalues)
sum2 = random.choice(easyvalues)
gameLoop()
else:
pygame.draw.rect(gameDisplay, inactive_color, (x, y, width, height))
text_to_button(text, black, x, y, width, height)
def question():
if value == "easy":
sum1 = random.choice(easyvalues)
sum2 = random.choice(easyvalues)
if value == "medium":
sum1 = random.choice(mediumvalues)
sum2 = random.choice(mediumvalues)
if value == "hard":
sum1 = random.choice(hardvalues)
sum2 = random.choice(hardvalues)
else:
sum1 = random.choice(easyvalues)
sum2 = random.choice(easyvalues)
print sum1
print sum2
print sum1 + sum2
print value
#Here I am trying to enable raw input from the user
def answer():
answer = ""
font = pygame.font.SysFont("Arial", 50)
pygame.draw.rect(gameDisplay, white, [200,250,400,100])
message_to_screen("What is: " + str(sum1) + " + " + str(sum2),
black,
-100,
"medium")
input = True
while input:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.unicode.isdigit():
answer += event.unicode
if event.key == K_BACKSPACE:
answer = answer[:-1]
# This is temporary, while I program the rest
elif event.key == K_RETURN:
if answer == sum1 + sum2:
message_to_screen("Correct!", green, 0, "large")
else:
message_to_screen("Wrong!", red, 0, "large")
elif event.key == pygame.K_KP_ENTER:
if answer == sum1 + sum2:
message_to_screen("Correct!", green, 0, "large")
else:
message_to_screen("Wrong!", red, 0, "large")
elif event.key == K_ESCAPE:
pygame.quit()
quit()
elif event.type == pygame.QUIT:
pygame.quit()
quit()
block = font.render("Answer: " + answer, True, black)
rect = 210,280
gameDisplay.blit(block, rect)
pygame.display.flip()
## This is a reference so that I can get my code working
##def name():
## pygame.init()
## screen = pygame.display.set_mode((480, 360))
## name = ""
## font = pygame.font.Font(None, 50)
## while True:
## for evt in pygame.event.get():
## if evt.type == KEYDOWN:
## if evt.unicode.isalpha():
## name += evt.unicode
## elif evt.key == K_BACKSPACE:
## name = name[:-1]
## elif evt.key == K_RETURN:
## name = ""
## elif evt.type == QUIT:
## return
## screen.fill((0, 0, 0))
## block = font.render(name, True, (255, 255, 255))
## rect = block.get_rect()
## rect.center = screen.get_rect().center
## screen.blit(block, rect)
## pygame.display.flip()
##def input_box():
##
## pygame.draw.rect(gameDisplay, grey, [(display_width / 2) - 150,(display_height / 2) + 160,300,80])
##
##
## for event in pygame.get():
## if event.type == pygame.KEYDOWN:
# This definition defines a start screen so that my game doesnt play straight away
def startScreen():
menu = True
while menu:
# This allows me to quit the game without any problems
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
quit()
# This takes away the menu screen and starts the game once you've pressed 'Enter'
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
menu = False
gameDisplay.fill(grey)
message_to_screen("Major League Mathematics", black, -200, "menu")
#button building
button("Easy", black, 100, 220, 150, 100, lightGrey, white, value = "easy")
button("Medium", black, 325, 220, 150, 100, lightGrey, white, value = "medium")
button("Hard", black, 550, 220, 150, 100, lightGrey, white, value = "hard")
message_to_screen("Select a difficulty to start!", black, 250, "small")
pygame.display.update()
# This is my main loop that my game will run off
def gameLoop():
gameExit = False
gameOver = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
quit()
gameDisplay.fill(grey)
answer()
pygame.display.update()
clock.tick(FPS)
pygame.quit()
quit()
startScreen()
最佳答案
在 question()
函数内部,如果值不等于 "easy"
、"medium"
或 "hard "
,它会遇到一个未定义的 sum1
变量。由于您没有具体说明在哪一行引发异常,因此这将是我最好的选择。
编辑:
好的,我之所以问您是否了解一些变量作用域概念,是因为我看到您有一些函数没有被调用,并尝试访问其作用域之外的变量。我强烈建议您进一步阅读有关变量作用域的内容,但作为直接答案,代码中的以下更改应该适合您(无法从此处测试它):
- 在您的按钮功能中
if x + width > cur[0] > x and y + height > cur[1] > y:
pygame.draw.rect(gameDisplay, active_color, (x, y, width, height))
if click[0] == 1 and value != None:
if value == "easy":
easyvalues = list(range(10, 100 + 1))
sum1 = random.choice(easyvalues)
sum2 = random.choice(easyvalues)
gameLoop(sum1, sum2)
if value == "medium":
mediumvalues = list(range(100, 1000 + 1))
sum1 = random.choice(mediumvalues)
sum2 = random.choice(mediumvalues)
gameLoop(sum1, sum2)
if value == "hard":
hardvalues = list(range(1000, 10000 + 1))
sum1 = random.choice(hardvalues)
sum2 = random.choice(hardvalues)
gameLoop(sum1, sum2)
else:
easyvalues = list(range(10, 100 + 1))
sum1 = random.choice(easyvalues)
sum2 = random.choice(easyvalues)
gameLoop(sum1, sum2)
- 在你的 gameLoop 函数中
def gameLoop(sum1, sum2):
gameExit = False
gameOver = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
quit()
gameDisplay.fill(grey)
answer(sum1, sum2)
pygame.display.update()
clock.tick(FPS)
pygame.quit()
quit()
- 在您的回答功能中
def answer(sum1, sum2):
关于python - 我的代码中的变量无法正确定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30606695/