-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinalSemesterProject.py
More file actions
252 lines (205 loc) · 9.01 KB
/
Copy pathFinalSemesterProject.py
File metadata and controls
252 lines (205 loc) · 9.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
## I hereby certify that this program is solely the result of my own work and
## is in compliance with the Academic Integrity policy of the course syllabus
## and the academic integrity policy of the CS department.
import Draw
import time
import random
canvasWidth = 1000
canvasHeight = 900
noteWidth = 18
noteHeight = 15
noteGap = 200
def makeNoteList():
key = ["A", "B", "C", "D", "E", "F", "G"]
hand = ["R", "L"]
octave = ["low", "high"]
currentX = 0
noteList = []
# generate a list of 100 notes composed of randomized elements
for i in range(100):
noteList += [[random.choice(key), random.choice(hand), Draw.BLACK, random.choice(octave), currentX]]
currentX -= (noteWidth + noteGap)
return noteList
def moveNotes(Notes):
rightmostNote = 0
for i in range(len(Notes)):
Notes[i][4] += 0.8 # move notes 1.5 pixels at a time
# change note to yellow if within danger zone
if Notes[i][4] > canvasWidth - noteGap:
if Notes[i][2] == Draw.BLACK:
Notes[i][2] = Draw.ORANGE
# if rightmost note is off the screen or is green or is red, the next note becomes rightmost note
if Notes[rightmostNote][4] > canvasWidth or Notes[rightmostNote][2] == Draw.GREEN or Notes[rightmostNote][2] == Draw.RED:
rightmostNote = i
return rightmostNote # returns index of the right most note
def playGame(Notes):
# Introductory frame displays until the user presses the start button
while not Draw.mousePressed() or \
not(Draw.mouseX() > 375 and Draw.mouseX() < 545 and \
Draw.mouseY() > 200 and Draw.mouseY() < 270):
Draw.clear()
Draw.setFontFamily('Courier')
Draw.setFontSize(50)
Draw.string("PIANO NOTES GAME", 250, 140)
Draw.string("Press ", 200, 210)
Draw.setColor(Draw.GREEN)
Draw.filledRect(375, 200, 170, 70)
Draw.setColor(Draw.BLACK)
Draw.string("START", 385, 210)
Draw.string("to begin", 580, 210)
Draw.setFontSize(25)
Draw.setColor(Draw.RED)
Draw.string("Instructions: As the notes move past the screen, \
\ntype the letter of the rightmost note. Good luck!", 100, 50)
Draw.setColor(Draw.BLACK)
## Draw the piano in the opening frame
# frame of piano keys
Draw.rect(10, 325, canvasWidth-20, canvasHeight-525)
# white keys and letters
Draw.setFontFamily('Times')
x = 10
y = ord("C")
for i in range(17):
Draw.rect(x, 325, (canvasWidth-20)/17, canvasHeight-525)
Draw.setFontSize(16)
Draw.setFontBold(True)
Draw.string(str(chr(y)), x+25, 650)
y += 1
x += (canvasWidth-20)/17
if y == ord("H"):
y = ord("A")
# black keys
y = 1
for i in range(16):
if i != 2 and i != 6 and i!= 9 and i != 13:
Draw.filledRect((canvasWidth-20)/(17/y) - 5, 325, (canvasWidth-20)/17 - 25, canvasHeight-625)
y += 1
Draw.setFontBold(False)
Draw.show()
# initial variables for the game
startingTime = 61 + time.time()
currentTime = int(startingTime - time.time())
currentScore = 0
# game ends when time runs out
while currentTime > 0:
# time goes down by the second
currentTime = int(startingTime - time.time())
moveNotes(Notes)
# discount any typing the user does before beginning the game
if currentTime >= 60:
if Draw.hasNextKeyTyped():
irrelevantType = Draw.nextKeyTyped()
# game responds to the user typing a key
elif Draw.hasNextKeyTyped():
pressed = Draw.nextKeyTyped()
# if the user pressed the correct key, increment score by 1 and change note to green
if pressed.upper() == Notes[moveNotes(Notes)][0] or pressed == Notes[moveNotes(Notes)][0]:
currentScore += 1
Notes[moveNotes(Notes)][2] = Draw.GREEN
# if the user pressed the wrong key, decrement score by 1 and change note to red
else:
currentScore -= 1
Notes[moveNotes(Notes)][2] = Draw.RED
drawBoard(Notes, currentTime, currentScore)
# final screen once user finishes game
Draw.clear()
Draw.setBackground(Draw.BLACK)
Draw.setColor(Draw.WHITE)
Draw.setFontBold(True)
Draw.setFontSize(80)
Draw.string("Your score: " + str(currentScore), 250, 300)
Draw.setFontSize(40)
# low score, you can do better
if currentScore <= 6:
Draw.string("You can do better, Beethoven", 260, 400)
# medium score, not too shabby
elif currentScore > 6 and currentScore < 21:
Draw.string("Not too shabby, Debussy", 280, 400)
# high score, well done!
else:
Draw.string("You're practically Mozart!", 265, 400)
Draw.show()
def drawBoard(Notes, currentTime, currentScore):
Draw.clear()
# frame of piano keys
Draw.rect(10, 325, canvasWidth-20, canvasHeight-525)
# score and time
Draw.setFontSize(40)
Draw.setFontFamily("Times")
Draw.setFontBold(False)
Draw.string("Score: " + str(currentScore), 140, 720)
Draw.string("Time: " + str(currentTime), 640, 720)
# white keys and letters
x = 10
y = ord("C")
for i in range(17):
Draw.rect(x, 325, (canvasWidth-20)/17, canvasHeight-525)
Draw.setFontSize(16)
Draw.setFontBold(True)
Draw.string(str(chr(y)), x+25, 650)
y += 1
x += (canvasWidth-20)/17
if y == ord("H"):
y = ord("A")
# black keys
y = 1
for i in range(16):
if i != 2 and i != 6 and i!= 9 and i != 13:
Draw.filledRect((canvasWidth-20)/(17/y) - 5, 325, (canvasWidth-20)/17 - 25, canvasHeight-625)
y += 1
# treble clef and bass clef images that are downloaded in my comp sci folder
Draw.picture("treble.gif", 3, 25)
Draw.picture("bass.gif", -10, 175)
# music bars
z = 45
m = 185
for i in range(5):
Draw.line(10, z, canvasWidth-10, z) # right hand
Draw.line(10, m, canvasWidth-10, m) # left hand
z += 15
m += 15
for i in range(len(Notes)):
Draw.setColor(Notes[i][2])
# RIGHT HANDS NOTES
if Notes[i][0] == "C" and Notes[i][1] == "R" and Notes[i][3] == "low":
Draw.filledOval(Notes[i][4], 112, noteWidth, noteHeight) # Notes[i][4] refers to the currentX
Draw.line(Notes[i][4] - 3, 119, Notes[i][4] + noteWidth + 3, 119)
elif Notes[i][0] == "C" and Notes[i][1] == "R" and Notes[i][3] == "high":
Draw.filledOval(Notes[i][4], 60, noteWidth, noteHeight)
elif Notes[i][0] == "D" and Notes[i][1] == "R" and Notes[i][3] == "low":
Draw.filledOval(Notes[i][4], 105, noteWidth, noteHeight)
elif Notes[i][0] == "D" and Notes[i][1] == "R" and Notes[i][3] == "high":
Draw.filledOval(Notes[i][4], 52, noteWidth, noteHeight)
elif Notes[i][0] == "E" and Notes[i][1] == "R" and Notes[i][3] == "low":
Draw.filledOval(Notes[i][4], 99, noteWidth, noteHeight)
elif Notes[i][0] == "E" and Notes[i][1] == "R" and Notes[i][3] == "high":
Draw.filledOval(Notes[i][4], 45, noteWidth, noteHeight)
elif Notes[i][0] == "F" and Notes[i][1] == "R":
Draw.filledOval(Notes[i][4], 91, noteWidth, noteHeight)
elif Notes[i][0] == "G" and Notes[i][1] == "R":
Draw.filledOval(Notes[i][4], 84, noteWidth, noteHeight)
elif Notes[i][0] == "A" and Notes[i][1] == "R":
Draw.filledOval(Notes[i][4], 76, noteWidth, noteHeight)
elif Notes[i][0] == "B" and Notes[i][1] == "R":
Draw.filledOval(Notes[i][4], 69, noteWidth, noteHeight)
# LEFT HAND NOTES
elif Notes[i][0] == "C" and Notes[i][1] == "L":
Draw.filledOval(Notes[i][4], 216, noteWidth, noteHeight)
elif Notes[i][0] == "D" and Notes[i][1] == "L":
Draw.filledOval(Notes[i][4], 209, noteWidth, noteHeight)
elif Notes[i][0] == "E" and Notes[i][1] == "L":
Draw.filledOval(Notes[i][4], 200, noteWidth, noteHeight)
elif Notes[i][0] == "F" and Notes[i][1] == "L":
Draw.filledOval(Notes[i][4], 193, noteWidth, noteHeight)
elif Notes[i][0] == "G" and Notes[i][1] == "L":
Draw.filledOval(Notes[i][4], 185, noteWidth, noteHeight)
elif Notes[i][0] == "A" and Notes[i][1] == "L":
Draw.filledOval(Notes[i][4], 178, noteWidth, noteHeight)
elif Notes[i][0] == "B" and Notes[i][1] == "L":
Draw.filledOval(Notes[i][4], 171, noteWidth, noteHeight)
Draw.show()
def main():
Draw.setCanvasSize(canvasWidth, canvasHeight)
notes = makeNoteList()
playGame(notes)
main()