我已經被要求用我的表兄弟的Pygame代碼發現問題。我對Python並不擅長,使用其他語言,我一直無法通過搜索或調試來發現問題。基本上他得到一個「playGame
未定義」錯誤,playGame
是一個函數。這個其他問題通常是因爲:未定義Python pygame函數
- 函數被調用時聲明
- 功能不同的範圍從它被稱爲內聲明的前
這些都不似乎是這個問題,所以我希望有人更熟悉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()
希望的錯誤是明顯的在這裏的人,我還沒有刪除,導致該問題的任何密碼(我刪除開始變量聲明和功能代碼的內容),我可以提供完整的代碼,如果人需要它。
單擊按鈕時會調用它: def按鈕(msg,x,y,action = None): 定義遊戲高於一切是我已經嘗試過的一種方式,它不起作用。 – Sparks
是的,剛發現它。那麼,在哪裏使用「按鈕」?它似乎應該被用作回調,但也許你正在調用它,例如你的代碼中有'command = button()'而不是'command = button'。 –
請注意,'exit()'是Python中的一個內置函數,因此找到'exit'這個事實並不能告訴任何事情;它可能只是使用內置。 –