2016-10-28 135 views
0

我已经被要求用我的表兄弟的Pygame代码发现问题。我对Python并不擅长,使用其他语言,我一直无法通过搜索或调试来发现问题。基本上他得到一个playGame未定义”错误,playGame是一个函数。这个其他问题通常是因为:未定义Python pygame函数

  1. 函数被调用时声明
  2. 功能不同的范围从它被称为内声明的前

这些都不似乎是这个问题,所以我希望有人更熟悉Python可以发现它。我在下面复制了他的代码,其中很多内容(我希望)与简化的问题无关。

功能playGame不起作用,并通过点击按钮调用 def button(msg, x, y, action = None): 。有趣的是,退出函数工作正常,据我所知,它被调用并声明与playGame完全一样。

# --------------- SETUP --------------- 
# Importing necessary modules 
import pygame 
import math 
import random 
# --------------- DISPLAY --------------- 
# Setting up the display 
pygame.init() 
screen = pygame.display.set_mode((width, height)) 
pygame.display.set_caption(title) 
# --------------- GLOBALS --------------- 
#removed globals from stackoverflow version 


# --------------- FUNCTIONS --------------- 


# Blitting the text 
def blitText(angle, power): 
    #code 


# Drawing the tank model 
def drawTank(): 
    #code 


# Creating the buttons 
def button(msg, x, y, action = None): 
    mousePos = pygame.mouse.get_pos()                   # Gets the mouse position 
    mouseClick = pygame.mouse.get_pressed() 

    (buttonWidth, buttonHeight) = (175, 45)                  # Sets the button width and height 
    if x + (buttonWidth/2) > mousePos[0] > x - (buttonWidth/2) and y + buttonHeight > mousePos[1] > y:  # Checks if the mouse is over the button 
     pygame.draw.rect(screen, darkGrey, [x - (buttonWidth/2), y, buttonWidth, buttonHeight])     # Draws a dark grey button 
     if mouseClick[0] == 1 and action != None:                 # Checks if the button is clicked 
      if action == "play": 
       playGame() 
      elif action == "exit": 
       exit() 
    else: 
     pygame.draw.rect(screen, grey, [x - (buttonWidth/2), y, buttonWidth, buttonHeight])      # Draws a light grey button if not 

    screen.blit(msg, [x - (buttonWidth/2), y])                # Writes the text over the button 


# Defining the shell 
class shell(pygame.sprite.Sprite):    # Creates the shell() class 
    def __init__(self):        # Defines an initiation fuction for this class 
     super().__init__()        # Call the parent class constructor 
     self.image = pygame.Surface([2, 2])    # Defines the bullet as a 2x4 surface 
     self.image.fill(black)       # Paints the bullet black 
     self.rect = self.image.get_rect()    # Gets the area size of the bullet 

    def update(self):                                # Defines a function as update for this class 
     (bulletChangeX, bulletChangeY) = (((maxAngle - angle)/maxAngle) * (bulletSpeed * power), (angle/maxAngle) * (bulletSpeed * power))   # Ccalculates the changes in x and y 
     bulletChangeY -= vert                               # Changes the trajectory of the bullet 
     self.rect.y -= bulletChangeY                             # Moves the bullet in the y axis 
     self.rect.x += bulletChangeX                             # Moves the bullet in the x axis 


# --------------- TITLE SCREEN --------------- 
# Creating the main menu 
menu = True 
while menu:              # Starts the loop 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT:         # Checks if pygame has been closed 
      exit()               # Exits python 
    screen.fill(white)            # Fills the screen white 
    screen.blit(titleText, [0, 0])         # Writes the title text 

    button(startButton, width/2, (height/3) * 2, "play")  # Calls the button function for the start button 
    button(exitButton, width/2, ((height/3) * 2) + 70, "exit") # Calls the button function for the exit button 

    # Updating the display 
    pygame.display.update()           # Updates the display 
    clock.tick(fps) 
# --------------- MAIN LOOP --------------- 


# Running the program 
def playGame(): 
    #code. This function has no return value. 


# --------------- EXIT --------------- 


# Exits PyGame and Python 
def exit(): 
    pygame.quit() 
    quit() 

希望的错误是明显的在这里的人,我还没有删除,导致该问题的任何密码(我删除开始变量声明和功能代码的内容),我可以提供完整的代码,如果人需要它。

+1

单击按钮时会调用它: def按钮(msg,x,y,action = None): 定义游戏高于一切是我已经尝试过的一种方式,它不起作用。 – Sparks

+0

是的,刚发现它。那么,在哪里使用“按钮”?它似乎应该被用作回调,但也许你正在调用它,例如你的代码中有'command = button()'而不是'command = button'。 –

+0

请注意,'exit()'是Python中的一个内置函数,因此找到'exit'这个事实并不能告诉任何事情;它可能只是使用内置。 –

回答

0

是,themistake是显而易见的 - 如你所说的那样:

鳕鱼e为试图调用定义之前它的函数 - 的while menu代码绘制菜单屏幕,并绘制了按钮前放置playGame函数 - 在那个点上哪个名字是未声明的。

虽然Python确实在顶层模块上运行代码,但最好的做法是在顶层只留下一些常量和变量声明,并将类似块while menu: ...的代码放入函数中。 (可能被称为main - 但其名称没有顶级语言要求)

然后,在文件的最底部,调用该函数,并调用该函数 - 这次是正确的,在模块体 -

所以 - 沿东西:

def main(): 
    # Creating the main menu 
    menu = True 
    while menu:              # Starts the loop 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT:         # Checks if pygame has been closed 
       exit()               # Exits python 
     screen.fill(white)            # Fills the screen white 
     screen.blit(titleText, [0, 0])         # Writes the title text 

     button(startButton, width/2, (height/3) * 2, "play")  # Calls the button function for the start button 
     button(exitButton, width/2, ((height/3) * 2) + 70, "exit") # Calls the button function for the exit button 

     # Updating the display 
     pygame.display.update()           # Updates the display 
     clock.tick(fps) 

,并在最底部,将单main()电话会提出这样的特定错误消失。