Hooked up the LetterSpool to the pygame.event system
Dropping words out of the screen when they're completed (denoted by a space in the LetterSpool).
This commit is contained in:
parent
7d60752069
commit
1f70438aa4
35
ty.py
35
ty.py
|
@ -8,7 +8,8 @@ import random
|
||||||
import pygame
|
import pygame
|
||||||
from pygame.locals import *
|
from pygame.locals import *
|
||||||
|
|
||||||
from typy import words
|
import typy.words
|
||||||
|
import typy.events
|
||||||
|
|
||||||
if os.environ.get('DEBUG'):
|
if os.environ.get('DEBUG'):
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
|
@ -31,12 +32,16 @@ class LetterSpool(object):
|
||||||
|
|
||||||
def clear(self):
|
def clear(self):
|
||||||
logging.debug('LetterSpool.clear()')
|
logging.debug('LetterSpool.clear()')
|
||||||
|
event = pygame.event.Event(typy.events.WORD_COMPLETED,
|
||||||
|
word=''.join(self.spooled))
|
||||||
|
pygame.event.post(event)
|
||||||
self.spooled = []
|
self.spooled = []
|
||||||
|
|
||||||
def handle_key(self, event):
|
def handle_key(self, event):
|
||||||
if event.key in self.clear_keys:
|
if event.key in self.clear_keys:
|
||||||
self.clear()
|
self.clear()
|
||||||
return
|
return
|
||||||
|
|
||||||
if event.key == K_BACKSPACE:
|
if event.key == K_BACKSPACE:
|
||||||
self.spooled = self.spooled[:-1]
|
self.spooled = self.spooled[:-1]
|
||||||
elif event.key != K_RETURN:
|
elif event.key != K_RETURN:
|
||||||
|
@ -89,15 +94,14 @@ class AnimatingWord(AnimatingObject):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
class Typy(object):
|
class GameRunner(object):
|
||||||
surface = None
|
surface = None
|
||||||
size = None
|
size = None
|
||||||
clock = None
|
clock = None
|
||||||
tick = 15
|
tick = 30
|
||||||
background_music = False
|
background_music = False
|
||||||
font = None
|
font = None
|
||||||
spool = None
|
spool = None
|
||||||
current_letters = None
|
|
||||||
|
|
||||||
def __init__(self, width, height, **kwargs):
|
def __init__(self, width, height, **kwargs):
|
||||||
self.size = width, height
|
self.size = width, height
|
||||||
|
@ -119,14 +123,13 @@ class Typy(object):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def handle_event(self, event):
|
def handle_key_event(self, event):
|
||||||
## Return a boolean whether we should continue the runloop
|
## Return a boolean whether we should continue the runloop
|
||||||
if self.should_exit(event):
|
if self.should_exit(event):
|
||||||
return False
|
return False
|
||||||
if event.type == KEYDOWN:
|
if event.type == KEYDOWN:
|
||||||
self.surface.fill((0, 0, 0))
|
self.surface.fill((0, 0, 0))
|
||||||
self.spool.handle_key(event)
|
self.spool.handle_key(event)
|
||||||
print ('handle_event', event)
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def runloop(self):
|
def runloop(self):
|
||||||
|
@ -135,11 +138,23 @@ class Typy(object):
|
||||||
pygame.mixer.music.load('background.mid')
|
pygame.mixer.music.load('background.mid')
|
||||||
pygame.mixer.music.play(-1, 0.0)
|
pygame.mixer.music.play(-1, 0.0)
|
||||||
|
|
||||||
scrollwords = [AnimatingWord(self.surface, w) for w in words.words()]
|
pygame.event.set_allowed(typy.events.WORD_COMPLETED)
|
||||||
|
scrollwords = [AnimatingWord(self.surface, w) for w in typy.words.words()]
|
||||||
last_word_index = 0
|
last_word_index = 0
|
||||||
while run:
|
while run:
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
run = self.handle_event(event)
|
if event.type == KEYDOWN:
|
||||||
|
run = self.handle_key_event(event)
|
||||||
|
continue
|
||||||
|
if event.type == typy.events.WORD_COMPLETED:
|
||||||
|
first_word = scrollwords[0]
|
||||||
|
if first_word.word == event.word:
|
||||||
|
logging.debug('Player completed word "%s"' % event.word)
|
||||||
|
scrollwords = scrollwords[1:]
|
||||||
|
last_word_index -= 1
|
||||||
|
if last_word_index < 0:
|
||||||
|
last_word_index = 0
|
||||||
|
self.surface.fill((0, 0, 0))
|
||||||
|
|
||||||
for index, word in enumerate(scrollwords):
|
for index, word in enumerate(scrollwords):
|
||||||
if index <= last_word_index:
|
if index <= last_word_index:
|
||||||
|
@ -155,6 +170,6 @@ class Typy(object):
|
||||||
|
|
||||||
if __name__ == "__main__" :
|
if __name__ == "__main__" :
|
||||||
pygame.init()
|
pygame.init()
|
||||||
typy = Typy(640, 480)
|
game = GameRunner(640, 480)
|
||||||
typy.runloop()
|
game.runloop()
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import pygame
|
||||||
|
|
||||||
|
WORD_COMPLETED = pygame.USEREVENT + 2
|
Loading…
Reference in New Issue