Brickbreaker

Dependencies:   NokiaLCD PS2 mbed

Fork of Pong by William Johnston

Committer:
wjohnsto
Date:
Mon Feb 28 00:22:23 2011 +0000
Revision:
2:d1031c73e187
Parent:
1:3cc8b1413557
Child:
3:e1328f84b107

        

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 2:d1031c73e187 82 paddle2.setLives(3);
wjohnsto 2:d1031c73e187 83 paddle1.setLives(3);
wjohnsto 0:93dce1e528b9 84 state = RESET;
wjohnsto 0:93dce1e528b9 85 break;
wjohnsto 0:93dce1e528b9 86 }
wjohnsto 0:93dce1e528b9 87 if(!sw1) {
wjohnsto 0:93dce1e528b9 88 while(!sw1);
wjohnsto 0:93dce1e528b9 89 state = RUN;
wjohnsto 0:93dce1e528b9 90 }
wjohnsto 0:93dce1e528b9 91 break;
wjohnsto 1:3cc8b1413557 92 case RUN: // Set RGB LED to Blue and run program
wjohnsto 0:93dce1e528b9 93 r = g = 1;
wjohnsto 0:93dce1e528b9 94 b = 0;
wjohnsto 1:3cc8b1413557 95 if(!sw2) { // Reset if SW2 is pressed
wjohnsto 0:93dce1e528b9 96 while(!sw2);
wjohnsto 2:d1031c73e187 97 paddle2.setLives(3);
wjohnsto 2:d1031c73e187 98 paddle1.setLives(3);
wjohnsto 0:93dce1e528b9 99 state = RESET;
wjohnsto 0:93dce1e528b9 100 break;
wjohnsto 0:93dce1e528b9 101 }
wjohnsto 1:3cc8b1413557 102 if(!sw1) { // Pause if SW1 is pressed
wjohnsto 0:93dce1e528b9 103 while(!sw1);
wjohnsto 0:93dce1e528b9 104 state = PAUSE;
wjohnsto 0:93dce1e528b9 105 break;
wjohnsto 0:93dce1e528b9 106 }
wjohnsto 1:3cc8b1413557 107 if (ps2kb.processing(&evt_kb)) { // Executes if a key is pressed
wjohnsto 0:93dce1e528b9 108 temp = evt_kb.scancode[0];
wjohnsto 1:3cc8b1413557 109 for (int i = 1; i < evt_kb.length; i++) { // Parse keyboard input into a key
wjohnsto 0:93dce1e528b9 110 temp <<= 4;
wjohnsto 0:93dce1e528b9 111 temp |= evt_kb.scancode[i];
wjohnsto 0:93dce1e528b9 112 }
wjohnsto 1:3cc8b1413557 113 switch(temp) { // Use key enumerator to move paddle1
wjohnsto 0:93dce1e528b9 114 case UP:
wjohnsto 0:93dce1e528b9 115 if(paddle1.getY()>2)
wjohnsto 0:93dce1e528b9 116 paddle1.move(lcd, -2);
wjohnsto 0:93dce1e528b9 117 break;
wjohnsto 0:93dce1e528b9 118 case DOWN:
wjohnsto 0:93dce1e528b9 119 if(paddle1.getY()+paddle1.getHeight()<128)
wjohnsto 0:93dce1e528b9 120 paddle1.move(lcd, 2);
wjohnsto 0:93dce1e528b9 121 break;
wjohnsto 0:93dce1e528b9 122 }
wjohnsto 0:93dce1e528b9 123 }
wjohnsto 1:3cc8b1413557 124 if(count%2) // Only let CPU move once every 2 times through the loop
wjohnsto 0:93dce1e528b9 125 paddle2.moveCPU(lcd, theBall.getY());
wjohnsto 1:3cc8b1413557 126 if(++count==5) { // Only move the ball once every 5 times through the loop
wjohnsto 0:93dce1e528b9 127 count = 0;
wjohnsto 0:93dce1e528b9 128 if(theBall.hitP1((paddle1.getX()+paddle1.getWidth()), paddle1.getY(), paddle1.getHeight()))
wjohnsto 0:93dce1e528b9 129 theBall.reverseX();
wjohnsto 0:93dce1e528b9 130 if(theBall.hitP2(paddle2.getX(), paddle2.getY(), paddle2.getHeight()))
wjohnsto 0:93dce1e528b9 131 theBall.reverseX();
wjohnsto 1:3cc8b1413557 132 if(theBall.hitX()) { // If the ball hits one of the sides of the screen
wjohnsto 1:3cc8b1413557 133 if(theBall.getX()<7) { // If the ball hit paddle1's side
wjohnsto 1:3cc8b1413557 134 if(!paddle1.loseLife()) { // If paddle1 has no more lives
wjohnsto 0:93dce1e528b9 135 paddle1.setLives(3);
wjohnsto 0:93dce1e528b9 136 paddle2.setLives(3);
wjohnsto 0:93dce1e528b9 137 paddle2.addPoint();
wjohnsto 0:93dce1e528b9 138 }
wjohnsto 0:93dce1e528b9 139 }
wjohnsto 1:3cc8b1413557 140 else if(theBall.getX()>120) { // If the ball hit paddle2's side
wjohnsto 1:3cc8b1413557 141 if(!paddle2.loseLife()) { // If paddle2 has no more lives
wjohnsto 0:93dce1e528b9 142 paddle2.setLives(3);
wjohnsto 0:93dce1e528b9 143 paddle1.setLives(3);
wjohnsto 0:93dce1e528b9 144 paddle1.addPoint();
wjohnsto 0:93dce1e528b9 145 }
wjohnsto 0:93dce1e528b9 146 }
wjohnsto 0:93dce1e528b9 147 theBall.reverseX();
wjohnsto 1:3cc8b1413557 148 state = RESET; // Reset the objects
wjohnsto 0:93dce1e528b9 149 }
wjohnsto 0:93dce1e528b9 150 if(theBall.hitY())
wjohnsto 0:93dce1e528b9 151 theBall.reverseY();
wjohnsto 0:93dce1e528b9 152 theBall.move(lcd);
wjohnsto 0:93dce1e528b9 153 }
wjohnsto 0:93dce1e528b9 154 break;
wjohnsto 0:93dce1e528b9 155 }
wjohnsto 0:93dce1e528b9 156 drawScreen(paddle1, paddle2, theBall, false);
wjohnsto 0:93dce1e528b9 157 drawScores(paddle1, paddle2);
wjohnsto 0:93dce1e528b9 158 }
wjohnsto 0:93dce1e528b9 159 }