Skip to content

Instantly share code, notes, and snippets.

@vwood
Created February 29, 2012 10:03
Show Gist options
  • Save vwood/1939572 to your computer and use it in GitHub Desktop.
Save vwood/1939572 to your computer and use it in GitHub Desktop.
Willpower Game
#!/usr/bin/env python
#
# Willpower trainer
#
# It's a really bad, ad hoc state machine
#
import random
import pygame
from pygame.locals import *
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
def blit_text(surface, text, x, y, size, color):
font = pygame.font.Font(None, size)
text = font.render(text, 1, color)
if x == "center":
x = surface.get_width() / 2
if y == "center":
y = surface.get_height() / 2
pos = text.get_rect(centerx = x, centery = y)
surface.blit(text, pos)
class State(object):
def __init__(self, game):
super(State, self).__init__()
self.game = game
def input(self, key):
pass
def update(self):
pass
def quit(self):
exit()
class MenuState(State):
def __init__(self, game):
super(MenuState, self).__init__(game)
self.background = pygame.Surface(game.screen.get_size())
self.background = self.background.convert()
self.background.fill(white)
blit_text(self.background, "Willpower Trainer", "center", 16, 36, black)
blit_text(self.background, "Press Space to Start", "center", 220, 24, blue)
self.set_difficulty(4)
def set_difficulty(self, diff):
if diff < 2:
diff = 2
self.difficulty = diff
def input(self, key):
if key.isdigit():
self.set_difficulty(int(key))
self.display()
elif key == " ":
self.game.set_state(PlayState(self.game, self.difficulty))
def display(self):
self.game.screen.blit(self.background, (0, 0))
blit_text(self.game.screen, str(self.difficulty), "center", "center", 128, red)
pygame.display.flip()
class PlayState(State):
alphabet = [chr(i) for i in range(ord("A"), ord("Z") + 1)]
def __init__(self, game, difficulty):
super(PlayState, self).__init__(game)
self.background = pygame.Surface(game.screen.get_size())
self.background = self.background.convert()
self.background.fill(white)
self.difficulty = difficulty
blit_text(self.background, "Willpower Trainer", "center", 16, 36, black)
self.new_word()
self.between = False
def reset_delay(self, delay = 20):
self.ticks_til_next = delay
def new_word(self):
self.waiting_for_input = False
self.before_display = True
self.pos = 0
self.string = "".join([random.choice(PlayState.alphabet) for i in range(self.difficulty)])
self.reset_delay()
self.current_input = ""
self.correct = True
def update(self):
self.ticks_til_next -= 1
if self.ticks_til_next <= 0:
if self.before_display:
self.before_display = False
elif self.between:
self.between = False
elif not self.waiting_for_input:
self.pos += 1
if self.pos == len(self.string):
self.waiting_for_input = True
else:
self.between = True
self.display()
self.reset_delay(10)
return
elif self.current_input != "":
self.current_input = ""
elif self.waiting_for_input and self.pos <= 0:
self.new_word()
self.display()
self.reset_delay()
def display(self):
self.game.screen.blit(self.background, (0, 0))
if self.current_input != "":
if self.pos >= 0 and self.current_input == self.string[self.pos]:
blit_text(self.game.screen, str(self.current_input), "center", "center", 128, green)
else:
blit_text(self.game.screen, str(self.current_input), "center", "center", 128, red)
elif not self.waiting_for_input and not self.before_display and not self.between:
blit_text(self.game.screen, self.string[self.pos], "center", "center", 128, blue)
elif self.waiting_for_input and self.pos <= 0:
if self.correct:
blit_text(self.game.screen, "YES", "center", "center", 128, green)
else:
blit_text(self.game.screen, "NO", "center", "center", 128, red)
pygame.display.flip()
def input(self, key):
if self.waiting_for_input:
self.current_input = key.upper()
self.pos -= 1
if self.pos < 0 or self.pos >= len(self.string):
self.display()
return
if self.current_input != self.string[self.pos]:
self.correct = False
self.reset_delay()
self.display()
def quit(self):
self.game.set_state(MenuState(self.game))
class Willpower_Game(object):
def __init__(self):
super(Willpower_Game, self).__init__()
pygame.init()
if not pygame.font:
exit()
self.screen = pygame.display.set_mode((240, 240))
pygame.display.set_caption('Willpower trainer')
self.set_state(MenuState(self))
self.clock = pygame.time.Clock()
def set_state(self, state):
self.state = state
self.state.display()
def run(self):
while True:
self.clock.tick(60)
self.state.update()
for event in pygame.event.get():
if event.type == QUIT:
exit()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
self.state.quit()
else:
self.state.input(event.unicode)
if __name__ == "__main__":
game = Willpower_Game()
game.run()
@vwood
Copy link
Author

vwood commented Feb 29, 2012

Found this. It's ancient as of time of posting.

I do remember reading an article that said a task similar to this was shown to aid willpower. I'm sure the idea was to prove a link between working memory and willpower. Anyway, have at it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment