pygame NameError: name 'self' is not defined but can't see indentation issue

pygame NameError: name 'self' is not defined but can't see indentation issue

我已经看到 This question,但我在我的代码中看不到任何缩进问题。如何修复错误:

NameError: name 'self' is not defined

我的代码:

import PySimpleGUI as sg
hp = 0

import pygame, random
#Let's import the Car Class
from player import Car
pygame.init()
def play():
        font = pygame.font.Font(None, 100)
        GREEN = (20, 255, 140)
        GREY = (210, 210 ,210)
        WHITE = (255, 255, 255)
        RED = (255, 0, 0)
        PURPLE = (255, 0, 255)
        YELLOW = (255, 255, 0)
        CYAN = (0, 255, 255)
        BLUE = (100, 100, 255)

        speed = 1
        colorList = (RED, GREEN, PURPLE, YELLOW, CYAN, BLUE)


        SCREENWIDTH=800
        SCREENHEIGHT=600
        lack_bar = pygame.Surface((SCREENWIDTH, 35))

        size = (SCREENWIDTH, SCREENHEIGHT)
        screen = pygame.display.set_mode(size)
        pygame.display.set_caption("Car Racing")




        #This will be a list that will contain all the sprites we intend to use in our game.
        all_sprites_list = pygame.sprite.Group()


        playerCar = Car(RED, 60, 80, 70)
        playerCar.rect.x = 160
        playerCar.rect.y = SCREENHEIGHT - 100

        car1 = Car(PURPLE, 60, 80, random.randint(50,100))
        car1.rect.x = 60
        car1.rect.y = -100

        car2 = Car(YELLOW, 60, 80, random.randint(50,100))
        car2.rect.x = 160
        car2.rect.y = -600

        car3 = Car(CYAN, 60, 80, random.randint(50,100))
        car3.rect.x = 260
        car3.rect.y = -300

        car4 = Car(BLUE, 60, 80, random.randint(50,100))
        car4.rect.x = 360
        car4.rect.y = -900


        # Add the car to the list of objects
        all_sprites_list.add(playerCar)
        all_sprites_list.add(car1)
        all_sprites_list.add(car2)
        all_sprites_list.add(car3)
        all_sprites_list.add(car4)

        all_coming_cars = pygame.sprite.Group()
        all_coming_cars.add(car1)
        all_coming_cars.add(car2)
        all_coming_cars.add(car3)
        all_coming_cars.add(car4)


        #Allowing the user to close the window...
        carryOn = True
        clock=pygame.time.Clock()

        while carryOn:
                for event in pygame.event.get():
                    if event.type==pygame.QUIT:
                        carryOn=False
                        end()
                    elif event.type==pygame.KEYDOWN:
                        if event.key==pygame.K_x:
                             playerCar.moveRight(10)
                #Check if there is a car collision
                car_collision_list = pygame.sprite.spritecollide(playerCar,all_coming_cars,False)
                for car in car_collision_list:
                    carryOn=False
                    end()
                self.text = font.render("text that should appear", True, (238, 58, 140))
                self.screen.blit(self.text, [400,300])




                keys = pygame.key.get_pressed()
                if keys[pygame.K_LEFT]:
                    playerCar.moveLeft(5)
                if keys[pygame.K_RIGHT]:
                    playerCar.moveRight(5)
                if keys[pygame.K_UP]:
                    speed += 0.05
                if keys[pygame.K_DOWN]:
                    speed -= 0.05


                #Game Logic
                for car in all_coming_cars:
                    car.moveForward(speed)
                    if car.rect.y > SCREENHEIGHT:
                        car.changeSpeed(random.randint(50,100))
                        car.repaint(random.choice(colorList))
                        car.rect.y = -200

                all_sprites_list.update()

                #Drawing on Screen
                screen.fill(GREEN)
                #Draw The Road
                pygame.draw.rect(screen, GREY, [40,0, 400,SCREENHEIGHT])
                #Draw Line painting on the road
                pygame.draw.line(screen, WHITE, [140,0],[140,SCREENHEIGHT],5)
                #Draw Line painting on the road
                pygame.draw.line(screen, WHITE, [240,0],[240,SCREENHEIGHT],5)
                #Draw Line painting on the road
                pygame.draw.line(screen, WHITE, [340,0],[340,SCREENHEIGHT],5)


                #Now let's draw all the sprites in one go. (For now we only have 1 sprite!)
                all_sprites_list.draw(screen)

                #Refresh Screen
                pygame.display.flip()

                #Number of frames per secong e.g. 60
                clock.tick(60)




def end():
        layout = [[sg.Text("Car Crash"), sg.Button("Try Again"), sg.Button("End")]]
        window = sg.Window("Car Crash!!!", layout)
        while True:
                        event, values = window.read()
                        if event == "End":
                                window.close()
                                pygame.quit()
                        if event == "Try Again":
                                window.close()
                                play()


play()

我的精灵class:

import pygame
WHITE = (255, 255, 255)

class Car(pygame.sprite.Sprite):
    #This class represents a car. It derives from the "Sprite" class in Pygame.

    def __init__(self, color, width, height, speed):
        # Call the parent class (Sprite) constructor
        super().__init__()

        # Pass in the color of the car, and its x and y position, width and height.
        # Set the background color and set it to be transparent
        self.image = pygame.Surface([width, height])
        self.image.fill(WHITE)
        self.image.set_colorkey(WHITE)

        #Initialise attributes of the car.
        self.width=width
        self.height=height
        self.color = color
        self.speed = speed

        # Draw the car (a rectangle!)
        pygame.draw.rect(self.image, self.color, [0, 0, self.width, self.height])

        # Instead we could load a proper picture of a car...
        # self.image = pygame.image.load("car.png").convert_alpha()

        # Fetch the rectangle object that has the dimensions of the image.
        self.rect = self.image.get_rect()

    def moveRight(self, pixels):
        self.rect.x += pixels

    def moveLeft(self, pixels):
        self.rect.x -= pixels

    def moveForward(self, speed):
        self.rect.y += self.speed * speed / 20

    def moveBackward(self, speed):
        self.rect.y -= self.speed * speed / 20

    def changeSpeed(self, speed):
        self.speed = speed

    def repaint(self, color):
        self.color = color
        pygame.draw.rect(self.image, self.color, [0, 0, self.width, self.height])

这是怎么回事?我正在关注 The answer to this question for how to show text on the screen (picking things up) - what have I missed? My code (except from own contributions) comes from HERE

整个回溯:

Traceback (most recent call last):

  File "C:\Users\tom\Documents\python\In development\Doge those cars\Doge those cars.py", line 154, in <module>
    play()
  File "C:\Users\tom\Documents\python\In development\Doge those cars\Doge those cars.py", line 90, in play
    self.text = font.render("text that should appear", True, (238, 58, 140))
NameError: name 'self' is not defined

追溯: 在你的文件 Doge those cars.py 第 90 行,即在 func play() 有一行表示 self.text = font.render("text that should appear", True, (238, 58, 140)) 但它不在您的 class 中,并且没有 'self' 定义。

答案在评论里说了。 self 未在您的代码中定义为函数的参数或全局变量。事实上,你的 play 函数甚至没有在 class 中定义,所以我不太确定你为什么要使用 self 关键字。