ST7735 Pong by Jonne Valola, derived work from William Johnston\'s mbed Pong for NokiaLCD / PS2 keyboard This pong uses a rotary encoder hooked to pins 21 and 22, ST7735_TFT library by me, RotaryEncoder library by Shinichiro Nakamura. All copyrights of respective owners. Use on your own risk and discretion.
Dependencies: mbed ST7735_TFT RotaryEncoder TFT_fonts_old
paddle.cpp@0:2353da390056, 2011-12-12 (annotated)
- Committer:
- smultron1977
- Date:
- Mon Dec 12 21:35:42 2011 +0000
- Revision:
- 0:2353da390056
Original 2 hour hack
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
smultron1977 | 0:2353da390056 | 1 | #include "mbed.h" |
smultron1977 | 0:2353da390056 | 2 | #include "paddle.h" |
smultron1977 | 0:2353da390056 | 3 | |
smultron1977 | 0:2353da390056 | 4 | Paddle::Paddle() { |
smultron1977 | 0:2353da390056 | 5 | int x=y=width=height=color=score=0; |
smultron1977 | 0:2353da390056 | 6 | lives = 3; |
smultron1977 | 0:2353da390056 | 7 | } |
smultron1977 | 0:2353da390056 | 8 | |
smultron1977 | 0:2353da390056 | 9 | Paddle::Paddle(int x, int y, int w, int h, int c, int l, int s) |
smultron1977 | 0:2353da390056 | 10 | : x(x), y(y), width(w), height(h), color(c), lives(l), score(s) {} |
smultron1977 | 0:2353da390056 | 11 | |
smultron1977 | 0:2353da390056 | 12 | /* |
smultron1977 | 0:2353da390056 | 13 | * Member Function move: |
smultron1977 | 0:2353da390056 | 14 | * Description: Colors in the previous paddle black |
smultron1977 | 0:2353da390056 | 15 | * and moves paddle to new position. |
smultron1977 | 0:2353da390056 | 16 | */ |
smultron1977 | 0:2353da390056 | 17 | void Paddle::move(ST7735_TFT &lcd, int increment) { |
smultron1977 | 0:2353da390056 | 18 | draw(lcd, true); |
smultron1977 | 0:2353da390056 | 19 | y += increment; |
smultron1977 | 0:2353da390056 | 20 | } |
smultron1977 | 0:2353da390056 | 21 | |
smultron1977 | 0:2353da390056 | 22 | |
smultron1977 | 0:2353da390056 | 23 | /* |
smultron1977 | 0:2353da390056 | 24 | * Member Function moveCPU: |
smultron1977 | 0:2353da390056 | 25 | * Description: Colors in the previous paddle black |
smultron1977 | 0:2353da390056 | 26 | * and moves paddle to new position. |
smultron1977 | 0:2353da390056 | 27 | * inc variable allows paddle to only move every |
smultron1977 | 0:2353da390056 | 28 | * other function call. |
smultron1977 | 0:2353da390056 | 29 | */ |
smultron1977 | 0:2353da390056 | 30 | void Paddle::moveCPU(ST7735_TFT &lcd, int _y) { |
smultron1977 | 0:2353da390056 | 31 | static int inc = 1; |
smultron1977 | 0:2353da390056 | 32 | draw(lcd, true); |
smultron1977 | 0:2353da390056 | 33 | if(_y>y+height/2 && y+height<128) y += inc; |
smultron1977 | 0:2353da390056 | 34 | else if(_y+5<y+height/2 && y>0) y -= inc; |
smultron1977 | 0:2353da390056 | 35 | inc = (inc) ? 0 : 1; |
smultron1977 | 0:2353da390056 | 36 | } |
smultron1977 | 0:2353da390056 | 37 | |
smultron1977 | 0:2353da390056 | 38 | void Paddle::draw(ST7735_TFT &lcd, bool isBlack) const { |
smultron1977 | 0:2353da390056 | 39 | lcd.fillrect(x, y, x + width, y + height, (isBlack) ? 0x000000 : color); |
smultron1977 | 0:2353da390056 | 40 | } |
smultron1977 | 0:2353da390056 | 41 | |
smultron1977 | 0:2353da390056 | 42 | bool Paddle::loseLife() { |
smultron1977 | 0:2353da390056 | 43 | return --lives; |
smultron1977 | 0:2353da390056 | 44 | } |
smultron1977 | 0:2353da390056 | 45 | |
smultron1977 | 0:2353da390056 | 46 | void Paddle::addPoint() { |
smultron1977 | 0:2353da390056 | 47 | ++score; |
smultron1977 | 0:2353da390056 | 48 | } |
smultron1977 | 0:2353da390056 | 49 | |
smultron1977 | 0:2353da390056 | 50 | int Paddle::size() const { |
smultron1977 | 0:2353da390056 | 51 | return width*height; |
smultron1977 | 0:2353da390056 | 52 | } |
smultron1977 | 0:2353da390056 | 53 | |
smultron1977 | 0:2353da390056 | 54 | int Paddle::getWidth() const { |
smultron1977 | 0:2353da390056 | 55 | return width; |
smultron1977 | 0:2353da390056 | 56 | } |
smultron1977 | 0:2353da390056 | 57 | |
smultron1977 | 0:2353da390056 | 58 | int Paddle::getHeight() const { |
smultron1977 | 0:2353da390056 | 59 | return height; |
smultron1977 | 0:2353da390056 | 60 | } |
smultron1977 | 0:2353da390056 | 61 | |
smultron1977 | 0:2353da390056 | 62 | int Paddle::getX() const { |
smultron1977 | 0:2353da390056 | 63 | return x; |
smultron1977 | 0:2353da390056 | 64 | } |
smultron1977 | 0:2353da390056 | 65 | |
smultron1977 | 0:2353da390056 | 66 | int Paddle::getY() const { |
smultron1977 | 0:2353da390056 | 67 | return y; |
smultron1977 | 0:2353da390056 | 68 | } |
smultron1977 | 0:2353da390056 | 69 | |
smultron1977 | 0:2353da390056 | 70 | int Paddle::getLives() const { |
smultron1977 | 0:2353da390056 | 71 | return lives; |
smultron1977 | 0:2353da390056 | 72 | } |
smultron1977 | 0:2353da390056 | 73 | |
smultron1977 | 0:2353da390056 | 74 | int Paddle::getScore() const { |
smultron1977 | 0:2353da390056 | 75 | return score; |
smultron1977 | 0:2353da390056 | 76 | } |
smultron1977 | 0:2353da390056 | 77 | |
smultron1977 | 0:2353da390056 | 78 | void Paddle::setLives(int l) { |
smultron1977 | 0:2353da390056 | 79 | lives = l; |
smultron1977 | 0:2353da390056 | 80 | } |