Simple Pong game on NokiaLCD with PS2
Dependencies: mbed PS2 NokiaLCD
Revision 0:93dce1e528b9, committed 2011-02-27
- Comitter:
- wjohnsto
- Date:
- Sun Feb 27 23:35:17 2011 +0000
- Child:
- 1:3cc8b1413557
- Commit message:
- Version 0.0
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/NokiaLCD.lib Sun Feb 27 23:35:17 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/wjohnsto/code/NokiaLCD/#740a742e0efa
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PS2.lib Sun Feb 27 23:35:17 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/wjohnsto/code/PS2/#ce15490e89e9
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ball.cpp Sun Feb 27 23:35:17 2011 +0000 @@ -0,0 +1,74 @@ +#include "mbed.h" +#include "ball.h" + +Ball::Ball() { + int x=y=width=height=color=xInc=yInc=0; +} + +Ball::Ball(int x, int y, int w, int h, int c, int xi, int yi) + : x(x), y(y), width(w), height(h), color(c), xInc(xi), yInc(yi) {} + +void Ball::move(NokiaLCD &lcd) { + lcd.fill(x,y,width,height,0x000000); + x += xInc; y += yInc; +} + +void Ball::draw(NokiaLCD &lcd, bool isBlack) const { + lcd.fill(x, y, width, height, (isBlack) ? 0x000000 : color); +} + +int Ball::size() const { + return width*height; +} + +int Ball::getX() const { + return x; +} + +int Ball::getY() const { + return y; +} + +bool Ball::hitX() { + return (x+width<=width) || (x+width>=132); +} + +bool Ball::hitY() { + return (y<=0) || (y+height>=130); +} + +bool Ball::hitP1(int _x, int _y, int _height) { + bool hit = ((_x>=x) && (xInc<0)) && + (((_y<=y) && (_y+_height>=y+height)) || + ((_y>=y) && (_y<=y+height)) || + ((_y+_height>=y) && (_y+_height<=y+height)) + ); + if(hit) { + if(_y+_height-y < 4 && yInc>0) yInc = 2; + if(y-_y < 4 && yInc<0) yInc = -2; + else yInc = (yInc>0) ? 1 : -1; + } + return hit; +} + +bool Ball::hitP2(int _x, int _y, int _height) { + bool hit = ((_x<=x+width) && (xInc>0)) && + (((_y<=y) && (_y+_height>=y+height)) || + ((_y>=y) && (_y<=y+height)) || + ((_y+_height>=y) && (_y+_height<=y+height)) + ); + if(hit) { + if(_y+_height-y < 4 && yInc>0) yInc = 2; + if(y-_y < 4 && yInc<0) yInc = -2; + else yInc = (yInc>0) ? 1 : -1; + } + return hit; +} + +void Ball::reverseX() { + xInc *= -1; +} + +void Ball::reverseY() { + yInc *= -1; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ball.h Sun Feb 27 23:35:17 2011 +0000 @@ -0,0 +1,26 @@ +#include "NokiaLCD.h" + +class Ball { + /* This class creates a ball object */ + + // Attributes + int x,y,width,height,color,xInc,yInc; + +public: + // Constructor + Ball(); + Ball(int x, int y, int w, int h, int c, int xi, int yi); + + // Member functions + void move(NokiaLCD &lcd); + void draw(NokiaLCD &lcd, bool isBlack) const; + int getX() const; + int getY() const; + bool hitX(); + bool hitY(); + bool hitP1(int _x, int _y, int _height); + bool hitP2(int _x, int _y, int _height); + int size() const; + void reverseX(); + void reverseY(); +}; \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Feb 27 23:35:17 2011 +0000 @@ -0,0 +1,144 @@ +#include "mbed.h" +#include "NokiaLCD.h" +#include "PS2Keyboard.h" +#include "ball.h" +#include "paddle.h" + +typedef enum { + RESET, RUN, PAUSE +} STATES; + +NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type + +PS2Keyboard ps2kb(p12, p11); // CLK, DAT + +DigitalIn sw2(p24); +DigitalIn sw1(p25); + +PwmOut g(p21); +PwmOut b(p22); +PwmOut r(p23); + +enum BUTTONS{ + UP = 0xe75, + DOWN = 0xe72, +}; + +void drawScreen(Paddle paddle1, Paddle paddle2, Ball theBall, bool isBlack) { + paddle1.draw(lcd, isBlack); + paddle2.draw(lcd ,isBlack); + theBall.draw(lcd ,isBlack); +} + +void drawScores(Paddle paddle1, Paddle paddle2) { + lcd.locate(7,0); + lcd.putc('0' + paddle1.getScore()); + lcd.locate(9,0); + lcd.putc('0' + paddle2.getScore()); + lcd.fill(66,0,2,130,0xFFFFFF); + lcd.locate(7,15); + lcd.putc('0' + paddle1.getLives()); + lcd.locate(9,15); + lcd.putc('0' + paddle2.getLives()); +} + +int main() { + PS2Keyboard::keyboard_event_t evt_kb; + lcd.background(0x000000); + lcd.cls(); + Paddle paddle1, paddle2; + Ball theBall; + int temp, count=0; + drawScreen(paddle1, paddle2, theBall, false); + drawScores(paddle1, paddle2); + STATES state = RESET; + while(1) { + switch(state) { + case RESET: + lcd.cls(); + paddle1 = Paddle(1,10,5,25,0xFFFFFF,paddle1.getLives(),paddle1.getScore()); + paddle2 = Paddle(125,3,5,25,0xFFFFFF,paddle2.getLives(),paddle2.getScore()); + theBall = Ball(6,25,5,5,0xFFFF00,1,1); + drawScreen(paddle1, paddle2, theBall, false); + drawScores(paddle1, paddle2); + state = PAUSE; + break; + case PAUSE: + r = g = 0; + b = 1; + if(!sw2) { + while(!sw2); + state = RESET; + break; + } + if(!sw1) { + while(!sw1); + state = RUN; + } + break; + case RUN: + r = g = 1; + b = 0; + if(!sw2) { + while(!sw2); + state = RESET; + break; + } + if(!sw1) { + while(!sw1); + state = PAUSE; + break; + } + if (ps2kb.processing(&evt_kb)) { + temp = evt_kb.scancode[0]; + for (int i = 1; i < evt_kb.length; i++) { + temp <<= 4; + temp |= evt_kb.scancode[i]; + } + switch(temp) { + case UP: + if(paddle1.getY()>2) + paddle1.move(lcd, -2); + break; + case DOWN: + if(paddle1.getY()+paddle1.getHeight()<128) + paddle1.move(lcd, 2); + break; + } + } + if(count%3) + paddle2.moveCPU(lcd, theBall.getY()); + if(++count==5) { + count = 0; + if(theBall.hitP1((paddle1.getX()+paddle1.getWidth()), paddle1.getY(), paddle1.getHeight())) + theBall.reverseX(); + if(theBall.hitP2(paddle2.getX(), paddle2.getY(), paddle2.getHeight())) + theBall.reverseX(); + if(theBall.hitX()) { + if(theBall.getX()<7) { + if(!paddle1.loseLife()) { + paddle1.setLives(3); + paddle2.setLives(3); + paddle2.addPoint(); + } + } + else if(theBall.getX()>120) { + if(!paddle2.loseLife()) { + paddle2.setLives(3); + paddle1.setLives(3); + paddle1.addPoint(); + } + } + theBall.reverseX(); + state = RESET; + } + if(theBall.hitY()) + theBall.reverseY(); + theBall.move(lcd); + } + break; + } + drawScreen(paddle1, paddle2, theBall, false); + drawScores(paddle1, paddle2); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sun Feb 27 23:35:17 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/029aa53d7323
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/paddle.cpp Sun Feb 27 23:35:17 2011 +0000 @@ -0,0 +1,67 @@ +#include "mbed.h" +#include "paddle.h" + +Paddle::Paddle() { + int x=y=width=height=color=score=0; + lives = 3; +} + +Paddle::Paddle(int x, int y, int w, int h, int c, int l, int s) + : x(x), y(y), width(w), height(h), color(c), lives(l), score(s) {} + +void Paddle::move(NokiaLCD &lcd, int increment) { + lcd.fill(x,y,width,height, 0x000000); + y += increment; +} + +void Paddle::moveCPU(NokiaLCD &lcd, int _y) { + static int inc = 1; + lcd.fill(x,y,width,height, 0x000000); + if(_y>y+height/2 && y+height<130) y += inc; + else if(_y+5<y+height/2 && y>0) y -= inc; + inc = (inc) ? 0 : 1; +} + +void Paddle::draw(NokiaLCD &lcd, bool isBlack) const { + lcd.fill(x, y, width, height, (isBlack) ? 0x000000 : color); +} + +bool Paddle::loseLife() { + return --lives; +} + +void Paddle::addPoint() { + ++score; +} + +int Paddle::size() const { + return width*height; +} + +int Paddle::getWidth() const { + return width; +} + +int Paddle::getHeight() const { + return height; +} + +int Paddle::getX() const { + return x; +} + +int Paddle::getY() const { + return y; +} + +int Paddle::getLives() const { + return lives; +} + +int Paddle::getScore() const { + return score; +} + +void Paddle::setLives(int l) { + lives = l; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/paddle.h Sun Feb 27 23:35:17 2011 +0000 @@ -0,0 +1,29 @@ +#include "NokiaLCD.h" + +class Paddle { + /* This class creates a paddle object */ + + // Attributes + int x,y,width,height,color,lives,score; + +public: + // Constructor + Paddle(); + Paddle(int x, int y, int w, int h, int c, int l, int s); + + // Member functions + void move(NokiaLCD &lcd, int increment); + void moveCPU(NokiaLCD &lcd, int _y); + void draw(NokiaLCD &lcd, bool isBlack) const; + bool loseLife(); + void addPoint(); + int size() const; + int getWidth() const; + int getHeight() const; + int getX() const; + int getY() const; + int getLives() const; + int getScore() const; + void setLives(int l); + +}; \ No newline at end of file