0

this is the code for my Character class:

right = [2, 0]

class Character(pygame.sprite.Sprite):
    def __init__(self):
        super(Character, self).__init__()

        self.image = pygame.image.load("character_n.png").convert()
        self.image.set_colorkey((25, 25, 25))

        self.rect = self.image.get_rect()

char = Character()

char.rect[0] = 375
char.rect[1] = 400

And the code in my game loop that moves the character to the right if the joystick is moved to the right:

h_axis_pos = my_joystick.get_axis(0)
v_axis_pos = my_joystick.get_axis(1)

if h_axis_pos >= 0.50:
    if char.rect.right < 800:
       char.rect = char.rect.move(right)

This code worked fine when the character was a simple image, but does not work now.

I am confused, since self.rect is initialized and the character is set at the coordinates I selected. I tried to use char.rect[0] in the if and increment it, but still the same error appears:

if char.rect.right < 800:
AttributeError: 'pygame.Surface' object has no attribute 'rect'

Can someone shed some light on this? Thanks.

Ferduun
  • 137
  • 13
  • 1
    This doesn't seem to be the same `char`, since it says it is an instance of `pygame.Surface` not `Character`. Can you show more of the code? – Daniel Roseman Jul 04 '15 at 15:07
  • http://dumptext.com/dXBQFTZE It is very (very!) messy still, but I am sure there's only one char. – Ferduun Jul 04 '15 at 15:23
  • 1
    Note that instead of `char.rect = char.rect.move(right)`, you could simply use `char.rect.move_ip(right)` – sloth Jul 06 '15 at 07:31

1 Answers1

2

In this bit of code:

 if char.rect.right < 800:
      idle = 0
      char.rect = char.rect.move(right)
      char = pygame.image.load("right_n.png")

In the line:

char = pygame.image.load("right_n.png")

You are overwriting your char and assigning a pygame.Surface to it. So on the next iteration of the game loop it fails.

KrTG
  • 51
  • 3
  • Actually, that bit was commented. However, this: if idle: char = pygame.image.load("character_n.png") wasn't, and it was causing the problem. Thanks! – Ferduun Jul 04 '15 at 15:40