#!/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 try: LANGUAGE = sys.argv[1] except IndexError: LANGUAGE = "ipa" with file("data/%s/index" % LANGUAGE) 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: %s" % LANGUAGE, "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()