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@9:05fd30d491f1, 2017-04-22 (annotated)
- Committer:
- Nathanj94
- Date:
- Sat Apr 22 12:21:58 2017 +0000
- Revision:
- 9:05fd30d491f1
- Parent:
- 8:48c5adc809e1
- Child:
- 10:2e441532206e
Removed redundancy and organised header/main ready for final notes
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 | 9:05fd30d491f1 | 12 | //INITILISATION FUNCTION// |
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 | 9:05fd30d491f1 | 21 | //UPDATE FUNCTIONS// |
Nathanj94 | 8:48c5adc809e1 | 22 | void Basket::move_stick(Direction d, float mag) |
Nathanj94 | 0:7e132fca6044 | 23 | { |
Nathanj94 | 8:48c5adc809e1 | 24 | stick_speed = int(mag*6.0f); |
Nathanj94 | 0:7e132fca6044 | 25 | |
Nathanj94 | 2:5d4f2c3f3c0a | 26 | if (d == E) { |
Nathanj94 | 5:32421eb42841 | 27 | x_ref += stick_speed; |
Nathanj94 | 2:5d4f2c3f3c0a | 28 | } else if (d == W) { |
Nathanj94 | 5:32421eb42841 | 29 | x_ref -= stick_speed; |
Nathanj94 | 0:7e132fca6044 | 30 | } |
Nathanj94 | 0:7e132fca6044 | 31 | |
Nathanj94 | 5:32421eb42841 | 32 | if (x_ref < 1) { |
Nathanj94 | 5:32421eb42841 | 33 | x_ref = 1; |
Nathanj94 | 5:32421eb42841 | 34 | } |
Nathanj94 | 5:32421eb42841 | 35 | if (x_ref > WIDTH - basket_width - 1) { |
Nathanj94 | 5:32421eb42841 | 36 | x_ref = WIDTH - basket_width - 1; |
Nathanj94 | 0:7e132fca6044 | 37 | } |
Nathanj94 | 0:7e132fca6044 | 38 | } |
Nathanj94 | 0:7e132fca6044 | 39 | |
Nathanj94 | 8:48c5adc809e1 | 40 | void Basket::move_LR(Gamepad &pad) |
Nathanj94 | 8:48c5adc809e1 | 41 | { |
Nathanj94 | 8:48c5adc809e1 | 42 | if (pad.check_event(Gamepad::R_PRESSED) == true) { |
Nathanj94 | 8:48c5adc809e1 | 43 | x_ref += 12.0f; |
Nathanj94 | 8:48c5adc809e1 | 44 | } else if (pad.check_event(Gamepad::L_PRESSED) == true) { |
Nathanj94 | 8:48c5adc809e1 | 45 | x_ref -= 12.0f; |
Nathanj94 | 8:48c5adc809e1 | 46 | } |
Nathanj94 | 8:48c5adc809e1 | 47 | |
Nathanj94 | 8:48c5adc809e1 | 48 | if (x_ref < 1) { |
Nathanj94 | 8:48c5adc809e1 | 49 | x_ref = 1; |
Nathanj94 | 8:48c5adc809e1 | 50 | } |
Nathanj94 | 8:48c5adc809e1 | 51 | if (x_ref > WIDTH - basket_width - 1) { |
Nathanj94 | 8:48c5adc809e1 | 52 | x_ref = WIDTH - basket_width - 1; |
Nathanj94 | 8:48c5adc809e1 | 53 | } |
Nathanj94 | 8:48c5adc809e1 | 54 | } |
Nathanj94 | 8:48c5adc809e1 | 55 | |
Nathanj94 | 9:05fd30d491f1 | 56 | //SCORE FUNCTIONS// |
Nathanj94 | 6:2aec1ed2a75a | 57 | void Basket::add_score_1() |
Nathanj94 | 0:7e132fca6044 | 58 | { |
Nathanj94 | 5:32421eb42841 | 59 | score++; |
Nathanj94 | 0:7e132fca6044 | 60 | } |
Nathanj94 | 0:7e132fca6044 | 61 | |
Nathanj94 | 6:2aec1ed2a75a | 62 | void Basket::add_score_2() |
Nathanj94 | 6:2aec1ed2a75a | 63 | { |
Nathanj94 | 6:2aec1ed2a75a | 64 | score = score + 2; |
Nathanj94 | 6:2aec1ed2a75a | 65 | } |
Nathanj94 | 6:2aec1ed2a75a | 66 | |
Nathanj94 | 6:2aec1ed2a75a | 67 | void Basket::add_score_5() |
Nathanj94 | 6:2aec1ed2a75a | 68 | { |
Nathanj94 | 6:2aec1ed2a75a | 69 | score = score + 5; |
Nathanj94 | 6:2aec1ed2a75a | 70 | } |
Nathanj94 | 6:2aec1ed2a75a | 71 | |
Nathanj94 | 6:2aec1ed2a75a | 72 | void Basket::add_score_10() |
Nathanj94 | 6:2aec1ed2a75a | 73 | { |
Nathanj94 | 6:2aec1ed2a75a | 74 | score = score + 10; |
Nathanj94 | 6:2aec1ed2a75a | 75 | } |
Nathanj94 | 6:2aec1ed2a75a | 76 | |
Nathanj94 | 5:32421eb42841 | 77 | int Basket::get_score() |
Nathanj94 | 0:7e132fca6044 | 78 | { |
Nathanj94 | 5:32421eb42841 | 79 | return score; |
Nathanj94 | 3:d8deea6454df | 80 | } |
Nathanj94 | 3:d8deea6454df | 81 | |
Nathanj94 | 9:05fd30d491f1 | 82 | //DISPLAY FUNCTIONS// |
Nathanj94 | 9:05fd30d491f1 | 83 | void Basket::draw(N5110 &lcd) |
Nathanj94 | 9:05fd30d491f1 | 84 | { |
Nathanj94 | 9:05fd30d491f1 | 85 | lcd.drawRect(x_ref,y_ref, 1, 2, 0); |
Nathanj94 | 9:05fd30d491f1 | 86 | lcd.drawRect(x_ref + 1, y_ref + 2,1,2,0); |
Nathanj94 | 9:05fd30d491f1 | 87 | lcd.setPixel(x_ref + 2, y_ref + 4); |
Nathanj94 | 9:05fd30d491f1 | 88 | lcd.drawRect(x_ref + 2, y_ref + 5, basket_width - 4, 1, 0); |
Nathanj94 | 9:05fd30d491f1 | 89 | lcd.setPixel(x_ref + 9, y_ref + 4); |
Nathanj94 | 9:05fd30d491f1 | 90 | lcd.drawRect(x_ref + 10, y_ref + 2, 1, 2, 0); |
Nathanj94 | 9:05fd30d491f1 | 91 | lcd.drawRect(x_ref + 11, y_ref, 1, 2, 0); |
Nathanj94 | 9:05fd30d491f1 | 92 | } |
Nathanj94 | 9:05fd30d491f1 | 93 | |
Nathanj94 | 5:32421eb42841 | 94 | int Basket::get_x() |
Nathanj94 | 4:4ce558075c32 | 95 | { |
Nathanj94 | 5:32421eb42841 | 96 | return x_ref; |
Nathanj94 | 4:4ce558075c32 | 97 | } |
Nathanj94 | 4:4ce558075c32 | 98 | |
Nathanj94 | 5:32421eb42841 | 99 | int Basket::get_y() |
Nathanj94 | 3:d8deea6454df | 100 | { |
Nathanj94 | 5:32421eb42841 | 101 | return y_ref; |
Nathanj94 | 2:5d4f2c3f3c0a | 102 | } |