Simple Pong game on NokiaLCD with PS2
Dependencies: mbed PS2 NokiaLCD
Diff: ball.cpp
- Revision:
- 0:93dce1e528b9
- Child:
- 1:3cc8b1413557
--- /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