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@13:6865b08e82c3, 2017-05-04 (annotated)
- Committer:
- Nathanj94
- Date:
- Thu May 04 12:24:04 2017 +0000
- Revision:
- 13:6865b08e82c3
- Parent:
- 11:5ab36993ebc3
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 | 10:2e441532206e | 13 | //Sets x and y reference co-ordinates and sets the default score to 0 |
Nathanj94 | 5:32421eb42841 | 14 | void Basket::init(int y, int width) |
Nathanj94 | 0:7e132fca6044 | 15 | { |
Nathanj94 | 5:32421eb42841 | 16 | y_ref = y; |
Nathanj94 | 10:2e441532206e | 17 | x_ref = WIDTH/2 - width/2; //basket will be central on display |
Nathanj94 | 0:7e132fca6044 | 18 | basket_width = width; |
Nathanj94 | 5:32421eb42841 | 19 | score = 0; |
Nathanj94 | 5:32421eb42841 | 20 | } |
Nathanj94 | 5:32421eb42841 | 21 | |
Nathanj94 | 9:05fd30d491f1 | 22 | //UPDATE FUNCTIONS// |
Nathanj94 | 10:2e441532206e | 23 | //Both move functions can be used simultaneously during a game |
Nathanj94 | 10:2e441532206e | 24 | |
Nathanj94 | 10:2e441532206e | 25 | //Move the basket with the joystick |
Nathanj94 | 8:48c5adc809e1 | 26 | void Basket::move_stick(Direction d, float mag) |
Nathanj94 | 0:7e132fca6044 | 27 | { |
Nathanj94 | 11:5ab36993ebc3 | 28 | stick_speed = int(mag*9.0f); //for a full push (mag = 1) basket will move 6 pixels per loop |
Nathanj94 | 0:7e132fca6044 | 29 | |
Nathanj94 | 10:2e441532206e | 30 | if (d == E) { //E = right |
Nathanj94 | 5:32421eb42841 | 31 | x_ref += stick_speed; |
Nathanj94 | 10:2e441532206e | 32 | } else if (d == W) { //W = left |
Nathanj94 | 5:32421eb42841 | 33 | x_ref -= stick_speed; |
Nathanj94 | 0:7e132fca6044 | 34 | } |
Nathanj94 | 0:7e132fca6044 | 35 | |
Nathanj94 | 10:2e441532206e | 36 | if (x_ref < 1) { //set boundary on the left of the display |
Nathanj94 | 5:32421eb42841 | 37 | x_ref = 1; |
Nathanj94 | 5:32421eb42841 | 38 | } |
Nathanj94 | 10:2e441532206e | 39 | if (x_ref > WIDTH - basket_width - 1) { //set boundary on the right of the display |
Nathanj94 | 5:32421eb42841 | 40 | x_ref = WIDTH - basket_width - 1; |
Nathanj94 | 0:7e132fca6044 | 41 | } |
Nathanj94 | 0:7e132fca6044 | 42 | } |
Nathanj94 | 0:7e132fca6044 | 43 | |
Nathanj94 | 10:2e441532206e | 44 | //Move the basket with the L and R buttons, this is supposed to be the easier option |
Nathanj94 | 8:48c5adc809e1 | 45 | void Basket::move_LR(Gamepad &pad) |
Nathanj94 | 8:48c5adc809e1 | 46 | { |
Nathanj94 | 8:48c5adc809e1 | 47 | if (pad.check_event(Gamepad::R_PRESSED) == true) { |
Nathanj94 | 10:2e441532206e | 48 | x_ref += 12.0f; //if R is pressed move 12 pixels right (one default basket width) |
Nathanj94 | 8:48c5adc809e1 | 49 | } else if (pad.check_event(Gamepad::L_PRESSED) == true) { |
Nathanj94 | 10:2e441532206e | 50 | x_ref -= 12.0f; //if L is pressed move 12 pixels left (one default basket width) |
Nathanj94 | 8:48c5adc809e1 | 51 | } |
Nathanj94 | 8:48c5adc809e1 | 52 | |
Nathanj94 | 10:2e441532206e | 53 | if (x_ref < 1) { //set boundary on the left of the display |
Nathanj94 | 8:48c5adc809e1 | 54 | x_ref = 1; |
Nathanj94 | 8:48c5adc809e1 | 55 | } |
Nathanj94 | 10:2e441532206e | 56 | if (x_ref > WIDTH - basket_width - 1) { //set boundary on the right of the display |
Nathanj94 | 8:48c5adc809e1 | 57 | x_ref = WIDTH - basket_width - 1; |
Nathanj94 | 8:48c5adc809e1 | 58 | } |
Nathanj94 | 8:48c5adc809e1 | 59 | } |
Nathanj94 | 8:48c5adc809e1 | 60 | |
Nathanj94 | 9:05fd30d491f1 | 61 | //SCORE FUNCTIONS// |
Nathanj94 | 10:2e441532206e | 62 | //Add different score for different objects (see Objects) |
Nathanj94 | 10:2e441532206e | 63 | |
Nathanj94 | 6:2aec1ed2a75a | 64 | void Basket::add_score_1() |
Nathanj94 | 0:7e132fca6044 | 65 | { |
Nathanj94 | 5:32421eb42841 | 66 | score++; |
Nathanj94 | 0:7e132fca6044 | 67 | } |
Nathanj94 | 0:7e132fca6044 | 68 | |
Nathanj94 | 6:2aec1ed2a75a | 69 | void Basket::add_score_2() |
Nathanj94 | 6:2aec1ed2a75a | 70 | { |
Nathanj94 | 6:2aec1ed2a75a | 71 | score = score + 2; |
Nathanj94 | 6:2aec1ed2a75a | 72 | } |
Nathanj94 | 6:2aec1ed2a75a | 73 | |
Nathanj94 | 6:2aec1ed2a75a | 74 | void Basket::add_score_5() |
Nathanj94 | 6:2aec1ed2a75a | 75 | { |
Nathanj94 | 6:2aec1ed2a75a | 76 | score = score + 5; |
Nathanj94 | 6:2aec1ed2a75a | 77 | } |
Nathanj94 | 6:2aec1ed2a75a | 78 | |
Nathanj94 | 6:2aec1ed2a75a | 79 | void Basket::add_score_10() |
Nathanj94 | 6:2aec1ed2a75a | 80 | { |
Nathanj94 | 6:2aec1ed2a75a | 81 | score = score + 10; |
Nathanj94 | 6:2aec1ed2a75a | 82 | } |
Nathanj94 | 6:2aec1ed2a75a | 83 | |
Nathanj94 | 5:32421eb42841 | 84 | int Basket::get_score() |
Nathanj94 | 0:7e132fca6044 | 85 | { |
Nathanj94 | 5:32421eb42841 | 86 | return score; |
Nathanj94 | 3:d8deea6454df | 87 | } |
Nathanj94 | 3:d8deea6454df | 88 | |
Nathanj94 | 9:05fd30d491f1 | 89 | //DISPLAY FUNCTIONS// |
Nathanj94 | 10:2e441532206e | 90 | //Draw the basket and make x and y reference coordinates accessible in other libraries |
Nathanj94 | 10:2e441532206e | 91 | |
Nathanj94 | 9:05fd30d491f1 | 92 | void Basket::draw(N5110 &lcd) |
Nathanj94 | 9:05fd30d491f1 | 93 | { |
Nathanj94 | 11:5ab36993ebc3 | 94 | lcd.drawRect(x_ref,y_ref, 1, 2, FILL_BLACK); |
Nathanj94 | 11:5ab36993ebc3 | 95 | lcd.drawRect(x_ref + 1, y_ref + 2,1,2,FILL_BLACK); |
Nathanj94 | 9:05fd30d491f1 | 96 | lcd.setPixel(x_ref + 2, y_ref + 4); |
Nathanj94 | 11:5ab36993ebc3 | 97 | lcd.drawRect(x_ref + 2, y_ref + 5, basket_width - 4, 1, FILL_BLACK); |
Nathanj94 | 9:05fd30d491f1 | 98 | lcd.setPixel(x_ref + 9, y_ref + 4); |
Nathanj94 | 11:5ab36993ebc3 | 99 | lcd.drawRect(x_ref + 10, y_ref + 2, 1, 2, FILL_BLACK); |
Nathanj94 | 11:5ab36993ebc3 | 100 | lcd.drawRect(x_ref + 11, y_ref, 1, 2, FILL_BLACK); |
Nathanj94 | 9:05fd30d491f1 | 101 | } |
Nathanj94 | 9:05fd30d491f1 | 102 | |
Nathanj94 | 5:32421eb42841 | 103 | int Basket::get_x() |
Nathanj94 | 4:4ce558075c32 | 104 | { |
Nathanj94 | 5:32421eb42841 | 105 | return x_ref; |
Nathanj94 | 4:4ce558075c32 | 106 | } |
Nathanj94 | 4:4ce558075c32 | 107 | |
Nathanj94 | 5:32421eb42841 | 108 | int Basket::get_y() |
Nathanj94 | 3:d8deea6454df | 109 | { |
Nathanj94 | 5:32421eb42841 | 110 | return y_ref; |
Nathanj94 | 2:5d4f2c3f3c0a | 111 | } |