![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Custom Game Controllers assembled in lab sessions and mounted with Nokia N5110 LCD display and a FRDM-K64F mbed plus various buttons, a joystick, potentiometer and piezo. Designed a game called 'Fruit Basket' to be played on the game controller where the player controls a basket and moves it catch objects that fall from random points along the top of the display to collect score.
Dependencies: Basket Catch_Model Fruit Gamepad N5110 Objects mbed
Project_Submission.cpp@13:09a9ffd8eb60, 2017-04-20 (annotated)
- Committer:
- Nathanj94
- Date:
- Thu Apr 20 14:27:40 2017 +0000
- Revision:
- 13:09a9ffd8eb60
- Parent:
- 12:d87c9ae89472
- Child:
- 14:e20bf6569607
Function made to allow user to select game speed before the game starts. Previous problem had been confusion between float and int for Catch_Model::init()
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Nathanj94 | 2:ada503e3486f | 1 | #include "mbed.h" |
Nathanj94 | 2:ada503e3486f | 2 | #include "Gamepad.h" |
Nathanj94 | 2:ada503e3486f | 3 | #include "N5110.h" |
Nathanj94 | 2:ada503e3486f | 4 | #include "Catch_Model.h" |
Nathanj94 | 2:ada503e3486f | 5 | |
Nathanj94 | 2:ada503e3486f | 6 | #define BASKET_Y 41 |
Nathanj94 | 2:ada503e3486f | 7 | #define BASKET_WIDTH 12 |
Nathanj94 | 7:32afc46c30f3 | 8 | #define LIVES 5 |
Nathanj94 | 2:ada503e3486f | 9 | |
Nathanj94 | 4:039294e6a8a5 | 10 | //OBJECTS// |
Nathanj94 | 2:ada503e3486f | 11 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
Nathanj94 | 3:69296f999fdf | 12 | Gamepad pad; |
Nathanj94 | 2:ada503e3486f | 13 | Catch_Model catchm; |
Nathanj94 | 11:a6a88a51dd57 | 14 | Objects objects; |
Nathanj94 | 4:039294e6a8a5 | 15 | |
Nathanj94 | 13:09a9ffd8eb60 | 16 | int objects_speed; |
Nathanj94 | 13:09a9ffd8eb60 | 17 | int lives; |
Nathanj94 | 12:d87c9ae89472 | 18 | |
Nathanj94 | 4:039294e6a8a5 | 19 | //FUNCTION PROTOTYPES// |
Nathanj94 | 2:ada503e3486f | 20 | void init(); |
Nathanj94 | 2:ada503e3486f | 21 | void render(); |
Nathanj94 | 4:039294e6a8a5 | 22 | void welcome(); |
Nathanj94 | 13:09a9ffd8eb60 | 23 | void instructions_general(); |
Nathanj94 | 13:09a9ffd8eb60 | 24 | void instructions_buttons(); |
Nathanj94 | 13:09a9ffd8eb60 | 25 | void set_brightness(); |
Nathanj94 | 13:09a9ffd8eb60 | 26 | int set_speed(); |
Nathanj94 | 4:039294e6a8a5 | 27 | |
Nathanj94 | 4:039294e6a8a5 | 28 | //MAIN// |
Nathanj94 | 2:ada503e3486f | 29 | int main() |
Nathanj94 | 2:ada503e3486f | 30 | { |
Nathanj94 | 4:039294e6a8a5 | 31 | int fps = 8; |
Nathanj94 | 4:039294e6a8a5 | 32 | |
Nathanj94 | 4:039294e6a8a5 | 33 | while(1) { |
Nathanj94 | 2:ada503e3486f | 34 | |
Nathanj94 | 4:039294e6a8a5 | 35 | init(); |
Nathanj94 | 13:09a9ffd8eb60 | 36 | |
Nathanj94 | 4:039294e6a8a5 | 37 | welcome(); |
Nathanj94 | 13:09a9ffd8eb60 | 38 | instructions_general(); |
Nathanj94 | 13:09a9ffd8eb60 | 39 | instructions_buttons(); |
Nathanj94 | 13:09a9ffd8eb60 | 40 | |
Nathanj94 | 13:09a9ffd8eb60 | 41 | set_brightness(); |
Nathanj94 | 13:09a9ffd8eb60 | 42 | set_speed(); |
Nathanj94 | 13:09a9ffd8eb60 | 43 | catchm.init(BASKET_Y,BASKET_WIDTH,set_speed(),LIVES); |
Nathanj94 | 13:09a9ffd8eb60 | 44 | |
Nathanj94 | 4:039294e6a8a5 | 45 | render(); |
Nathanj94 | 4:039294e6a8a5 | 46 | wait(1.0f/fps); |
Nathanj94 | 13:09a9ffd8eb60 | 47 | //int lives; |
Nathanj94 | 4:039294e6a8a5 | 48 | |
Nathanj94 | 4:039294e6a8a5 | 49 | do { |
Nathanj94 | 4:039294e6a8a5 | 50 | catchm.input(pad); |
Nathanj94 | 4:039294e6a8a5 | 51 | catchm.update(lcd, pad); |
Nathanj94 | 9:3e7fca03c7c1 | 52 | |
Nathanj94 | 9:3e7fca03c7c1 | 53 | catchm.check_a(lcd,pad); |
Nathanj94 | 9:3e7fca03c7c1 | 54 | catchm.check_b(lcd,pad); |
Nathanj94 | 10:92a658c3c5a4 | 55 | catchm.check_x(lcd,pad); |
Nathanj94 | 9:3e7fca03c7c1 | 56 | |
Nathanj94 | 4:039294e6a8a5 | 57 | lives = catchm.get_lives(); |
Nathanj94 | 4:039294e6a8a5 | 58 | render(); |
Nathanj94 | 4:039294e6a8a5 | 59 | wait(1.0f/fps); |
Nathanj94 | 11:a6a88a51dd57 | 60 | |
Nathanj94 | 12:d87c9ae89472 | 61 | } while (lives > 0); |
Nathanj94 | 2:ada503e3486f | 62 | } |
Nathanj94 | 2:ada503e3486f | 63 | } |
Nathanj94 | 2:ada503e3486f | 64 | |
Nathanj94 | 4:039294e6a8a5 | 65 | //FUNCTIONS// |
Nathanj94 | 2:ada503e3486f | 66 | void init() |
Nathanj94 | 2:ada503e3486f | 67 | { |
Nathanj94 | 4:039294e6a8a5 | 68 | // initialise LCD and Gamepad |
Nathanj94 | 2:ada503e3486f | 69 | lcd.init(); |
Nathanj94 | 2:ada503e3486f | 70 | pad.init(); |
Nathanj94 | 2:ada503e3486f | 71 | } |
Nathanj94 | 2:ada503e3486f | 72 | |
Nathanj94 | 2:ada503e3486f | 73 | void render() |
Nathanj94 | 2:ada503e3486f | 74 | { |
Nathanj94 | 4:039294e6a8a5 | 75 | // re-draw screen each loop |
Nathanj94 | 2:ada503e3486f | 76 | lcd.clear(); |
Nathanj94 | 2:ada503e3486f | 77 | catchm.draw(lcd); |
Nathanj94 | 2:ada503e3486f | 78 | lcd.refresh(); |
Nathanj94 | 2:ada503e3486f | 79 | } |
Nathanj94 | 4:039294e6a8a5 | 80 | |
Nathanj94 | 11:a6a88a51dd57 | 81 | void welcome() |
Nathanj94 | 11:a6a88a51dd57 | 82 | { |
Nathanj94 | 12:d87c9ae89472 | 83 | lcd.printString("FRUIT BASKET",0,2); |
Nathanj94 | 12:d87c9ae89472 | 84 | lcd.printString("press start",0,3); |
Nathanj94 | 4:039294e6a8a5 | 85 | lcd.refresh(); |
Nathanj94 | 4:039294e6a8a5 | 86 | |
Nathanj94 | 5:136b13a9b8b5 | 87 | while (pad.check_event(Gamepad::START_PRESSED) == false) { |
Nathanj94 | 4:039294e6a8a5 | 88 | pad.leds_on(); |
Nathanj94 | 4:039294e6a8a5 | 89 | wait(0.1); |
Nathanj94 | 4:039294e6a8a5 | 90 | pad.leds_off(); |
Nathanj94 | 4:039294e6a8a5 | 91 | wait(0.1); |
Nathanj94 | 4:039294e6a8a5 | 92 | } |
Nathanj94 | 11:a6a88a51dd57 | 93 | } |
Nathanj94 | 11:a6a88a51dd57 | 94 | |
Nathanj94 | 13:09a9ffd8eb60 | 95 | void instructions_general() |
Nathanj94 | 11:a6a88a51dd57 | 96 | { |
Nathanj94 | 11:a6a88a51dd57 | 97 | lcd.clear(); |
Nathanj94 | 13:09a9ffd8eb60 | 98 | lcd.printString("Move the",0,0); |
Nathanj94 | 13:09a9ffd8eb60 | 99 | lcd.printString("basket with",0,1); |
Nathanj94 | 13:09a9ffd8eb60 | 100 | lcd.printString("L/R or the",0,2); |
Nathanj94 | 13:09a9ffd8eb60 | 101 | lcd.printString("joystick to",0,3); |
Nathanj94 | 13:09a9ffd8eb60 | 102 | lcd.printString("catch the",0,4); |
Nathanj94 | 13:09a9ffd8eb60 | 103 | lcd.printString("fruit.",0,5); |
Nathanj94 | 13:09a9ffd8eb60 | 104 | lcd.refresh(); |
Nathanj94 | 13:09a9ffd8eb60 | 105 | |
Nathanj94 | 13:09a9ffd8eb60 | 106 | while (pad.check_event(Gamepad::START_PRESSED) == false) { |
Nathanj94 | 13:09a9ffd8eb60 | 107 | pad.leds_on(); |
Nathanj94 | 13:09a9ffd8eb60 | 108 | wait(0.1); |
Nathanj94 | 13:09a9ffd8eb60 | 109 | pad.leds_off(); |
Nathanj94 | 13:09a9ffd8eb60 | 110 | wait(0.1); |
Nathanj94 | 13:09a9ffd8eb60 | 111 | } |
Nathanj94 | 13:09a9ffd8eb60 | 112 | } |
Nathanj94 | 13:09a9ffd8eb60 | 113 | |
Nathanj94 | 13:09a9ffd8eb60 | 114 | void instructions_buttons() |
Nathanj94 | 13:09a9ffd8eb60 | 115 | { |
Nathanj94 | 13:09a9ffd8eb60 | 116 | lcd.clear(); |
Nathanj94 | 13:09a9ffd8eb60 | 117 | lcd.printString("Press A for a",0,0); |
Nathanj94 | 13:09a9ffd8eb60 | 118 | lcd.printString("second chance.",0,1); |
Nathanj94 | 13:09a9ffd8eb60 | 119 | lcd.printString("Press B for an",0,2); |
Nathanj94 | 13:09a9ffd8eb60 | 120 | lcd.printString("extra life.",0,3); |
Nathanj94 | 13:09a9ffd8eb60 | 121 | lcd.printString("Available when",0,4); |
Nathanj94 | 13:09a9ffd8eb60 | 122 | lcd.printString("you see the",0,5); |
Nathanj94 | 13:09a9ffd8eb60 | 123 | lcd.drawLine(69,43,73,47,1); |
Nathanj94 | 13:09a9ffd8eb60 | 124 | lcd.drawLine(73,47,77,39,1); |
Nathanj94 | 11:a6a88a51dd57 | 125 | lcd.refresh(); |
Nathanj94 | 11:a6a88a51dd57 | 126 | |
Nathanj94 | 11:a6a88a51dd57 | 127 | while (pad.check_event(Gamepad::START_PRESSED) == false) { |
Nathanj94 | 13:09a9ffd8eb60 | 128 | pad.leds_on(); |
Nathanj94 | 13:09a9ffd8eb60 | 129 | wait(0.1); |
Nathanj94 | 13:09a9ffd8eb60 | 130 | pad.leds_off(); |
Nathanj94 | 13:09a9ffd8eb60 | 131 | wait(0.1); |
Nathanj94 | 13:09a9ffd8eb60 | 132 | } |
Nathanj94 | 13:09a9ffd8eb60 | 133 | } |
Nathanj94 | 13:09a9ffd8eb60 | 134 | |
Nathanj94 | 13:09a9ffd8eb60 | 135 | void set_brightness() |
Nathanj94 | 13:09a9ffd8eb60 | 136 | { |
Nathanj94 | 13:09a9ffd8eb60 | 137 | lcd.clear(); |
Nathanj94 | 13:09a9ffd8eb60 | 138 | lcd.printString("Set Brightness",0,2); |
Nathanj94 | 13:09a9ffd8eb60 | 139 | lcd.refresh(); |
Nathanj94 | 13:09a9ffd8eb60 | 140 | |
Nathanj94 | 13:09a9ffd8eb60 | 141 | while (pad.check_event(Gamepad::START_PRESSED) == false) { |
Nathanj94 | 13:09a9ffd8eb60 | 142 | lcd.setBrightness(pad.read_pot()); |
Nathanj94 | 13:09a9ffd8eb60 | 143 | pad.leds(pad.read_pot()); |
Nathanj94 | 13:09a9ffd8eb60 | 144 | } |
Nathanj94 | 13:09a9ffd8eb60 | 145 | } |
Nathanj94 | 13:09a9ffd8eb60 | 146 | |
Nathanj94 | 13:09a9ffd8eb60 | 147 | int set_speed() |
Nathanj94 | 13:09a9ffd8eb60 | 148 | { |
Nathanj94 | 13:09a9ffd8eb60 | 149 | while (pad.check_event(Gamepad::START_PRESSED) == false) { |
Nathanj94 | 13:09a9ffd8eb60 | 150 | lcd.clear(); |
Nathanj94 | 13:09a9ffd8eb60 | 151 | lcd.printString("Set Speed",0,2); |
Nathanj94 | 11:a6a88a51dd57 | 152 | |
Nathanj94 | 13:09a9ffd8eb60 | 153 | if (pad.read_pot() > 0.9f) { |
Nathanj94 | 13:09a9ffd8eb60 | 154 | objects_speed = 5; |
Nathanj94 | 13:09a9ffd8eb60 | 155 | lcd.printString("High",0,3); |
Nathanj94 | 13:09a9ffd8eb60 | 156 | }else if (pad.read_pot() > 0.6f) { |
Nathanj94 | 13:09a9ffd8eb60 | 157 | objects_speed = 4; |
Nathanj94 | 13:09a9ffd8eb60 | 158 | lcd.printString("Normal",0,3); |
Nathanj94 | 13:09a9ffd8eb60 | 159 | }else if (pad.read_pot() > 0.3f) { |
Nathanj94 | 13:09a9ffd8eb60 | 160 | objects_speed = 3; |
Nathanj94 | 13:09a9ffd8eb60 | 161 | lcd.printString("Slow",0,3); |
Nathanj94 | 13:09a9ffd8eb60 | 162 | } else { |
Nathanj94 | 13:09a9ffd8eb60 | 163 | objects_speed = 2; |
Nathanj94 | 13:09a9ffd8eb60 | 164 | lcd.printString("Very Slow",0,3); |
Nathanj94 | 13:09a9ffd8eb60 | 165 | } |
Nathanj94 | 13:09a9ffd8eb60 | 166 | |
Nathanj94 | 11:a6a88a51dd57 | 167 | lcd.refresh(); |
Nathanj94 | 11:a6a88a51dd57 | 168 | } |
Nathanj94 | 12:d87c9ae89472 | 169 | |
Nathanj94 | 13:09a9ffd8eb60 | 170 | return objects_speed; |
Nathanj94 | 4:039294e6a8a5 | 171 | } |