36 lines
1.2 KiB
Python
36 lines
1.2 KiB
Python
import pygame
|
|
import math
|
|
|
|
class Player(pygame.sprite.Sprite):
|
|
def __init__(self, x, y):
|
|
super().__init__()
|
|
self.image = pygame.image.load("player.png").convert_alpha()
|
|
self.rect = self.image.get_rect()
|
|
self.rect.x = x
|
|
self.rect.y = y
|
|
self.speed = 2
|
|
self.dest_x = x
|
|
self.dest_y = y
|
|
self.direction = pygame.math.Vector2(1, 0)
|
|
|
|
def draw(self, win):
|
|
win.blit(self.image, self.rect)
|
|
|
|
def update(self):
|
|
if self.rect.x < self.dest_x:
|
|
self.rect.x += self.speed
|
|
if self.rect.x > self.dest_x:
|
|
self.rect.x -= self.speed
|
|
if self.rect.y < self.dest_y:
|
|
self.rect.y += self.speed
|
|
if self.rect.y > self.dest_y:
|
|
self.rect.y -= self.speed
|
|
|
|
direction = pygame.math.Vector2(self.dest_x - self.rect.centerx, self.dest_y - self.rect.centery)
|
|
angle = -math.degrees(math.atan2(direction.y, direction.x))
|
|
self.image = pygame.transform.rotate(self.image, angle)
|
|
self.rect = self.image.get_rect(center=self.rect.center)
|
|
self.direction = direction.normalize()
|
|
|
|
def dest(self, pos):
|
|
self.dest_x, self.dest_y = pos |