Library that draws a basket on a Nokia N5110 LCD display and allows it to be moved left/right on the display using buttons or a joystick.
Dependents: Game_Controller_Project
Basket.cpp@5:32421eb42841, 2017-03-23 (annotated)
- Committer:
- Nathanj94
- Date:
- Thu Mar 23 15:24:22 2017 +0000
- Revision:
- 5:32421eb42841
- Parent:
- 4:4ce558075c32
- Child:
- 6:2aec1ed2a75a
Improved names of functions and variables;; Added use of L/R buttons to move basket but not working while the buttons are held down, only with each press
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Nathanj94 | 0:7e132fca6044 | 1 | #include "Basket.h" |
Nathanj94 | 0:7e132fca6044 | 2 | |
Nathanj94 | 0:7e132fca6044 | 3 | Basket::Basket() |
Nathanj94 | 0:7e132fca6044 | 4 | { |
Nathanj94 | 0:7e132fca6044 | 5 | |
Nathanj94 | 0:7e132fca6044 | 6 | } |
Nathanj94 | 0:7e132fca6044 | 7 | |
Nathanj94 | 0:7e132fca6044 | 8 | Basket::~Basket() |
Nathanj94 | 0:7e132fca6044 | 9 | { |
Nathanj94 | 0:7e132fca6044 | 10 | |
Nathanj94 | 0:7e132fca6044 | 11 | } |
Nathanj94 | 2:5d4f2c3f3c0a | 12 | |
Nathanj94 | 5:32421eb42841 | 13 | void Basket::init(int y, int width) |
Nathanj94 | 0:7e132fca6044 | 14 | { |
Nathanj94 | 5:32421eb42841 | 15 | y_ref = y; |
Nathanj94 | 5:32421eb42841 | 16 | x_ref = WIDTH/2 - width/2; |
Nathanj94 | 0:7e132fca6044 | 17 | basket_width = width; |
Nathanj94 | 5:32421eb42841 | 18 | score = 0; |
Nathanj94 | 5:32421eb42841 | 19 | } |
Nathanj94 | 5:32421eb42841 | 20 | |
Nathanj94 | 5:32421eb42841 | 21 | void Basket::draw(N5110 &lcd) |
Nathanj94 | 5:32421eb42841 | 22 | { |
Nathanj94 | 5:32421eb42841 | 23 | lcd.drawRect(x_ref,y_ref, 1, 2, 0); |
Nathanj94 | 5:32421eb42841 | 24 | lcd.drawRect(x_ref + 1, y_ref + 2,1,2,0); |
Nathanj94 | 5:32421eb42841 | 25 | lcd.setPixel(x_ref + 2, y_ref + 4); |
Nathanj94 | 5:32421eb42841 | 26 | lcd.drawRect(x_ref + 2, y_ref + 5, basket_width - 4, 1, 0); |
Nathanj94 | 5:32421eb42841 | 27 | lcd.setPixel(x_ref + 9, y_ref + 4); |
Nathanj94 | 5:32421eb42841 | 28 | lcd.drawRect(x_ref + 10, y_ref + 2, 1, 2, 0); |
Nathanj94 | 5:32421eb42841 | 29 | lcd.drawRect(x_ref + 11, y_ref, 1, 2, 0); |
Nathanj94 | 0:7e132fca6044 | 30 | } |
Nathanj94 | 0:7e132fca6044 | 31 | |
Nathanj94 | 5:32421eb42841 | 32 | void Basket::move(Direction d, float mag, Gamepad &pad) |
Nathanj94 | 0:7e132fca6044 | 33 | { |
Nathanj94 | 5:32421eb42841 | 34 | stick_speed = int(mag*8.0f); |
Nathanj94 | 0:7e132fca6044 | 35 | |
Nathanj94 | 2:5d4f2c3f3c0a | 36 | if (d == E) { |
Nathanj94 | 5:32421eb42841 | 37 | x_ref += stick_speed; |
Nathanj94 | 2:5d4f2c3f3c0a | 38 | } else if (d == W) { |
Nathanj94 | 5:32421eb42841 | 39 | x_ref -= stick_speed; |
Nathanj94 | 0:7e132fca6044 | 40 | } |
Nathanj94 | 0:7e132fca6044 | 41 | |
Nathanj94 | 5:32421eb42841 | 42 | bumper_speed = 5; |
Nathanj94 | 5:32421eb42841 | 43 | |
Nathanj94 | 5:32421eb42841 | 44 | if (pad.check_event(Gamepad::R_PRESSED)) { |
Nathanj94 | 5:32421eb42841 | 45 | x_ref += bumper_speed; |
Nathanj94 | 5:32421eb42841 | 46 | } else if (pad.check_event(Gamepad::L_PRESSED)) { |
Nathanj94 | 5:32421eb42841 | 47 | x_ref -= bumper_speed; |
Nathanj94 | 0:7e132fca6044 | 48 | } |
Nathanj94 | 5:32421eb42841 | 49 | |
Nathanj94 | 5:32421eb42841 | 50 | if (x_ref < 1) { |
Nathanj94 | 5:32421eb42841 | 51 | x_ref = 1; |
Nathanj94 | 5:32421eb42841 | 52 | } |
Nathanj94 | 5:32421eb42841 | 53 | if (x_ref > WIDTH - basket_width - 1) { |
Nathanj94 | 5:32421eb42841 | 54 | x_ref = WIDTH - basket_width - 1; |
Nathanj94 | 0:7e132fca6044 | 55 | } |
Nathanj94 | 0:7e132fca6044 | 56 | } |
Nathanj94 | 0:7e132fca6044 | 57 | |
Nathanj94 | 5:32421eb42841 | 58 | void Basket::add_score() |
Nathanj94 | 0:7e132fca6044 | 59 | { |
Nathanj94 | 5:32421eb42841 | 60 | score++; |
Nathanj94 | 0:7e132fca6044 | 61 | } |
Nathanj94 | 0:7e132fca6044 | 62 | |
Nathanj94 | 5:32421eb42841 | 63 | int Basket::get_score() |
Nathanj94 | 0:7e132fca6044 | 64 | { |
Nathanj94 | 5:32421eb42841 | 65 | return score; |
Nathanj94 | 3:d8deea6454df | 66 | } |
Nathanj94 | 3:d8deea6454df | 67 | |
Nathanj94 | 5:32421eb42841 | 68 | int Basket::get_x() |
Nathanj94 | 4:4ce558075c32 | 69 | { |
Nathanj94 | 5:32421eb42841 | 70 | return x_ref; |
Nathanj94 | 4:4ce558075c32 | 71 | } |
Nathanj94 | 4:4ce558075c32 | 72 | |
Nathanj94 | 5:32421eb42841 | 73 | int Basket::get_y() |
Nathanj94 | 3:d8deea6454df | 74 | { |
Nathanj94 | 5:32421eb42841 | 75 | return y_ref; |
Nathanj94 | 2:5d4f2c3f3c0a | 76 | } |