Ошибка атрибута: у объекта 'Ball' нет атрибута 'x'

1

У меня есть ошибка атрибута, которая мешает мне продолжать, кто-нибудь может ее отладить. Я пробовал перемещать вещи в классе мяча, но ничего не сработало. Я застрял.

        import random
        import pygame

        class Ball(pygame.sprite.Sprite):
            def __init__(self, x, y):
                pygame.sprite.Sprite.__init__(self)
                self.image = pygame.Surface((100, 100)).convert_alpha()
                self.image.fill((160, 70, 0))
                self.rect = pygame.Rect(self.x, self.y, 100, 100)
                self.x = self.rect.x
                self.y = self.rect.y
                self.speed_x = 0
                self.speed_y = 0
                self.radiusx = 0
                self.radiusy = 100
                self.mask = pygame.mask.from_surface(self.image)
                # Pass the midbottom position as an argument to 'get_rect'.
                self.rect = self.image.get_rect(midbottom=(x, y))
                # Set the x and y attributes afterwards.
                # Alternatively.
                #self.rect.midbottom = x, y

            def update(self, screen_rect):  # Pass a rect with the size of the screen.
                self.x += self.speed_x
                self.y += self.speed_y
                self.rect.topleft = (self.x, self.y)
                if not screen_rect.contains(self.rect):
                    # Clamp the rect if it outside of the screen.
                    self.rect.clamp_ip(screen_rect)
                    self.x, self.y = self.rect.topleft

            def render(self, screen):
                screen.blit(self.image, self.rect)

Ошибка: Traceback (последний последний вызов): Файл "/Users/maxi/Desktop/Pygame_Resources/codingandcaring github/Qaru Help.py", строка 233, в главном() файле "/Users/maxi/Desktop/Pygame_Resources/codingandcaring github/Qaru Help.py ", строка 101, в основном баскетболе = Ball (start_x, start_y) Файл"/Users/maxi/Desktop/Pygame_Resources/codingandcaring github/Qaru Help.py ", строка 9, в init self.rect = pygame.Rect(self.x, self.y, 100, 100) AttributeError: объект "Ball" не имеет атрибута "x"

        class Goal(pygame.sprite.Sprite):
            def __init__(self, x, y):
                pygame.sprite.Sprite.__init__(self)
                # self.image = pygame.image.load('goal.png').convert_alpha()
                # self.image = pygame.transform.scale(self.image, (220, 220))
                self.image = pygame.Surface((220, 220)).convert_alpha()
                self.image.fill((60, 80, 110))
                self.x = x
                self.y = y
                self.rect = pygame.Rect(self.x, self.y, 220, 220)

            def render(self, screen):
                screen.blit(self.image, self.rect)


        class Ring(pygame.sprite.Sprite):
            def __init__(self, x, y):
                pygame.sprite.Sprite.__init__(self)
                # self.image = pygame.image.load('ring.png').convert_alpha()
                # self.image = pygame.transform.scale(self.image, (400, 400))
                self.image = pygame.Surface((400, 400)).convert_alpha()
                self.image.fill((60, 180, 110))
                self.x = x
                self.y = y
                self.rect = pygame.Rect(self.x, self.y, 400, 400)
                self.mask = pygame.mask.from_surface(self.image)

            def render(self, screen):
                screen.blit(self.image, self.rect)


        class Baddie(pygame.sprite.Sprite):
            def __init__(self, x, y, z):
                # self.image = pygame.image.load().convert_alpha()
                # self.image = pygame.transform.scale(self.image, (220, 220))
                self.image = pygame.Surface((90, 90)).convert_alpha()
                self.image.fill((250, 50, 0))
                self.rect = self.image.get_rect()
                self.x = x
                self.y = y

            def render(self, screen):
                screen.blit(self.image, (self.x, self.y))

        def pause():
            while True:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        pygame.quit()
                        sys.exit()
                    elif event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_ESCAPE:
                            return

        def main():
            width = 1200
            height = 722

            pygame.init()
            screen = pygame.display.set_mode((width, height))
            screen_rect = screen.get_rect()
            clock = pygame.time.Clock()  # Add a clock to limit the frame rate.
            pygame.display.set_caption('Basketball Shootout')
            font = pygame.font.Font(None, 25)
            start_x, start_y = screen_rect.midbottom
            basketball = Ball(start_x, start_y)
            snd_file = 'Game.ogg'
            mixer.init()
            mixer.music.load(snd_file)
            mixer.music.play()


            badtimer = 100
            badtimer1 = 0
            badguys = [[640, 100]]

            # court = pygame.image.load('halfcourt.jpg')
            # court = pygame.transform.scale(court, (1200, 722))
            court = pygame.Surface((1200, 722))
            court.fill((30, 30, 30))
            basketball = Ball(50, 50)
            goal = Goal(487, 0)
            ring = Ring(400, 400)
            # The player is not needed since the 'basketball' is already
            # the playable ball instance.
            # player = Ball  # Just remove this line.

            # badguyimg1 = pygame.image.load("wpierdol.png")
            # badguyimg1 = pygame.transform.scale(badguyimg1, (100, 100))
            # badguyimg2 = pygame.image.load("bad_guy2.gif")
            # badguyimg2 = pygame.transform.scale(badguyimg2, (100, 100))
            # badguyimg3 = pygame.image.load("bad_guy3.gif")
            # badguyimg3 = pygame.transform.scale(badguyimg3, (100, 100))
            badguyimg1 = pygame.Surface((90, 90))
            badguyimg1.fill((60, 50, 210))
            badguyimg2 = pygame.Surface((90, 90))
            badguyimg2.fill((250, 50, 210))
            badguyimg3 = pygame.Surface((90, 90))
            badguyimg3.fill((250, 50, 130))
            badlist = [badguyimg1, badguyimg2, badguyimg3]

            score = 0  # The score variable was missing.

            stop_game = False

            while not stop_game:
                # Event handling.
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        stop_game = True
                    # Either set the speed here or increment the 'basketball.y'
                    # in the while loop with 'pygame.key.get_pressed'.
                    elif event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_UP:
                            basketball.speed_y = -5
                        elif event.key == pygame.K_DOWN:
                            basketball.speed_y = 5
                        elif event.key == pygame.K_LEFT:
                            basketball.speed_x = -5
                        elif event.key == pygame.K_RIGHT:
                            basketball.speed_x = 5
                    elif event.type == pygame.KEYUP:
                        # Stop the ball.
                        if event.key == pygame.K_UP:
                            basketball.speed_y = 0
                        elif event.key == pygame.K_DOWN:
                            basketball.speed_y = 0
                        elif event.key == pygame.K_LEFT and basketball.speed_x < 0:
                            basketball.speed_x = 0
                        elif event.key == pygame.K_RIGHT and basketball.speed_x > 0:
                            basketball.speed_x = 0

                # Don't call get_pressed in the event loop (for every event).
                # pressed = pygame.key.get_pressed()
                # if pressed[pygame.K_UP]:
                #     basketball.y -= 5
                # elif pressed[pygame.K_DOWN]:
                #     basketball.y += 5
                # if pressed[pygame.K_LEFT]:  # if not elif, to separate vertical and horizontal movement.
                #     basketball.x -= 5
                # elif pressed[pygame.K_RIGHT]:
                #     basketball.x += 5

                # Updating.
                basketball.update(screen_rect)
                badtimer -= 1

                point = pygame.sprite.collide_mask(basketball, ring)  # Use basketball not player.
                if point:
                    # The score will be incremented continually.
                    score = score + 1
                    pygame.font.init() # you have to call this at the start, 
                    # if you want to use this module.
                    myfont = pygame.font.SysFont('Comic Sans MS', 30)
                    textsurface = myfont.render('%d' %(score), False, (0, 0, 0))
                    screen.blit(textsurface,(0,0))

                # Update the bad guys.
                if badtimer == 0:
                    badguys.append([1040, random.randint(50,430)])
                    badtimer = 100-(badtimer1*2)
                if badtimer1 >= 35:
                    badtimer1 = 35
                else:
                    badtimer1 += 5

                # You can 'enumerate' the badguys list to get the index
                # and the item at the same time.
                for index, badguy in enumerate(badguys[:]):
                    if badguy[0] < -64:
                        # Don't modify a list while you're iterating over it.
                        # Iterate over a slice copy: badguys[:]
                        badguys.pop(index)
                    badguy[0] -= 7

                # Drawing.
                screen.blit(court, (0,0))

                text = font.render(
                    'Dodge the other team to get to the goal!',
                    True, (0, 0, 0))
                screen.blit(text, (430, 630))
                goal.render(screen)
                # You forgot to render the ring.
                ring.render(screen)

                for badguy in badguys:
                    screen.blit(badguyimg1, badguy)  # The 'dest'ination arg was missing.

                basketball.render(screen)

                pygame.display.update()
                clock.tick(60)  # Limit the frame rate to 60 FPS.

            pygame.quit()

        if __name__ == '__main__':
            main()

