pygame NameError: name 'self' is not defined but can't see indentation issue [closed] (关闭)


我见过 这个问题 但我在我的代码中看不到任何缩进问题。我该如何解决这个错误。

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()

我的sprite类:

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])

这是怎么回事?我的代码如下 这个问题的答案是 如何在屏幕上显示文本(拾取东西)–我错过了什么?我的代码(除了自己的贡献)来自于 这里

整个回溯。

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

解决方案:

答案在评论中已经说过了。self 在你的代码中没有定义’self’,无论是作为函数的参数还是作为全局变量。事实上,你的 play 函数甚至没有在类中定义,所以我不太清楚为什么你要使用 self 关键字。

给TA打赏
共{{data.count}}人
人已打赏
未分类

在R中把邮件从一个文件夹移到另一个文件夹中

2022-9-8 7:09:39

未分类

在材料用户界面中进行类似于sass的插值,以获得类似于BEM的类名。

2022-9-8 7:09:41

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索