Files
sightwords/game.py
2025-12-21 08:27:20 -06:00

183 lines
5.2 KiB
Python

##################################
#
# Sight Words - For Kids by Kids
#
##################################
# TODO:
# - Add Scene to Pick Wordlists
# - Create CheckBox Object
# - Add Image to main screen
# - Add Image to Word Game
# - Make the Colors easier to change
# - Add Delay 'animation' to word changing
# - Add alphabet list
import random
import pygame
import pygame.freetype
import os
FONT = "resources/fonts/iosevka-regular.ttf"
HEIGHT = 600
WIDTH = 800
DISPLAY = (WIDTH, HEIGHT)
DEPTH = 0
FLAGS = 0
CORRECT = 0
MISSED = 0
class Scene(object):
def __init__(self):
pass
def render(self, screen):
raise NotImplementedError
def update(self):
raise NotImplementedError
def handle_events(self, events):
raise NotImplementedError
class GameScene(object):
def next_word(self):
return self.words[random.randint(0,len(self.words)-1)]
def write_word(self, screen, text):
# blank the word area
pygame.draw.rect(screen,
(20,20,20),
(0,0,WIDTH*0.75, HEIGHT),
0
)
# Write the current word
text_surface, _ = self.font.render(text, pygame.Color("DeepPink"))
text_rect = text_surface.get_rect(center=((WIDTH//8)*3, HEIGHT//2))
screen.blit(text_surface, text_rect)
pygame.display.update()
def __init__(self):
super(GameScene, self).__init__()
self.font = pygame.freetype.Font(FONT, 72)
self.sfont = pygame.freetype.Font(FONT, 24)
self.sound_correct = pygame.mixer.Sound('resources/sounds/MENU A_Select.wav')
self.sound_missed = pygame.mixer.Sound('resources/sounds/ALERT_Error.wav')
self.CORRECT = 0
self.MISSED = 0
self.updating = False
self.words = []
# Load all the words in the wordlists
path = "resources/wordlists/"
filelist = os.listdir(path)
for wordlistf in filelist:
with open(path+wordlistf) as f:
for line in f:
self.words.append(line.rstrip())
print(self.words)
self.current_word = self.next_word()
pass
def render(self, screen):
# Blank the Screen
screen.fill((20,20,20))
if self.updating:
self.write_word(screen, "...")
pygame.time.wait(200)
self.updating = False
self.write_word(screen, self.current_word)
# Write the Correct Score
yes_surface, _ = self.font.render(str(self.CORRECT), pygame.Color("Green"))
yes_rect = yes_surface.get_rect(center=((WIDTH//8)*7, (HEIGHT//3)))
screen.blit(yes_surface, yes_rect)
# Write the Missed Score
no_surface, _ = self.font.render(str(self.MISSED), pygame.Color("Red"))
no_rect = no_surface.get_rect(center=((WIDTH//8)*7, (HEIGHT//3)*2))
screen.blit(no_surface, no_rect)
def update(self):
pass
def handle_events(self, events):
for e in events:
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_q:
pygame.quit()
elif e.key == pygame.K_m:
self.current_word = self.next_word()
pygame.mixer.Sound.play(self.sound_correct)
self.CORRECT += 1
self.updating = True
elif e.key == pygame.K_z:
self.current_word = self.next_word()
pygame.mixer.Sound.play(self.sound_missed)
self.MISSED += 1
self.updating = True
class TitleScene(object):
def __init__(self):
super(TitleScene, self).__init__()
self.font = pygame.freetype.Font(FONT, 72)
self.sfont = pygame.freetype.Font(FONT, 24)
def render(self, screen):
# beware: ugly!
screen.fill((20, 20, 20))
text1, _ = self.font.render('Sight Words', pygame.Color("DeepPink"))
text1_rect = text1.get_rect(center=(WIDTH//2,HEIGHT//2))
screen.blit(text1, text1_rect)
text2, _ = self.sfont.render('> press space to start <', pygame.Color("Purple"))
text2_rect = text2.get_rect(center=(WIDTH//2,(HEIGHT - 50)))
screen.blit(text2, text2_rect)
def update(self):
pass
def handle_events(self, events):
for e in events:
if e.type == pygame.KEYDOWN and e.key == pygame.K_SPACE:
self.manager.go_to(GameScene())
#pygame.quit()
class SceneMananger(object):
def __init__(self):
self.go_to(TitleScene())
def go_to(self, scene):
self.scene = scene
self.scene.manager = self
def main():
pygame.init()
screen = pygame.display.set_mode(DISPLAY, FLAGS, DEPTH)
pygame.display.set_caption("Sight Words - For Kids By Kids")
clock = pygame.time.Clock()
running = True
manager = SceneMananger()
while running:
clock.tick(30)
if pygame.event.get(pygame.QUIT):
running = False
return
manager.scene.handle_events(pygame.event.get())
manager.scene.update()
manager.scene.render(screen)
pygame.display.flip()
if __name__ == "__main__":
main()