-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubfunctions.py
190 lines (157 loc) · 6.41 KB
/
subfunctions.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
180
181
182
183
184
185
186
187
188
189
190
import random
import numpy as np
import config as cfg
import pygame
import math
cfg.set_args()
def get_random_position():
x = random.randint(100, cfg.screen_width - cfg.score_section_width - 100)
y = random.randint(100, cfg.screen_height - 100)
position = (x, y)
dx = random.randint(-5, 5)
dy = random.randint(-5, 5)
direction = (dx, dy)
return position, direction
def get_random_gap():
start_gap = random.randint(cfg.min_gap, cfg.max_gap)
start_line = random.randint(cfg.min_line, cfg.max_line)
return start_gap, start_line
def do_positions_intersect(pos1, pos2, pos_history2):
"""Checks if two positions intersect with each other."""
for i in range(len(pos_history2) - 1):
if do_segments_intersect(pos1, pos2, pos_history2[i], pos_history2[i + 1]):
return True
return False
def do_points_intersect(point1, point2, radius=2):
distance = math.sqrt((point2[0] - point1[0]) ** 2 + (point2[1] - point1[1]) ** 2)
return distance < radius
def do_segments_intersect(seg1_start, seg1_end, seg2_start, seg2_end):
"""Checks if two line segments intersect with each other."""
x1, y1 = seg1_start
x2, y2 = seg1_end
x3, y3 = seg2_start
x4, y4 = seg2_end
# calculate the direction of the lines
u1 = (x2 - x1, y2 - y1)
u2 = (x4 - x3, y4 - y3)
v1 = (-u1[1], u1[0])
v2 = (-u2[1], u2[0])
# calculate the scalar values of the intersection point along each line segment
# check if denominators are 0
de1 = u1[0] * u2[1] - u1[1] * u2[0]
de2 = u1[0] * u2[1] - u1[1] * u2[0]
if de1 == 0 or de2 == 0:
return False
t1 = ((x3 - x1) * u2[1] - (y3 - y1) * u2[0]) / de1
t2 = ((x3 - x1) * u1[1] - (y3 - y1) * u1[0]) / de2
# check if the intersection point is within both line segments
if t1 >= 0 and t1 <= 1 and t2 >= 0 and t2 <= 1:
return True
else:
return False
def check_for_collisions(players, game_state):
for player in players:
if not player["alive"]:
continue
# Alternative check using game_state variable
gs_pos = player["game_state_pos"]
gs_stack = np.column_stack(gs_pos)
player_pairs = False
if not player["gap"]:
# Select points from game stack that would lead to collision and compare to position
coll_points = np.nonzero(
(game_state >= 1) & (game_state <= 5) & (game_state != player["id"] + 2)
)
player_pairs = np.any(
(coll_points[0][:, np.newaxis] == gs_pos[0])
& (coll_points[1][:, np.newaxis] == gs_pos[1])
)
# Check if colliding with walls
wall_points = np.nonzero(game_state == -1)
wall_pairs = np.any(
(wall_points[0][:, np.newaxis] == gs_pos[0])
& (wall_points[1][:, np.newaxis] == gs_pos[1])
)
# slow list solution
# line_coll = game_state[
# np.where(
# (game_state >= 2) & (game_state <= 5) & (game_state != player["id"] + 2)
# )[0]
# ].tolist()
# coll_points_line = set(zip(*line_coll)) & set(zip(*gs_list))
# wall_coll = game_state[np.where(game_state == -1)[0]].tolist()
# coll_points_wall = set(zip(*wall_coll)) & set(zip(*gs_list))
# If collision: kill player, otherwise: update game state
if wall_pairs or player_pairs:
player["alive"] = False
else:
# At first change to player id
game_state[gs_pos[0], gs_pos[1]] = player["id"] + 2
# After direct collision period: change to 1
if len(player["pos_history"]) >= 8 and not player["gap_history"][7]:
x, y = player["pos_history"][7]
x_pos = (
np.arange(
int(round(x, 0)) - int(player["size"] / 2),
int(round(x, 0)) + int(player["size"] / 2),
),
)
y_pos = (
np.arange(
int(round(y, 0)) - int(player["size"] / 2),
int(round(y, 0)) + int(player["size"] / 2),
),
)
if not player["gap"]:
game_state[x_pos, y_pos] = 1
# player_pos = player["pos"]
# player_history = player["pos_history"]
# gap_history = player["gap_history"]
# player_size = player["size"]
# # check collision with own line
# for pos, gap in zip(player_history[5:], gap_history[5:]):
# if not gap:
# intersect = do_points_intersect(pos, player_pos)
# if intersect:
# player["alive"] = False
# break
# # check collision with other player lines
# for other_player in players:
# if other_player["id"] == player["id"]:
# continue
# other_history = other_player["pos_history"]
# other_gap = other_player["gap_history"]
# for pos, gap in zip(other_history, other_gap):
# if not gap:
# intersect = do_points_intersect(pos, player_pos)
# if intersect:
# player["alive"] = False
# break
# # check collision with screen edges
# if (
# player_pos[0] < player_size
# or player_pos[0] > cfg.screen_width - cfg.score_section_width - player_size
# or player_pos[1] < player_size
# or player_pos[1] > cfg.screen_height - player_size
# ):
# player["alive"] = False
# break
return players, game_state
def update_player_direction(player):
# update directions
keys = pygame.key.get_pressed()
# Handle left key
if keys[pygame.key.key_code(player["left"])]:
player["angle"] -= player["del_angle"] # Rotate left by 10 degrees
# Handle right key
elif keys[pygame.key.key_code(player["right"])]:
player["angle"] += player["del_angle"]
# Normalize the angle to keep it within 0-360 degrees range
if player["angle"] < 0:
player["angle"] += 360
elif player["angle"] >= 360:
player["angle"] -= 360
# Convert the angle to radians and update the direction vector
rad_angle = math.radians(player["angle"])
player["dir"] = [math.cos(rad_angle), math.sin(rad_angle)]
return player