-
Notifications
You must be signed in to change notification settings - Fork 12
Game with score and sound effects #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
QWERTY-L
wants to merge
3
commits into
XavierCodingClub:master
Choose a base branch
from
QWERTY-L:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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() | ||
|
|
||
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 <gundlach@gmail.com>'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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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']) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh yeah here it is
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
me casually showing off my github skills 😎
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
although they are completely useless in this context
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol, That is pretty cool tho
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, i want to actually contribute to a project in this club. I never used github other than for stupid things. Those repos are private ofc just in case I ever want a career in cs.