ReflexBall Rally
 All Data Structures Files Functions Variables Macros
main.c
Go to the documentation of this file.
1 #include <eZ8.h> // special encore constants, macros and flash routines
2 #include <sio.h> // special encore serial i/o routines
3 #include "reflexball.h"
4 #include "ansi.h"
5 #include "LED.h"
6 #include "time.h"
7 #include "buttons.h"
8 #include "gameport.h"
9 #include "asciidisplay.h"
10 #include "ascii.h"
11 
12 unsigned long wheelTimer; // Used to add a small delay between reading the analog wheel
13 
14 void main() {
15  int input; // Input from the keyboard
16  unsigned char buttons; // Input from either the hardware buttons or the sterring wheel's button
17  char wheel; // Output from readSterringWheel()
18 
19  initTimers(); // Initialize the Timer1 used for delay_ms() and mills()
20  initLED(); // Initialize LED display
21  initButtons(); // Initialize hardware buttons
22  initGameport(); // Initialize steering wheel buttons and driving wheel
23  init_uart(_UART0,_DEFFREQ,BAUD_115200); // Start the UART at 115200 8N1
24 
25  color(2,0); // Green forground, black background
26 
27 Start:
28  clrscr(); // Clear the screen
29 
30  LEDsetString(" ReflexBall RALLY!"); // Scroll this text on the display
31 
32  initStartMenu(3,1,224,82); // x1, y1, x2, y2
33  while(!startMenu()); // Wait for any key to be pressed
34  clrscr();
35  printMenu();
36  while(!updateMenu()); // Wait until difficulty is choosen
37 
38  LEDsetString(" "); // Clear display
39  initReflexBall(3,15,224,82,1); // x1, y1, x2, y2, style
40 
41  for(;;) {
42  buttons = getGameportButtons(); // Read steering wheel's button
43 
44  if (buttons & 0xE) // Gear backward or button press
45  startGame();
46  else if (millis() - wheelTimer > 20) { // We have to limit the update rate or it will move to fast
47  wheelTimer = millis();
48  wheel = readSteeringWheel(); // Read sterring wheel
49  if (wheel != 0)
50  moveStriker(wheel);
51  }
52  else { // The driving wheel overrules the other controls
53  buttons = readButtons(); // Hardware buttons
54  if (buttons) {
55  if (buttons & 0x2) // Center
56  startGame();
57  else if (buttons & 0x4) // Left
58  moveStriker(-1);
59  else if (buttons & 0x1) // Right
60  moveStriker(1);
61  }
62  else if (kbhit()) { // Check if a buttons has been pressed on the keyboard
63  input = getch(); // Read the keyboard
64  if (input == ' ') // Space
65  startGame();
66  else if (input == 68) // Left
67  moveStriker(-2);
68  else if (input == 67) // Right
69  moveStriker(2);
70  }
71  }
72  updateGame(); // Update the game - this has to be called frequenctly
73  if (restartGame)
74  goto Start; // Goto back to the start of the game
75  }
76 }