This commit is contained in:
chrys 2016-03-30 22:48:40 +02:00
commit e85b076537
10 changed files with 210 additions and 2 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
__pycache__/

View File

@ -4,8 +4,9 @@
from storm_games import *
# Initial variable settings
gameName = "Mine Racer"
mode = "menu"
sounds = initialize_gui("Mine Racer")
sounds = initialize_gui(gameName)
def game():
pygame.mixer.music.load("sounds/music_car.ogg")

View File

@ -39,6 +39,7 @@ def initialize_gui(gameTitle):
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])
@ -53,7 +54,6 @@ def game_menu(*options):
eval(options[i] + "()")
continue
except:
pygame.mixer.music.fadeout(500)
time.sleep(0.25)
return options[i]
continue

64
numnastics/numnastics.py Executable file
View File

@ -0,0 +1,64 @@
#!/bin/python
# -*- coding: utf-8 -*-
from storm_games import *
# Initial variable settings
mode = "menu"
sounds = initialize_gui("Numnastics")
def game(mode):
i = 0
startTime = time.time()
tries = 0
numberList = list("123456789")
random.shuffle(numberList)
while ''.join(numberList) != "123456789":
event = pygame.event.wait()
if event.type == pygame.KEYDOWN:
# Escape is the back/exit key, close the game if not playing, or return to menu if playing.
if event.key == pygame.K_ESCAPE:
if mode != "menu":
mode = "menu"
return mode
elif mode == "menu": exit_game()
elif event.key in [pygame.K_1, pygame.K_2, pygame.K_3, pygame.K_4, pygame.K_5, pygame.K_6,pygame.K_7, pygame.K_8, pygame.K_9]:
i = numberList.index((pygame.key.name(event.key)))
speak(numberList[i])
elif event.key in [pygame.K_LEFT, pygame.K_UP]:
if i > 0: i = i - 1
speak(numberList[i])
elif event.key in [pygame.K_RIGHT, pygame.K_DOWN]:
if i < len(numberList) - 1: i = i + 1
speak(numberList[i])
elif event.key == pygame.K_SPACE:
speak(str(' '.join(numberList[i:len(numberList)])))
continue
elif event.key == pygame.K_RETURN:
if i != -1:
reversedNumberList = numberList[i:len(numberList)]
reversedNumberList.reverse()
del numberList[i:len(numberList)]
numberList.extend(reversedNumberList)
tries = tries + 1
sounds['flip'].play()
speak(str(' '.join(numberList[i:len(numberList)])))
else:
i = -1
sounds['error'].play()
endTime = round(time.time() - startTime, 2)
message = [
"Congratulations! You beat Numnastics in " + str(tries) + " tries.",\
"Your time was " + str(endTime) + " seconds."]
display_message(message)
sounds['win'].play()
time.sleep(sounds['win'].get_length())
return "menu"
# Game starts at main menu
mode = game_menu("start game", "instructions", "credits", "exit_game")
while True:
if mode == "menu": mode = game_menu("start game", "instructions", "credits", "exit_game")
if mode == "start game": mode = game(mode)
time.sleep(.001)

BIN
numnastics/sounds/error.ogg Normal file

Binary file not shown.

BIN
numnastics/sounds/flip.ogg Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

142
numnastics/storm_games.py Executable file
View File

@ -0,0 +1,142 @@
#!/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
import random
import requests
import speechd
import time
config = configparser.ConfigParser()
spd = speechd.Client()
#def write_config(config, path = gamePath + "/config"):
#with open(path, 'w') as configfile:
#config.write(configfile)
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):
# Check for, and possibly create, storm-games path
global HOME
global gamePath
HOME = BaseDirectory.xdg_config_home + "/storm-games"
gamePath = HOME + "/" + str.lower(str.replace(gameTitle, " ", "-"))
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 display_message(info):
info.append("Press escape or enter to continue.")
info.reverse()
info.append("Use the up and down arrow keys to navigate this message.")
info.reverse()
i = 0
speak(str(info[0:len(info)]))
while True:
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)
def instructions():
info = (
"Welcome to " + gameName + ": brought to you by Storm Dragon. Use the up and down arrows to navigate these instructions.",\
"The object of the game is to arrange the random string of numbers so they read one through nine in as few tries as possible.",\
"You can use the up or left arrow to move back in the string, and the down or right arrow to move forward, or close to the end of the string of numbers.",\
"you can also jump directly to the number you want by pressing it on your keyboard. If you want to go to the number 8 in the string, just press 8.",\
"When you are on the number you want, press the enter key and that number, plus all the numbers to the end of the string, will be reversed.",\
"For example, if you have the string of numbers 1 2 3 4 5 6 9 8 7, pressing enter while on the number 9 will reverse 9 8 7, making the string 1 2 3 4 5 6 7 8 9 and you will win the game.",\
"If you need to her the string of numbers from your current position, press the spacebar.",\
"Have fun, and good luck!",\
"Press escape or enter to return to the game menu.")
i = 0
speak(info[i])
while True:
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)
def credits():
info = (
gameName + ": brought to you by Storm Dragon",\
"Billy Wolfe, designer and coder.",\
"http://stormdragon.tk",\
"Press escape or enter to return to the game menu.")
i = 0
speak(info[i])
while True:
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)
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()
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:
pygame.mixer.music.fadeout(500)
time.sleep(0.25)
return options[i]
continue
speak(options[i])
event = pygame.event.clear()
time.sleep(0.001)