Kaif Kutchwala 201267448 ELEC2645 Project

Dependencies:   mbed

Committer:
KaifK
Date:
Fri May 22 23:51:06 2020 +0000
Revision:
19:074443a8c89b
Parent:
18:100e7d3509b5
Child:
20:c697902b844f
Version 1 Complete

Who changed what in which revision?

UserRevisionLine numberNew contents of line
KaifK 0:e47aa16004e6 1 /*
KaifK 4:08a0ff6668df 2 Shoot
KaifK 0:e47aa16004e6 3 ELEC2645 Project
KaifK 0:e47aa16004e6 4 EL18KK
KaifK 0:e47aa16004e6 5 201267448
KaifK 0:e47aa16004e6 6 */
KaifK 0:e47aa16004e6 7
KaifK 0:e47aa16004e6 8 #include "mbed.h"
KaifK 0:e47aa16004e6 9 #include "Gamepad.h"
KaifK 0:e47aa16004e6 10 #include "N5110.h"
KaifK 3:f86c1cf90d0d 11 #include "Ball.h"
KaifK 3:f86c1cf90d0d 12 #include "Splash.h"
KaifK 4:08a0ff6668df 13 #include "Game.h"
KaifK 18:100e7d3509b5 14 #include "Instructions.h"
KaifK 18:100e7d3509b5 15 #include "Sprites.h"
KaifK 0:e47aa16004e6 16
KaifK 4:08a0ff6668df 17 //objects
KaifK 0:e47aa16004e6 18 Gamepad pad;
KaifK 0:e47aa16004e6 19 N5110 lcd;
KaifK 9:e6566d09f087 20 Ball ball(lcd);
KaifK 9:e6566d09f087 21 Splash splash(lcd, ball);
KaifK 9:e6566d09f087 22 Game game(lcd, pad, ball);
KaifK 18:100e7d3509b5 23 Instructions instruction(lcd, pad, ball, game);
KaifK 5:ed71996c0534 24 //functions
KaifK 16:1f196a0e12be 25 void init();
KaifK 5:ed71996c0534 26 void display_menu();
KaifK 17:2fbe40177b9c 27 void game_loop();
KaifK 16:1f196a0e12be 28 void how_to();
KaifK 16:1f196a0e12be 29 void display_settings();
KaifK 4:08a0ff6668df 30 //variables
KaifK 18:100e7d3509b5 31 int page = 1;
KaifK 2:21973e665a32 32
KaifK 18:100e7d3509b5 33 int main() {
KaifK 16:1f196a0e12be 34 init();
KaifK 18:100e7d3509b5 35 while (1) {
KaifK 11:59119247dd68 36 //ball.level_loop();
KaifK 4:08a0ff6668df 37 display_menu();
KaifK 18:100e7d3509b5 38 page = 1;
KaifK 4:08a0ff6668df 39 if (pad.A_pressed()) {
KaifK 17:2fbe40177b9c 40 game_loop();
KaifK 18:100e7d3509b5 41 } else if (pad.B_pressed()) {
KaifK 5:ed71996c0534 42 wait(0.2); //debounce
KaifK 18:100e7d3509b5 43 while(!pad.B_pressed()) {how_to();}
KaifK 18:100e7d3509b5 44 pad.reset_buttons();
KaifK 18:100e7d3509b5 45 wait(0.2);
KaifK 18:100e7d3509b5 46 } else if (pad.X_pressed()) {
KaifK 5:ed71996c0534 47 while(!pad.B_pressed()) {
KaifK 16:1f196a0e12be 48 display_settings();
KaifK 5:ed71996c0534 49 }
KaifK 18:100e7d3509b5 50 } else if (pad.Y_pressed()) {
KaifK 6:09a555c0d997 51 lcd.turnOff();
KaifK 6:09a555c0d997 52 pad.leds(0.0);
KaifK 6:09a555c0d997 53 }
KaifK 4:08a0ff6668df 54 }
KaifK 4:08a0ff6668df 55 }
KaifK 16:1f196a0e12be 56 void init() {
KaifK 16:1f196a0e12be 57 lcd.init();
KaifK 16:1f196a0e12be 58 lcd.setContrast(0.5);
KaifK 16:1f196a0e12be 59 pad.init();
KaifK 16:1f196a0e12be 60 lcd.backLightOn();
KaifK 18:100e7d3509b5 61 splash.displayInfo();
KaifK 18:100e7d3509b5 62 splash.playIntro();
KaifK 16:1f196a0e12be 63 }
KaifK 18:100e7d3509b5 64 void display_menu() {
KaifK 9:e6566d09f087 65 splash.drawLogo(13, 0);
KaifK 4:08a0ff6668df 66 lcd.printString("Play", 2, 2);
KaifK 4:08a0ff6668df 67 lcd.printString("How-To?", 2, 3);
KaifK 4:08a0ff6668df 68 lcd.printString("Settings", 2, 4);
KaifK 4:08a0ff6668df 69 lcd.printString("Exit", 2, 5);
KaifK 4:08a0ff6668df 70 lcd.drawSprite(28, 15, 9, 9, (int*) a_button);
KaifK 4:08a0ff6668df 71 lcd.drawSprite(45, 23, 9, 9, (int*) b_button);
KaifK 4:08a0ff6668df 72 lcd.drawSprite(52, 31, 9, 9, (int*) x_button);
KaifK 4:08a0ff6668df 73 lcd.drawSprite(28, 39, 9, 9, (int*) y_button);
KaifK 16:1f196a0e12be 74 //highscore
KaifK 16:1f196a0e12be 75 lcd.drawSprite(65, 21, 9 , 15, (int *)highscore_icon);
KaifK 16:1f196a0e12be 76 lcd.drawRect(65, 30, 15, 12, FILL_TRANSPARENT);
KaifK 16:1f196a0e12be 77 int highscore = game.get_highscore();
KaifK 16:1f196a0e12be 78 char buffer[12];
KaifK 16:1f196a0e12be 79 sprintf(buffer,"%d",highscore);
KaifK 16:1f196a0e12be 80 lcd.printString(buffer,67,4);
KaifK 15:5bf3f951d337 81 lcd.refresh();
KaifK 15:5bf3f951d337 82 wait(0.1);
KaifK 16:1f196a0e12be 83 lcd.clear();
KaifK 16:1f196a0e12be 84 }
KaifK 17:2fbe40177b9c 85 void game_loop() {
KaifK 17:2fbe40177b9c 86 wait(0.2); //debounce
KaifK 17:2fbe40177b9c 87 game.init();
KaifK 17:2fbe40177b9c 88 game.play();
KaifK 17:2fbe40177b9c 89 lcd.clear();
KaifK 17:2fbe40177b9c 90 lcd.printString("GAME OVER",15,2);
KaifK 17:2fbe40177b9c 91 lcd.refresh();
KaifK 17:2fbe40177b9c 92 wait(2);
KaifK 17:2fbe40177b9c 93 pad.reset_buttons(); //to ensure no accidental selection on menu
KaifK 17:2fbe40177b9c 94 }
KaifK 16:1f196a0e12be 95 void how_to() {
KaifK 19:074443a8c89b 96 _pad->leds(0.0);
KaifK 18:100e7d3509b5 97 if(pad.get_direction() == N) {
KaifK 18:100e7d3509b5 98 page--;
KaifK 18:100e7d3509b5 99 } else if (pad.get_direction() == S || page == 3 || page == 4) {
KaifK 18:100e7d3509b5 100 page++;
KaifK 18:100e7d3509b5 101 }
KaifK 18:100e7d3509b5 102 if(page > 7) {page = 7;}
KaifK 18:100e7d3509b5 103 if(page < 1) {page = 1;}
KaifK 18:100e7d3509b5 104 instruction.set_page(page);
KaifK 18:100e7d3509b5 105 instruction.display();
KaifK 18:100e7d3509b5 106 wait(0.2);
KaifK 16:1f196a0e12be 107 }
KaifK 16:1f196a0e12be 108 void display_settings() {
KaifK 16:1f196a0e12be 109 lcd.printString("Settings",20,0);
KaifK 16:1f196a0e12be 110 lcd.printString("Set Contrast",0,1);
KaifK 16:1f196a0e12be 111 lcd.printString("using Pot 1",0,2);
KaifK 16:1f196a0e12be 112 lcd.printString("Hit B to Exit",0,5);
KaifK 16:1f196a0e12be 113 lcd.drawRect(0, 24, 84, 8, FILL_TRANSPARENT);
KaifK 16:1f196a0e12be 114 float pot_1_val = pad.read_pot1();
KaifK 16:1f196a0e12be 115 printf("POT 1 = %.2f \n",pot_1_val);
KaifK 16:1f196a0e12be 116 lcd.drawRect(0, 24, (int)(84*pot_1_val), 8, FILL_BLACK);
KaifK 16:1f196a0e12be 117 lcd.setContrast(pot_1_val);
KaifK 16:1f196a0e12be 118 lcd.refresh();
KaifK 16:1f196a0e12be 119 wait_ms(100);
KaifK 16:1f196a0e12be 120 lcd.clear();
KaifK 16:1f196a0e12be 121 }
KaifK 16:1f196a0e12be 122