[PyGame] AttributeError while switching between Sprite Animations - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: [PyGame] AttributeError while switching between Sprite Animations (/thread-33919.html) |
AttributeError while switching between Sprite Animations - paulmills3 - Jun-09-2021 Getting an AttributeError(AttributeError: 'builtin_function_or_method' object has no attribute 'get_rect') after I added code to switch Sprite Animations when arrow keys are clicked. Wondering what I need to change? Below is my Sprite1 Class and the relevant code from the main file. (PyGame) Sprite1 Class: ------------------------------------------------------ import pygame class Sprite1(pygame.sprite.Sprite): def __init__(self): super().__init__() self.faceUp = True self.faceDown = False self.faceLeft = False self.faceRight = False self.image = pygame.image.load('player.png').convert_alpha self.rect = self.image.get_rect() def facing(self): if self.faceUp == True: self.image = pygame.image.load('player.png').convert_alpha self.rect = self.image.get_rect() elif self.faceDown == True: self.image = pygame.image.load('playerDown.png').convert_alpha self.rect = self.image.get_rect() elif self.faceLeft == True: self.image = pygame.image.load('playerLeft.png').convert_alpha self.rect = self.image.get_rect() elif self.faceRight == True: self.image = pygame.image.load('playerRight.png').convert_alpha self.rect = self.image.get_rect()Relevant Bits from Main File: ---------------------------------------------------------- import pygame from sprite1 import Sprite1 pygame.init() #game Window screen_width = 800 screen_height = 400 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption('Warrior Quest') #setup player player = Sprite1() player.rect.x = 400 player.rect.y = 380 #game loop running running = True while running: draw_bg() if main_menu == True: if start_button.draw(): main_menu = False if cancel_button.draw(): running = False else: player.draw(screen) keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and player.rect.x>5: player.faceLeft = True player.facing() player.rect.x -= 5 if keys[pygame.K_RIGHT] and player.rect.x<790: player.faceRight = True player.facing() player.rect.x += 5 if keys[pygame.K_UP] and player.rect.y>10: player.faceUp = True player.facing() player.rect.y -= 5 if keys[pygame.K_DOWN]and player.rect.y<395: player.faceDown = True player.facing() player.rect.y += 5 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.display.update() pygame.quit() RE: AttributeError while switching between Sprite Animations - BashBedlam - Jun-09-2021 The problem is in the line ... self.image = pygame.image.load('player.png').convert_alphaIt's missing a set of parentheses in order to actually call the method. Try this: self.image = pygame.image.load('player.png').convert_alpha () RE: AttributeError while switching between Sprite Animations - noahcentineo - Jul-12-2021 What you've got so far has been fixed, and it now works as anticipated. You can shoot each other, but that's all there is to it. However, it appears to be a solid start. WIDTH, HEIGHT = 900, 500 WIN = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption('First Game!') WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) YELLOW = (255, 255, 0) BORDER = pygame.Rect(WIDTH//2 - 5, 0, 10, HEIGHT) SPACESHIP_WIDTH, SPACESHIP_HEIGHT = 55, 44 |