Python Forum
[PyGame] AttributeError while switching between Sprite Animations
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] AttributeError while switching between Sprite Animations
#1
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:
------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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:
----------------------------------------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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()
Reply
#2
The problem is in the line ...

1
self.image = pygame.image.load('player.png').convert_alpha
It's missing a set of parentheses in order to actually call the method. Try this:

1
self.image = pygame.image.load('player.png').convert_alpha ()
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] My Pygame Sprite not appearing... noodlespinbot 3 4,934 Oct-30-2020, 06:51 AM
Last Post: robinmurphy
  My Pygame Sprite not appearing... noodlespinbot 1 2,892 Apr-08-2020, 11:25 AM
Last Post: pyzyx3qwerty
  [PyGame] Terrible Sprite controls, need help. michael1789 16 8,154 Dec-18-2019, 10:32 PM
Last Post: michael1789
  [PyGame] Sprite image.get_rect() moves sprite to 0, 0 michael1789 2 5,793 Dec-13-2019, 08:37 PM
Last Post: michael1789
  Pygame sprite not moving michael1789 1 3,913 Nov-10-2019, 03:54 AM
Last Post: michael1789
  Sprite not rendering Clunk_Head 2 3,505 Oct-03-2019, 11:27 AM
Last Post: Clunk_Head
  Need help making a sprite GalaxyCoyote 4 4,342 Aug-11-2019, 09:12 PM
Last Post: metulburr
  creating sprite mask pfaber11 5 4,942 Jun-12-2019, 09:39 PM
Last Post: pfaber11
  [PyGame] assigning rect to sprite pfaber11 1 2,808 May-18-2019, 05:39 PM
Last Post: metulburr
  moving a sprite pfaber11 3 3,344 May-15-2019, 12:52 PM
Last Post: pfaber11

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020