-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
137 lines (105 loc) · 4.03 KB
/
main.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
import json
import socket
import threading
import pygame
import ball
import constants
import field
import robot
pygame.init()
screen = pygame.display.set_mode((1200, 600))
# Title and Icon🤖
pygame.display.set_caption('🤖Neon VSS Simulation🤖')
icon = pygame.image.load('resources/icon.png')
pygame.display.set_icon(icon)
clock = pygame.time.Clock()
NOPLAN_LISTENER = ('localhost', 5778)
listener_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
NOPLAN_SENDER = ('localhost', 5777)
sender_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
class Game:
def no_plan_listener(self):
listener_sock.bind(NOPLAN_LISTENER)
while True:
data, address = listener_sock.recvfrom(4096)
if data:
self.message = data.decode('utf-8')
print(self.message)
def sent_to_no_plan(self, data):
sender_sock.sendto(bytes(json.dumps(data), 'utf-8'), NOPLAN_SENDER)
def __init__(self, blue_robots=None, yellow_robots=None, ball_pos=(0, 0)):
if yellow_robots is None:
yellow_robots = []
if blue_robots is None:
blue_robots = []
self.message = []
self.field = field.Field()
self.ball = ball.Ball(pos=ball_pos)
self.robots = []
self.running = False
for robot_ in blue_robots:
self.robots.append(
robot.Robot('blue', self.field, robot_['start_position'], robot_['orientation'],
radio_id=robot_['radio_id'], vision_id=robot_['vision_id'])
)
for robot_ in yellow_robots:
self.robots.append(
robot.Robot('yellow', self.field, robot_['start_position'], robot_['orientation'],
radio_id=robot_['radio_id'], vision_id=robot_['vision_id'])
)
def build_for_noplan(self):
self.timestamp += 1
entities_data = {
't_capture': self.timestamp,
'robots_blue': [
{
'x': r.position[0],
'y': r.position[1],
'orientation': r.orientation,
'robot_id': r.vision_id
} for r in self.robots if r.team_color == 'blue'
],
'robots_yellow': [
{
'x': r.position[0],
'y': r.position[1],
'orientation': r.orientation,
'robot_id': r.vision_id
} for r in self.robots if r.team_color == 'yellow'
],
'balls': [{'x': self.ball.position[0], 'y': self.ball.position[1], 'speed': {'x': 0, 'y': 0}}]}
data = {
'detection': entities_data,
'geometry': {}
}
return data
def start(self):
self.timestamp = 0
self.running = True
listener_thread = threading.Thread(target=self.no_plan_listener, args=())
listener_thread.start()
while self.running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
if event.type == pygame.MOUSEBUTTONUP:
pos = pygame.mouse.get_pos()
w, h = pygame.display.get_surface().get_size()
self.ball.position = (pos[0] - w/2)*2, (pos[1] - h/2)*2
screen.fill(constants.COLORS.NEON_BLACK)
self.field.update(screen)
self.ball.update(screen)
for rb in self.robots:
try:
rb_message = list(filter(lambda x: x[0] == rb.vision_id, json.loads(self.message)))
if rb_message:
rb.update(screen, rb_message[0])
else:
rb.update(screen, [0, 1, 0, 0])
except:
pass
pygame.display.update()
self.sent_to_no_plan(self.build_for_noplan())
config = json.loads(open('match_config.json', 'r').read())
match = Game(**config)
match.start()