2019-11-24 11:26:13 -05:00
#!/bin/python
# -*- coding: utf-8 -*-
""" Standard initializations and functions shared by all games. """
import configparser
import os
from os import listdir
from os . path import isfile , join
from inspect import isfunction
from xdg import BaseDirectory
import pygame
2019-11-30 19:44:17 -05:00
import pyperclip
2019-11-24 11:26:13 -05:00
import random
import requests
import speechd
2019-11-26 19:53:36 -05:00
import subprocess
2019-11-24 11:26:13 -05:00
import time
localConfig = configparser . ConfigParser ( )
globalConfig = configparser . ConfigParser ( )
spd = speechd . Client ( )
def write_config ( writeGlobal = False ) :
if writeGlobal == False :
with open ( gamePath , ' w ' ) as configfile :
localConfig . write ( configfile )
else :
with open ( globalPath , ' w ' ) as configfile :
globalConfig . write ( configfile )
2019-11-27 14:55:02 -05:00
def read_config ( section , value , readGlobal = False ) :
if readGlobal == False :
with open ( gamePath , ' r ' ) as configfile :
return localConfig . read ( section , value )
else :
return globalConfig . read ( section , value )
2019-11-24 11:26:13 -05:00
def speak ( text , interupt = True ) :
if interupt == True : spd . cancel ( )
spd . say ( text )
def exit_game ( ) :
spd . close ( )
2019-11-26 18:42:07 -05:00
pygame . mixer . music . stop ( )
2019-11-24 11:26:13 -05:00
pygame . quit ( )
exit ( )
def initialize_gui ( gameTitle ) :
# Check for, and possibly create, storm-games path
global globalPath
global gamePath
globalPath = BaseDirectory . xdg_config_home + " /storm-games "
gamePath = globalPath + " / " + str . lower ( str . replace ( gameTitle , " " , " - " ) + " config " )
globalPath = globalPath + " /config "
if not os . path . exists ( gamePath ) : os . makedirs ( gamePath )
# Seed the random generator to the clock
random . seed ( )
# Set game's name
global gameName
gameName = 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 instructions ( ) :
2019-11-30 18:31:23 -05:00
# Read in the instructions file
try :
with open ( ' files/instructions.txt ' , ' r ' ) as f :
info = f . readlines ( )
except :
info = [ " Instructions file is missing. " ]
display_text ( info )
2019-11-24 11:26:13 -05:00
def credits ( ) :
2019-11-30 18:31:23 -05:00
info = [
2019-11-24 11:26:13 -05:00
gameName + " : brought to you by Storm Dragon " , \
" Billy Wolfe, designer and coder. " , \
2019-11-30 18:31:23 -05:00
" https://social.wolfe.casa/storm " ]
display_text ( info )
def display_text ( text ) :
2019-11-24 11:26:13 -05:00
i = 0
2019-11-30 19:44:17 -05:00
text . insert ( 0 , " Press space to read the whole text. Use up and down arrows to navigate the text line by line. Press c to copy the current line to the clipboard or t to copy the entire text. Press enter or escape when you are done reading. " )
2019-11-30 18:31:23 -05:00
text . append ( " End of text. " )
speak ( text [ i ] )
2019-11-24 11:26:13 -05:00
while True :
event = pygame . event . wait ( )
if event . type == pygame . KEYDOWN :
if event . key == pygame . K_ESCAPE or event . key == pygame . K_RETURN : return
2019-11-30 18:31:23 -05:00
if event . key == pygame . K_DOWN and i < len ( text ) - 1 : i = i + 1
2019-11-24 11:26:13 -05:00
if event . key == pygame . K_UP and i > 0 : i = i - 1
2019-11-30 18:31:23 -05:00
if event . key == pygame . K_SPACE :
speak ( ' ' . join ( text [ 1 : ] ) )
else :
speak ( text [ i ] )
2019-11-30 19:44:17 -05:00
if event . key == pygame . K_c :
try :
pyperclip . copy ( text [ i ] )
speak ( " Copied " + text [ i ] + " to the clipboard. " )
except :
speak ( " Failed to copy the text to the clipboard. " )
if event . key == pygame . K_t :
try :
pyperclip . copy ( ' ' . join ( text [ 1 : - 1 ] ) )
speak ( " Copied entire message to the clipboard. " )
except :
speak ( " Failed to copy the text to the clipboard. " )
2019-11-24 11:26:13 -05:00
event = pygame . event . clear ( )
time . sleep ( 0.001 )
def game_menu ( * options ) :
loop = True
pygame . mixer . music . load ( " sounds/music_menu.ogg " )
pygame . mixer . music . set_volume ( 0.75 )
pygame . mixer . music . play ( - 1 )
i = 0
speak ( options [ i ] )
while loop == True :
event = pygame . event . wait ( )
if event . type == pygame . KEYDOWN :
if event . key == pygame . K_ESCAPE : exit_game ( )
2019-11-26 18:42:07 -05:00
if event . key == pygame . K_DOWN and i < len ( options ) - 1 :
i = i + 1
2019-11-26 20:03:25 -05:00
if options [ i ] != " donate " : pygame . mixer . music . unpause ( )
2019-11-26 18:42:07 -05:00
if event . key == pygame . K_UP and i > 0 :
i = i - 1
2019-11-26 20:03:25 -05:00
if options [ i ] != " donate " : pygame . mixer . music . unpause ( )
2019-11-24 11:26:13 -05:00
if event . key == pygame . K_RETURN :
try :
eval ( options [ i ] + " () " )
continue
except :
return options [ i ]
continue
speak ( options [ i ] )
event = pygame . event . clear ( )
time . sleep ( 0.001 )
2019-11-26 18:42:07 -05:00
2019-11-27 14:55:02 -05:00
def donate ( ) :
2019-11-26 18:42:07 -05:00
pygame . mixer . music . pause ( )
2019-11-27 14:55:02 -05:00
subprocess . call ( [ " xdg-open " , " https://liberapay.com/stormdragon2976/donate " ] )