0

I've read all the similar topics but i can't find the issue. I write a python game and i want my player moving by directional arrows and rotate by mouse.

When i write the code about rotation, i have exit error:

Player object has no attribute position

I've tried many different solutions i found on internet, but nothing changes.

class Player(pygame.sprite.Sprite):
    #change_x = 0
    #change_y = 0

    def __init__(self, x, y):
        super().__init__()
        self.image = pygame.image.load("player1.gif").convert()
        #self.image.fill(WHITE)
        self.rect = self.image.get_rect()
        self.rect.y = y
        self.rect.x = x
        self.change_x = 0
        self.change_y = 0
        self.walls = None

    def changespeed(self, x, y):
        self.change_x += x
        self.change_y += y

    def move(self, walls):
        self.rect.x += self.change_x
        block_hit_list = pygame.sprite.spritecollide(self, walls, False)
        for block in block_hit_list:
            if self.change_x > 0:
                self.rect.right = block.rect.left
            else:
                self.rect.left = block.rect.right
        self.rect.y += self.change_y
        block_hit_list = pygame.sprite.spritecollide(self, walls, False)
        for block in block_hit_list:
            if self.change_y > 0:
                self.rect.bottom = block.rect.top
            else:
                self.rect.top = block.rect.bottom

    def rotate(self):
        mouse_x, mouse_y = pygame.mouse.get_pos()
        rel_x, rel_y = mouse_x - self.rect.x, mouse_y - self.rect.y
        angle = (180 / math.pi) * -math.atan2(rel_y, rel_x)
        self.image = pygame.transform.rotate(self.image, int(angle))
        self.rect = self.image.get_rect(center=self.position)

This is my code after adding the rotate function. Where's the fault and how can i call rotate in the main loop?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • @Rabbid76 what should i set in the "self.position ="? I try to set "self.position = 100, 100", as my player default position when game runs, and i haven't got any error but the player don't rotate, and don't shoot with mouse button as before. – Vaggelis Karof Feb 23 '19 at 20:41
  • @Rabbid76 I don't want to rotate it itself. I want to rotate it with my mouse to "aim enemies"... – Vaggelis Karof Feb 23 '19 at 21:06
  • @Rabbid76 so what should i do? add something ? change something ? what exactly is the issue – Vaggelis Karof Feb 23 '19 at 21:12
  • See [How do I make my player rotate towards mouse position?](https://stackoverflow.com/questions/56627414/how-do-i-make-my-player-rotate-towards-mouse-position/56627834#56627834) and [How to rotate an image(player) to the mouse direction?](https://stackoverflow.com/questions/58603835/how-to-rotate-an-imageplayer-to-the-mouse-direction) – Rabbid76 Oct 04 '20 at 17:30

1 Answers1

1

An image (pygame.Surface) can be rotated by pygame.transform.rotate.
If that is done progressively an an image, then the image gets distorted and rapidly increases. See the answer to How do I rotate an image around its center using Pygame?

To deal with that, you've to keep the original image and assign a copy of the .image attribute in the constructor of the class Player:

self.image_source = pygame.image.load("player1.gif").convert()
self.image = self.image_source.copy()

Rotate the original image and and update the attribute .image in the method rotate:

self.image = pygame.transform.rotate(self.image_source, int(angle))
self.rect = self.image.get_rect(center=self.rect.center)
class Player(pygame.sprite.Sprite):

    def __init__(self, x, y):
        super().__init__()

        self.image_source = pygame.image.load("player1.gif").convert()
        self.image = self.image_source.copy()
        self.rect = self.image.get_rect()
        self.rect.y = y
        self.rect.x = x
        self.change_x = 0
        self.change_y = 0
        self.walls = None

    def rotate(self):
        mouse_x, mouse_y = pygame.mouse.get_pos()
        rel_x, rel_y = mouse_x - self.rect.x, mouse_y - self.rect.y
        angle = (180 / math.pi) * -math.atan2(rel_y, rel_x)
        self.image = pygame.transform.rotate(self.image_source, int(angle))
        self.rect = self.image.get_rect(center=self.rect.center)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • @Rappid76 Ok thx for all these but now my player rotates according to my mouse's position the moment that i click on the run of my file ...and not during the time that I'm in the game playing. Furthermore the bullets (as expected) keep going in the same direction as before – Vaggelis Karof Feb 23 '19 at 21:31
  • @Rappid76 I said that now the player rotates according my mouse's position THE MOMENT I CLICK "RUN".. In the game, the player don't rotate. Sorry for the bullets, i haven't post code for bullets – Vaggelis Karof Feb 23 '19 at 21:39
  • @VaggelisKarof Ok may be. But when do you call the method `rotate` on the `Player` object? I can't see this code, too. – Rabbid76 Feb 23 '19 at 21:41
  • in the while loop – Vaggelis Karof Feb 23 '19 at 21:42
  • @VaggelisKarof You can call `player.rotate()` right before you draw the player sprite. – Rabbid76 Feb 23 '19 at 21:46
  • I try it too. But nothing happend. (I draw the player out of the while loop) – Vaggelis Karof Feb 23 '19 at 21:48