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:
Fri Apr 28 17:15:43 2017 +0000
Revision:
16:d70fd9ff28b9
Parent:
15:03868ddf5bf3
Child:
17:d0853ac51e38
Fixed issues with the 'Game Over' screen

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 13:09a9ffd8eb60 43 set_speed();
Nathanj94 13:09a9ffd8eb60 44 catchm.init(BASKET_Y,BASKET_WIDTH,set_speed(),LIVES);
Nathanj94 16:d70fd9ff28b9 45
Nathanj94 16:d70fd9ff28b9 46 render();
Nathanj94 16:d70fd9ff28b9 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 16:d70fd9ff28b9 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
Nathanj94 4:039294e6a8a5 58 render();
Nathanj94 4:039294e6a8a5 59 wait(1.0f/fps);
Nathanj94 16:d70fd9ff28b9 60
Nathanj94 16:d70fd9ff28b9 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 16:d70fd9ff28b9 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 16:d70fd9ff28b9 76 lcd.clear();
Nathanj94 16:d70fd9ff28b9 77 catchm.draw(lcd,pad);
Nathanj94 2:ada503e3486f 78 lcd.refresh();
Nathanj94 2:ada503e3486f 79 }
Nathanj94 4:039294e6a8a5 80
Nathanj94 16:d70fd9ff28b9 81 void welcome()
Nathanj94 11:a6a88a51dd57 82 {
Nathanj94 16:d70fd9ff28b9 83 lcd.printString("FRUIT BASKET",0,2);
Nathanj94 12:d87c9ae89472 84 lcd.printString("press start",0,3);
Nathanj94 4:039294e6a8a5 85 lcd.refresh();
Nathanj94 16:d70fd9ff28b9 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 16:d70fd9ff28b9 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 16:d70fd9ff28b9 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 16:d70fd9ff28b9 140
Nathanj94 13:09a9ffd8eb60 141 while (pad.check_event(Gamepad::START_PRESSED) == false) {
Nathanj94 15:03868ddf5bf3 142 lcd.setBrightness(pad.read_pot()*10);
Nathanj94 13:09a9ffd8eb60 143 pad.leds(pad.read_pot());
Nathanj94 16:d70fd9ff28b9 144 }
Nathanj94 13:09a9ffd8eb60 145 }
Nathanj94 13:09a9ffd8eb60 146
Nathanj94 13:09a9ffd8eb60 147 int set_speed()
Nathanj94 16:d70fd9ff28b9 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 16:d70fd9ff28b9 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 16:d70fd9ff28b9 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 16:d70fd9ff28b9 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 16:d70fd9ff28b9 165 }
Nathanj94 16:d70fd9ff28b9 166
Nathanj94 11:a6a88a51dd57 167 lcd.refresh();
Nathanj94 11:a6a88a51dd57 168 }
Nathanj94 16:d70fd9ff28b9 169
Nathanj94 13:09a9ffd8eb60 170 return objects_speed;
Nathanj94 4:039294e6a8a5 171 }