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@8:48c5adc809e1, 2017-04-18 (annotated)
- Committer:
- Nathanj94
- Date:
- Tue Apr 18 14:27:06 2017 +0000
- Revision:
- 8:48c5adc809e1
- Parent:
- 7:7b71dd93b2f8
- Child:
- 9:05fd30d491f1
Basket can be moved correctly/consistently with L and R buttons
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 | 8:48c5adc809e1 | 32 | void Basket::move_stick(Direction d, float mag) |
Nathanj94 | 0:7e132fca6044 | 33 | { |
Nathanj94 | 8:48c5adc809e1 | 34 | stick_speed = int(mag*6.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 | if (x_ref < 1) { |
Nathanj94 | 5:32421eb42841 | 43 | x_ref = 1; |
Nathanj94 | 5:32421eb42841 | 44 | } |
Nathanj94 | 5:32421eb42841 | 45 | if (x_ref > WIDTH - basket_width - 1) { |
Nathanj94 | 5:32421eb42841 | 46 | x_ref = WIDTH - basket_width - 1; |
Nathanj94 | 0:7e132fca6044 | 47 | } |
Nathanj94 | 0:7e132fca6044 | 48 | } |
Nathanj94 | 0:7e132fca6044 | 49 | |
Nathanj94 | 8:48c5adc809e1 | 50 | void Basket::move_LR(Gamepad &pad) |
Nathanj94 | 8:48c5adc809e1 | 51 | { |
Nathanj94 | 8:48c5adc809e1 | 52 | if (pad.check_event(Gamepad::R_PRESSED) == true) { |
Nathanj94 | 8:48c5adc809e1 | 53 | x_ref += 12.0f; |
Nathanj94 | 8:48c5adc809e1 | 54 | } else if (pad.check_event(Gamepad::L_PRESSED) == true) { |
Nathanj94 | 8:48c5adc809e1 | 55 | x_ref -= 12.0f; |
Nathanj94 | 8:48c5adc809e1 | 56 | } |
Nathanj94 | 8:48c5adc809e1 | 57 | |
Nathanj94 | 8:48c5adc809e1 | 58 | if (x_ref < 1) { |
Nathanj94 | 8:48c5adc809e1 | 59 | x_ref = 1; |
Nathanj94 | 8:48c5adc809e1 | 60 | } |
Nathanj94 | 8:48c5adc809e1 | 61 | if (x_ref > WIDTH - basket_width - 1) { |
Nathanj94 | 8:48c5adc809e1 | 62 | x_ref = WIDTH - basket_width - 1; |
Nathanj94 | 8:48c5adc809e1 | 63 | } |
Nathanj94 | 8:48c5adc809e1 | 64 | } |
Nathanj94 | 8:48c5adc809e1 | 65 | |
Nathanj94 | 6:2aec1ed2a75a | 66 | void Basket::add_score_1() |
Nathanj94 | 0:7e132fca6044 | 67 | { |
Nathanj94 | 5:32421eb42841 | 68 | score++; |
Nathanj94 | 0:7e132fca6044 | 69 | } |
Nathanj94 | 0:7e132fca6044 | 70 | |
Nathanj94 | 6:2aec1ed2a75a | 71 | void Basket::add_score_2() |
Nathanj94 | 6:2aec1ed2a75a | 72 | { |
Nathanj94 | 6:2aec1ed2a75a | 73 | score = score + 2; |
Nathanj94 | 6:2aec1ed2a75a | 74 | } |
Nathanj94 | 6:2aec1ed2a75a | 75 | |
Nathanj94 | 6:2aec1ed2a75a | 76 | void Basket::add_score_5() |
Nathanj94 | 6:2aec1ed2a75a | 77 | { |
Nathanj94 | 6:2aec1ed2a75a | 78 | score = score + 5; |
Nathanj94 | 6:2aec1ed2a75a | 79 | } |
Nathanj94 | 6:2aec1ed2a75a | 80 | |
Nathanj94 | 6:2aec1ed2a75a | 81 | void Basket::add_score_10() |
Nathanj94 | 6:2aec1ed2a75a | 82 | { |
Nathanj94 | 6:2aec1ed2a75a | 83 | score = score + 10; |
Nathanj94 | 6:2aec1ed2a75a | 84 | } |
Nathanj94 | 6:2aec1ed2a75a | 85 | |
Nathanj94 | 5:32421eb42841 | 86 | int Basket::get_score() |
Nathanj94 | 0:7e132fca6044 | 87 | { |
Nathanj94 | 5:32421eb42841 | 88 | return score; |
Nathanj94 | 3:d8deea6454df | 89 | } |
Nathanj94 | 3:d8deea6454df | 90 | |
Nathanj94 | 5:32421eb42841 | 91 | int Basket::get_x() |
Nathanj94 | 4:4ce558075c32 | 92 | { |
Nathanj94 | 5:32421eb42841 | 93 | return x_ref; |
Nathanj94 | 4:4ce558075c32 | 94 | } |
Nathanj94 | 4:4ce558075c32 | 95 | |
Nathanj94 | 5:32421eb42841 | 96 | int Basket::get_y() |
Nathanj94 | 3:d8deea6454df | 97 | { |
Nathanj94 | 5:32421eb42841 | 98 | return y_ref; |
Nathanj94 | 2:5d4f2c3f3c0a | 99 | } |