Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/toisto-proto.py
blob: b4c8512377ecffd2b84274636a1c77207a81c9f5 (plain)
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
#!/usr/bin/env python

from __future__ import with_statement

import time
import sys
import math
import random
import copy

import pygame

pygame.init()

# keycodes of the OLPC gamepad keys:
OLPC_LEFT = 260
OLPC_UP = 264
OLPC_RIGHT = 262
OLPC_DOWN = 258
OLPC_CIRCLE = 265
OLPC_CROSS = 259
OLPC_SQUARE = 263
OLPC_CHECK = 257

with file("data/ipa/index") as infile:
    lines = infile.read().decode("utf-8").split("\n")

entries = [line.split() for line in lines if line]
entries = [(letter, sound, pygame.mixer.Sound(sample)) 
               for (letter, sound, sample) in entries]

letters = [letter for (letter, sound, sample) in entries]


def play_blocking(sample):
    sample.play()
    while pygame.mixer.get_busy():
        pass

def text_main():
    while True:
        letter, sound, sample = random.choice(entries)
        print sound
        play_blocking(sample)

        answer = raw_input(letters+"? ")
        if answer == letter:
            print " == "+letter
        else:
            print " != "+letter

def draw_text(surface, text, (x, y), color=(255, 255, 255), align=0):
    
    s = font.render(text, True, color)
    surface.blit(s, (x-align*s.get_width(), y))

class Game(object):
    def __init__(self):
        self.user_pause = False
        self.round = 0
        self.score = 0
        self.time0 = None
        self.selected = None
        self.choices = None

    def stats(self):
        gametime = time.time() - self.time0
        return ["This is a prototype",
                "Language: IPA",
                "Game time: %d:%02d" % (gametime / 60, gametime % 60),
                "Score: %d" % self.score, 
                "Questions: %d" % self.round,
                ]

    def run(self):
        self.time0 = time.time()

        self.selected = 0

        while True:
            self.play_round()
            self.round += 1

    def play_round(self):
#        s.fill((0, 0, 0), (0, 0, DISPLAY_W, DISPLAY_H))
        pygame.display.update()

        choices = random.sample(entries, 4)
        self.letter, self.sound, self.sample = random.choice(choices)
        
        self.choices = [letter for letter, sound, sample
                        in choices]
        print self.sound
        play_blocking(self.sample)

        while self.update():
            pass

    def draw_all(self):
        for i in xrange((DISPLAY_W/S)*(DISPLAY_H/S)):
            x = i % (DISPLAY_W/S)
            y = i / (DISPLAY_W/S)

            try:
                draw_text(s, unichr(i+0x250), (x*S, y*S))
            except:
                pass

    def draw_choices(self, reveal_answer=False):
        for x, text in enumerate(self.choices):
            if reveal_answer:
                if self.choices[x] != self.letter:
                    color = (0, 0, 0)
                else:
                    color = (0, 255, 0) if x == self.selected else (127,0,0)
            else:
                color = (255, 255, 255) if x == self.selected else (127,127,127)
                
            x_coord = (DISPLAY_W-len(self.choices)*S*2)/2+x*S*2+S
            y_coord = DISPLAY_H/2
            for degree in range(0, 360, 60):
                pygame.draw.circle(s, color, (x_coord+math.cos(degree/180.0*math.pi)*S*0.55, y_coord+S/2+math.sin(degree/180.0*math.pi)*S*0.55), int(S*0.38))
            pygame.draw.circle(s, (255, 255, 0), (x_coord, y_coord+S/2), int(S*0.7))
            draw_text(s, text, (x_coord, y_coord), (0,0,0), align=0.5)

    def update(self):
        s.fill((0, 0, 0), (0, 0, DISPLAY_W, DISPLAY_H))

        for y, text in enumerate(self.stats()):
            draw_text(s, text, (0, y*S))

        self.draw_choices()
#        self.draw_all()

        pygame.display.update() # XXX update less

#        for e in pygame.event.get():
        for e in ([pygame.event.wait()] + pygame.event.get()):
            if e.type == pygame.QUIT:
                sys.exit(0)
            if e.type == pygame.KEYDOWN:
                # control keys:
                if e.key == pygame.K_ESCAPE:
                    sys.exit(0)
                elif e.key in [pygame.K_F11, pygame.K_F12] or e.unicode == "f":
                    pygame.display.toggle_fullscreen()
#                elif e.key == pygame.K_PAUSE or e.unicode == 'p':
#                    self.user_pause = not self.user_pause
                elif self.user_pause:
                    self.user_pause = False
                    # keys below this line don't work while paused
                # game keys:
                elif e.key in (pygame.K_LEFT, OLPC_LEFT):
                    self.selected = (self.selected - 1) % len(self.choices)
                elif e.key in (pygame.K_RIGHT, OLPC_RIGHT):
                    self.selected = (self.selected + 1) % len(self.choices)
                elif e.key in [pygame.K_SPACE, pygame.K_RETURN, 
                               OLPC_CIRCLE, OLPC_CROSS, OLPC_SQUARE, OLPC_CHECK]:
                    if self.choices[self.selected] == self.letter:
                        self.score += 1
                    s.fill((0,0,0), (0, 0, DISPLAY_W, DISPLAY_H))
                    self.draw_choices(reveal_answer=True)
                    return False
                else:
                    print "Unhandled key (%s): '%s'" % (e.key, e.unicode)
                    pass

#        fps.tick(10)
        return True

if __name__ == "__main__":
#    pygame.init()

    pygame.mouse.set_visible(False)

    s = pygame.display.set_mode(pygame.display.list_modes()[0], pygame.FULLSCREEN)
    DISPLAY_W, DISPLAY_H = s.get_size()
    background = pygame.Surface(s.get_size())

    S = min(DISPLAY_W/20, DISPLAY_H/6)
    print DISPLAY_W, DISPLAY_H, S

#    font = pygame.font.SysFont(None, S)
    font = pygame.font.Font("DejaVuSans-Bold.ttf", S)

    fps = pygame.time.Clock()

    while True:
        g = Game()
        g.run()