-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspelling-game.py
executable file
·213 lines (162 loc) · 6.09 KB
/
spelling-game.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python2
# Copyright 2017 Joseph Wright <[email protected]>
import argparse
import sys
import signal
import os
import timeit
import curses
from curses import panel
import logging
import engine
class game():
def __init__(self, parsed_args, screen):
screen.clear()
logging.info("===== init ====")
self.model = engine.GameModel(".spelling")
self.view = engine.GameView(screen)
self.screen = screen
self.win_panel = []
self.start_panel = []
self.setup_screen()
self.logo_panel()
self.args = parsed_args
self.model.load_scores2()
self.model.load_words()
self.process_args()
self.show_menu()
signal.signal(signal.SIGINT, self.on_quit)
def setup_screen(self):
screen_y, screen_x = self.screen.getmaxyx()
win = curses.newwin(screen_y, screen_x, 0, 0)
curses.curs_set(0)
self.win_panel = panel.new_panel(win)
self.echo_bar("Init")
def logo_panel(self):
"""create curses panel for home screen and bring to front"""
screen_y, screen_x = self.screen.getmaxyx()
win = curses.newwin(screen_y, screen_x, 0, 0)
self.start_panel = panel.new_panel(win)
f = open("logo.txt", 'r').read()
self.start_panel.window().addstr(f)
logo_height = len(f.split("\n"))
start_msg = "Press any key to start\n Escape to quit"
self.start_panel.window().addstr(logo_height, 2, start_msg)
self.start_panel.top()
self.win_panel.bottom()
self.pflush()
def pflush(self):
panel.update_panels()
curses.doupdate()
def echo_bar(self, text):
self.win_panel.window().move(0,0)
self.win_panel.window().clrtoeol()
self.win_panel.window().addstr(0, 0, "echo: " + text, curses.A_BOLD)
def prompt(self, text):
self.win_panel.window().move(5, 5)
self.win_panel.window().clrtoeol()
self.win_panel.window().addstr(5, 5, "prompt: " + text, curses.A_BOLD)
def show_menu(self):
"""start screen display and start on key press"""
c = self.screen.getkey()
self.start_panel.bottom()
self.win_panel.top()
self.pflush()
self.start()
def process_args(self):
"""switch to do one time commands and possibly exit"""
if self.args.command:
curses.endwin()
command = self.args.command
if command[0] == "add":
logging.info("adding word " + command[1] )
added = self.model.add_word(command[1])
if added != None:
print "already in word list"
logging.warn("already " )
sys.exit(1)
if command[0] == "rm":
logging.warn("Removing word not implemented")
logging.info("removing word")
sys.exit(0)
if command[0] == "stats":
self.model.stats()
sys.exit(0)
if command[0] == "test":
self.model.generate_schedule()
sys.exit(0)
sys.exit(1)
def start(self):
self.process_args()
self.challenge_loop()
def challenge_loop(self):
"""Main game loop"""
logging.info("Starting game")
logging.info(self.model.wordlist)
wl = self.model.generate_schedule()
logging.debug(wl)
for word in wl:
score = self.challenge_word(word)
self.model.record_score(score)
self.model.stats()
def challenge_word(self, word):
"""runs a typing challenge loop
and returns (word, attempts, seconds_elapsed)
"""
prompt = "\n$ %s\n> " % (word)
start_time = timeit.default_timer()
started = True
attempts = 1
correct = False
char_index = 0
answer = ""
while started or not correct:
started = False
self.prompt(word)
c = self.win_panel.window().getch()
# escape key
if c == 27:
sys.exit(0)
answer = answer + chr(c)
self.echo_bar(answer)
if chr(c) == word[char_index]:
char_index += 1
if char_index == len(word):
correct = True
elapsed = timeit.default_timer() - start_time
self.echo_bar("")
#self.echo_bar("CORRECT! %s %s" % (attempts, elapsed))
return {"word": word, "attempts": attempts, "time": elapsed}
else:
attempts += 1
correct = False
char_index = 0
answer = ""
curses.beep()
#self.win_panel.window().refresh()
self.echo_bar("")
def on_quit(self, signal, frame):
logging.info("Good bye")
sys.exit(0)
guide = """# Examples
- examples
- here
"""
parser = argparse.ArgumentParser(description='Spelling game',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=guide)
parser.add_argument('-m','--message', help='Log a message')
parser.add_argument('-t','--test', help="Test run", action='store_true')
parser.add_argument('-a','--add', help="add word to game", action='store_true')
parser.add_argument('-r','--remove', help="remove word from game",
action='store_true')
parser.add_argument('-c','--config', help="config folder", default=".spelling/")
parser.add_argument('command', type=str, nargs='*', help="'add' or 'rm'")
args = parser.parse_args()
logging.basicConfig(format='%(levelname)s: %(message)s',
filename='session.log',
level=logging.DEBUG)
def main(stdscr1):
session = game(args, stdscr1)
if __name__ == "__main__":
curses.wrapper(main)