В коде может быть больше ошибок, я не уверен, когда пытался пропустить ошибку атрибута и добавить другие вещи. Протестируйте со всем кодом.

  • 2
    Вы должны добавить полную трассировку и правильно сделать отступ
  • 0
    Вы должны определить self.x и self.y прежде чем использовать их для создания прямоугольника.
Теги:
pygame
attributes

2 ответа

2

В __init__() для Ball (строка # 9) вы ссылаетесь на self.xself.y), но вы не задали эти переменные.

  • 0
    Когда я переместил строку «self.rect = pygame.Rect (self.x, self.y, 100, 100)», он сказал:
  • 0
    Traceback (последний вызов был последним): Файл "/ Users / maxi / Desktop / Pygame_Resources / codingandcaring github / Stack Overflow Help.py", строка 233, в файле <module> main () "/ Users / maxi / Desktop / Pygame_Resources / codingandcaring github / Stack Overflow Help.py ", строка 101, в основном баскетбольном файле = Ball (start_x, start_y)" / Users / maxi / Desktop / Pygame_Resources / codingandcaring github / Stack Overflow Help.py ", строка 9, в init self .x = self.rect.x AttributeError: У объекта 'Ball' нет атрибута 'rect'
Показать ещё 1 комментарий
1

Вы не можете использовать переменную или атрибут, прежде чем вы определили его. Либо передайте x и y в pygame.Rect вместо self.x, self.y,

class Ball(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((100, 100)).convert_alpha()
        self.image.fill((160, 70, 0))
        self.rect = pygame.Rect(x, y, 100, 100)
        self.x = self.rect.x
        self.y = self.rect.y

или назначьте x и y для self.x, self.y сначала и создайте прямоугольник потом:

        self.x = x
        self.y = y
        self.rect = pygame.Rect(self.x, self.y, 100, 100)
  • 0
    то же самое произошло с другим спрайтом, я попытался использовать тот же метод здесь, но метод, похоже, не работает. 'self.rect = pygame.Rect (x, y, 100, 100)'
  • 0
    Убедитесь, что переменные или атрибуты определены, прежде чем использовать их. Мне нужно увидеть ваш код, чтобы сказать, что именно не так.
Показать ещё 4 комментария

Ещё вопросы

Сообщество Overcoder
Наверх
Меню