/
TypingGame.py
70 lines (67 loc) · 2.4 KB
/
TypingGame.py
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
import pygame, random
pygame.init()
def striplist(l):
return([x.strip() for x in l])
def openit(name):
f = open()
return striplist(f.readlines())
shuffle_it = lambda lst:random.shuffle(lst)
#shuffle_it(lst)
#return shuffled lst
width,height = (640,480)
screen = pygame.display.set_mode((width,height))
def get_words(text):
for i in text:
if len(i.split()) > 5:
words.extend(i.split())
def clean_text(remove = [',','.','@','%','#','$','*','(',')','^']):
I = 0
for i in lines:
lines[I] = filter(lambda x: x not in remove,i)
I += 1
class TypingGameWord(pygame.sprite.Sprite):
def __init__(self, word):
pygame.sprite.Sprite.__init__(self)
self.font = pygame.font.Font(None,100)
self.originalWord = word
self.word = word
self.updateSurface()
def checkLetter(self, letter):
"Checks a letter that the player typed. Returns true if the word is empty, otherwise false."
if letter == self.word[0]:
self.word = self.word[1:]
self.updateSurface()
return self.word == ""
def updateSurface(self):
self.image = self.font.render(self.word, True, (255,255,255))
self.rect = self.image.get_rect()
self.rect.center = (320,240)
running = True
word= TypingGameWord("Test #1!") #set our initial word
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
else:
if word.checkLetter(event.unicode): #event.unicode is the letter the user typed
try:
wordStr = random.choice(words)
word = TypingGameWord(wordStr)
except:
wordfile = open('words.txt', 'r')
words = wordfile.readlines()
wordStr = random.choice(words).strip()
word = TypingGameWord(wordStr)
wordfile.close()
try:
words.remove(wordStr)
except ValueError:
words.remove(wordStr+'\n')
screen.fill((0,0,0))
screen.blit(word.image, word.rect)
pygame.display.flip()
pygame.quit()
quit()