0% found this document useful (0 votes)
45 views

Final

This document contains code for a game called "Reverse Name That Tune" where the player hears short clips of songs played backwards and tries to name the song. It defines classes to represent audio clips that can be played normally or reversed. The code selects a random song within a chosen category, plays it backwards, displays song options, and checks the player's input against the answer until they get it correct or choose to play again.

Uploaded by

api-359332959
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Final

This document contains code for a game called "Reverse Name That Tune" where the player hears short clips of songs played backwards and tries to name the song. It defines classes to represent audio clips that can be played normally or reversed. The code selects a random song within a chosen category, plays it backwards, displays song options, and checks the player's input against the answer until they get it correct or choose to play again.

Uploaded by

api-359332959
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1 # Ngoan Nguyen

2 # Chris Pina
3 # Ken Vader
4 # Final Project
5 # CST-205
6
7 import random
8
9 # 1, 2, 3 are categories
10 titlesDictionary = {
11 "1-01" : "Every Breath You Take - The Police", "1-02" : "Eye Of The Tiger -
Survivor", "1-03" : "Girls Just Want To Have Fun - Cyndi Lauper", "1-04" :
"Thriller - Michael Jackson", "1-05" : "Wake Me Up Before You Go-Go - Wham!",
12 "2-01" : "All I Want For Christmas - Mariah Carey", "2-02" : "Feliz Navidad -
Jose Feliciano", "2-03" : "It's The Most Wonderful Time Of The Year - Andy
Williams", "2-04" : "Last Christmas - Wham!", "2-05" : "Rockin' Around The
Christmas Three - Brenda Lee",
13 "3-01" : "Can't Feel My Face - The Weeked", "3-02" : "Hello - Adele", "3-03" :
"On My Mind - Ellie Goulding", "3-04" : "Sorry - Justin Bieber", "3-05" :
"Wildest Dreams - Taylor Swift"
14 }
15
16 # keeps track of game variables
17 baseDirectory = "C:\\Users\\zero\\Documents\\GitHub\\CST-205-Final-Project\\sounds\\"
18 maxSongsPerCategory = 5
19 randomInteger = None
20 gameOver = false
21 currentSound = None
22
23 # Audio class. Attributes are title as a string, hint as a string,
24 # and sound object. Class methods are playSound to play the clip normally
25 # and playReverse to play the clip backwards
26 class Audio(object):
27
28 # Initialization requires a title(string), hint(string), and file location(string)
29 # Initializer then takes file passed in and creates two sound objects, one normal
30 # and one reversed
31 def __init__(self, title, soundFile):
32 self.title = title
33 self.soundFile = soundFile
34 self.soundFile = makeSound(self.soundFile)
35 self.reversedSound = self.reverse()
36
37 # Plays sound clip normally
38 def playSound(self):
39 play(self.soundFile)
40
41 # Stops playing the audio clip
42 def stopSound(self):
43 stopPlaying(self.soundFile)
44
45 # Plays reversed version of audio
46 def playReverse(self):
47 play(self.reversedSound)
48
49 # Reverses audio, returns new audio object
50 def reverse(self):
51 reversed = makeEmptySound(getLength(self.soundFile), 11025)
52
53 # steps through loop from end to beginning, copying sample values
54 # into reversed sound object from beginning to end, thus reversing
55 # the sound object
56 for index, i in enumerate(range(getLength(self.soundFile) - 1, -1, -1)):
57 value = getSampleValueAt(self.soundFile, i)
58 setSampleValueAt(reversed, index, value)
59
60 return reversed
61
62 # displays a welcome message
63 def welcome():
64 showInformation("Welcome to Reverse Name That Tune\nThe ultimate test of your
musical knowledge.You will get to hear a very short clip of a popular song twice.
Your job is to correctly name the song. The twist... the song will be played
BACKWARDS! To win the game, guess one correct title. Miss all attempts in a
category and it is Game Over! Type EXIT at any time to quit. Ready to start?")
65
66 # Displays a list of categories to choose from. User enters an integer to choose
67 # Function returns an integer
68 def selectCategory():
69 selection = requestString("Select from the following categories:\n[1] 80's\n[2]
Holiday\n[3] Top Hits\n\nPlease enter your selection as an integer")
70
71 # loop if user selection was invalid (escaped)
72 if selection == None:
73 return selectCategory()
74 elif int(selection) < 1 or int(selection) > maxSongsPerCategory:
75 showInformation("Invalid input. Please enter an integer between 1 and %d" %
maxSongsPerCategory)
76 return selectCategory()
77 return selection
78
79 # Helper to create file name
80 def parseSelection(category, choice):
81 return str(category) + "-0" + str(choice)
82
83 # Requests for user input, accepts a string to prompt
84 def getInput(string):
85 return requestString(string)
86
87 # selects and plays a random song within the selected category
88 def playRandomSong(categorySelection):
89 global currentSound
90 randomIndex = random.randint(1, maxSongsPerCategory)
91 randomFile = parseSelection(categorySelection, randomIndex)
92 songTitle = titlesDictionary[randomFile] + randomFile
93 currentSound = Audio(songTitle, baseDirectory + randomFile + ".wav")
94 currentSound.playReverse()
95 return randomFile
96
97 # Creates string to prompt the user for the song selections
98 def displaySongSelection(selection):
99 displayArray = []
100 for p in range(0, maxSongsPerCategory):
101 title = "[" + str(p + 1) + "] " + titlesDictionary[str(selection) + "-0" + str(p
+ 1)] + "\n"
102 displayArray.append(title)
103 displayArray.append("-------\n[9] Start Over")
104 return displayArray
105
106 def playGame():
107 # resets the game over variable in case we restart the game
108 global gameOver
109 gameOver = false
110
111 # stops any currently playing sound
112 if currentSound:
113 currentSound.stopSound()
114
115 # displays welcome message
116 welcome()
117
118 # Asks the user to choose a category
119 categorySelection = selectCategory()
120
121 # Picks random song from category
122 randomFile = playRandomSong(categorySelection)
123
124 displayArray = displaySongSelection(categorySelection)
125
126 # initial user input
127 userInput = getInput("" .join(displayArray)).lower()
128
129 # if the user didn't enter 'exit', or the game was not over, or the user did not
press escape
130 while (userInput != "exit" and gameOver == false and userInput != None):
131 parsedSongChoice = parseSelection(categorySelection, userInput)
132
133 # user guessed the right song!
134 if parsedSongChoice == randomFile:
135 tryAgain = getInput("CONGRATULATIONS! Would you like to play
again?\n\n[Y]es\n[N]o").lower()
136 if tryAgain.lower() == "y":
137 playGame()
138 else:
139 gameOver = true
140 reset()
141 elif userInput == "9":
142 playGame()
143 else:
144 userInput = getInput("" .join(displayArray) + "\nTry Again!").lower()
145
146 def reset():
147 gameOver = false
148 currentSound = None
149

You might also like