60 lines
No EOL
1.8 KiB
Python
60 lines
No EOL
1.8 KiB
Python
#!/usr/bin/python3
|
|
|
|
from sprite_stacking_engine.settings import *
|
|
import math
|
|
|
|
|
|
class Player(pygame.sprite.Sprite):
|
|
def __init__(self, engine):
|
|
self.engine = engine
|
|
self.app = engine.app
|
|
self.group = self.engine.main_group
|
|
super().__init__(self.group)
|
|
|
|
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
|
|
|
|
size = vec2([50, 50]) # create a surface with a red dot on it
|
|
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): # move the player and rotate the view according to the key presses
|
|
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 |