diff --git a/RockPaperScissors.py b/RockPaperScissors.py index b7cc796..93de9f7 100644 --- a/RockPaperScissors.py +++ b/RockPaperScissors.py @@ -1,44 +1,117 @@ ## # # Author: FX Coding Club +# +# Edited by Luke Sequeira +# # Date 9/24/2020 # +# +# 1 => rock +# 2 => paper +# 3 => scissors +# +# + +import random +from libs import playsound + +sound = True + +score = 0 # Scores and +games = 0 # Game played + +foo = input("Play with sound? y or n (if you do not choose a valid option, y is selected by default)") +if(foo == 'n'): + sound = False + print("Sound has been turned off") -# These are the possible choices in the game, using the randint function, the computer will be a choice based on the integer it chooses -list = ['rock', 'paper', 'scissors'] +print("Welcome to Rock-Paper-Scissors. Type end anytime to exit.") +def game(): + global score + global games + global sound + + games += 1 + + random.seed() + computerChoice = random.randint(1, 3) -# Compare var1 and computerChoice to see who wins + #get a text value from the int chosen + if(computerChoice == 1): + comptext = "rock" + if(computerChoice == 2): + comptext = "paper" + if(computerChoice == 3): + comptext = "scissors" -# The following is all the comparisons if the computer chooses "Rock" -if computerChoice == 'rock' and var1 == 'scissors'): - print('You chose ' + var1 + ' and the computer chose ' + - computerChoice + ', The computer wins!') + playerChoice = input("Choose rock, paper or scisors\n>>> ") + if(playerChoice == 'end'): + print("Ending Game, We'll miss you!") + exit() + if(playerChoice == 'cheat'): + score += int(input("How much do you want to add to you score? ")); + game() + + tc = playerChoice[0] + if(tc != 's' and tc != 'r' and tc != 'p'): + print("Please enter something begining with r, p or s (roc is ok but not ock)") + playerChoice = input("Choose rock, paper or scisors\n>>> ") -elif computerChoice == 'rock' and var1 == 'paper'): - print('You chose ' + var1 + ' and the computer chose ' + - computerChoice + ', You win!') + # Compare playerChoice and computerChoice to see who wins -# The following is all the comparisons if the computer chooses "Paper" -elif computerChoice == 'paper' and var1 == 'rock'): - print('You chose ' + var1 + ' and the computer chose ' + - computerChoice + ', The computer wins!') + # The following is all the comparisons if the computer chooses "Rock" + if (computerChoice == 1 and playerChoice[0] == 's'): + print('You chose ' + playerChoice + ' and the computer chose ' + + comptext + ', The computer wins!') + if(sound): + playsound.playsound("boo.wav") -elif computerChoice == 'paper' and var1 == 'scissors'): - print('You chose ' + var1 + ' and the computer chose ' + - computerChoice + ', You win!') + elif (computerChoice == 1 and playerChoice[0] == 'p'): + print('You chose ' + playerChoice + ' and the computer chose ' + + comptext + ', You win!') + score += 1 + if(sound): + playsound.playsound("correct_answer_music.mp3") + + # The following is all the comparisons if the computer chooses "Paper" + elif (computerChoice == 2 and playerChoice[0] == 'r'): + print('You chose ' + playerChoice + ' and the computer chose ' + + comptext + ', The computer wins!') + if(sound): + playsound.playsound("boo.wav") -# The following is all the comparisons if the computer chooses "Scissors" -elif computerChoice == 'scissors' and var1 == 'paper'): - print('You chose ' + var1 + ' and the computer chose ' + - computerChoice + ', The computer wins!') + elif (computerChoice == 2 and playerChoice[0] == 's'): + print('You chose ' + playerChoice + ' and the computer chose ' + + comptext + ', You win!') + score += 1 + if(sound): + playsound.playsound("correct_answer_music.mp3") -elif computerChoice == 'scissors' and var1 == 'rock'): - print('You chose ' + var1 + ' and the computer chose ' + - computerChoice + ', You win!') + # The following is all the comparisons if the computer chooses "Scissors" + elif (computerChoice == 3 and playerChoice[0] == 'p'): + print('You chose ' + playerChoice + ' and the computer chose ' + + comptext + ', The computer wins!') + score += 1 + if(sound): + playsound.playsound("boo.wav") -# The case in which both the computer and the user chose the same one, and they tie -else: - print('You chose ' + var1 + ' and the computer chose ' + - computerChoice + ', you tied!') + elif (computerChoice == 3 and playerChoice[0] == 'r'): + print('You chose ' + playerChoice + ' and the computer chose ' + + comptext + ', You win!') + score += 1 + if(sound): + playsound.playsound("correct_answer_music.mp3") + + # The case in which both the computer and the user chose the same one, and they tie + else: + print('You chose ' + str(playerChoice) + ' and the computer chose ' + + comptext + ', you tied!') + score += 0.5 + print("You've won " + str(score) + " out of " + str(games) + " games (draws count as 0.5). That is a " + str(score/games * 100) + "% avergage!") + game() + +game() + diff --git a/boo.wav b/boo.wav new file mode 100644 index 0000000..5e58a38 Binary files /dev/null and b/boo.wav differ diff --git a/correct_answer_music.mp3 b/correct_answer_music.mp3 new file mode 100644 index 0000000..1212876 Binary files /dev/null and b/correct_answer_music.mp3 differ diff --git a/libs/__init__.py b/libs/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/libs/__pycache__/__init__.cpython-37.pyc b/libs/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000..8846b05 Binary files /dev/null and b/libs/__pycache__/__init__.cpython-37.pyc differ diff --git a/libs/__pycache__/playsound.cpython-37.pyc b/libs/__pycache__/playsound.cpython-37.pyc new file mode 100644 index 0000000..9c4d763 Binary files /dev/null and b/libs/__pycache__/playsound.cpython-37.pyc differ diff --git a/libs/playsound.py b/libs/playsound.py new file mode 100644 index 0000000..1414011 --- /dev/null +++ b/libs/playsound.py @@ -0,0 +1,125 @@ +class PlaysoundException(Exception): + pass + +def _playsoundWin(sound, block = True): + ''' + Utilizes windll.winmm. Tested and known to work with MP3 and WAVE on + Windows 7 with Python 2.7. Probably works with more file formats. + Probably works on Windows XP thru Windows 10. Probably works with all + versions of Python. + + Inspired by (but not copied from) Michael Gundlach 's mp3play: + https://github.com/michaelgundlach/mp3play + + I never would have tried using windll.winmm without seeing his code. + ''' + from ctypes import c_buffer, windll + from random import random + from time import sleep + from sys import getfilesystemencoding + + def winCommand(*command): + buf = c_buffer(255) + command = ' '.join(command).encode(getfilesystemencoding()) + errorCode = int(windll.winmm.mciSendStringA(command, buf, 254, 0)) + if errorCode: + errorBuffer = c_buffer(255) + windll.winmm.mciGetErrorStringA(errorCode, errorBuffer, 254) + exceptionMessage = ('\n Error ' + str(errorCode) + ' for command:' + '\n ' + command.decode() + + '\n ' + errorBuffer.value.decode()) + raise PlaysoundException(exceptionMessage) + return buf.value + + alias = 'playsound_' + str(random()) + winCommand('open "' + sound + '" alias', alias) + winCommand('set', alias, 'time format milliseconds') + durationInMS = winCommand('status', alias, 'length') + winCommand('play', alias, 'from 0 to', durationInMS.decode()) + + if block: + sleep(float(durationInMS) / 1000.0) + +def _playsoundOSX(sound, block = True): + ''' + Utilizes AppKit.NSSound. Tested and known to work with MP3 and WAVE on + OS X 10.11 with Python 2.7. Probably works with anything QuickTime supports. + Probably works on OS X 10.5 and newer. Probably works with all versions of + Python. + + Inspired by (but not copied from) Aaron's Stack Overflow answer here: + http://stackoverflow.com/a/34568298/901641 + + I never would have tried using AppKit.NSSound without seeing his code. + ''' + from AppKit import NSSound + from Foundation import NSURL + from time import sleep + + if '://' not in sound: + if not sound.startswith('/'): + from os import getcwd + sound = getcwd() + '/' + sound + sound = 'file://' + sound + url = NSURL.URLWithString_(sound) + nssound = NSSound.alloc().initWithContentsOfURL_byReference_(url, True) + if not nssound: + raise IOError('Unable to load sound named: ' + sound) + nssound.play() + + if block: + sleep(nssound.duration()) + +def _playsoundNix(sound, block=True): + """Play a sound using GStreamer. + + Inspired by this: + https://gstreamer.freedesktop.org/documentation/tutorials/playback/playbin-usage.html + """ + if not block: + raise NotImplementedError( + "block=False cannot be used on this platform yet") + + # pathname2url escapes non-URL-safe characters + import os + try: + from urllib.request import pathname2url + except ImportError: + # python 2 + from urllib import pathname2url + + import gi + gi.require_version('Gst', '1.0') + from gi.repository import Gst + + Gst.init(None) + + playbin = Gst.ElementFactory.make('playbin', 'playbin') + if sound.startswith(('http://', 'https://')): + playbin.props.uri = sound + else: + playbin.props.uri = 'file://' + pathname2url(os.path.abspath(sound)) + + set_result = playbin.set_state(Gst.State.PLAYING) + if set_result != Gst.StateChangeReturn.ASYNC: + raise PlaysoundException( + "playbin.set_state returned " + repr(set_result)) + + # FIXME: use some other bus method than poll() with block=False + # https://lazka.github.io/pgi-docs/#Gst-1.0/classes/Bus.html + bus = playbin.get_bus() + bus.poll(Gst.MessageType.EOS, Gst.CLOCK_TIME_NONE) + playbin.set_state(Gst.State.NULL) + + +from platform import system +system = system() + +if system == 'Windows': + playsound = _playsoundWin +elif system == 'Darwin': + playsound = _playsoundOSX +else: + playsound = _playsoundNix + +del system \ No newline at end of file diff --git a/libs/setup.py b/libs/setup.py new file mode 100644 index 0000000..2c778b8 --- /dev/null +++ b/libs/setup.py @@ -0,0 +1,39 @@ +from codecs import open +from inspect import getsource +from os.path import abspath, dirname, join +from setuptools import setup + +here = abspath(dirname(getsource(lambda:0))) + +with open(join(here, 'README.rst'), encoding='utf-8') as f: + long_description = f.read() + +setup(name = 'playsound', + version = '1.2.1', + description = long_description.splitlines()[2][1:-1], + long_description = long_description, + url = 'https://github.com/TaylorSMarks/playsound', + author = 'Taylor Marks', + author_email = 'taylor@marksfam.com', + license = 'MIT', + classifiers = ['Development Status :: 5 - Production/Stable', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: MIT License', + 'Operating System :: OS Independent', + 'Programming Language :: Python :: 2', + 'Programming Language :: Python :: 2.3', + 'Programming Language :: Python :: 2.4', + 'Programming Language :: Python :: 2.5', + 'Programming Language :: Python :: 2.6', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.1', + 'Programming Language :: Python :: 3.2', + 'Programming Language :: Python :: 3.3', + 'Programming Language :: Python :: 3.4', + 'Programming Language :: Python :: 3.5', + 'Topic :: Multimedia :: Sound/Audio :: MIDI', + 'Topic :: Multimedia :: Sound/Audio :: Players', + 'Topic :: Multimedia :: Sound/Audio :: Players :: MP3'], + keywords = 'sound playsound music wave wav mp3 media song play audio', + py_modules = ['playsound']) \ No newline at end of file