Reputation: 47
so I have this script here and I am getting this error 'list' object has no attribute 'get_rect' I am not sure how to fix this
error 'list' object has no attribute 'get_rect'
what this does is that it moves my player image into my square hitbox
def draw(self):
self.topleft = (self.x,self.y)
player_rect = rights.get_rect(center = self.rect.center)
player_rect.centerx += 0 # 10 is just an example
player_rect.centery += 0 # 15 is just an example
and this is my player class where I stored the code down below
# my players class
class player:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.isJump = False
self.JumpCount = 10
self.fall = 0
self.speed = 5
self.standingleft =[pygame.image.load("s1.png"),
pygame.image.load("s2.png"),
pygame.image.load("s3.png"),
pygame.image.load("s4.png"),
pygame.image.load("s5.png"),
pygame.image.load("s6.png"),
pygame.image.load("s7.png"),
pygame.image.load("s8.png"),
pygame.image.load("s9.png"),
pygame.image.load("s10.png"),
pygame.image.load("s11.png"),
pygame.image.load("s12.png"),
pygame.image.load("s13.png"),
pygame.image.load("s14.png"),
pygame.image.load("s15.png")]
self.standingright = [pygame.image.load("d1.png"),
pygame.image.load("d2.png"),
pygame.image.load("d3.png"),
pygame.image.load("d4.png"),
pygame.image.load("d5.png"),
pygame.image.load("d6.png"),
pygame.image.load("d7.png"),
pygame.image.load("d8.png"),
pygame.image.load("d9.png"),
pygame.image.load("d10.png"),
pygame.image.load("d11.png"),
pygame.image.load("d12.png"),
pygame.image.load("d13.png"),
pygame.image.load("d14.png"),
pygame.image.load("d15.png")]
self.rights = [pygame.image.load("L1.png"),
pygame.image.load("L2.png"),
pygame.image.load("L3.png"),
pygame.image.load("L4.png"),
pygame.image.load("L5.png"),
pygame.image.load("L6.png"),
pygame.image.load("L7.png"),
pygame.image.load("L8.png"),
pygame.image.load("L9.png"),
pygame.image.load("L10.png"),
pygame.image.load("L11.png"),
pygame.image.load("L12.png"),
pygame.image.load("L13.png"),
pygame.image.load("L14.png"),
pygame.image.load("L15.png")]
self.lefts = [pygame.image.load("e1.png"),
pygame.image.load("e2.png"),
pygame.image.load("e3.png"),
pygame.image.load("e4.png"),
pygame.image.load("e5.png"),
pygame.image.load("e6.png"),
pygame.image.load("e7.png"),
pygame.image.load("e8.png"),
pygame.image.load("e9.png"),
pygame.image.load("e10.png"),
pygame.image.load("e11.png"),
pygame.image.load("e12.png"),
pygame.image.load("e13.png"),
pygame.image.load("e14.png"),
pygame.image.load("e15.png")]
self.left_index = 0
self.right_index = 0
self.stand_index = 0
self.anim_index = 0
self.direction = "left"
self.direction = "right"
self.direction = "standright"
self.rights = [pygame.transform.scale(image,(image.get_width()-400,image.get_height()-400)) for image in self.rights]
self.lefts = [pygame.transform.scale(image,(image.get_width()-10,image.get_height()-10)) for image in self.lefts]
self.standingright = [pygame.transform.scale(image,(image.get_width()-10,image.get_height()-10)) for image in self.standingright]
self.standingleft = [pygame.transform.scale(image,(image.get_width()-10,image.get_height()-10)) for image in self.standingleft]
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.topleft = (self.x,self.y)
player_rect = rights.get_rect(center = self.rect.center)
player_rect.centerx += 0 # 10 is just an example
player_rect.centery += 0 # 15 is just an example
window.blit(heart, player_rect)
if self.direction == "right":
window.blit(self.right[self.anim_index],self.rect)
self.anim_index += 1
if self.anim_index == len(self.right):
self.anim_index = 0
player classplayer classplayer classplayer classplayer classplayer classplayer class
Upvotes: 1
Views: 907
Reputation: 210998
All the images are scaled to the same size by
pygame.transform.scale(image,(image.get_width()-400,image.get_height()-400))
Hence you can get the size from one representative surface object. Use the 1st surface in the list to get the size (rights[0]
):
player_rect = rights.get_rect(center = self.rect.center)
player_rect = right[0].get_rect(center = self.rect.center)
Upvotes: 1
Reputation: 2780
You do not indicate which line the error was on, but I can guess it is this line.
player_rect = rights.get_rect(center = self.rect.center)
The error is relatively explanatory. "'list' object has no attribute 'get_rect'". says that tried to call get_rect() on a list, not a object like a surface that has a get_rect().
rights
a list of images/surfaces and you are calling get_rect() on that list rather that on one of the things in the list.
Upvotes: 0