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

Committer:
Nathanj94
Date:
Thu Apr 27 12:40:30 2017 +0000
Revision:
15:03868ddf5bf3
Parent:
14:e20bf6569607
Child:
16:d70fd9ff28b9
All documentation added

Who changed what in which revision?

UserRevisionLine numberNew 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 14:e20bf6569607 15 Basket basket;
Nathanj94 4:039294e6a8a5 16
Nathanj94 13:09a9ffd8eb60 17 int objects_speed;
Nathanj94 13:09a9ffd8eb60 18 int lives;
Nathanj94 12:d87c9ae89472 19
Nathanj94 4:039294e6a8a5 20 //FUNCTION PROTOTYPES//
Nathanj94 2:ada503e3486f 21 void init();
Nathanj94 2:ada503e3486f 22 void render();
Nathanj94 4:039294e6a8a5 23 void welcome();
Nathanj94 15:03868ddf5bf3 24 void game_over(int score);
Nathanj94 13:09a9ffd8eb60 25 void instructions_general();
Nathanj94 13:09a9ffd8eb60 26 void instructions_buttons();
Nathanj94 13:09a9ffd8eb60 27 void set_brightness();
Nathanj94 13:09a9ffd8eb60 28 int set_speed();
Nathanj94 4:039294e6a8a5 29
Nathanj94 4:039294e6a8a5 30 //MAIN//
Nathanj94 2:ada503e3486f 31 int main()
Nathanj94 2:ada503e3486f 32 {
Nathanj94 4:039294e6a8a5 33 int fps = 8;
Nathanj94 4:039294e6a8a5 34
Nathanj94 14:e20bf6569607 35 init();
Nathanj94 14:e20bf6569607 36 welcome();
Nathanj94 14:e20bf6569607 37 instructions_general();
Nathanj94 14:e20bf6569607 38 instructions_buttons();
Nathanj94 14:e20bf6569607 39 set_brightness();
Nathanj94 14:e20bf6569607 40
Nathanj94 4:039294e6a8a5 41 while(1) {
Nathanj94 13:09a9ffd8eb60 42
Nathanj94 13:09a9ffd8eb60 43 set_speed();
Nathanj94 13:09a9ffd8eb60 44 catchm.init(BASKET_Y,BASKET_WIDTH,set_speed(),LIVES);
Nathanj94 13:09a9ffd8eb60 45
Nathanj94 4:039294e6a8a5 46 render();
Nathanj94 4:039294e6a8a5 47 wait(1.0f/fps);
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 9:3e7fca03c7c1 55
Nathanj94 4:039294e6a8a5 56 lives = catchm.get_lives();
Nathanj94 15:03868ddf5bf3 57 if (lives == 0) {
Nathanj94 15:03868ddf5bf3 58 game_over(basket.get_score());
Nathanj94 15:03868ddf5bf3 59 }
Nathanj94 15:03868ddf5bf3 60
Nathanj94 4:039294e6a8a5 61 render();
Nathanj94 4:039294e6a8a5 62 wait(1.0f/fps);
Nathanj94 11:a6a88a51dd57 63
Nathanj94 12:d87c9ae89472 64 } while (lives > 0);
Nathanj94 2:ada503e3486f 65 }
Nathanj94 2:ada503e3486f 66 }
Nathanj94 2:ada503e3486f 67
Nathanj94 4:039294e6a8a5 68 //FUNCTIONS//
Nathanj94 2:ada503e3486f 69 void init()
Nathanj94 2:ada503e3486f 70 {
Nathanj94 4:039294e6a8a5 71 // initialise LCD and Gamepad
Nathanj94 2:ada503e3486f 72 lcd.init();
Nathanj94 2:ada503e3486f 73 pad.init();
Nathanj94 2:ada503e3486f 74 }
Nathanj94 2:ada503e3486f 75
Nathanj94 2:ada503e3486f 76 void render()
Nathanj94 2:ada503e3486f 77 {
Nathanj94 4:039294e6a8a5 78 // re-draw screen each loop
Nathanj94 2:ada503e3486f 79 lcd.clear();
Nathanj94 2:ada503e3486f 80 catchm.draw(lcd);
Nathanj94 2:ada503e3486f 81 lcd.refresh();
Nathanj94 2:ada503e3486f 82 }
Nathanj94 4:039294e6a8a5 83
Nathanj94 11:a6a88a51dd57 84 void welcome()
Nathanj94 11:a6a88a51dd57 85 {
Nathanj94 12:d87c9ae89472 86 lcd.printString("FRUIT BASKET",0,2);
Nathanj94 12:d87c9ae89472 87 lcd.printString("press start",0,3);
Nathanj94 4:039294e6a8a5 88 lcd.refresh();
Nathanj94 4:039294e6a8a5 89
Nathanj94 5:136b13a9b8b5 90 while (pad.check_event(Gamepad::START_PRESSED) == false) {
Nathanj94 4:039294e6a8a5 91 pad.leds_on();
Nathanj94 4:039294e6a8a5 92 wait(0.1);
Nathanj94 4:039294e6a8a5 93 pad.leds_off();
Nathanj94 4:039294e6a8a5 94 wait(0.1);
Nathanj94 4:039294e6a8a5 95 }
Nathanj94 11:a6a88a51dd57 96 }
Nathanj94 11:a6a88a51dd57 97
Nathanj94 15:03868ddf5bf3 98 void game_over(int score)
Nathanj94 14:e20bf6569607 99 {
Nathanj94 14:e20bf6569607 100 lcd.clear();
Nathanj94 14:e20bf6569607 101 lcd.printString(" GAME OVER ",0,2);
Nathanj94 14:e20bf6569607 102
Nathanj94 14:e20bf6569607 103 char buffer[14];
Nathanj94 15:03868ddf5bf3 104 int _score = score;
Nathanj94 14:e20bf6569607 105 int print_score;
Nathanj94 14:e20bf6569607 106
Nathanj94 14:e20bf6569607 107 if ((score == 0)||(score <= 9)) {
Nathanj94 15:03868ddf5bf3 108 print_score = sprintf(buffer, " Score: 000%d ", _score);
Nathanj94 14:e20bf6569607 109 } else if (score <= 99) {
Nathanj94 15:03868ddf5bf3 110 print_score = sprintf(buffer, " Score: 00%2d ", _score);
Nathanj94 14:e20bf6569607 111 } else if (score <= 999 ) {
Nathanj94 15:03868ddf5bf3 112 print_score = sprintf(buffer, " Score: 0%3d ", _score);
Nathanj94 14:e20bf6569607 113 } else {
Nathanj94 15:03868ddf5bf3 114 print_score = sprintf(buffer, " Score: %4d ", _score);
Nathanj94 14:e20bf6569607 115 }
Nathanj94 14:e20bf6569607 116
Nathanj94 14:e20bf6569607 117 if (print_score <= 14) {
Nathanj94 14:e20bf6569607 118 lcd.printString(buffer,0,3);
Nathanj94 14:e20bf6569607 119 }
Nathanj94 14:e20bf6569607 120 lcd.refresh();
Nathanj94 14:e20bf6569607 121 wait(8.0);
Nathanj94 14:e20bf6569607 122 }
Nathanj94 14:e20bf6569607 123
Nathanj94 13:09a9ffd8eb60 124 void instructions_general()
Nathanj94 11:a6a88a51dd57 125 {
Nathanj94 11:a6a88a51dd57 126 lcd.clear();
Nathanj94 13:09a9ffd8eb60 127 lcd.printString("Move the",0,0);
Nathanj94 13:09a9ffd8eb60 128 lcd.printString("basket with",0,1);
Nathanj94 13:09a9ffd8eb60 129 lcd.printString("L/R or the",0,2);
Nathanj94 13:09a9ffd8eb60 130 lcd.printString("joystick to",0,3);
Nathanj94 13:09a9ffd8eb60 131 lcd.printString("catch the",0,4);
Nathanj94 13:09a9ffd8eb60 132 lcd.printString("fruit.",0,5);
Nathanj94 13:09a9ffd8eb60 133 lcd.refresh();
Nathanj94 13:09a9ffd8eb60 134
Nathanj94 13:09a9ffd8eb60 135 while (pad.check_event(Gamepad::START_PRESSED) == false) {
Nathanj94 13:09a9ffd8eb60 136 pad.leds_on();
Nathanj94 13:09a9ffd8eb60 137 wait(0.1);
Nathanj94 13:09a9ffd8eb60 138 pad.leds_off();
Nathanj94 13:09a9ffd8eb60 139 wait(0.1);
Nathanj94 13:09a9ffd8eb60 140 }
Nathanj94 13:09a9ffd8eb60 141 }
Nathanj94 13:09a9ffd8eb60 142
Nathanj94 13:09a9ffd8eb60 143 void instructions_buttons()
Nathanj94 13:09a9ffd8eb60 144 {
Nathanj94 13:09a9ffd8eb60 145 lcd.clear();
Nathanj94 13:09a9ffd8eb60 146 lcd.printString("Press A for a",0,0);
Nathanj94 13:09a9ffd8eb60 147 lcd.printString("second chance.",0,1);
Nathanj94 13:09a9ffd8eb60 148 lcd.printString("Press B for an",0,2);
Nathanj94 13:09a9ffd8eb60 149 lcd.printString("extra life.",0,3);
Nathanj94 13:09a9ffd8eb60 150 lcd.printString("Available when",0,4);
Nathanj94 13:09a9ffd8eb60 151 lcd.printString("you see the",0,5);
Nathanj94 13:09a9ffd8eb60 152 lcd.drawLine(69,43,73,47,1);
Nathanj94 13:09a9ffd8eb60 153 lcd.drawLine(73,47,77,39,1);
Nathanj94 11:a6a88a51dd57 154 lcd.refresh();
Nathanj94 11:a6a88a51dd57 155
Nathanj94 11:a6a88a51dd57 156 while (pad.check_event(Gamepad::START_PRESSED) == false) {
Nathanj94 13:09a9ffd8eb60 157 pad.leds_on();
Nathanj94 13:09a9ffd8eb60 158 wait(0.1);
Nathanj94 13:09a9ffd8eb60 159 pad.leds_off();
Nathanj94 13:09a9ffd8eb60 160 wait(0.1);
Nathanj94 13:09a9ffd8eb60 161 }
Nathanj94 13:09a9ffd8eb60 162 }
Nathanj94 13:09a9ffd8eb60 163
Nathanj94 13:09a9ffd8eb60 164 void set_brightness()
Nathanj94 13:09a9ffd8eb60 165 {
Nathanj94 13:09a9ffd8eb60 166 lcd.clear();
Nathanj94 13:09a9ffd8eb60 167 lcd.printString("Set Brightness",0,2);
Nathanj94 13:09a9ffd8eb60 168 lcd.refresh();
Nathanj94 13:09a9ffd8eb60 169
Nathanj94 13:09a9ffd8eb60 170 while (pad.check_event(Gamepad::START_PRESSED) == false) {
Nathanj94 15:03868ddf5bf3 171 lcd.setBrightness(pad.read_pot()*10);
Nathanj94 13:09a9ffd8eb60 172 pad.leds(pad.read_pot());
Nathanj94 13:09a9ffd8eb60 173 }
Nathanj94 13:09a9ffd8eb60 174 }
Nathanj94 13:09a9ffd8eb60 175
Nathanj94 13:09a9ffd8eb60 176 int set_speed()
Nathanj94 13:09a9ffd8eb60 177 {
Nathanj94 13:09a9ffd8eb60 178 while (pad.check_event(Gamepad::START_PRESSED) == false) {
Nathanj94 13:09a9ffd8eb60 179 lcd.clear();
Nathanj94 13:09a9ffd8eb60 180 lcd.printString("Set Speed",0,2);
Nathanj94 11:a6a88a51dd57 181
Nathanj94 13:09a9ffd8eb60 182 if (pad.read_pot() > 0.9f) {
Nathanj94 13:09a9ffd8eb60 183 objects_speed = 5;
Nathanj94 13:09a9ffd8eb60 184 lcd.printString("High",0,3);
Nathanj94 13:09a9ffd8eb60 185 }else if (pad.read_pot() > 0.6f) {
Nathanj94 13:09a9ffd8eb60 186 objects_speed = 4;
Nathanj94 13:09a9ffd8eb60 187 lcd.printString("Normal",0,3);
Nathanj94 13:09a9ffd8eb60 188 }else if (pad.read_pot() > 0.3f) {
Nathanj94 13:09a9ffd8eb60 189 objects_speed = 3;
Nathanj94 13:09a9ffd8eb60 190 lcd.printString("Slow",0,3);
Nathanj94 13:09a9ffd8eb60 191 } else {
Nathanj94 13:09a9ffd8eb60 192 objects_speed = 2;
Nathanj94 13:09a9ffd8eb60 193 lcd.printString("Very Slow",0,3);
Nathanj94 13:09a9ffd8eb60 194 }
Nathanj94 13:09a9ffd8eb60 195
Nathanj94 11:a6a88a51dd57 196 lcd.refresh();
Nathanj94 11:a6a88a51dd57 197 }
Nathanj94 12:d87c9ae89472 198
Nathanj94 13:09a9ffd8eb60 199 return objects_speed;
Nathanj94 4:039294e6a8a5 200 }