Skip to content

Commit

Permalink
add celebrate music
Browse files Browse the repository at this point in the history
  • Loading branch information
vertexi committed Feb 19, 2022
1 parent f2140ed commit 19f65f1
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 58 deletions.
4 changes: 3 additions & 1 deletion control.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ def __init__(self, time_threshold, callback_handle):

def tick(self):
if utime.ticks_diff(utime.ticks_ms(), self.start_time) > self.time_threshold:
self.callback_handle()
res = self.callback_handle()
self.start_time = utime.ticks_ms()
return res
return True


class JoystickEvent:
Expand Down
17 changes: 11 additions & 6 deletions libs/buzzer_music.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,21 @@ def stop(self):
self.stopped = True

def start(self):
self.beat = -1
self.timer = 0
self.pwms = []
for pin in self.pins:
self.pwms.append(PWM(pin))
self.stopped = False

def toggle(self):
if self.stopped:
self.start()
else:
self.stop()
def continue_music(self):
if (self.timer % (self.tempo * self.end) == 0 and (not (self.timer == 0))):
self.beat = -1
self.timer = 0
self.pwms = []
for pin in self.pins:
self.pwms.append(PWM(pin))
self.stopped = False

def tick(self):
if (not self.stopped):
Expand Down Expand Up @@ -272,4 +277,4 @@ def tick(self):

return True
else:
return False
return False
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@
accelerometer.set_events([accelerometer.accel.getAcceleration_y, (-2, -0.5), 120, game.move_left],
[accelerometer.accel.getAcceleration_y, (0.5, 2), 120, game.move_right],
[accelerometer.accel.getAcceleration_x, (-2, -0.5), 120, game.move_down],
[accelerometer.accel.getAcceleration_x, (0.5, 2), 120, game.rotate])
[accelerometer.accel.getAcceleration_x, (0.5, 2), 250, game.rotate])
controller.set_accelerometer(accelerometer)

# set game controller
game.set_controller(controller)

# set theme music
game.set_theme_music(music.theme_music_event)
game.set_musics(music.musics)

game.run()
112 changes: 77 additions & 35 deletions music.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,90 @@
from control import DelayEvent

