Pseudo-code for Gameplay module ****************************************************************************************** InitializeGameplay //Takes a priority number, returns true Initialize MyPriority variable with passed in priority number Initialize the necessary port lines and hardware Sample port lines for coin sensor and save state to Coin_Sensor_Last_State Set CurrentState to Waiting_for_TOT Post event ES_INIT to Gameplay service End of InitializeGameplay PostGameplay Takes an input event and posts to service End of PostGameplay CheckTotEvent //Takes no input returns true if event is found local ReturnVal, Coin_Sensor_Current_State Set ReturnVal to false Set current state of port line PD3 to Coin_Sensor_Current_State If Coin_Sensor_Current_State is different than Coin_Sensor_Last_State Set ReturnVal to true Post Event TOT_BEAM_BROKEN to Gameplay and HPServo services Set Coin_Sensor_Last_State to Coin_Sensor_Current_State Return ReturnVal End of CheckTotEvent Rand_Hit //Generates random hit or random dodge event, posts to Gameplay and BeamBreak or DodgeRegister services //Only used at the start of the game to assign the first hit Get a random number and set it to RandTime If RandTime % 3 is equal to 0 Post Event HIT_POST to Gameplay and BeamBreak services with parameter LEFT_TIMER Elseif RandTime % 8 is equal to 1 Post Event HIT_POST to Gameplay and BeamBreak services with parameter MIDDLE_TIMER Elseif RandTime % 8 is equal to 2 Post Event HIT_POST to Gameplay and BeamBreak services with parameter RIGHT_TIMER Endif End of Rand_Hit_or_Dodge Rand_Hit_or_Dodge //Generates random hit or random dodge event, posts to Gameplay and BeamBreak or DodgeRegister services Get a random number and set it to RandTime If RandTime % 8 is equal to 0 or 1 Post Event HIT_POST to Gameplay and BeamBreak services with parameter LEFT_TIMER Elseif RandTime % 8 is equal to 2 or 3 Post Event HIT_POST to Gameplay and BeamBreak services with parameter MIDDLE_TIMER Elseif RandTime % 8 is equal to 4 or 5 Post Event HIT_POST to Gameplay and BeamBreak services with parameter RIGHT_TIMER Elseif RandTime % 8 is equal to 6 Post Event DODGE_POST to Gameplay and DodgeRegister services with parameter 'L' Elseif RandTime % 8 is equal to 7 Post Event HIT_POST to Gameplay and DodgeRegister services with parameter 'R' Endif End of Rand_Hit_or_Dodge LEDFlicker //Takes no input, returns nothing Set FlickerCount to 0 If FlickerCount is equal to 0 Call LightLED with parameters 'L' and 'W' Increment FlickerCount variable If FlickerCount is equal to 1 Call LightLED with parameters 'M' and 'W' Increment FlickerCount variable If FlickerCount is equal to 2 Call LightLED with parameters 'R' and 'W' Increment FlickerCount variable If FlickerCount is equal to 3 Call LightLED with parameters 'L' and 'R' Increment FlickerCount variable If FlickerCount is equal to 4 Call LightLED with parameters 'M' and 'W' Increment FlickerCount variable If FlickerCount is equal to 5 Call LightLED with parameters 'R' and 'R' Increment FlickerCount variable If FlickerCount is equal to 6 Call LightLED with parameters 'L' and 'G' Increment FlickerCount variable If FlickerCount is equal to 7 Call LightLED with parameters 'M' and 'G' Increment FlickerCount variable If FlickerCount is equal to 8 Call LightLED with parameters 'R' and 'G' Increment FlickerCount variable If FlickerCount is equal to 9 Call LightLED with parameters 'L' and 'B' Increment FlickerCount variable If FlickerCount is equal to 10 Call LightLED with parameters 'M' and 'B' Increment FlickerCount variable If FlickerCount is equal to 11 Call LightLED with parameters 'R' and 'B' Increment FlickerCount variable If FlickerCount is equal to 12 Call LightLED with parameters '0' and '0' Increment FlickerCount variable End of LEDFlicker Flicker //Takes in parameters WhichLED and WhatColor and turns corresponding LED on Set DodgeCount to 0 If DodgeCount is equal to 0 Call LightLED with parameters WhichLED and WhatColor Increment DodgeCount variable Else Call LightLED with parameters '0' and '0' to turn off all LEDs Set DodgeCount to 0 Endif End of Flicker RunGameplaySM // The EventType field of ThisEvent will be one of: TOTBeamBroken, Hit_Or_Dodge[LHit,RHit,MHit,LDodge,RDodge], // ES_Timeout[FirstTimer,LEDTimer,GameTimer,EndgameTimer, AudioTimer], ButtonPress[L,R] // Returns ES_NO_EVENT Set NextState to CurrentState Based on the state of the CurrentState variable choose one of the following blocks of code: CurrentState is Waiting_for_TOT Initialize Fun Timer If ThisEvent is TOT_BEAM_BROKEN Set NextState to Welcoming_TATER Initialize Standby Timer Turn off LEDs by calling LightLED function Initialize Flicker Timer Turn off audio playback by calling Stop_Talking function Initialize Audio Timer Elseif ThisEvent is ES_TIMEOUT and EventParam is FUN_TIMER Call LEDFlicker function Post ThisEvent to TimerServo service Endif Initialize WAIT_TOT_TIMER End Waiting_for_TOT block CurrentState is Welcoming_TATER If ThisEvent is ES_TIMEOUT and EventParam is WAIT_TOT_TIMER Set NextState to Hit_or_Dodge Post ThisEvent to HPServo and TimerServo services Call Rand_Hit function Elseif ThisEvent is ES_TIMEOUT and EventPram is FLICKER_TIMER Call Ficker function with parameters 'M' and 'W' Initialize Flicker Timer Elseif ThisEvent is ES_TIMEOUT and EventParam is AUDIO_TIMER Call Stop_Talking function Call Show_Me_What_You_Got function Endif End Welcoming_TATER block CurrentState is Hit_or_Dodge If ThisEvent is HIT_POST Call Stop_Talking function Set NextState to Wait_for_Hit Post ThisEvent to Gameplay service Initialize LED Timer If EventParam is LEFT_TIMER Call LightLED function with parameters 'L' and 'B' Set LitLED to 'L' If EventParam is MIDDLE_TIMER Call LightLED function with parameters 'M' and 'B' Set LitLED to 'M' If EventParam is RIGHT_TIMER Call LightLED function with parameters 'R' and 'B' Set LitLED to 'R' Elseif ThisEvent is DODGE_POST Call Stop_Talking function Set NextState to Wait_for_Dodge Initialize LED Timer Initialize Flicker Timer Set LitLED to EventParam Elseif ThisEvent is ES_TIMEOUT and EventParam is STANDBY_TIMER Post Event GAME_OVER to TimerServo, Gameplay and HPServo services with parameter LOSE_VALUE Elseif ThisEvent is GAME_OVER and EventParam is LOSE_VALUE Set NextState to Game_Ending Initialize Audio Timer Initialize Flicker Timer Set Win_or_Lose to 'L' Call Lose_Audio function Call TotServoRelease function Stop Standby Timer Endif End Hit_or_Dodge block CurrentState is Wait_for_Hit If EventType is ES_TIMEOUT and EventParam is LED_TIMER Set NextState to Changing_LED Initialize LED Timer Call Stop_Talking function Call Bad_Hit_Audio function Set all LEDs to off by calling LightLED function with parameters '0' and '0' Elseif EventType is HIT_SUCCESS Set NextState to Changing_LED Initialize LED Timer Initialize Standby Timer Call Stop_Talking function Call Good_Hit_Audio function Set all LEDs to off by calling LighLED function with parameters '0' and '0' Set related LED to green by calling LightLED function with LitLED and 'G' parameters Elseif EventType is HIT_MISTAKE Set NextState to Changing_LED Initialize LED Timer Initialize Standby Timer Call Stop_Talking function Call Lose_Audio function Set all LEDs to off by calling LighLED function with parameters '0' and '0' Set related LED to red by calling LightLED function with LitLED and 'R' parameters Elseif EventType is ES_TIMEOUT and EventParam is STANDBY_TIMER Post Event GAME_OVER to TimerServo, Gameplay, and HP Servo services with parameter LOSE_VALUE Elseif EventType is GAME_OVER and EventParam is LOSE_VALUE Set NextState to Game_Ending Initialize Audio Timer Initialize Flicker Timer Set Win_or_Lose to 'L' Call Stop_Talking function Call Lose_Audio function Call TotServoRelease function Stop Standby Timer Endif End Wait_for_Hit block CurrentState is Wait_for_Dodge If EventType is DODGE_SUCCESS Set NextState to Changing_LED Initialize LED Timer Call LightLED with LitLED and 'G' parameters Call Stop_Talking function Call Good_Hit_Audio function Initialize Standby Timer Elseif EventType is DODGE_MISTAKE Initialize Standby Timer Call Stop_Talking function Call Bad_Hit_Audio function Elseif EventType is ES_TIMEOUT with EventParam STANDBY_TIMER Post Event GAME_OVER to TimerServo, Gameplay, and HP Servo services with parameter LOSE_VALUE Elseif EventType is GAME_OVER and EventParam is LOSE_VALUE Set NextState to Game_Ending Initialize Audio Timer Initialize Flicker Timer Set Win_or_Lose to 'L' Call Stop_Talking function Call Lose_Audio function Call TotServoRelease function Stop Standby Timer Endif End Wait_for_Dodge block CurrentState is Changing_LED Call Stop_Talking function If EventType is ES_TIMEOUT and EventParam is LED_TIMER Set NextState is Hit_or_Dodge Call Rand_Hit_or_Dodge function Turn off LEDs by calling LightLED function with parameters '0' and '0' Elseif EventType is ES_TIMEOUT and EventParam is STANDBY_TIMER Post Event GAME_OVER to TimerServo, Gameplay, and HP Servo services with parameter LOSE_VALUE Elseif EventType is GAME_OVER and EventParam is WIN_VALUE Set NextState to Game_Ending Initialize Audio Timer Initialize Flicker Timer Set Win_or_Lose to 'W' Call Stop_Talking function Call Win_Audio function Call TotServoRelease function Stop Standby Timer Turn off LEDs by calling LightLED with parameters '0' and '0' Elseif EventType is GAME_OVER and EventParam is LOSE_VALUE Set NextState to Game_Ending Initialize Audio Timer Initialize Flicker Timer Set Win_or_Lose to 'L' Call Stop_Talking function Call Lose_Audio function Call TotServoRelease function Stop Standby Timer Turn off LEDs by calling LightLED with parameters '0' and '0' Endif End Changing_LED block CurrentState is Game_Ending If EventType is ES_TIMEOUT and EventParam is AUDIO_TIMER Post Event to HPServo and TimerServo Services Call Stop_Taling function Call Goodbye_Moonmen function Set NextState to Endgame_Transition Call TotServoReset function Post ReturnEvent to Gameplay Services Elseif EventType is ES_TIMEOUT and EventParam is FLICKER_TIMER and Win_or_Lose is 'W' Call Flicker function with parametes 'A' and 'G' Initialize Flicker Timer Elseif EventType is ES_TIMEOUT and EventParam is FLICKER_TIMER and Win_or_Lose is 'L' Call Flicker function with parametes 'A' and 'R' Initialize Flicker Timer Endif End Game_Ending block CurrentState is Endgame_Transition Set NextState to Waiting_for_TOT Post ReturnEvent to Gameplay service End Endgae_Transition block Set CurrentState to NextState Return ReturnEvent End of RunGameplaySM