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:
Tue Apr 18 14:29:05 2017 +0000
Revision:
11:a6a88a51dd57
Parent:
10:92a658c3c5a4
Child:
12:d87c9ae89472
Basket updated. Trying to make code more efficient by taking speed_select loop out of the main game loop.

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 BALL_SPEED 3
Nathanj94 7:32afc46c30f3 9 #define LIVES 5
Nathanj94 2:ada503e3486f 10
Nathanj94 4:039294e6a8a5 11 //OBJECTS//
Nathanj94 2:ada503e3486f 12 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Nathanj94 3:69296f999fdf 13 Gamepad pad;
Nathanj94 2:ada503e3486f 14 Catch_Model catchm;
Nathanj94 11:a6a88a51dd57 15 Objects objects;
Nathanj94 4:039294e6a8a5 16
Nathanj94 4:039294e6a8a5 17 //FUNCTION PROTOTYPES//
Nathanj94 2:ada503e3486f 18 void init();
Nathanj94 2:ada503e3486f 19 void render();
Nathanj94 4:039294e6a8a5 20 void welcome();
Nathanj94 11:a6a88a51dd57 21 void speed_select();
Nathanj94 4:039294e6a8a5 22
Nathanj94 4:039294e6a8a5 23 //MAIN//
Nathanj94 2:ada503e3486f 24 int main()
Nathanj94 2:ada503e3486f 25 {
Nathanj94 4:039294e6a8a5 26 int fps = 8;
Nathanj94 4:039294e6a8a5 27
Nathanj94 4:039294e6a8a5 28 while(1) {
Nathanj94 2:ada503e3486f 29
Nathanj94 4:039294e6a8a5 30 init();
Nathanj94 4:039294e6a8a5 31 welcome();
Nathanj94 11:a6a88a51dd57 32 //speed_select();
Nathanj94 4:039294e6a8a5 33 render();
Nathanj94 4:039294e6a8a5 34 wait(1.0f/fps);
Nathanj94 4:039294e6a8a5 35 int lives;
Nathanj94 4:039294e6a8a5 36
Nathanj94 4:039294e6a8a5 37 do {
Nathanj94 4:039294e6a8a5 38 catchm.input(pad);
Nathanj94 4:039294e6a8a5 39 catchm.update(lcd, pad);
Nathanj94 9:3e7fca03c7c1 40
Nathanj94 9:3e7fca03c7c1 41 catchm.check_a(lcd,pad);
Nathanj94 9:3e7fca03c7c1 42 catchm.check_b(lcd,pad);
Nathanj94 10:92a658c3c5a4 43 catchm.check_x(lcd,pad);
Nathanj94 9:3e7fca03c7c1 44
Nathanj94 4:039294e6a8a5 45 lives = catchm.get_lives();
Nathanj94 4:039294e6a8a5 46 render();
Nathanj94 4:039294e6a8a5 47 wait(1.0f/fps);
Nathanj94 11:a6a88a51dd57 48
Nathanj94 11:a6a88a51dd57 49 } while(lives > 0);
Nathanj94 2:ada503e3486f 50 }
Nathanj94 2:ada503e3486f 51 }
Nathanj94 2:ada503e3486f 52
Nathanj94 4:039294e6a8a5 53 //FUNCTIONS//
Nathanj94 2:ada503e3486f 54 void init()
Nathanj94 2:ada503e3486f 55 {
Nathanj94 4:039294e6a8a5 56 // initialise LCD and Gamepad
Nathanj94 2:ada503e3486f 57 lcd.init();
Nathanj94 2:ada503e3486f 58 pad.init();
Nathanj94 2:ada503e3486f 59
Nathanj94 4:039294e6a8a5 60 // initialise game model
Nathanj94 4:039294e6a8a5 61 catchm.init(BASKET_Y,BASKET_WIDTH,BALL_SPEED,LIVES);
Nathanj94 2:ada503e3486f 62
Nathanj94 2:ada503e3486f 63 }
Nathanj94 2:ada503e3486f 64
Nathanj94 2:ada503e3486f 65 void render()
Nathanj94 2:ada503e3486f 66 {
Nathanj94 4:039294e6a8a5 67 // re-draw screen each loop
Nathanj94 2:ada503e3486f 68 lcd.clear();
Nathanj94 2:ada503e3486f 69 catchm.draw(lcd);
Nathanj94 2:ada503e3486f 70 lcd.refresh();
Nathanj94 2:ada503e3486f 71 }
Nathanj94 4:039294e6a8a5 72
Nathanj94 11:a6a88a51dd57 73 void welcome()
Nathanj94 11:a6a88a51dd57 74 {
Nathanj94 4:039294e6a8a5 75
Nathanj94 4:039294e6a8a5 76 lcd.printString(" Press Start ",0,2);
Nathanj94 4:039294e6a8a5 77 lcd.refresh();
Nathanj94 4:039294e6a8a5 78
Nathanj94 5:136b13a9b8b5 79 while (pad.check_event(Gamepad::START_PRESSED) == false) {
Nathanj94 4:039294e6a8a5 80 pad.leds_on();
Nathanj94 4:039294e6a8a5 81 wait(0.1);
Nathanj94 4:039294e6a8a5 82 pad.leds_off();
Nathanj94 4:039294e6a8a5 83 wait(0.1);
Nathanj94 4:039294e6a8a5 84 }
Nathanj94 9:3e7fca03c7c1 85 pad.tone(500, 0.5);
Nathanj94 11:a6a88a51dd57 86 }
Nathanj94 11:a6a88a51dd57 87
Nathanj94 11:a6a88a51dd57 88 void speed_select()
Nathanj94 11:a6a88a51dd57 89 {
Nathanj94 11:a6a88a51dd57 90 lcd.clear();
Nathanj94 11:a6a88a51dd57 91 lcd.refresh();
Nathanj94 11:a6a88a51dd57 92
Nathanj94 11:a6a88a51dd57 93 while (pad.check_event(Gamepad::START_PRESSED) == false) {
Nathanj94 11:a6a88a51dd57 94
Nathanj94 11:a6a88a51dd57 95 objects.speed_select(pad);
Nathanj94 11:a6a88a51dd57 96
Nathanj94 11:a6a88a51dd57 97 char buffer[14];
Nathanj94 11:a6a88a51dd57 98 float speed = objects.get_speed();
Nathanj94 11:a6a88a51dd57 99 int print_speed = sprintf(buffer, "%10.2f", speed);
Nathanj94 11:a6a88a51dd57 100
Nathanj94 11:a6a88a51dd57 101 if (speed > 1) {
Nathanj94 11:a6a88a51dd57 102 lcd.printString("INSANE",0,0);
Nathanj94 11:a6a88a51dd57 103 } else if (speed != 0) {
Nathanj94 11:a6a88a51dd57 104 lcd.printString(buffer,0,0);
Nathanj94 11:a6a88a51dd57 105 }
Nathanj94 11:a6a88a51dd57 106 lcd.refresh();
Nathanj94 11:a6a88a51dd57 107 }
Nathanj94 4:039294e6a8a5 108 }