cli-games/mine-racer/storm_games.py

80 lines
2.5 KiB
Python
Raw Normal View History

#!/bin/python
# -*- coding: utf-8 -*-
"""Standard initializations and functions shared by all games."""
import os
from os import listdir
from os.path import isfile, join
2016-03-23 13:40:30 -04:00
from inspect import isfunction
import pygame
import speechd
import time
spd = speechd.Client()
def speak(text, interupt = True):
if interupt == True: spd.cancel()
spd.say(text)
def exit_game():
spd.close()
pygame.quit()
exit()
def initialize_gui(gameTitle):
# start pygame
pygame.init()
# start the display (required by the event loop)
pygame.display.set_mode((320, 200))
pygame.display.set_caption(gameTitle)
# Load sounds from the sound directory and creates a list like that {'bottle': 'bottle.ogg'}
soundFiles = [f for f in listdir("sounds/") if isfile(join("sounds/", f)) and (f.split('.')[1].lower() in ["ogg","wav"])]
#lets make a dict with pygame.mixer.Sound() objects {'bottle':<soundobject>}
soundData = {}
for f in soundFiles:
soundData[f.split('.')[0]] = pygame.mixer.Sound("sounds/" + f)
soundData['game-intro'].play()
time.sleep(soundData['game-intro'].get_length())
return soundData
def game_menu(*options):
loop = True
pygame.mixer.music.load("sounds/music_menu.ogg")
pygame.mixer.music.play(-1)
i = 0
speak(options[i])
while loop == True:
event = pygame.event.wait()
2016-03-23 13:40:30 -04:00
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE: exit_game()
if event.key == pygame.K_DOWN and i < len(options) - 1: i = i + 1
if event.key == pygame.K_UP and i > 0: i = i - 1
if event.key == pygame.K_RETURN:
try:
eval(options[i] + "()")
continue
except:
2016-03-23 13:40:30 -04:00
return options[i]
continue
speak(options[i])
event = pygame.event.clear()
time.sleep(0.001)
2016-03-23 13:40:30 -04:00
def credits():
info = (
"Mine Racer: brought to you by Storm Dragon",\
2016-03-23 13:40:30 -04:00
"Billy Wolfe, designer and coder.",\
"http://stormdragon.tk",\
"Press escape or enter to return to the game menu.")
2016-03-23 13:40:30 -04:00
i = 0
speak(info[i])
while True:
2016-03-23 13:40:30 -04:00
event = pygame.event.wait()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE or event.key == pygame.K_RETURN: return
if event.key == pygame.K_DOWN and i < len(info) - 1: i = i + 1
if event.key == pygame.K_UP and i > 0: i = i - 1
speak(info[i])
event = pygame.event.clear()
time.sleep(0.001)