-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
65 lines (53 loc) · 2.01 KB
/
player.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import pygame as pg
import math
from pygame import Vector2
class Player:
FOV = 70
HALF_FOV = FOV/2
SPEED = 4
ROTATE_SPEED = 120
def __init__(self, pos=Vector2(2,2), angle=45):
self.pos = pos
self.angle = angle
def angle_xy(self):
"""Returns the players current angle degrees as a normalized Vector2"""
return Vector2(
math.cos(math.radians(self.angle)),
math.sin(math.radians(self.angle))
).normalize()
def update(self, delta, map):
keys = pg.key.get_pressed()
# Move
if keys[pg.K_UP]:
self.try_move_along_angle(delta, map, 1, self.angle_xy())
if keys[pg.K_DOWN]:
self.try_move_along_angle(delta, map, -1, self.angle_xy())
if keys[pg.K_LALT] or keys[pg.K_RALT]:
right = -Vector2(self.angle_xy().y, -self.angle_xy().x)
# Strafe
if keys[pg.K_LEFT]:
self.try_move_along_angle(delta, map, -1, right)
if keys[pg.K_RIGHT]:
self.try_move_along_angle(delta, map, 1, right)
else:
# Rotate
if keys[pg.K_LEFT]:
self.angle -= Player.ROTATE_SPEED * delta
if keys[pg.K_RIGHT]:
self.angle += Player.ROTATE_SPEED * delta
def try_move_along_angle(self, delta, map, dir, angle):
"""Attempts to move the player along its forward angle.
Arguments:
delta -- delta time
map -- the map to check for collisions.
dir -- -1 or 1, corrosponding to back and forward"""
if dir != -1 and dir != 1:
raise ValueError('dir needs to be 1 or -1')
xmotion = angle.x * Player.SPEED * delta
xmotion *= dir
if map[int(self.pos.x+xmotion)][int(self.pos.y)] == 0:
self.pos.x += xmotion
ymotion = angle.y * Player.SPEED * delta
ymotion *= dir
if map[int(self.pos.x)][int(self.pos.y+ymotion)] == 0:
self.pos.y += ymotion