Working out some scrolling text and handling of user-entered strings
Added the LetterSpool class to handle the current "set" of user-entered words
This commit is contained in:
parent
31e05ccb26
commit
7f5253286c
61
ty.py
61
ty.py
|
@ -1,21 +1,55 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
import random
|
import random
|
||||||
|
|
||||||
import pygame
|
import pygame
|
||||||
from pygame.locals import *
|
from pygame.locals import *
|
||||||
|
|
||||||
class ExitException(Exception):
|
from typy import words
|
||||||
pass
|
|
||||||
|
if os.environ.get('DEBUG'):
|
||||||
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
|
|
||||||
|
class LetterSpool(object):
|
||||||
|
spooled = None
|
||||||
|
parent = None
|
||||||
|
font = None
|
||||||
|
clear_keys = (K_RETURN, K_SPACE,)
|
||||||
|
color = (255, 255, 255,)
|
||||||
|
|
||||||
|
def __init__(self, parent, **kwargs):
|
||||||
|
self.parent = parent
|
||||||
|
self.font = pygame.font.SysFont('Courier', 32)
|
||||||
|
self.spooled = []
|
||||||
|
|
||||||
|
def clear(self):
|
||||||
|
logging.debug('LetterSpool.clear()')
|
||||||
|
self.spooled = []
|
||||||
|
|
||||||
|
def handle_key(self, event):
|
||||||
|
if event.key in self.clear_keys:
|
||||||
|
self.clear()
|
||||||
|
return
|
||||||
|
if event.key == K_BACKSPACE:
|
||||||
|
self.spooled = self.spooled[:-1]
|
||||||
|
else:
|
||||||
|
self.spooled.append(event.unicode)
|
||||||
|
self.parent.blit(self.font.render(''.join(self.spooled), 0, self.color),
|
||||||
|
(0,0,))
|
||||||
|
|
||||||
|
|
||||||
class Typy(object):
|
class Typy(object):
|
||||||
surface = None
|
surface = None
|
||||||
size = None
|
size = None
|
||||||
clock = None
|
clock = None
|
||||||
tick = 30
|
tick = 15
|
||||||
background_music = False
|
background_music = False
|
||||||
font = None
|
font = 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
|
||||||
|
@ -28,6 +62,7 @@ class Typy(object):
|
||||||
pygame.mouse.set_visible(False)
|
pygame.mouse.set_visible(False)
|
||||||
|
|
||||||
self.font = pygame.font.SysFont('Courier', 48)
|
self.font = pygame.font.SysFont('Courier', 48)
|
||||||
|
self.spool = LetterSpool(self.surface)
|
||||||
|
|
||||||
def should_exit(self, event):
|
def should_exit(self, event):
|
||||||
if event.type == QUIT:
|
if event.type == QUIT:
|
||||||
|
@ -42,19 +77,33 @@ class Typy(object):
|
||||||
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.surface.blit(self.font.render(event.unicode, 0, (255,255,255)),
|
self.spool.handle_key(event)
|
||||||
(0,0))
|
|
||||||
print ('handle_event', event)
|
print ('handle_event', event)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def runloop(self):
|
def runloop(self):
|
||||||
|
run = True
|
||||||
if self.background_music:
|
if self.background_music:
|
||||||
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)
|
||||||
run = True
|
|
||||||
|
word_x = 480
|
||||||
|
step = 0.05
|
||||||
|
text = 'hello'
|
||||||
|
word = self.font.render(text, 0, (255,255,255))
|
||||||
|
f_w, f_h = self.font.size(text)
|
||||||
|
off_screen = len(text) * f_w
|
||||||
while run:
|
while run:
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
run = self.handle_event(event)
|
run = self.handle_event(event)
|
||||||
|
|
||||||
|
if word_x >= -off_screen:
|
||||||
|
# Rect(left, top, width, height)
|
||||||
|
self.surface.fill((0,0,0),
|
||||||
|
rect=pygame.Rect(word_x, 100, f_w, f_h))
|
||||||
|
self.surface.blit(word, (word_x, 100))
|
||||||
|
word_x = word_x - step
|
||||||
|
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
pygame.mixer.music.stop()
|
pygame.mixer.music.stop()
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
def words():
|
||||||
|
return 'four score and seven years ago'.split(' ')
|
Loading…
Reference in New Issue