FINAL VERSION

Dependencies:   mbed

Committer:
jamesheavey
Date:
Tue Apr 23 17:09:47 2019 +0000
Revision:
15:b867a6620f96
Parent:
14:38560375d420
Child:
16:1761dfe801af
tilt works, need to add a motion control func and laser class

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jamesheavey 0:7d4d2023ed9c 1 ///////// pre-processor directives ////////
jamesheavey 0:7d4d2023ed9c 2 #include "mbed.h"
jamesheavey 0:7d4d2023ed9c 3 #include "Gamepad.h"
jamesheavey 0:7d4d2023ed9c 4 #include "N5110.h"
jamesheavey 0:7d4d2023ed9c 5 #include "PongEngine.h"
jamesheavey 3:5719d0fe94ed 6 #include "Bitmap.h"
jamesheavey 4:6bd488b5b31a 7 #include<iostream>
jamesheavey 8:1ab6d90c4d60 8 #include "Sprites.h"
jamesheavey 14:38560375d420 9 //#include "MotionControl.h"
jamesheavey 0:7d4d2023ed9c 10
jamesheavey 0:7d4d2023ed9c 11 #ifdef WITH_TESTING
jamesheavey 0:7d4d2023ed9c 12 # include "tests.h"
jamesheavey 0:7d4d2023ed9c 13 #endif
jamesheavey 0:7d4d2023ed9c 14
jamesheavey 9:f6f0f39538c7 15 #define PADDLE_WIDTH 15
jamesheavey 0:7d4d2023ed9c 16 #define PADDLE_HEIGHT 2
jamesheavey 0:7d4d2023ed9c 17 #define BALL_SIZE 2
jamesheavey 0:7d4d2023ed9c 18 #define BALL_SPEED 3
jamesheavey 0:7d4d2023ed9c 19
jamesheavey 0:7d4d2023ed9c 20 /////////////// structs /////////////////
jamesheavey 0:7d4d2023ed9c 21 struct UserInput {
jamesheavey 0:7d4d2023ed9c 22 Direction d;
jamesheavey 0:7d4d2023ed9c 23 float mag;
jamesheavey 0:7d4d2023ed9c 24 };
jamesheavey 0:7d4d2023ed9c 25 /////////////// objects ///////////////
jamesheavey 0:7d4d2023ed9c 26 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
jamesheavey 0:7d4d2023ed9c 27 Gamepad pad;
jamesheavey 0:7d4d2023ed9c 28 PongEngine pong;
jamesheavey 0:7d4d2023ed9c 29
jamesheavey 0:7d4d2023ed9c 30 ///////////// prototypes ///////////////
jamesheavey 0:7d4d2023ed9c 31 void init();
jamesheavey 0:7d4d2023ed9c 32 void update_game(UserInput input);
jamesheavey 0:7d4d2023ed9c 33 void render();
jamesheavey 0:7d4d2023ed9c 34 void welcome();
jamesheavey 10:9f445faa892c 35 void end_screen();
jamesheavey 10:9f445faa892c 36 void main_menu();
jamesheavey 0:7d4d2023ed9c 37
jamesheavey 0:7d4d2023ed9c 38 ///////////// functions ////////////////
jamesheavey 0:7d4d2023ed9c 39 int main()
jamesheavey 0:7d4d2023ed9c 40 {
jamesheavey 0:7d4d2023ed9c 41 #ifdef WITH_TESTING
jamesheavey 0:7d4d2023ed9c 42 int number_of_failures = run_all_tests();
jamesheavey 0:7d4d2023ed9c 43
jamesheavey 0:7d4d2023ed9c 44 if(number_of_failures > 0) return number_of_failures;
jamesheavey 0:7d4d2023ed9c 45 #endif
jamesheavey 10:9f445faa892c 46 main_menu();
jamesheavey 3:5719d0fe94ed 47
jamesheavey 0:7d4d2023ed9c 48 }
jamesheavey 0:7d4d2023ed9c 49
jamesheavey 0:7d4d2023ed9c 50 // initialies all classes and libraries
jamesheavey 0:7d4d2023ed9c 51 void init()
jamesheavey 0:7d4d2023ed9c 52 {
jamesheavey 0:7d4d2023ed9c 53 // need to initialise LCD and Gamepad
jamesheavey 0:7d4d2023ed9c 54 lcd.init();
jamesheavey 0:7d4d2023ed9c 55 pad.init();
jamesheavey 0:7d4d2023ed9c 56
jamesheavey 0:7d4d2023ed9c 57 // initialise the game with correct ball and paddle sizes
jamesheavey 0:7d4d2023ed9c 58 pong.init(PADDLE_WIDTH,PADDLE_HEIGHT,BALL_SIZE,BALL_SPEED);
jamesheavey 0:7d4d2023ed9c 59
jamesheavey 0:7d4d2023ed9c 60 }
jamesheavey 0:7d4d2023ed9c 61
jamesheavey 0:7d4d2023ed9c 62 // this function draws each frame on the LCD
jamesheavey 0:7d4d2023ed9c 63 void render()
jamesheavey 0:7d4d2023ed9c 64 {
jamesheavey 0:7d4d2023ed9c 65 // clear screen, re-draw and refresh
jamesheavey 0:7d4d2023ed9c 66 lcd.clear();
jamesheavey 0:7d4d2023ed9c 67 pong.draw(lcd);
jamesheavey 0:7d4d2023ed9c 68 lcd.refresh();
jamesheavey 0:7d4d2023ed9c 69 }
jamesheavey 0:7d4d2023ed9c 70
jamesheavey 0:7d4d2023ed9c 71 // simple splash screen displayed on start-up
jamesheavey 0:7d4d2023ed9c 72 void welcome() {
jamesheavey 3:5719d0fe94ed 73 Bitmap breakwhite(breakwhite_data, 48, 84);
jamesheavey 3:5719d0fe94ed 74 Bitmap breakblack(breakblack_data, 48, 84);
jamesheavey 0:7d4d2023ed9c 75
jamesheavey 0:7d4d2023ed9c 76 // wait flashing LEDs until start button is pressed
jamesheavey 14:38560375d420 77 //bool joy = true;
jamesheavey 14:38560375d420 78
jamesheavey 15:b867a6620f96 79 while (pad.check_event(Gamepad::START_PRESSED) == false) { //maybe add a jingle
jamesheavey 3:5719d0fe94ed 80
jamesheavey 3:5719d0fe94ed 81 breakwhite.render(lcd, 0, 0);
jamesheavey 14:38560375d420 82 lcd.printString(" JOYSTICK ",10,5);
jamesheavey 3:5719d0fe94ed 83 lcd.refresh();
jamesheavey 0:7d4d2023ed9c 84 pad.leds_on();
jamesheavey 3:5719d0fe94ed 85 wait(0.5);
jamesheavey 14:38560375d420 86
jamesheavey 3:5719d0fe94ed 87 lcd.clear();
jamesheavey 14:38560375d420 88
jamesheavey 3:5719d0fe94ed 89 breakblack.render(lcd, 0, 0);
jamesheavey 14:38560375d420 90 lcd.printString(" JOYSTICK ",10,5);
jamesheavey 3:5719d0fe94ed 91 lcd.refresh();
jamesheavey 0:7d4d2023ed9c 92 pad.leds_off();
jamesheavey 3:5719d0fe94ed 93 wait(0.5);
jamesheavey 14:38560375d420 94
jamesheavey 14:38560375d420 95 //return joy; make a variable in paddle called joy
jamesheavey 14:38560375d420 96
jamesheavey 14:38560375d420 97
jamesheavey 0:7d4d2023ed9c 98 }
jamesheavey 0:7d4d2023ed9c 99
jamesheavey 10:9f445faa892c 100 }
jamesheavey 10:9f445faa892c 101
jamesheavey 10:9f445faa892c 102 void end_screen() {
jamesheavey 15:b867a6620f96 103 lcd.clear();
jamesheavey 15:b867a6620f96 104 lcd.printString(" RESTART? ",2,2);
jamesheavey 15:b867a6620f96 105 lcd.printString(" PRESS START ",1,4);
jamesheavey 15:b867a6620f96 106 lcd.refresh();
jamesheavey 15:b867a6620f96 107 pad.tone(750.0,0.3);
jamesheavey 15:b867a6620f96 108 wait(0.4);
jamesheavey 15:b867a6620f96 109
jamesheavey 15:b867a6620f96 110 pad.tone(300.0,0.3);
jamesheavey 15:b867a6620f96 111 wait(0.4);
jamesheavey 10:9f445faa892c 112
jamesheavey 10:9f445faa892c 113 while (pad.check_event(Gamepad::START_PRESSED) == false) {
jamesheavey 10:9f445faa892c 114 lcd.clear();
jamesheavey 10:9f445faa892c 115 lcd.printString(" RESTART? ",2,2);
jamesheavey 10:9f445faa892c 116 lcd.printString(" PRESS START ",1,4);
jamesheavey 10:9f445faa892c 117 lcd.refresh();
jamesheavey 10:9f445faa892c 118 // if (pad.check_event(Gamepad::START_PRESSED == true) {
jamesheavey 10:9f445faa892c 119 // return true;
jamesheavey 10:9f445faa892c 120 // }
jamesheavey 10:9f445faa892c 121 // else if (pad.check_event(Gamepad::BACK_PRESSED == true) {
jamesheavey 10:9f445faa892c 122 // return false;
jamesheavey 10:9f445faa892c 123 // }
jamesheavey 10:9f445faa892c 124
jamesheavey 10:9f445faa892c 125 }
jamesheavey 10:9f445faa892c 126 main_menu();
jamesheavey 10:9f445faa892c 127 }
jamesheavey 10:9f445faa892c 128
jamesheavey 13:418a71b24b37 129 void victory_screen() {
jamesheavey 15:b867a6620f96 130 lcd.clear();
jamesheavey 15:b867a6620f96 131 lcd.printString(" VICTORY! ",2,2);
jamesheavey 15:b867a6620f96 132 lcd.refresh();
jamesheavey 15:b867a6620f96 133 pad.tone(1000.0,0.4);
jamesheavey 15:b867a6620f96 134 wait(0.5);
jamesheavey 15:b867a6620f96 135
jamesheavey 15:b867a6620f96 136 pad.tone(1000.0,0.4);
jamesheavey 15:b867a6620f96 137 wait(0.5);
jamesheavey 15:b867a6620f96 138
jamesheavey 15:b867a6620f96 139 pad.tone(4000.0,0.6);
jamesheavey 15:b867a6620f96 140 wait(0.6);
jamesheavey 13:418a71b24b37 141 while (pad.check_event(Gamepad::START_PRESSED) == false) {
jamesheavey 13:418a71b24b37 142 lcd.clear();
jamesheavey 13:418a71b24b37 143 lcd.printString(" VICTORY! ",2,2);
jamesheavey 13:418a71b24b37 144 lcd.refresh();
jamesheavey 15:b867a6620f96 145
jamesheavey 15:b867a6620f96 146
jamesheavey 15:b867a6620f96 147
jamesheavey 13:418a71b24b37 148 // if (pad.check_event(Gamepad::START_PRESSED == true) {
jamesheavey 13:418a71b24b37 149 // return true;
jamesheavey 13:418a71b24b37 150 // }
jamesheavey 13:418a71b24b37 151 // else if (pad.check_event(Gamepad::BACK_PRESSED == true) {
jamesheavey 13:418a71b24b37 152 // return false;
jamesheavey 13:418a71b24b37 153 // }
jamesheavey 13:418a71b24b37 154
jamesheavey 13:418a71b24b37 155 }
jamesheavey 13:418a71b24b37 156 main_menu();
jamesheavey 13:418a71b24b37 157 }
jamesheavey 13:418a71b24b37 158
jamesheavey 10:9f445faa892c 159 void main_menu() {
jamesheavey 10:9f445faa892c 160 int fps = 8; // frames per second
jamesheavey 10:9f445faa892c 161 bool pause = false;
jamesheavey 10:9f445faa892c 162 init(); // initialise and then display welcome screen...
jamesheavey 10:9f445faa892c 163 welcome(); // waiting for the user to start
jamesheavey 10:9f445faa892c 164
jamesheavey 10:9f445faa892c 165 Bitmap three(three_data, 48, 84);
jamesheavey 10:9f445faa892c 166 Bitmap two(two_data, 48, 84);
jamesheavey 10:9f445faa892c 167 Bitmap one(one_data, 48, 84);
jamesheavey 10:9f445faa892c 168
jamesheavey 10:9f445faa892c 169 lcd.clear();
jamesheavey 10:9f445faa892c 170 three.render(lcd, 0, 0);
jamesheavey 10:9f445faa892c 171 lcd.refresh();
jamesheavey 15:b867a6620f96 172 pad.tone(500.0,0.5);
jamesheavey 10:9f445faa892c 173 wait(1);
jamesheavey 10:9f445faa892c 174
jamesheavey 10:9f445faa892c 175 lcd.clear();
jamesheavey 10:9f445faa892c 176 two.render(lcd, 0, 0);
jamesheavey 10:9f445faa892c 177 lcd.refresh();
jamesheavey 15:b867a6620f96 178 pad.tone(500.0,0.5);
jamesheavey 10:9f445faa892c 179 wait(1);
jamesheavey 10:9f445faa892c 180
jamesheavey 10:9f445faa892c 181 lcd.clear();
jamesheavey 10:9f445faa892c 182 one.render(lcd, 0, 0);
jamesheavey 10:9f445faa892c 183 lcd.refresh();
jamesheavey 15:b867a6620f96 184 pad.tone(1000.0,1);
jamesheavey 10:9f445faa892c 185 wait(1);
jamesheavey 10:9f445faa892c 186
jamesheavey 10:9f445faa892c 187 render(); // first draw the initial frame
jamesheavey 10:9f445faa892c 188 wait(1.0f/fps); // and wait for one frame period
jamesheavey 10:9f445faa892c 189
jamesheavey 10:9f445faa892c 190
jamesheavey 10:9f445faa892c 191 // game loop - read input, update the game state and render the display
jamesheavey 10:9f445faa892c 192 while (1) {
jamesheavey 10:9f445faa892c 193 pong.read_input(pad);
jamesheavey 10:9f445faa892c 194 pong.update(pad);
jamesheavey 10:9f445faa892c 195 render();
jamesheavey 12:f714a9e8c55b 196 if (pong.check_goal(pad) == true) {
jamesheavey 12:f714a9e8c55b 197 lcd.setBrightness(0);
jamesheavey 12:f714a9e8c55b 198 wait(0.1);
jamesheavey 12:f714a9e8c55b 199 lcd.setBrightness(1);
jamesheavey 12:f714a9e8c55b 200 wait(0.1);
jamesheavey 12:f714a9e8c55b 201 lcd.setBrightness(0);
jamesheavey 12:f714a9e8c55b 202 wait(0.1);
jamesheavey 12:f714a9e8c55b 203 lcd.setBrightness(1);
jamesheavey 12:f714a9e8c55b 204 wait(0.1);
jamesheavey 12:f714a9e8c55b 205 lcd.setBrightness(0);
jamesheavey 12:f714a9e8c55b 206 wait(0.1);
jamesheavey 12:f714a9e8c55b 207 lcd.setBrightness(1);
jamesheavey 12:f714a9e8c55b 208 wait(0.1);
jamesheavey 12:f714a9e8c55b 209 lcd.setBrightness(0);
jamesheavey 12:f714a9e8c55b 210 wait(0.1);
jamesheavey 12:f714a9e8c55b 211 lcd.setBrightness(1);
jamesheavey 12:f714a9e8c55b 212 }
jamesheavey 10:9f445faa892c 213 if (pad.check_event(Gamepad::BACK_PRESSED) == true) {
jamesheavey 10:9f445faa892c 214 pause = !pause;
jamesheavey 10:9f445faa892c 215 }
jamesheavey 10:9f445faa892c 216 while (pause == true) {
jamesheavey 10:9f445faa892c 217 lcd.clear();
jamesheavey 10:9f445faa892c 218 lcd.printString(" PAUSED ",0,2);
jamesheavey 10:9f445faa892c 219 lcd.refresh();
jamesheavey 10:9f445faa892c 220 if (pad.check_event(Gamepad::BACK_PRESSED) == true) {
jamesheavey 10:9f445faa892c 221 pause = !pause;
jamesheavey 10:9f445faa892c 222 }
jamesheavey 10:9f445faa892c 223 wait(1.0f/fps);
jamesheavey 10:9f445faa892c 224 }
jamesheavey 10:9f445faa892c 225 if (pong.get_lives() == 0) {
jamesheavey 10:9f445faa892c 226 //if (end_screen() == true) {
jamesheavey 10:9f445faa892c 227 // main_menu();
jamesheavey 10:9f445faa892c 228 //}
jamesheavey 10:9f445faa892c 229 //else {
jamesheavey 10:9f445faa892c 230 // main_menu();
jamesheavey 10:9f445faa892c 231 //}
jamesheavey 14:38560375d420 232 pad.leds_off(); //turns last led off (doesnt run through and update bored so last led doent turn off via lives leds
jamesheavey 10:9f445faa892c 233 end_screen();
jamesheavey 10:9f445faa892c 234 }
jamesheavey 13:418a71b24b37 235 if (pong.get_num_left() == 0) {
jamesheavey 13:418a71b24b37 236 victory_screen();
jamesheavey 13:418a71b24b37 237 }
jamesheavey 10:9f445faa892c 238 wait(1.0f/fps);
jamesheavey 10:9f445faa892c 239 }
jamesheavey 10:9f445faa892c 240 }
jamesheavey 10:9f445faa892c 241