-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
179 lines (154 loc) · 5.92 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from settings import *
import pygame as pg
import math
class Player:
def __init__(self, game):
self.game = game
self.x, self.y = PLYR_POS
self.angle = PLYR_ANGLE
self.rel = 0
self.secret_open = False
self.gate_fullopen = False
self.prev_time = pg.time.get_ticks()
#
self.health = 100
def update(self):
self.small_events()
self.mouse_control()
self.movement()
def small_events(self):
self.interact_event()
self.timed_event()
def interact_event(self):
key = pg.key.get_pressed()
if key[pg.K_e]:
for adj in self.get_adjs():
if adj in self.game.proxes.locks:
lock = self.game.proxes.locks[adj]
lock[0] = True
def timed_event(self):
if self.map_pos not in self.game.map.secret_map:
curr_time = pg.time.get_ticks()
if curr_time - self.prev_time >= 5000 and self.secret_open:
self.secret_open = False
self.prev_time = curr_time
# -------------------------
def mouse_control(self):
mx, my = pg.mouse.get_pos()
if mx < MOUSE_LBORD or mx > MOUSE_RBORD:
pg.mouse.set_pos([HWIDTH, HHEIGHT])
self.rel = pg.mouse.get_rel()[0]
self.rel = max(-MOUSE_MAXREL, min(MOUSE_MAXREL, self.rel))
self.angle += self.rel * MOUSE_SENS * self.game.dtime
def movement(self):
cos_a = math.cos(self.angle)
sin_a = math.sin(self.angle)
speed = PLYR_SPD * self.game.dtime
speed_cos = cos_a * speed
speed_sin = sin_a * speed
dx, dy = 0, 0
key = pg.key.get_pressed()
if key[pg.K_w]:
dx += speed_cos
dy += speed_sin
if key[pg.K_s]:
dx -= speed_cos
dy -= speed_sin
if key[pg.K_a]:
dx += speed_sin
dy -= speed_cos
if key[pg.K_d]:
dx -= speed_sin
dy += speed_cos
if key[pg.K_p]: # for alpha testing cheat
self.x, self.y = 33, 23
if key[pg.K_m]: # for alpha testing cheat
print(self.x, self.y)
if key[pg.K_LEFT]:
self.angle -= PLYR_RSPD * self.game.dtime
if key[pg.K_RIGHT]:
self.angle += PLYR_RSPD * self.game.dtime
self.angle %= math.tau
self.can_walk(dx, dy)
# -------------------------
def can_walk(self, dx, dy):
scale = PLYR_SCALE / self.game.dtime
if self.get_unwall_cors(int(self.x + dx * scale), int(self.y)):
self.x += dx
if self.get_unwall_cors(int(self.x), int(self.y + dy * scale)):
self.y += dy
if self.get_secret_cors(int(self.x + dx * scale), int(self.y)) and self.secret_open:
self.x += dx
if self.get_secret_cors(int(self.x), int(self.y + dy * scale)) and self.secret_open:
self.y += dy
if self.get_gate_cors(int(self.x + dx * scale), int(self.y)) and self.gate_fullopen:
self.x += dx
if self.get_gate_cors(int(self.x), int(self.y + dy * scale)) and self.gate_fullopen:
self.y += dy
def get_unwall_cors(self, x, y):
return (x, y) not in self.game.map.cor_map
def get_secret_cors(self, x, y):
key = pg.key.get_pressed()
if key[pg.K_e]:
for adj in self.get_adjs():
if adj in self.game.proxes.secrets:
secret = self.game.proxes.secrets[adj]
secret[0] = True
self.secret_open = True
return (x, y) in self.game.map.secret_map
def get_gate_cors(self, x, y):
return (x, y) in self.game.map.gate_map
def get_adjs(self):
ways = [-1, 0], [0, -1], [1, 0], [0, 1] # [-1, -1], [1, -1], [1, 1], [-1, 1]
area = [(self.map_pos[0] + dx, self.map_pos[1] + dy) for dx, dy in ways]
area.append(self.map_pos)
return area
def get_an_adjs(self):
ways = [1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]
deg_ang = math.degrees(self.angle)
angle_index = 0
if deg_ang < 0:
deg_ang += 360
if deg_ang < 22.5 or 337.5 < deg_ang < 360:
angle_index = 0
if 22.4 < deg_ang < 67.5:
angle_index = 1
if 67.4 < deg_ang < 112.5:
angle_index = 2
if 112.4 < deg_ang < 157.5:
angle_index = 3
if 157.4 < deg_ang < 202.5:
angle_index = 4
if 202.4 < deg_ang < 247.5:
angle_index = 5
if 247.4 < deg_ang < 292.5:
angle_index = 6
if 292.4 < deg_ang < 337.5:
angle_index = 7
first_div, last_div, i = [], [], 0
for pos in ways:
if i > 7:
i = 0
if i < angle_index:
last_div.append(pos)
else:
first_div.append(pos)
i += 1
first_div += last_div
# print(first_div)
area = [(self.map_pos[0] + dx, self.map_pos[1] + dy) for dx, dy in first_div]
area.append(self.map_pos)
for pos in area:
if pos not in self.game.map.cor_map:
return pos
def draw(self):
pg.draw.line(self.game.screen, 'yellow', (self.x * self.game.map.scale, self.y * self.game.map.scale),
(self.x * self.game.map.scale + WIDTH * math.cos(self.angle),
self.y * self.game.map.scale + WIDTH * math.sin(self.angle)), 2)
pg.draw.circle(self.game.screen, 'green', (self.x * self.game.map.scale, self.y * self.game.map.scale), 5)
@property
def pos(self):
return self.x, self.y
@property
def map_pos(self):
return int(self.x), int(self.y)