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 May 04 10:30:48 2017 +0000
Revision:
17:d0853ac51e38
Parent:
16:d70fd9ff28b9
Final revision - made minor adjustments to UI in main file as well as adding in-line commenting

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 16:d70fd9ff28b9 12
Nathanj94 16:d70fd9ff28b9 13 Gamepad pad;
Nathanj94 2:ada503e3486f 14 Catch_Model catchm;
Nathanj94 11:a6a88a51dd57 15 Objects objects;
Nathanj94 14:e20bf6569607 16 Basket basket;
Nathanj94 4:039294e6a8a5 17
Nathanj94 13:09a9ffd8eb60 18 int objects_speed;
Nathanj94 13:09a9ffd8eb60 19 int lives;
Nathanj94 12:d87c9ae89472 20
Nathanj94 4:039294e6a8a5 21 //FUNCTION PROTOTYPES//
Nathanj94 2:ada503e3486f 22 void init();
Nathanj94 2:ada503e3486f 23 void render();
Nathanj94 4:039294e6a8a5 24 void welcome();
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 16:d70fd9ff28b9 33 int fps = 8;
Nathanj94 16:d70fd9ff28b9 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 16:d70fd9ff28b9 40
Nathanj94 4:039294e6a8a5 41 while(1) {
Nathanj94 16:d70fd9ff28b9 42
Nathanj94 17:d0853ac51e38 43 set_speed();//don't init game until speed is set manually
Nathanj94 13:09a9ffd8eb60 44 catchm.init(BASKET_Y,BASKET_WIDTH,set_speed(),LIVES);
Nathanj94 17:d0853ac51e38 45 //printf("game initialised\n");
Nathanj94 16:d70fd9ff28b9 46
Nathanj94 16:d70fd9ff28b9 47 render();
Nathanj94 16:d70fd9ff28b9 48 wait(1.0f/fps);
Nathanj94 4:039294e6a8a5 49
Nathanj94 4:039294e6a8a5 50 do {
Nathanj94 4:039294e6a8a5 51 catchm.input(pad);
Nathanj94 4:039294e6a8a5 52 catchm.update(lcd, pad);
Nathanj94 16:d70fd9ff28b9 53
Nathanj94 17:d0853ac51e38 54 catchm.check_a(lcd,pad); //game runs better if these functions are
Nathanj94 17:d0853ac51e38 55 catchm.check_b(lcd,pad); //seperate from Catch_Model::update()
Nathanj94 9:3e7fca03c7c1 56
Nathanj94 4:039294e6a8a5 57 lives = catchm.get_lives();
Nathanj94 15:03868ddf5bf3 58
Nathanj94 4:039294e6a8a5 59 render();
Nathanj94 17:d0853ac51e38 60 wait(1.0f/fps); // frame rate
Nathanj94 16:d70fd9ff28b9 61
Nathanj94 16:d70fd9ff28b9 62 } while (lives > 0);
Nathanj94 2:ada503e3486f 63 }
Nathanj94 2:ada503e3486f 64 }
Nathanj94 2:ada503e3486f 65
Nathanj94 4:039294e6a8a5 66 //FUNCTIONS//
Nathanj94 2:ada503e3486f 67 void init()
Nathanj94 2:ada503e3486f 68 {
Nathanj94 16:d70fd9ff28b9 69 // initialise LCD and Gamepad
Nathanj94 2:ada503e3486f 70 lcd.init();
Nathanj94 2:ada503e3486f 71 pad.init();
Nathanj94 2:ada503e3486f 72 }
Nathanj94 2:ada503e3486f 73
Nathanj94 2:ada503e3486f 74 void render()
Nathanj94 2:ada503e3486f 75 {
Nathanj94 4:039294e6a8a5 76 // re-draw screen each loop
Nathanj94 16:d70fd9ff28b9 77 lcd.clear();
Nathanj94 16:d70fd9ff28b9 78 catchm.draw(lcd,pad);
Nathanj94 2:ada503e3486f 79 lcd.refresh();
Nathanj94 2:ada503e3486f 80 }
Nathanj94 4:039294e6a8a5 81
Nathanj94 16:d70fd9ff28b9 82 void welcome()
Nathanj94 11:a6a88a51dd57 83 {
Nathanj94 16:d70fd9ff28b9 84 lcd.printString("FRUIT BASKET",0,2);
Nathanj94 12:d87c9ae89472 85 lcd.printString("press start",0,3);
Nathanj94 4:039294e6a8a5 86 lcd.refresh();
Nathanj94 16:d70fd9ff28b9 87
Nathanj94 5:136b13a9b8b5 88 while (pad.check_event(Gamepad::START_PRESSED) == false) {
Nathanj94 4:039294e6a8a5 89 pad.leds_on();
Nathanj94 4:039294e6a8a5 90 wait(0.1);
Nathanj94 4:039294e6a8a5 91 pad.leds_off();
Nathanj94 4:039294e6a8a5 92 wait(0.1);
Nathanj94 4:039294e6a8a5 93 }
Nathanj94 11:a6a88a51dd57 94 }
Nathanj94 11:a6a88a51dd57 95
Nathanj94 13:09a9ffd8eb60 96 void instructions_general()
Nathanj94 11:a6a88a51dd57 97 {
Nathanj94 11:a6a88a51dd57 98 lcd.clear();
Nathanj94 17:d0853ac51e38 99 lcd.printString("Move the",0,0); // instructions for moving basket
Nathanj94 13:09a9ffd8eb60 100 lcd.printString("basket with",0,1);
Nathanj94 13:09a9ffd8eb60 101 lcd.printString("L/R or the",0,2);
Nathanj94 13:09a9ffd8eb60 102 lcd.printString("joystick to",0,3);
Nathanj94 13:09a9ffd8eb60 103 lcd.printString("catch the",0,4);
Nathanj94 13:09a9ffd8eb60 104 lcd.printString("fruit.",0,5);
Nathanj94 13:09a9ffd8eb60 105 lcd.refresh();
Nathanj94 16:d70fd9ff28b9 106
Nathanj94 13:09a9ffd8eb60 107 while (pad.check_event(Gamepad::START_PRESSED) == false) {
Nathanj94 13:09a9ffd8eb60 108 pad.leds_on();
Nathanj94 13:09a9ffd8eb60 109 wait(0.1);
Nathanj94 13:09a9ffd8eb60 110 pad.leds_off();
Nathanj94 13:09a9ffd8eb60 111 wait(0.1);
Nathanj94 13:09a9ffd8eb60 112 }
Nathanj94 13:09a9ffd8eb60 113 }
Nathanj94 13:09a9ffd8eb60 114
Nathanj94 13:09a9ffd8eb60 115 void instructions_buttons()
Nathanj94 13:09a9ffd8eb60 116 {
Nathanj94 13:09a9ffd8eb60 117 lcd.clear();
Nathanj94 17:d0853ac51e38 118 lcd.printString("Press A for a",0,0); // A button cancels an object mid-drop
Nathanj94 17:d0853ac51e38 119 lcd.printString("second chance.",0,1); // and immediately drops another randomly
Nathanj94 17:d0853ac51e38 120 lcd.printString("Press B for an",0,2); // B button adds an extra life
Nathanj94 13:09a9ffd8eb60 121 lcd.printString("extra life.",0,3);
Nathanj94 13:09a9ffd8eb60 122 lcd.printString("Available when",0,4);
Nathanj94 13:09a9ffd8eb60 123 lcd.printString("you see the",0,5);
Nathanj94 17:d0853ac51e38 124 lcd.drawLine(69,43,73,47,1); //draw
Nathanj94 17:d0853ac51e38 125 lcd.drawLine(73,47,77,39,1); //tick
Nathanj94 11:a6a88a51dd57 126 lcd.refresh();
Nathanj94 16:d70fd9ff28b9 127
Nathanj94 11:a6a88a51dd57 128 while (pad.check_event(Gamepad::START_PRESSED) == false) {
Nathanj94 13:09a9ffd8eb60 129 pad.leds_on();
Nathanj94 13:09a9ffd8eb60 130 wait(0.1);
Nathanj94 13:09a9ffd8eb60 131 pad.leds_off();
Nathanj94 13:09a9ffd8eb60 132 wait(0.1);
Nathanj94 13:09a9ffd8eb60 133 }
Nathanj94 13:09a9ffd8eb60 134 }
Nathanj94 13:09a9ffd8eb60 135
Nathanj94 13:09a9ffd8eb60 136 void set_brightness()
Nathanj94 13:09a9ffd8eb60 137 {
Nathanj94 13:09a9ffd8eb60 138 lcd.clear();
Nathanj94 13:09a9ffd8eb60 139 lcd.printString("Set Brightness",0,2);
Nathanj94 13:09a9ffd8eb60 140 lcd.refresh();
Nathanj94 16:d70fd9ff28b9 141
Nathanj94 13:09a9ffd8eb60 142 while (pad.check_event(Gamepad::START_PRESSED) == false) {
Nathanj94 17:d0853ac51e38 143 lcd.setBrightness(pad.read_pot());
Nathanj94 13:09a9ffd8eb60 144 pad.leds(pad.read_pot());
Nathanj94 16:d70fd9ff28b9 145 }
Nathanj94 13:09a9ffd8eb60 146 }
Nathanj94 13:09a9ffd8eb60 147
Nathanj94 13:09a9ffd8eb60 148 int set_speed()
Nathanj94 16:d70fd9ff28b9 149 {
Nathanj94 13:09a9ffd8eb60 150 while (pad.check_event(Gamepad::START_PRESSED) == false) {
Nathanj94 13:09a9ffd8eb60 151 lcd.clear();
Nathanj94 13:09a9ffd8eb60 152 lcd.printString("Set Speed",0,2);
Nathanj94 16:d70fd9ff28b9 153
Nathanj94 17:d0853ac51e38 154 if (pad.read_pot() > 0.75f) { // value of pot selects speed objects will fall at
Nathanj94 17:d0853ac51e38 155 objects_speed = 5; // int value translates to pixels per game loop
Nathanj94 17:d0853ac51e38 156 lcd.printString("Fast",0,3);
Nathanj94 17:d0853ac51e38 157 } else if (pad.read_pot() > 0.5f) {
Nathanj94 13:09a9ffd8eb60 158 objects_speed = 4;
Nathanj94 13:09a9ffd8eb60 159 lcd.printString("Normal",0,3);
Nathanj94 17:d0853ac51e38 160 } else if (pad.read_pot() > 0.25f) {
Nathanj94 13:09a9ffd8eb60 161 objects_speed = 3;
Nathanj94 13:09a9ffd8eb60 162 lcd.printString("Slow",0,3);
Nathanj94 13:09a9ffd8eb60 163 } else {
Nathanj94 13:09a9ffd8eb60 164 objects_speed = 2;
Nathanj94 13:09a9ffd8eb60 165 lcd.printString("Very Slow",0,3);
Nathanj94 16:d70fd9ff28b9 166 }
Nathanj94 16:d70fd9ff28b9 167
Nathanj94 11:a6a88a51dd57 168 lcd.refresh();
Nathanj94 11:a6a88a51dd57 169 }
Nathanj94 16:d70fd9ff28b9 170
Nathanj94 17:d0853ac51e38 171 return objects_speed; // game initialised with this value
Nathanj94 4:039294e6a8a5 172 }