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:
Sat Apr 22 12:33:18 2017 +0000
Revision:
14:e20bf6569607
Parent:
13:09a9ffd8eb60
Child:
15:03868ddf5bf3
Redundancy removed and files organised ready for notes to be written.; "Game Over" screen added after game end.

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 14:e20bf6569607 24 void game_over();
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 4:039294e6a8a5 57 render();
Nathanj94 4:039294e6a8a5 58 wait(1.0f/fps);
Nathanj94 11:a6a88a51dd57 59
Nathanj94 12:d87c9ae89472 60 } while (lives > 0);
Nathanj94 14:e20bf6569607 61
Nathanj94 14:e20bf6569607 62 game_over();
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 4:039294e6a8a5 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 2:ada503e3486f 77 lcd.clear();
Nathanj94 2:ada503e3486f 78 catchm.draw(lcd);
Nathanj94 2:ada503e3486f 79 lcd.refresh();
Nathanj94 2:ada503e3486f 80 }
Nathanj94 4:039294e6a8a5 81
Nathanj94 11:a6a88a51dd57 82 void welcome()
Nathanj94 11:a6a88a51dd57 83 {
Nathanj94 12:d87c9ae89472 84 lcd.printString("FRUIT BASKET",0,2);
Nathanj94 12:d87c9ae89472 85 lcd.printString("press start",0,3);
Nathanj94 4:039294e6a8a5 86 lcd.refresh();
Nathanj94 4:039294e6a8a5 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 14:e20bf6569607 96 void game_over()
Nathanj94 14:e20bf6569607 97 {
Nathanj94 14:e20bf6569607 98 lcd.clear();
Nathanj94 14:e20bf6569607 99 lcd.printString(" GAME OVER ",0,2);
Nathanj94 14:e20bf6569607 100
Nathanj94 14:e20bf6569607 101 char buffer[14];
Nathanj94 14:e20bf6569607 102 int score = basket.get_score();
Nathanj94 14:e20bf6569607 103 int print_score;
Nathanj94 14:e20bf6569607 104
Nathanj94 14:e20bf6569607 105 if ((score == 0)||(score <= 9)) {
Nathanj94 14:e20bf6569607 106 print_score = sprintf(buffer, " Score: 000%d ", score);
Nathanj94 14:e20bf6569607 107 } else if (score <= 99) {
Nathanj94 14:e20bf6569607 108 print_score = sprintf(buffer, " Score: 00%2d ", score);
Nathanj94 14:e20bf6569607 109 } else if (score <= 999 ) {
Nathanj94 14:e20bf6569607 110 print_score = sprintf(buffer, " Score: 0%3d ", score);
Nathanj94 14:e20bf6569607 111 } else {
Nathanj94 14:e20bf6569607 112 print_score = sprintf(buffer, " Score: %4d ", score);
Nathanj94 14:e20bf6569607 113 }
Nathanj94 14:e20bf6569607 114
Nathanj94 14:e20bf6569607 115 if (print_score <= 14) {
Nathanj94 14:e20bf6569607 116 lcd.printString(buffer,0,3);
Nathanj94 14:e20bf6569607 117 }
Nathanj94 14:e20bf6569607 118 lcd.refresh();
Nathanj94 14:e20bf6569607 119 wait(8.0);
Nathanj94 14:e20bf6569607 120 }
Nathanj94 14:e20bf6569607 121
Nathanj94 13:09a9ffd8eb60 122 void instructions_general()
Nathanj94 11:a6a88a51dd57 123 {
Nathanj94 11:a6a88a51dd57 124 lcd.clear();
Nathanj94 13:09a9ffd8eb60 125 lcd.printString("Move the",0,0);
Nathanj94 13:09a9ffd8eb60 126 lcd.printString("basket with",0,1);
Nathanj94 13:09a9ffd8eb60 127 lcd.printString("L/R or the",0,2);
Nathanj94 13:09a9ffd8eb60 128 lcd.printString("joystick to",0,3);
Nathanj94 13:09a9ffd8eb60 129 lcd.printString("catch the",0,4);
Nathanj94 13:09a9ffd8eb60 130 lcd.printString("fruit.",0,5);
Nathanj94 13:09a9ffd8eb60 131 lcd.refresh();
Nathanj94 13:09a9ffd8eb60 132
Nathanj94 13:09a9ffd8eb60 133 while (pad.check_event(Gamepad::START_PRESSED) == false) {
Nathanj94 13:09a9ffd8eb60 134 pad.leds_on();
Nathanj94 13:09a9ffd8eb60 135 wait(0.1);
Nathanj94 13:09a9ffd8eb60 136 pad.leds_off();
Nathanj94 13:09a9ffd8eb60 137 wait(0.1);
Nathanj94 13:09a9ffd8eb60 138 }
Nathanj94 13:09a9ffd8eb60 139 }
Nathanj94 13:09a9ffd8eb60 140
Nathanj94 13:09a9ffd8eb60 141 void instructions_buttons()
Nathanj94 13:09a9ffd8eb60 142 {
Nathanj94 13:09a9ffd8eb60 143 lcd.clear();
Nathanj94 13:09a9ffd8eb60 144 lcd.printString("Press A for a",0,0);
Nathanj94 13:09a9ffd8eb60 145 lcd.printString("second chance.",0,1);
Nathanj94 13:09a9ffd8eb60 146 lcd.printString("Press B for an",0,2);
Nathanj94 13:09a9ffd8eb60 147 lcd.printString("extra life.",0,3);
Nathanj94 13:09a9ffd8eb60 148 lcd.printString("Available when",0,4);
Nathanj94 13:09a9ffd8eb60 149 lcd.printString("you see the",0,5);
Nathanj94 13:09a9ffd8eb60 150 lcd.drawLine(69,43,73,47,1);
Nathanj94 13:09a9ffd8eb60 151 lcd.drawLine(73,47,77,39,1);
Nathanj94 11:a6a88a51dd57 152 lcd.refresh();
Nathanj94 11:a6a88a51dd57 153
Nathanj94 11:a6a88a51dd57 154 while (pad.check_event(Gamepad::START_PRESSED) == false) {
Nathanj94 13:09a9ffd8eb60 155 pad.leds_on();
Nathanj94 13:09a9ffd8eb60 156 wait(0.1);
Nathanj94 13:09a9ffd8eb60 157 pad.leds_off();
Nathanj94 13:09a9ffd8eb60 158 wait(0.1);
Nathanj94 13:09a9ffd8eb60 159 }
Nathanj94 13:09a9ffd8eb60 160 }
Nathanj94 13:09a9ffd8eb60 161
Nathanj94 13:09a9ffd8eb60 162 void set_brightness()
Nathanj94 13:09a9ffd8eb60 163 {
Nathanj94 13:09a9ffd8eb60 164 lcd.clear();
Nathanj94 13:09a9ffd8eb60 165 lcd.printString("Set Brightness",0,2);
Nathanj94 13:09a9ffd8eb60 166 lcd.refresh();
Nathanj94 13:09a9ffd8eb60 167
Nathanj94 13:09a9ffd8eb60 168 while (pad.check_event(Gamepad::START_PRESSED) == false) {
Nathanj94 13:09a9ffd8eb60 169 lcd.setBrightness(pad.read_pot());
Nathanj94 13:09a9ffd8eb60 170 pad.leds(pad.read_pot());
Nathanj94 13:09a9ffd8eb60 171 }
Nathanj94 13:09a9ffd8eb60 172 }
Nathanj94 13:09a9ffd8eb60 173
Nathanj94 13:09a9ffd8eb60 174 int set_speed()
Nathanj94 13:09a9ffd8eb60 175 {
Nathanj94 13:09a9ffd8eb60 176 while (pad.check_event(Gamepad::START_PRESSED) == false) {
Nathanj94 13:09a9ffd8eb60 177 lcd.clear();
Nathanj94 13:09a9ffd8eb60 178 lcd.printString("Set Speed",0,2);
Nathanj94 11:a6a88a51dd57 179
Nathanj94 13:09a9ffd8eb60 180 if (pad.read_pot() > 0.9f) {
Nathanj94 13:09a9ffd8eb60 181 objects_speed = 5;
Nathanj94 13:09a9ffd8eb60 182 lcd.printString("High",0,3);
Nathanj94 13:09a9ffd8eb60 183 }else if (pad.read_pot() > 0.6f) {
Nathanj94 13:09a9ffd8eb60 184 objects_speed = 4;
Nathanj94 13:09a9ffd8eb60 185 lcd.printString("Normal",0,3);
Nathanj94 13:09a9ffd8eb60 186 }else if (pad.read_pot() > 0.3f) {
Nathanj94 13:09a9ffd8eb60 187 objects_speed = 3;
Nathanj94 13:09a9ffd8eb60 188 lcd.printString("Slow",0,3);
Nathanj94 13:09a9ffd8eb60 189 } else {
Nathanj94 13:09a9ffd8eb60 190 objects_speed = 2;
Nathanj94 13:09a9ffd8eb60 191 lcd.printString("Very Slow",0,3);
Nathanj94 13:09a9ffd8eb60 192 }
Nathanj94 13:09a9ffd8eb60 193
Nathanj94 11:a6a88a51dd57 194 lcd.refresh();
Nathanj94 11:a6a88a51dd57 195 }
Nathanj94 12:d87c9ae89472 196
Nathanj94 13:09a9ffd8eb60 197 return objects_speed;
Nathanj94 4:039294e6a8a5 198 }