# tetris theme modified by https://onlinesequencer.net/2591896
song = '0 E6 4 43;4 B5 2 43;6 C6 2 43;8 D6 2 43;10 E6 1 43;11 D6 1 43;12 C6 ' \
'2 43;14 B5 2 43;20 A5 2 43;22 C6 2 43;24 E6 4 43;28 D6 2 43;30 C6 2 ' \
'43;32 B5 6 43;38 C6 2 43;40 D6 4 43;44 E6 4 43;48 C6 4 43;52 A5 4 ' \
'43;56 A5 8 43;66 D6 4 43;70 F6 2 43;72 A6 4 43;76 G6 2 43;78 F6 2 ' \
'43;80 E6 6 43;86 C6 2 43;88 E6 4 43;92 D6 2 43;94 C6 2 43;96 B5 4 ' \
'43;100 B5 2 43;102 C6 2 43;104 D6 4 43;108 E6 4 43;112 C6 4 43;116 ' \
'A5 4 43;120 A5 4 43;128 E6 4 43;132 B5 2 43;134 C6 2 43;136 D6 2 ' \
'43;138 E6 1 43;139 D6 1 43;140 C6 2 43;142 B5 2 43;144 A5 4 43;148 ' \
'A5 2 43;150 C6 2 43;152 E6 4 43;156 D6 2 43;158 C6 2 43;160 B5 6 ' \
'43;166 C6 2 43;168 D6 4 43;172 E6 4 43;176 C6 4 43;180 A5 4 43;184 ' \
'A5 8 43;194 D6 4 43;198 F6 2 43;200 A6 4 43;204 G6 2 43;206 F6 2 ' \
'43;208 E6 6 43;214 C6 2 43;216 E6 4 43;220 D6 2 43;222 C6 2 43;224 ' \
'B5 4 43;228 B5 2 43;230 C6 2 43;232 D6 4 43;236 E6 4 43;240 C6 4 ' \
'43;244 A5 4 43;248 A5 4 43;256 E5 8 43;264 C5 8 43;272 D5 8 43;280 ' \
'B4 8 43;288 C5 8 43;296 A4 8 43;304 G#4 8 43;312 B4 4 43;320 E5 8 ' \
'43;328 C5 8 43;336 D5 8 43;344 B4 8 43;352 C5 4 43;356 E5 4 43;360 ' \
'A5 8 43;368 G#5 8 43;384 E6 4 43;388 B5 2 43;390 C6 2 43;392 D6 2 ' \
'43;394 E6 1 43;395 D6 1 43;396 C6 2 43;398 B5 2 43;400 A5 4 43;404 ' \
'A5 2 43;406 C6 2 43;408 E6 4 43;412 D6 2 43;414 C6 2 43;416 B5 6 ' \
'43;422 C6 2 43;424 D6 4 43;428 E6 4 43;432 C6 4 43;436 A5 4 43;440 ' \
'A5 8 43;450 D6 4 43;454 F6 2 43;456 A6 4 43;460 G6 2 43;462 F6 2 ' \
'43;464 E6 6 43;470 C6 2 43;472 E6 4 43;476 D6 2 43;478 C6 2 43;480 ' \
'B5 4 43;484 B5 2 43;486 C6 2 43;488 D6 4 43;492 E6 4 43;496 C6 4 ' \
'43;500 A5 4 43;504 A5 4 43;512 E6 4 43;516 B5 2 43;518 C6 2 43;520 ' \
'D6 2 43;522 E6 1 43;523 D6 1 43;524 C6 2 43;526 B5 2 43;528 A5 4 ' \
'43;532 A5 2 43;534 C6 2 43;536 E6 4 43;540 D6 2 43;542 C6 2 43;544 ' \
'B5 6 43;550 C6 2 43;552 D6 4 43;556 E6 4 43;560 C6 4 43;564 A5 4 ' \
'43;568 A5 8 43;578 D6 4 43;582 F6 2 43;584 A6 4 43;588 G6 2 43;590 ' \
'F6 2 43;592 E6 6 43;598 C6 2 43;600 E6 4 43;604 D6 2 43;606 C6 2 ' \
'43;608 B5 4 43;612 B5 2 43;614 C6 2 43;616 D6 4 43;620 E6 4 43;624 ' \
'C6 4 43;628 A5 4 43;632 A5 4 43;16 A5 4 43 '
theme_music = '0 E6 4 43;4 B5 2 43;6 C6 2 43;8 D6 2 43;10 E6 1 43;11 D6 1 43;12 C6 ' \
'2 43;14 B5 2 43;20 A5 2 43;22 C6 2 43;24 E6 4 43;28 D6 2 43;30 C6 2 ' \
'43;32 B5 6 43;38 C6 2 43;40 D6 4 43;44 E6 4 43;48 C6 4 43;52 A5 4 ' \
'43;56 A5 8 43;66 D6 4 43;70 F6 2 43;72 A6 4 43;76 G6 2 43;78 F6 2 ' \
'43;80 E6 6 43;86 C6 2 43;88 E6 4 43;92 D6 2 43;94 C6 2 43;96 B5 4 ' \
'43;100 B5 2 43;102 C6 2 43;104 D6 4 43;108 E6 4 43;112 C6 4 43;116 ' \
'A5 4 43;120 A5 4 43;128 E6 4 43;132 B5 2 43;134 C6 2 43;136 D6 2 ' \
'43;138 E6 1 43;139 D6 1 43;140 C6 2 43;142 B5 2 43;144 A5 4 43;148 ' \
'A5 2 43;150 C6 2 43;152 E6 4 43;156 D6 2 43;158 C6 2 43;160 B5 6 ' \
'43;166 C6 2 43;168 D6 4 43;172 E6 4 43;176 C6 4 43;180 A5 4 43;184 ' \
'A5 8 43;194 D6 4 43;198 F6 2 43;200 A6 4 43;204 G6 2 43;206 F6 2 ' \
'43;208 E6 6 43;214 C6 2 43;216 E6 4 43;220 D6 2 43;222 C6 2 43;224 ' \
'B5 4 43;228 B5 2 43;230 C6 2 43;232 D6 4 43;236 E6 4 43;240 C6 4 ' \
'43;244 A5 4 43;248 A5 4 43;256 E5 8 43;264 C5 8 43;272 D5 8 43;280 ' \
'B4 8 43;288 C5 8 43;296 A4 8 43;304 G#4 8 43;312 B4 4 43;320 E5 8 ' \
'43;328 C5 8 43;336 D5 8 43;344 B4 8 43;352 C5 4 43;356 E5 4 43;360 ' \
'A5 8 43;368 G#5 8 43;384 E6 4 43;388 B5 2 43;390 C6 2 43;392 D6 2 ' \
'43;394 E6 1 43;395 D6 1 43;396 C6 2 43;398 B5 2 43;400 A5 4 43;404 ' \
'A5 2 43;406 C6 2 43;408 E6 4 43;412 D6 2 43;414 C6 2 43;416 B5 6 ' \
'43;422 C6 2 43;424 D6 4 43;428 E6 4 43;432 C6 4 43;436 A5 4 43;440 ' \
'A5 8 43;450 D6 4 43;454 F6 2 43;456 A6 4 43;460 G6 2 43;462 F6 2 ' \
'43;464 E6 6 43;470 C6 2 43;472 E6 4 43;476 D6 2 43;478 C6 2 43;480 ' \
'B5 4 43;484 B5 2 43;486 C6 2 43;488 D6 4 43;492 E6 4 43;496 C6 4 ' \
'43;500 A5 4 43;504 A5 4 43;512 E6 4 43;516 B5 2 43;518 C6 2 43;520 ' \
'D6 2 43;522 E6 1 43;523 D6 1 43;524 C6 2 43;526 B5 2 43;528 A5 4 ' \
'43;532 A5 2 43;534 C6 2 43;536 E6 4 43;540 D6 2 43;542 C6 2 43;544 ' \
'B5 6 43;550 C6 2 43;552 D6 4 43;556 E6 4 43;560 C6 4 43;564 A5 4 ' \
'43;568 A5 8 43;578 D6 4 43;582 F6 2 43;584 A6 4 43;588 G6 2 43;590 ' \
'F6 2 43;592 E6 6 43;598 C6 2 43;600 E6 4 43;604 D6 2 43;606 C6 2 ' \
'43;608 B5 4 43;612 B5 2 43;614 C6 2 43;616 D6 4 43;620 E6 4 43;624 ' \
'C6 4 43;628 A5 4 43;632 A5 4 43;16 A5 4 43 '

