home   |  video   |  gallery   |  circuitry   |  code   |  schematics   |  bom   |  gems of wisdom  


CODE

Signaling Module:
[Header File]  [Pseudocode]  [C Code]

Timing Module:
[Header File]  [Pseudocode]  [C Code]

Pinout:

[Pinout Diagram]
Game Play Module:
[Header File]  [Pseudocode]  [C Code]

Main Module:
[Header File]  [Pseudocode]  [C Code]

Main C Code

/* State machine for Railroad HandCar arcade game */

/**************************************************/
/* Defines                                        */
/**************************************************/

#define PLAY
//#define TEST

/**************************************************/
/* Includes                                       */
/**************************************************/

#include <stdio.h>
#include <timers12.h>

#include "Timing.h"
#include "Signaling.h"
#include "GamePlay.h"
#include "Main.h"

/**************************************************/
/* Define Program Variables                       */
/**************************************************/
 
unsigned int  HandCarSpeed;
unsigned char VillainPos, Jumping = FALSE;

/**************************************************/
/* Main                                           */
/**************************************************/

#ifdef PLAY

void main(void) {
  char GameState = INITIALIZING;
  
  while(TRUE) {
    switch(GameState) {

      /* Initialize timer and IO ports at startup */

    
      case INITIALIZING:
           printf("Initializing. ");
           printf("Insert penny. ");
                 
           InitializeTimer();
           InitializePorts();
         
           GameState = WAITING_FOR_PENNY;
           break;					 
                     
      /* Initialize/place villain. Wait for penny */
    
      case WAITING_FOR_PENNY:           
           HandCarSpeed = 0;
      		 SetScrollSpeed(0); 

           if (VillainPos > SERVO_MIN) {
             AdvanceVillain(BK);
             Wait(100);
           }
           if (CheckForPenny()) {
             Beep(1);   
             printf("Found penny. ");
             printf("Press start. ");
             GameState = WAITING_FOR_START;
           }
           break;

      /* Set ready light. Wait for 'start' button */
   
      case WAITING_FOR_START:
           ReadyLED(HI);
         
           if (CheckStartButton()) {
             Beep(2);
             ReadyLED(LO);
         
             TMRS12_InitTimer(GAME_TIMER, GAME_TIME);

             printf("Start pressed. ");
             printf("Playing game...\n\r"); 
             GameState = PLAYING_GAME;
           }
           break;

      /* Main game execution. Advance villain and */
      /* poll every 10 msec whether player caught */
      /* jumping or colliding. Advance background */

      case PLAYING_GAME:
           if (!Expired(GAME_TIMER)) {
             if (!Caught()) {
               char JumpEvent = GetJumpEvent(); 

               if (JumpEvent == JUMPED) {
                 Jump(UP);
                 Jumping = TRUE;
               }
               if (JumpEvent == LANDED) {
                 Jump(DN);
                 Jumping = FALSE;
               }
               if (!Colliding()) {
                 HandCarSpeed = CalculateVelocity(); 
                 VibrateHandle(OFF);           
               }
               else {
                 HandCarSpeed = 0;
                 VibrateHandle(ON);
               }
               SetScrollSpeed(HandCarSpeed);
               CalculateChase(HandCarSpeed);              
             }
             else {
               /* Game lost (caught by the villain) */

               Beep(3);
               printf("You were caught. Take a dry cracker.\n\r");
               GameState = INITIALIZING;
             }
             Wait(10);
           }
           else {
             /* Game won (played until GAME_TIME) */
             Beep(3);
             printf("You win! Put Cheez Whiz on your cracker.\n\r");
             DispenseSWAG(); 
             GameState = INITIALIZING;
           }
           break;

      /* Shouldn't reach this. Include for safety */

      default:
           break;
    }
  }
}
#endif

#ifdef TEST

void main(void) {
  InitializeTimer();
  InitializePorts();
}
#endif