Simple Pong game on NokiaLCD with PS2
Dependencies: mbed PS2 NokiaLCD
paddle.cpp@0:93dce1e528b9, 2011-02-27 (annotated)
- Committer:
- wjohnsto
- Date:
- Sun Feb 27 23:35:17 2011 +0000
- Revision:
- 0:93dce1e528b9
- Child:
- 1:3cc8b1413557
Version 0.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wjohnsto | 0:93dce1e528b9 | 1 | #include "mbed.h" |
wjohnsto | 0:93dce1e528b9 | 2 | #include "paddle.h" |
wjohnsto | 0:93dce1e528b9 | 3 | |
wjohnsto | 0:93dce1e528b9 | 4 | Paddle::Paddle() { |
wjohnsto | 0:93dce1e528b9 | 5 | int x=y=width=height=color=score=0; |
wjohnsto | 0:93dce1e528b9 | 6 | lives = 3; |
wjohnsto | 0:93dce1e528b9 | 7 | } |
wjohnsto | 0:93dce1e528b9 | 8 | |
wjohnsto | 0:93dce1e528b9 | 9 | Paddle::Paddle(int x, int y, int w, int h, int c, int l, int s) |
wjohnsto | 0:93dce1e528b9 | 10 | : x(x), y(y), width(w), height(h), color(c), lives(l), score(s) {} |
wjohnsto | 0:93dce1e528b9 | 11 | |
wjohnsto | 0:93dce1e528b9 | 12 | void Paddle::move(NokiaLCD &lcd, int increment) { |
wjohnsto | 0:93dce1e528b9 | 13 | lcd.fill(x,y,width,height, 0x000000); |
wjohnsto | 0:93dce1e528b9 | 14 | y += increment; |
wjohnsto | 0:93dce1e528b9 | 15 | } |
wjohnsto | 0:93dce1e528b9 | 16 | |
wjohnsto | 0:93dce1e528b9 | 17 | void Paddle::moveCPU(NokiaLCD &lcd, int _y) { |
wjohnsto | 0:93dce1e528b9 | 18 | static int inc = 1; |
wjohnsto | 0:93dce1e528b9 | 19 | lcd.fill(x,y,width,height, 0x000000); |
wjohnsto | 0:93dce1e528b9 | 20 | if(_y>y+height/2 && y+height<130) y += inc; |
wjohnsto | 0:93dce1e528b9 | 21 | else if(_y+5<y+height/2 && y>0) y -= inc; |
wjohnsto | 0:93dce1e528b9 | 22 | inc = (inc) ? 0 : 1; |
wjohnsto | 0:93dce1e528b9 | 23 | } |
wjohnsto | 0:93dce1e528b9 | 24 | |
wjohnsto | 0:93dce1e528b9 | 25 | void Paddle::draw(NokiaLCD &lcd, bool isBlack) const { |
wjohnsto | 0:93dce1e528b9 | 26 | lcd.fill(x, y, width, height, (isBlack) ? 0x000000 : color); |
wjohnsto | 0:93dce1e528b9 | 27 | } |
wjohnsto | 0:93dce1e528b9 | 28 | |
wjohnsto | 0:93dce1e528b9 | 29 | bool Paddle::loseLife() { |
wjohnsto | 0:93dce1e528b9 | 30 | return --lives; |
wjohnsto | 0:93dce1e528b9 | 31 | } |
wjohnsto | 0:93dce1e528b9 | 32 | |
wjohnsto | 0:93dce1e528b9 | 33 | void Paddle::addPoint() { |
wjohnsto | 0:93dce1e528b9 | 34 | ++score; |
wjohnsto | 0:93dce1e528b9 | 35 | } |
wjohnsto | 0:93dce1e528b9 | 36 | |
wjohnsto | 0:93dce1e528b9 | 37 | int Paddle::size() const { |
wjohnsto | 0:93dce1e528b9 | 38 | return width*height; |
wjohnsto | 0:93dce1e528b9 | 39 | } |
wjohnsto | 0:93dce1e528b9 | 40 | |
wjohnsto | 0:93dce1e528b9 | 41 | int Paddle::getWidth() const { |
wjohnsto | 0:93dce1e528b9 | 42 | return width; |
wjohnsto | 0:93dce1e528b9 | 43 | } |
wjohnsto | 0:93dce1e528b9 | 44 | |
wjohnsto | 0:93dce1e528b9 | 45 | int Paddle::getHeight() const { |
wjohnsto | 0:93dce1e528b9 | 46 | return height; |
wjohnsto | 0:93dce1e528b9 | 47 | } |
wjohnsto | 0:93dce1e528b9 | 48 | |
wjohnsto | 0:93dce1e528b9 | 49 | int Paddle::getX() const { |
wjohnsto | 0:93dce1e528b9 | 50 | return x; |
wjohnsto | 0:93dce1e528b9 | 51 | } |
wjohnsto | 0:93dce1e528b9 | 52 | |
wjohnsto | 0:93dce1e528b9 | 53 | int Paddle::getY() const { |
wjohnsto | 0:93dce1e528b9 | 54 | return y; |
wjohnsto | 0:93dce1e528b9 | 55 | } |
wjohnsto | 0:93dce1e528b9 | 56 | |
wjohnsto | 0:93dce1e528b9 | 57 | int Paddle::getLives() const { |
wjohnsto | 0:93dce1e528b9 | 58 | return lives; |
wjohnsto | 0:93dce1e528b9 | 59 | } |
wjohnsto | 0:93dce1e528b9 | 60 | |
wjohnsto | 0:93dce1e528b9 | 61 | int Paddle::getScore() const { |
wjohnsto | 0:93dce1e528b9 | 62 | return score; |
wjohnsto | 0:93dce1e528b9 | 63 | } |
wjohnsto | 0:93dce1e528b9 | 64 | |
wjohnsto | 0:93dce1e528b9 | 65 | void Paddle::setLives(int l) { |
wjohnsto | 0:93dce1e528b9 | 66 | lives = l; |
wjohnsto | 0:93dce1e528b9 | 67 | } |