-
Notifications
You must be signed in to change notification settings - Fork 4
/
gui.py
executable file
·111 lines (88 loc) · 2.62 KB
/
gui.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
This creates the interface to switch between strategies.
"""
from common.world import *
from communication.robot_interface2 import *
from gooeypy.const import *
from strategy.strategies import *
from strategy.strategies import strategies
from vision2.vision import *
import pygame, gooeypy
import os, sys
import logging
#logging.basicConfig(level=logging.DEBUG)
strategy_dir = os.path.abspath('.') + '/strategy'
world = World()
v = Vision(world)
colour = 'yellow'
if len(sys.argv) > 1:
colour = sys.argv[1]
ai_name = 'null'
if len(sys.argv) > 2:
ai_name = sys.argv[2]
goal = 'left'
if len(sys.argv) > 3:
goal = sys.argv[3]
ai = strategies[ai_name]( world, RealRobotInterface() )
ai.setColour(colour)
def change_strategy(strategy):
global ai
ai = strategies[strategy]( world, RealRobotInterface() )
ai.setColour(colour)
print 'Changed strategy to:', strategy
def change_goal(new_goal):
global goal
if goal != new_goal:
goal = new_goal
world.swapGoals()
print 'Changed goal to:', goal
else:
print 'Goal is already:', goal
def change_colour(new_colour):
global colour
colour = new_colour
ai.setColour(colour)
print 'Changed colour to', colour
# Setup the GUI components:
pygame.init()
clock = pygame.time.Clock()
height = 21 * len(strategies)
screen = pygame.display.set_mode((640, height), pygame.SRCALPHA)
gui = gooeypy.Container(width=640, height=height, surface=screen)
hbox = gooeypy.HBox(x=0, y=0)
gui.add(hbox)
strategy_select = gooeypy.SelectBox(width=200, scrollable=True)
for strategy in strategies:
strategy_select.add(strategy, strategy)
hbox.add(strategy_select)
left_goal_button = gooeypy.Button('Blue->Left')
left_goal_button.click = lambda: change_goal('left')
hbox.add(left_goal_button)
right_goal_button = gooeypy.Button('Blue->Right')
right_goal_button.click = lambda: change_goal('right')
hbox.add(right_goal_button)
blue_button = gooeypy.Button('Blue')
blue_button.click = lambda: change_colour('blue')
hbox.add(blue_button)
yellow_button = gooeypy.Button('Yellow')
yellow_button.click = lambda: change_colour('yellow')
hbox.add(yellow_button)
quit = False
selected_strategy = None
while not quit:
# The main loop
v.processFrame()
ai.run()
ai.sendMessage()
# You'd think there'd be a 'connectable' event for the selection
# changing, but unfortunately not.
if len(strategy_select.values) != 0:
new_strategy = strategy_select.values.pop()
if new_strategy != selected_strategy:
selected_strategy = new_strategy
change_strategy(selected_strategy)
gui.run(pygame.event.get())
gui.draw()
pygame.display.flip()