Ich wurde gebeten, ein Problem mit dem Pygame-Code meines Cousins zu finden. Ich bin nicht groß auf Python, andere Sprachen mehr zu verwenden und ich war nicht in der Lage, das Problem durch Googlen oder Debuggen zu finden. Grundsätzlich bekommt er eine "playGame
ist nicht definiert" Fehler, playGame
ist eine Funktion. Weitere Fragen zu diesem Thema sind in der Regel, weil:Python pygame Funktion nicht definiert
- Die Funktion aufgerufen wird, bevor es deklariert wird
- Die Funktion in einem anderen Bereich deklariert wird, aus dem es
Beides genannt wird, scheint zu sein, Das Problem, also hoffe ich, dass jemand, der mit Python vertrauter ist, es erkennen kann. Ich habe seinen Code unten mit vielem kopiert, was (hoffentlich) irrelevant ist, um die Frage zu vereinfachen.
Die Funktion playGame
nicht funktioniert und wird durch Klicken auf eine Schaltfläche unter def button(msg, x, y, action = None):
genannt. Interessanterweise funktioniert die Exit-Funktion gut, die genau so heißt und deklariert wird wie playGame
, soweit ich das beurteilen kann.
# --------------- 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()
Hoffentlich wird der Fehler auf jemandem offensichtlich hier ist, und ich habe keinen Schlüsselcode entfernt, die die Probleme verursacht (entfernte ich Variablendeklarationen und den Inhalt des Funktionscode starten) ich den vollständigen Code, wenn die Menschen zur Verfügung stellen kann brauchen.
Es wird aufgerufen, wenn auf eine Schaltfläche geklickt wird: Def-Taste (msg, x, y, action = None): Definieren des Spiels über alles andere ist etwas, was ich bereits versucht habe und es hat nicht funktioniert. – Sparks
Ja, gerade gefunden. Also, wo wird 'Knopf' verwendet? Es scheint, dass es als ein Rückruf verwendet werden sollte, aber vielleicht rufen Sie es stattdessen, z. Sie haben 'command = button()' anstelle von 'command = button' irgendwo in Ihrem Code. –
Beachten Sie, dass 'exit()' eine eingebaute Funktion in Python ist, so dass die Tatsache, dass 'exit' gefunden wird, nichts aussagt; Es ist wahrscheinlich nur mit dem eingebauten. –