# https://onlinesequencer.net/145939
celebrate_music = '0 E5 1 0;1 E5 1 0;3 E5 1 0;5 C5 1 0;6 E5 1 0;8 G5 1 0;12 G4 1 0'


class MusicEvent(DelayEvent):
music: Music
def __init__(self, time_threshold, callback_handle, music):
super(MusicEvent, self).__init__(time_threshold, callback_handle)
self.music = music

def toggle(self):
self.music.toggle()
def start(self):
self.music.start()

def stop(self):
self.music.stop()

def continue_music(self):
self.music.continue_music()


# One buzzer on pin 23
theme_music = Music(song, looping=False, tempo=3, duty=256, pins=[Pin(23)])
theme_music_event = MusicEvent(40, theme_music.tick, theme_music)
theme_music = Music(theme_music, looping=True, tempo=3, duty=256, pins=[Pin(23)])
celebrate_music = Music(celebrate_music, looping=False, tempo=3, duty=256, pins=[Pin(23)])


class Musics:
def __init__(self, *args):
self.musics = []
for arg in args:
self.musics.append(MusicEvent(*arg))
self.current_music = self.musics[0]
self.pause_ = False

def tick(self, celebrate=False):
if celebrate:
self.current_music = self.musics[1]
self.current_music.start()
self.current_music.tick()
if not self.pause_:
if not self.current_music.tick():
self.current_music= self.musics[0]

def pause(self):
for music in self.musics:
music.stop()
self.pause_ = True

def continue_music(self):
for music in self.musics:
music.continue_music()
self.pause_ = False


musics = Musics([40, theme_music.tick, theme_music],
[40, celebrate_music.tick, celebrate_music])
32 changes: 18 additions & 14 deletions tetris.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import utime
from tetromino import Tetromino, tetrominos
import control
from music import MusicEvent
from music import Musics


class Score:
Expand Down Expand Up @@ -30,7 +30,7 @@ class Game:
game_over: bool
pause: bool
move_down_event: control.DelayEvent
theme_music: MusicEvent
musics: Musics

def __init__(self, display):
self.rows = 24
Expand Down Expand Up @@ -125,6 +125,8 @@ def detect_and_remove_line(self):
self.remove_line(i)
row += 1
self.score.add_score(self.level, row)
if row > 0:
self.celebrate()

def remove_line(self, row_num: int):
for i in range(row_num, 0, -1):
Expand All @@ -133,6 +135,12 @@ def remove_line(self, row_num: int):
for j in range(1, self.cols - 1):
self.game_map[0][j] = 0

def celebrate(self):
graphic.draw_num(self.score.score, graphic.score_pos_settings)
graphic.draw_num(self.score.rows, graphic.rows_pos_settings)
graphic.draw_img(celebrate=True)
self.musics.tick(True)

def add_tetromino(self):
self.tetromino = Tetromino(self.cols)
if self.collide_detect(self.game_map):
Expand Down Expand Up @@ -168,31 +176,27 @@ def fresh_lcd(self):
def set_controller(self, controller):
self.controller = controller

def set_theme_music(self, music):
self.theme_music = music
def set_musics(self, musics: Musics):
self.musics = musics

def start_game(self):
self.init_game()
self.musics.continue_music()

def pause_game(self):
self.pause = not self.pause
self.theme_music.toggle()
if self.pause:
self.musics.pause()
else:
self.musics.continue_music()

def run(self):
self.init_game()
while True:
counter = 0
while not self.game_over and not self.pause:
utime.sleep_ms(1)
score_ = self.score.score
self.move_down_event.tick()
self.theme_music.tick()
self.musics.tick()
self.controller.run()
self.fresh_lcd()
graphic.draw_img()

if score_ != self.score.score:
score_ = self.score.score
graphic.draw_num(score_, graphic.score_pos_settings)
graphic.draw_num(self.score.rows, graphic.rows_pos_settings)
graphic.draw_img(celebrate=True)

0 comments on commit 19f65f1

Please sign in to comment.