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 attributeposition
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?