Moss/sprite_stacking_engine/player.py

58 lines
1.5 KiB
Python
Raw Normal View History

2024-07-18 10:53:51 +02:00
#!/usr/bin/python3
from settings import *
import math
class Player(pygame.sprite.Sprite):
def __init__(self, app):
self.app = app
self.group = app.main_group
super().__init__(self.group)
self.group.change_layer(self, CENTER.y)
size = vec2([50, 50])
self.image = pygame.Surface(size, pygame.SRCALPHA)
pygame.draw.circle(self.image, "red", size / 2, size[0] / 2)
self.rect = self.image.get_rect(center=CENTER)
self.offset = vec2(0)
self.inc = vec2(0)
self.angle = 0
self.diag_move_corr = 1 / math.sqrt(2)
def control(self):
self.inc = vec2(0)
speed = PLAYER_SPEED * self.app.delta_time
rot_speed = PLAYER_ROT_SPEED * self.app.delta_time
key_state = pygame.key.get_pressed()
if key_state[pygame.K_LEFT]:
self.angle -= rot_speed
if key_state[pygame.K_RIGHT]:
self.angle += rot_speed
if key_state[pygame.K_w]:
self.inc += vec2(0, -speed).rotate_rad(-self.angle)
if key_state[pygame.K_s]:
self.inc += vec2(0, speed).rotate_rad(-self.angle)
if key_state[pygame.K_a]:
self.inc += vec2(-speed, 0).rotate_rad(-self.angle)
if key_state[pygame.K_d]:
self.inc += vec2(speed, 0).rotate_rad(-self.angle)
if self.inc.x and self.inc.y:
self.inc *= self.diag_move_corr
def update(self):
self.control()
self.move()
def move(self):
self.offset += self.inc