-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.py
executable file
·78 lines (64 loc) · 3.02 KB
/
run.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
#!/usr/bin/python
"""
Play Puyo Puyo by taking video input and sending commands to an Arduino based
Gamecube controller.
"""
from time import sleep
import cv2
import puyo
def main():
import argparse
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("video", help='Video source. An AVI filename, camera '
'index (ex: "0" to use /dev/video0), or format specifier (ex: '
'"footage/%%d.jpg"). Any format supported by the installed version of '
'OpenCV will work.')
parser.add_argument("gc_dev", help='Serial device for the Arduino '
'Gamecube controller. Example: "/dev/ttyACM0".')
ai_names = list(puyo.AI_REGISTRY.keys())
parser.add_argument("-a", "--ai", choices=ai_names,
default=puyo.DEFAULT_AI_NAME, help="AI to use. Choose one of: {} "
"(default: {})".format(ai_names, puyo.DEFAULT_AI_NAME))
parser.add_argument("--player2", "-2", dest="player", const=2,
action="store_const", help="Play as player 2. Plays on the right side "
"of the screen.")
parser.add_argument("--video-out", "-o", default=None,
help="AVI file to write video to.")
parser.add_argument("--level", "-l", default=None, type=int,
help="Reset the game and start on the given level by entering a "
"passcode. Default: start playing immediately. Passcodes are "
"available for the following levels: {}".format(puyo.PASSWORDS.keys()))
parser.add_argument("--mode", "-m", default="scenario",
choices=("scenario", "repeat"),
help="scenario (default): Assume the game is already started in "
"scenario and progress accordingly. repeat: Repeatedly do one "
"scenario level, specified by --level (-l).")
parser.add_argument("--debug", "-d", default=False, action="store_true",
help="Show debug window and other debug information.")
args = parser.parse_args()
#TODO: Make screen offset configurable
controller = puyo.GamecubeController(args.gc_dev)
driver = puyo.Driver(controller, args.ai, args.player, debug=args.debug)
if args.level is not None:
driver.reset_to_level(args.level)
if args.mode == "scenario":
driver.run(args.video, video_out=args.video_out)
elif args.mode == "repeat":
if not args.level:
parser.error("level argument must be given when mode is repeat")
won = 0
total = 0
while True:
state = driver.run(args.video, video_out=args.video_out, on_special_state="exit", debug=args.debug)
if state.special_state == "unknown":
break # Must have exited manually from debug mode
if state.special_state == "scenario_won":
won += 1
total += 1
print
print "AI: {}".format(args.ai)
print "Level: {}".format(args.level)
print "Match Result: {} ({} / {})".format(state.special_state, won, total)
driver.reset_to_level(args.level)
if __name__ == "__main__":
main()