Simple Pong game on NokiaLCD with PS2

Dependencies:   mbed PS2 NokiaLCD

Committer:
wjohnsto
Date:
Mon Feb 28 00:12:36 2011 +0000
Revision:
1:3cc8b1413557
Parent:
0:93dce1e528b9
Child:
2:d1031c73e187
Commented code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wjohnsto 0:93dce1e528b9 1 #include "mbed.h"
wjohnsto 0:93dce1e528b9 2 #include "NokiaLCD.h"
wjohnsto 0:93dce1e528b9 3 #include "PS2Keyboard.h"
wjohnsto 0:93dce1e528b9 4 #include "ball.h"
wjohnsto 0:93dce1e528b9 5 #include "paddle.h"
wjohnsto 0:93dce1e528b9 6
wjohnsto 1:3cc8b1413557 7 // State enumerator
wjohnsto 0:93dce1e528b9 8 typedef enum {
wjohnsto 0:93dce1e528b9 9 RESET, RUN, PAUSE
wjohnsto 0:93dce1e528b9 10 } STATES;
wjohnsto 0:93dce1e528b9 11
wjohnsto 0:93dce1e528b9 12 NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type
wjohnsto 0:93dce1e528b9 13
wjohnsto 0:93dce1e528b9 14 PS2Keyboard ps2kb(p12, p11); // CLK, DAT
wjohnsto 0:93dce1e528b9 15
wjohnsto 0:93dce1e528b9 16 DigitalIn sw2(p24);
wjohnsto 0:93dce1e528b9 17 DigitalIn sw1(p25);
wjohnsto 0:93dce1e528b9 18
wjohnsto 0:93dce1e528b9 19 PwmOut g(p21);
wjohnsto 0:93dce1e528b9 20 PwmOut b(p22);
wjohnsto 0:93dce1e528b9 21 PwmOut r(p23);
wjohnsto 0:93dce1e528b9 22
wjohnsto 1:3cc8b1413557 23 // Button enumerator for PS/2 keyboard
wjohnsto 0:93dce1e528b9 24 enum BUTTONS{
wjohnsto 0:93dce1e528b9 25 UP = 0xe75,
wjohnsto 0:93dce1e528b9 26 DOWN = 0xe72,
wjohnsto 0:93dce1e528b9 27 };
wjohnsto 0:93dce1e528b9 28
wjohnsto 1:3cc8b1413557 29 /*
wjohnsto 1:3cc8b1413557 30 * Subroutine drawScreen:
wjohnsto 1:3cc8b1413557 31 * Description: Draws both paddles
wjohnsto 1:3cc8b1413557 32 * and the ball.
wjohnsto 1:3cc8b1413557 33 */
wjohnsto 0:93dce1e528b9 34 void drawScreen(Paddle paddle1, Paddle paddle2, Ball theBall, bool isBlack) {
wjohnsto 0:93dce1e528b9 35 paddle1.draw(lcd, isBlack);
wjohnsto 0:93dce1e528b9 36 paddle2.draw(lcd ,isBlack);
wjohnsto 0:93dce1e528b9 37 theBall.draw(lcd ,isBlack);
wjohnsto 0:93dce1e528b9 38 }
wjohnsto 0:93dce1e528b9 39
wjohnsto 1:3cc8b1413557 40 /*
wjohnsto 1:3cc8b1413557 41 * Subroutine drawScores:
wjohnsto 1:3cc8b1413557 42 * Description: Draws the scoreboard
wjohnsto 1:3cc8b1413557 43 */
wjohnsto 0:93dce1e528b9 44 void drawScores(Paddle paddle1, Paddle paddle2) {
wjohnsto 0:93dce1e528b9 45 lcd.locate(7,0);
wjohnsto 0:93dce1e528b9 46 lcd.putc('0' + paddle1.getScore());
wjohnsto 0:93dce1e528b9 47 lcd.locate(9,0);
wjohnsto 0:93dce1e528b9 48 lcd.putc('0' + paddle2.getScore());
wjohnsto 0:93dce1e528b9 49 lcd.fill(66,0,2,130,0xFFFFFF);
wjohnsto 0:93dce1e528b9 50 lcd.locate(7,15);
wjohnsto 0:93dce1e528b9 51 lcd.putc('0' + paddle1.getLives());
wjohnsto 0:93dce1e528b9 52 lcd.locate(9,15);
wjohnsto 0:93dce1e528b9 53 lcd.putc('0' + paddle2.getLives());
wjohnsto 0:93dce1e528b9 54 }
wjohnsto 0:93dce1e528b9 55
wjohnsto 0:93dce1e528b9 56 int main() {
wjohnsto 1:3cc8b1413557 57 PS2Keyboard::keyboard_event_t evt_kb; // Setup keyboard interrupt
wjohnsto 0:93dce1e528b9 58 lcd.background(0x000000);
wjohnsto 0:93dce1e528b9 59 lcd.cls();
wjohnsto 0:93dce1e528b9 60 Paddle paddle1, paddle2;
wjohnsto 0:93dce1e528b9 61 Ball theBall;
wjohnsto 0:93dce1e528b9 62 int temp, count=0;
wjohnsto 0:93dce1e528b9 63 drawScreen(paddle1, paddle2, theBall, false);
wjohnsto 0:93dce1e528b9 64 drawScores(paddle1, paddle2);
wjohnsto 1:3cc8b1413557 65 STATES state = RESET; // Initial state is RESET
wjohnsto 0:93dce1e528b9 66 while(1) {
wjohnsto 0:93dce1e528b9 67 switch(state) {
wjohnsto 1:3cc8b1413557 68 case RESET: // Reset objects, draw the screen, state = PAUSE
wjohnsto 0:93dce1e528b9 69 lcd.cls();
wjohnsto 0:93dce1e528b9 70 paddle1 = Paddle(1,10,5,25,0xFFFFFF,paddle1.getLives(),paddle1.getScore());
wjohnsto 0:93dce1e528b9 71 paddle2 = Paddle(125,3,5,25,0xFFFFFF,paddle2.getLives(),paddle2.getScore());
wjohnsto 0:93dce1e528b9 72 theBall = Ball(6,25,5,5,0xFFFF00,1,1);
wjohnsto 0:93dce1e528b9 73 drawScreen(paddle1, paddle2, theBall, false);
wjohnsto 0:93dce1e528b9 74 drawScores(paddle1, paddle2);
wjohnsto 0:93dce1e528b9 75 state = PAUSE;
wjohnsto 0:93dce1e528b9 76 break;
wjohnsto 1:3cc8b1413557 77 case PAUSE: // Set RGB LED to Red, wait for switch input
wjohnsto 1:3cc8b1413557 78 r = 0;
wjohnsto 1:3cc8b1413557 79 b = g = 1;
wjohnsto 0:93dce1e528b9 80 if(!sw2) {
wjohnsto 0:93dce1e528b9 81 while(!sw2);
wjohnsto 0:93dce1e528b9 82 state = RESET;
wjohnsto 0:93dce1e528b9 83 break;
wjohnsto 0:93dce1e528b9 84 }
wjohnsto 0:93dce1e528b9 85 if(!sw1) {
wjohnsto 0:93dce1e528b9 86 while(!sw1);
wjohnsto 0:93dce1e528b9 87 state = RUN;
wjohnsto 0:93dce1e528b9 88 }
wjohnsto 0:93dce1e528b9 89 break;
wjohnsto 1:3cc8b1413557 90 case RUN: // Set RGB LED to Blue and run program
wjohnsto 0:93dce1e528b9 91 r = g = 1;
wjohnsto 0:93dce1e528b9 92 b = 0;
wjohnsto 1:3cc8b1413557 93 if(!sw2) { // Reset if SW2 is pressed
wjohnsto 0:93dce1e528b9 94 while(!sw2);
wjohnsto 0:93dce1e528b9 95 state = RESET;
wjohnsto 0:93dce1e528b9 96 break;
wjohnsto 0:93dce1e528b9 97 }
wjohnsto 1:3cc8b1413557 98 if(!sw1) { // Pause if SW1 is pressed
wjohnsto 0:93dce1e528b9 99 while(!sw1);
wjohnsto 0:93dce1e528b9 100 state = PAUSE;
wjohnsto 0:93dce1e528b9 101 break;
wjohnsto 0:93dce1e528b9 102 }
wjohnsto 1:3cc8b1413557 103 if (ps2kb.processing(&evt_kb)) { // Executes if a key is pressed
wjohnsto 0:93dce1e528b9 104 temp = evt_kb.scancode[0];
wjohnsto 1:3cc8b1413557 105 for (int i = 1; i < evt_kb.length; i++) { // Parse keyboard input into a key
wjohnsto 0:93dce1e528b9 106 temp <<= 4;
wjohnsto 0:93dce1e528b9 107 temp |= evt_kb.scancode[i];
wjohnsto 0:93dce1e528b9 108 }
wjohnsto 1:3cc8b1413557 109 switch(temp) { // Use key enumerator to move paddle1
wjohnsto 0:93dce1e528b9 110 case UP:
wjohnsto 0:93dce1e528b9 111 if(paddle1.getY()>2)
wjohnsto 0:93dce1e528b9 112 paddle1.move(lcd, -2);
wjohnsto 0:93dce1e528b9 113 break;
wjohnsto 0:93dce1e528b9 114 case DOWN:
wjohnsto 0:93dce1e528b9 115 if(paddle1.getY()+paddle1.getHeight()<128)
wjohnsto 0:93dce1e528b9 116 paddle1.move(lcd, 2);
wjohnsto 0:93dce1e528b9 117 break;
wjohnsto 0:93dce1e528b9 118 }
wjohnsto 0:93dce1e528b9 119 }
wjohnsto 1:3cc8b1413557 120 if(count%2) // Only let CPU move once every 2 times through the loop
wjohnsto 0:93dce1e528b9 121 paddle2.moveCPU(lcd, theBall.getY());
wjohnsto 1:3cc8b1413557 122 if(++count==5) { // Only move the ball once every 5 times through the loop
wjohnsto 0:93dce1e528b9 123 count = 0;
wjohnsto 0:93dce1e528b9 124 if(theBall.hitP1((paddle1.getX()+paddle1.getWidth()), paddle1.getY(), paddle1.getHeight()))
wjohnsto 0:93dce1e528b9 125 theBall.reverseX();
wjohnsto 0:93dce1e528b9 126 if(theBall.hitP2(paddle2.getX(), paddle2.getY(), paddle2.getHeight()))
wjohnsto 0:93dce1e528b9 127 theBall.reverseX();
wjohnsto 1:3cc8b1413557 128 if(theBall.hitX()) { // If the ball hits one of the sides of the screen
wjohnsto 1:3cc8b1413557 129 if(theBall.getX()<7) { // If the ball hit paddle1's side
wjohnsto 1:3cc8b1413557 130 if(!paddle1.loseLife()) { // If paddle1 has no more lives
wjohnsto 0:93dce1e528b9 131 paddle1.setLives(3);
wjohnsto 0:93dce1e528b9 132 paddle2.setLives(3);
wjohnsto 0:93dce1e528b9 133 paddle2.addPoint();
wjohnsto 0:93dce1e528b9 134 }
wjohnsto 0:93dce1e528b9 135 }
wjohnsto 1:3cc8b1413557 136 else if(theBall.getX()>120) { // If the ball hit paddle2's side
wjohnsto 1:3cc8b1413557 137 if(!paddle2.loseLife()) { // If paddle2 has no more lives
wjohnsto 0:93dce1e528b9 138 paddle2.setLives(3);
wjohnsto 0:93dce1e528b9 139 paddle1.setLives(3);
wjohnsto 0:93dce1e528b9 140 paddle1.addPoint();
wjohnsto 0:93dce1e528b9 141 }
wjohnsto 0:93dce1e528b9 142 }
wjohnsto 0:93dce1e528b9 143 theBall.reverseX();
wjohnsto 1:3cc8b1413557 144 state = RESET; // Reset the objects
wjohnsto 0:93dce1e528b9 145 }
wjohnsto 0:93dce1e528b9 146 if(theBall.hitY())
wjohnsto 0:93dce1e528b9 147 theBall.reverseY();
wjohnsto 0:93dce1e528b9 148 theBall.move(lcd);
wjohnsto 0:93dce1e528b9 149 }
wjohnsto 0:93dce1e528b9 150 break;
wjohnsto 0:93dce1e528b9 151 }
wjohnsto 0:93dce1e528b9 152 drawScreen(paddle1, paddle2, theBall, false);
wjohnsto 0:93dce1e528b9 153 drawScores(paddle1, paddle2);
wjohnsto 0:93dce1e528b9 154 }
wjohnsto 0:93dce1e528b9 155 }