2024-07-18 10:53:51 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
2024-07-19 18:24:45 +02:00
|
|
|
from sprite_stacking_engine.settings import *
|
2024-07-18 10:53:51 +02:00
|
|
|
import math
|
|
|
|
|
|
|
|
|
|
|
|
class Player(pygame.sprite.Sprite):
|
2024-07-19 18:24:45 +02:00
|
|
|
def __init__(self, engine):
|
|
|
|
self.engine = engine
|
|
|
|
self.app = engine.app
|
|
|
|
self.group = self.engine.main_group
|
2024-07-18 10:53:51 +02:00
|
|
|
super().__init__(self.group)
|
|
|
|
|
2024-07-19 13:38:42 +02:00
|
|
|
self.group.change_layer(self, CENTER.y) # simpy set the layer to the y center of the screen because that is -
|
|
|
|
# the position of the player on the screen
|
2024-07-18 10:53:51 +02:00
|
|
|
|
2024-07-19 13:38:42 +02:00
|
|
|
size = vec2([50, 50]) # create a surface with a red dot on it
|
2024-07-18 10:53:51 +02:00
|
|
|
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)
|
|
|
|
|
2024-07-19 13:38:42 +02:00
|
|
|
def control(self): # move the player and rotate the view according to the key presses
|
2024-07-18 10:53:51 +02:00
|
|
|
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
|