Skip to content

Commit

Permalink
Add game loop (closes #5)
Browse files Browse the repository at this point in the history
  • Loading branch information
rlwprog committed Mar 31, 2018
1 parent 6c4a47a commit 2131c69
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 5 deletions.
8 changes: 8 additions & 0 deletions song_match/cube/note_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,11 @@ def turn_on_light(self) -> None:
"""
cube_light = self._song.get_cube_light(self._cube.cube_id)
self._cube.set_lights(cube_light)

def flash_light_red(self) -> None:
"""Flash the light on the cube red.
:return: None
"""
cube_light = self._song.get_cube_light(1)
self._cube.set_lights(cube_light)
9 changes: 9 additions & 0 deletions song_match/cube/note_cubes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,12 @@ def turn_on_lights(self) -> None:
for cube in self._cubes:
note_cube = NoteCube(cube, self._song)
note_cube.turn_on_light()

def flash_lights_red(self) -> None:
"""Turn on the light for each note cube.
:return: None
"""
for cube in self._cubes:
note_cube = NoteCube(cube, self._song)
note_cube.flash_light_red()
63 changes: 58 additions & 5 deletions song_match/song_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class SongMatch:

def __init__(self):
self._robot = None
self._tappedNoteIndex = 0
self._cozmoError = True
self._song = MaryHadALittleLamb()
Note.init_mixer()

Expand All @@ -37,22 +39,73 @@ async def __setup(self) -> None:

self.__turn_on_cube_lights()

notes = [Note('E4'), Note('D4'), Note('C4')]
await self._robot.play_notes(notes)
# notes = [Note('E4'), Note('D4'), Note('C4')]
# await self._robot.play_notes(notes)

async def __tap_handler(self, evt, obj=None, tap_count=None, **kwargs):
cube = evt.obj
note_cube = NoteCube(cube, self._song)
await note_cube.blink_and_play_note()
notes = self._song._notes
self._tappedNoteIndex = note_cube._cube.cube_id - 1
print("Tap Handler: " + str(self._tappedNoteIndex))
await self._robot.play_note(notes[note_cube._cube.cube_id - 1])

def __turn_on_cube_lights(self) -> None:
note_cubes = NoteCubes(self.__get_cubes(), self._song)
note_cubes.turn_on_lights()

def __flash_cubes_red(self) -> None:
note_cubes = NoteCubes(self.__get_cubes(), self._song)
note_cubes.flash_lights_red()

def __get_cubes(self):
"""Convenience method to get the light cubes."""
return self._robot.world.light_cubes.values()

async def __init_game_loop(self) -> None:
while True:
await self._robot.world.wait_for(EvtObjectTapped)
gameContinue = True
roundCounter = 3
notes = self._song._notes
song = self._song._sequence

while gameContinue:
print("Beginning of round!")
game = 0
player = 0
coz = 0
# the notes are played for the player to repeat
while roundCounter <= len(song) and game < roundCounter:
await self._robot.play_note_notap(song[game])
game += 1

# the user repeats the notes
while player < game and gameContinue:
await self._robot.world.wait_for(EvtObjectTapped)

gameContinue = self._robot.compareNotes(notes[self._tappedNoteIndex], song[player])
if gameContinue:
player += 1
print("Game continues")

else:
print("Game over: Cozmo wins!")
self.__flash_cubes_red()
self.__flash_cubes_red()

# Cozmo repeats the notes
while coz < game and gameContinue and self._cozmoError:
gameContinue = await self._robot.play_note_with_error(song[coz], coz)
if gameContinue:
coz += 1
print("Game continues")

else:
print("Game over: Player wins!")
self.__flash_cubes_red()
self.__flash_cubes_red()
roundCounter += 1

# Condition for the player winning the game at the end
if game == len(song) and gameContinue:
print("Player wins!")
gameContinue = False
46 changes: 46 additions & 0 deletions song_match/song_robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from asyncio import sleep
from typing import List
from random import *

from cozmo.anim import EvtAnimationCompleted
from cozmo.anim import Triggers
Expand Down Expand Up @@ -48,11 +49,56 @@ async def play_note(self, note: Note) -> None:
await note_cube.blink_and_play_note()
await self.__wait_until_animation_finished()

async def play_note_with_error(self, note: Note, round: int):
"""Make Cozmo play a note.
:param note: The :class:`~song_match.song.note.Note` to play.
:return: None
"""
difficulty = .99
rand = random()

round_difficulty = difficulty ** round

cube_id = self._song.get_cube_id(note)

if round_difficulty < rand:
cozmoError = True
if cube_id == 1:
cube_id = 2
elif cube_id == 2:
cube_id = 3
else:
cube_id = 1
else:
cozmoError = True
note_cube = self.__get_note_cube(cube_id)
self.__tap_cube(cube_id)
await sleep(self._NOTE_DELAY)
await note_cube.blink_and_play_note()
await self.__wait_until_animation_finished()
return cozmoError

async def play_note_notap(self, note: Note) -> None:
cube_id = self._song.get_cube_id(note)
note_cube = self.__get_note_cube(cube_id)
await sleep(.5)
await note_cube.blink_and_play_note()

@property
def world(self) -> world:
"""Property for accessing :attr:`~cozmo.robot.Robot.world`."""
return self._robot.world

def compareNotes(self, firstNote: Note, secondNote: Note):
cube_idFirst = self._song.get_cube_id(firstNote)
cube_idSecond = self._song.get_cube_id(secondNote)
if cube_idFirst == cube_idSecond:
return True
else:
return False


def __get_note_cube(self, cube_id):
cube = self._robot.world.get_light_cube(cube_id)
return NoteCube(cube, self._song)
Expand Down

0 comments on commit 2131c69

Please sign in to comment.