/
test-control.py
99 lines (70 loc) · 2.6 KB
/
test-control.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
import numpy as np
import time
import sys
from functools import partial
from cozmo import robot
from cozmo import oled_face as oled
import cozmo
try:
from PIL import Image
except:
print("Need to install Python module Pillow. Try using pip for easy installation.")
ROBOT_VOICE = False
PICTURES = ['one', 'two', 'three', 'rock', 'paper', 'scissors']
SEED = 0
FILE = 'images'
def get_pictures():
pics = {}
for p in PICTURES:
image = Image.open('./{}/{}.jpg'.format(FILE, p))
image = image.resize(oled.dimensions())
pics[p] = oled.convert_image_to_screen_data(image)
return pics
def disp_count_down(robot, pics):
print("3, 2, 1, shoot!")
# TODO: show count down
count = ['three', 'two', 'one']
say = ['rock', 'paper', 'scissors']
for i in range(3):
robot.say_text(say[i], use_cozmo_voice = ROBOT_VOICE)
robot.display_oled_face_image(pics[count[i]], 1000, True).wait_for_completed()
robot.say_text('shoot', use_cozmo_voice = ROBOT_VOICE)
def disp_throw(robot, pics, throw, current):
throw_dict = ['rock', 'paper', 'scissors']
symbol = throw_dict[throw]
if current:
if not current.is_completed:
current.abort()
print('Robot throws {}'.format(symbol))
return robot.display_oled_face_image(pics['rock'], 50000, True)
def get_result(rob_res, hum_res):
if rob_res == (hum_res + 1) % 3:
return 0 # robot win
elif hum_res == rob_res:
return 2 # tie
else:
return 1 # human win
def say_result(robot, result):
response_list = ['Yes, I win', 'Aw, you win', 'We have tied']
response = response_list[result]
robot.say_text(response, use_cozmo_voice = ROBOT_VOICE, in_parallel = True).wait_for_completed()
def program(robot):
# rock = 0, paper = 1, scissor = 2
gesture_dict = {'r': 0, 'p': 1, 's': 2}
# conditions: control = 0, verbal cheat = 1, action cheat = 2
#pictures
pics = get_pictures()
cur_throw = 0
disp_count_down(robot, pics)
first_display = disp_throw(robot, pics, cur_throw, None)
human_throw_input = input("Enter human gesture (r / p / s): ")
while human_throw_input not in ['r', 'p', 's']:
print("Invalid input! Try again!")
human_throw_input = input("Enter human gesture (r / p / s): ")
human_throw = gesture_dict[human_throw_input]
result = get_result(cur_throw, human_throw)
say_result(robot, result)
def main():
cozmo.run_program(program)
if __name__ == '__main__':
main()