-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
103 lines (63 loc) · 2.52 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import pygame as pg
import math
from settings import *
class Player:
def __init__(self, game):
self.game = game
self.x, self.y = PLAYER_POS
self.angle = PLAYER_ANG
def movement(self):
# movement speed and rotation speed initialization
sin_a = math.sin(self.angle)
cos_a = math.cos(self.angle)
dx, dy = 0, 0
speed = PLAYER_SPD * self.game.delta_time
speed_sin = speed * sin_a
speed_cos = speed * cos_a
keys = pg.key.get_pressed()
# movement keys settings
if keys[pg.K_w]:
#dx, dy = dx + speed_sin, dy + speed_cos
dx += speed_cos
dy += speed_sin
if keys[pg.K_s]:
#dx, dy = dx - speed_sin, dy - speed_cos
dx += -speed_cos
dy += -speed_sin
if keys[pg.K_d]:
#dx, dy = dx - speed_sin, dy + speed_cos
dx += -speed_sin
dy += speed_cos
if keys[pg.K_a]:
#dx, dy = dx + speed_sin, dy - speed_cos
dx += speed_sin
dy += -speed_cos
#self.x += dx
#self.y += dy
self.collision(dx, dy)
# rotation keys settings
if keys[pg.K_LEFT]:
self.angle -= PLAYER_ROT * self.game.delta_time
if keys[pg.K_RIGHT]:
self.angle += PLAYER_ROT * self.game.delta_time
self.angle %= math.tau # 2*pi
# wall and collision settings
def wall(self,x, y):
return (x, y) not in self.game.map.worldmap
def collision(self, dx, dy):
if self.wall(int(self.x + dx), int(self.y)):
self.x += dx
if self.wall(int(self.x), int(self.y + dy)):
self.y += dy
# player and player's vision visualization
def draw(self):
pg.draw.line(self.game.screen, 'blue', (self.x * 100, self.y * 100,), (self.x * 100 + WIDTH * math.cos(self.angle), self.y * 100 + WIDTH * math.sin(self.angle)), 2)
pg.draw.circle(self.game.screen, 'green', (self.x * 100, self.y * 100), 15)
def update(self):
self.movement()
@property
def pos(self):
return self.x, self.y
@property
def map_pos(self):
return int(self.x), int(self.y)