Dependencies:   mbed

Committer:
KaifK
Date:
Fri May 22 01:46:19 2020 +0000
Revision:
17:2fbe40177b9c
Parent:
16:1f196a0e12be
Child:
18:100e7d3509b5
Refined Ball Shot animation, cleaned up code, updated and fixed level 7

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 0:e47aa16004e6 14
KaifK 16:1f196a0e12be 15 const int highscore_icon [9][15] = {
KaifK 16:1f196a0e12be 16 {0,0,0,0,0,1,1,1,1,1,0,0,0,0,0},
KaifK 16:1f196a0e12be 17 {0,0,0,0,0,1,0,0,0,1,0,0,0,0,0},
KaifK 16:1f196a0e12be 18 {0,0,0,0,0,1,0,0,0,1,0,0,0,0,0},
KaifK 16:1f196a0e12be 19 {1,1,1,1,0,1,0,0,0,1,0,0,0,0,0},
KaifK 16:1f196a0e12be 20 {1,0,0,1,0,1,0,0,0,1,0,1,1,1,1},
KaifK 16:1f196a0e12be 21 {1,0,0,1,0,1,0,0,0,1,0,1,0,0,1},
KaifK 16:1f196a0e12be 22 {1,0,0,1,0,1,0,0,0,1,0,1,0,0,1},
KaifK 16:1f196a0e12be 23 {1,0,0,1,0,1,0,0,0,1,0,1,0,0,1},
KaifK 16:1f196a0e12be 24 {1,1,1,1,0,1,1,1,1,1,0,1,1,1,1},
KaifK 16:1f196a0e12be 25 };
KaifK 4:08a0ff6668df 26 const int a_button [9][9] = {
KaifK 4:08a0ff6668df 27 {0,0,0,1,1,1,0,0,0},
KaifK 4:08a0ff6668df 28 {0,0,1,0,0,0,1,0,0},
KaifK 4:08a0ff6668df 29 {0,1,0,0,1,0,0,1,0},
KaifK 4:08a0ff6668df 30 {1,0,0,1,0,1,0,0,1},
KaifK 4:08a0ff6668df 31 {1,0,0,1,1,1,0,0,1},
KaifK 4:08a0ff6668df 32 {1,0,0,1,0,1,0,0,1},
KaifK 4:08a0ff6668df 33 {0,1,0,0,0,0,0,1,0},
KaifK 4:08a0ff6668df 34 {0,0,1,0,0,0,1,0,0},
KaifK 4:08a0ff6668df 35 {0,0,0,1,1,1,0,0,0}
KaifK 4:08a0ff6668df 36 };
KaifK 4:08a0ff6668df 37 const int b_button [9][9] = {
KaifK 4:08a0ff6668df 38 {0,0,0,1,1,1,0,0,0},
KaifK 4:08a0ff6668df 39 {0,0,1,0,0,0,1,0,0},
KaifK 4:08a0ff6668df 40 {0,1,0,1,1,1,0,1,0},
KaifK 4:08a0ff6668df 41 {1,0,0,1,0,0,1,0,1},
KaifK 4:08a0ff6668df 42 {1,0,0,1,1,1,0,0,1},
KaifK 4:08a0ff6668df 43 {1,0,0,1,0,0,1,0,1},
KaifK 4:08a0ff6668df 44 {0,1,0,1,1,1,0,1,0},
KaifK 4:08a0ff6668df 45 {0,0,1,0,0,0,1,0,0},
KaifK 4:08a0ff6668df 46 {0,0,0,1,1,1,0,0,0}
KaifK 4:08a0ff6668df 47 };
KaifK 4:08a0ff6668df 48 const int x_button [9][9] = {
KaifK 4:08a0ff6668df 49 {0,0,0,1,1,1,0,0,0},
KaifK 4:08a0ff6668df 50 {0,0,1,0,0,0,1,0,0},
KaifK 4:08a0ff6668df 51 {0,1,0,0,0,0,0,1,0},
KaifK 4:08a0ff6668df 52 {1,0,0,1,0,1,0,0,1},
KaifK 4:08a0ff6668df 53 {1,0,0,0,1,0,0,0,1},
KaifK 4:08a0ff6668df 54 {1,0,0,1,0,1,0,0,1},
KaifK 4:08a0ff6668df 55 {0,1,0,0,0,0,0,1,0},
KaifK 4:08a0ff6668df 56 {0,0,1,0,0,0,1,0,0},
KaifK 4:08a0ff6668df 57 {0,0,0,1,1,1,0,0,0}
KaifK 4:08a0ff6668df 58 };
KaifK 4:08a0ff6668df 59 const int y_button [9][9] = {
KaifK 4:08a0ff6668df 60 {0,0,0,1,1,1,0,0,0},
KaifK 4:08a0ff6668df 61 {0,0,1,0,0,0,1,0,0},
KaifK 4:08a0ff6668df 62 {0,1,0,0,0,0,0,1,0},
KaifK 4:08a0ff6668df 63 {1,0,0,1,0,1,0,0,1},
KaifK 4:08a0ff6668df 64 {1,0,0,0,1,0,0,0,1},
KaifK 4:08a0ff6668df 65 {1,0,0,0,1,0,0,0,1},
KaifK 4:08a0ff6668df 66 {0,1,0,0,1,0,0,1,0},
KaifK 4:08a0ff6668df 67 {0,0,1,0,0,0,1,0,0},
KaifK 4:08a0ff6668df 68 {0,0,0,1,1,1,0,0,0}
KaifK 4:08a0ff6668df 69 };
KaifK 4:08a0ff6668df 70 //objects
KaifK 0:e47aa16004e6 71 Gamepad pad;
KaifK 0:e47aa16004e6 72 N5110 lcd;
KaifK 9:e6566d09f087 73 Ball ball(lcd);
KaifK 9:e6566d09f087 74 Splash splash(lcd, ball);
KaifK 9:e6566d09f087 75 Game game(lcd, pad, ball);
KaifK 5:ed71996c0534 76 //functions
KaifK 16:1f196a0e12be 77 void init();
KaifK 5:ed71996c0534 78 void display_menu();
KaifK 17:2fbe40177b9c 79 void game_loop();
KaifK 16:1f196a0e12be 80 void how_to();
KaifK 16:1f196a0e12be 81 void display_settings();
KaifK 4:08a0ff6668df 82 //variables
KaifK 9:e6566d09f087 83
KaifK 2:21973e665a32 84
KaifK 4:08a0ff6668df 85 int main()
KaifK 4:08a0ff6668df 86 {
KaifK 16:1f196a0e12be 87 init();
KaifK 4:08a0ff6668df 88 while (1)
KaifK 4:08a0ff6668df 89 {
KaifK 11:59119247dd68 90 //ball.level_loop();
KaifK 4:08a0ff6668df 91 display_menu();
KaifK 4:08a0ff6668df 92 if (pad.A_pressed()) {
KaifK 17:2fbe40177b9c 93 game_loop();
KaifK 4:08a0ff6668df 94 }
KaifK 16:1f196a0e12be 95 else if (pad.B_pressed()) {
KaifK 5:ed71996c0534 96 wait(0.2); //debounce
KaifK 5:ed71996c0534 97 while(!pad.B_pressed()) {
KaifK 5:ed71996c0534 98 //Instructions Page
KaifK 5:ed71996c0534 99 wait_ms(100);
KaifK 5:ed71996c0534 100 lcd.refresh();
KaifK 5:ed71996c0534 101 lcd.clear();
KaifK 5:ed71996c0534 102 }
KaifK 5:ed71996c0534 103 }
KaifK 16:1f196a0e12be 104 else if (pad.X_pressed()) {
KaifK 5:ed71996c0534 105 while(!pad.B_pressed()) {
KaifK 16:1f196a0e12be 106 display_settings();
KaifK 5:ed71996c0534 107 }
KaifK 5:ed71996c0534 108 }
KaifK 16:1f196a0e12be 109 else if (pad.Y_pressed()) {
KaifK 6:09a555c0d997 110 lcd.turnOff();
KaifK 6:09a555c0d997 111 pad.leds(0.0);
KaifK 6:09a555c0d997 112 }
KaifK 4:08a0ff6668df 113 }
KaifK 4:08a0ff6668df 114 }
KaifK 16:1f196a0e12be 115 void init() {
KaifK 16:1f196a0e12be 116 lcd.init();
KaifK 16:1f196a0e12be 117 lcd.setContrast(0.5);
KaifK 16:1f196a0e12be 118 pad.init();
KaifK 16:1f196a0e12be 119 lcd.backLightOn();
KaifK 17:2fbe40177b9c 120 //splash.displayInfo();
KaifK 17:2fbe40177b9c 121 //splash.playIntro();
KaifK 16:1f196a0e12be 122 }
KaifK 4:08a0ff6668df 123 void display_menu()
KaifK 4:08a0ff6668df 124 {
KaifK 9:e6566d09f087 125 splash.drawLogo(13, 0);
KaifK 4:08a0ff6668df 126 lcd.printString("Play", 2, 2);
KaifK 4:08a0ff6668df 127 lcd.printString("How-To?", 2, 3);
KaifK 4:08a0ff6668df 128 lcd.printString("Settings", 2, 4);
KaifK 4:08a0ff6668df 129 lcd.printString("Exit", 2, 5);
KaifK 4:08a0ff6668df 130 lcd.drawSprite(28, 15, 9, 9, (int*) a_button);
KaifK 4:08a0ff6668df 131 lcd.drawSprite(45, 23, 9, 9, (int*) b_button);
KaifK 4:08a0ff6668df 132 lcd.drawSprite(52, 31, 9, 9, (int*) x_button);
KaifK 4:08a0ff6668df 133 lcd.drawSprite(28, 39, 9, 9, (int*) y_button);
KaifK 16:1f196a0e12be 134 //highscore
KaifK 16:1f196a0e12be 135 lcd.drawSprite(65, 21, 9 , 15, (int *)highscore_icon);
KaifK 16:1f196a0e12be 136 lcd.drawRect(65, 30, 15, 12, FILL_TRANSPARENT);
KaifK 16:1f196a0e12be 137 int highscore = game.get_highscore();
KaifK 16:1f196a0e12be 138 char buffer[12];
KaifK 16:1f196a0e12be 139 sprintf(buffer,"%d",highscore);
KaifK 16:1f196a0e12be 140 lcd.printString(buffer,67,4);
KaifK 15:5bf3f951d337 141 lcd.refresh();
KaifK 15:5bf3f951d337 142 wait(0.1);
KaifK 16:1f196a0e12be 143 lcd.clear();
KaifK 16:1f196a0e12be 144 }
KaifK 17:2fbe40177b9c 145 void game_loop() {
KaifK 17:2fbe40177b9c 146 wait(0.2); //debounce
KaifK 17:2fbe40177b9c 147 game.init();
KaifK 17:2fbe40177b9c 148 game.play();
KaifK 17:2fbe40177b9c 149 lcd.clear();
KaifK 17:2fbe40177b9c 150 lcd.printString("GAME OVER",15,2);
KaifK 17:2fbe40177b9c 151 lcd.refresh();
KaifK 17:2fbe40177b9c 152 wait(2);
KaifK 17:2fbe40177b9c 153 pad.reset_buttons(); //to ensure no accidental selection on menu
KaifK 17:2fbe40177b9c 154 }
KaifK 16:1f196a0e12be 155 void how_to() {
KaifK 16:1f196a0e12be 156 //page 1 - b to exit, buttons on gamepad
KaifK 16:1f196a0e12be 157 //page 2 - a to stop pointer, b to set power
KaifK 16:1f196a0e12be 158 //page 3- tips
KaifK 16:1f196a0e12be 159 //page 4- tips
KaifK 16:1f196a0e12be 160 //page 5- life after 5 points
KaifK 16:1f196a0e12be 161 }
KaifK 16:1f196a0e12be 162 void display_settings() {
KaifK 16:1f196a0e12be 163 lcd.printString("Settings",20,0);
KaifK 16:1f196a0e12be 164 lcd.printString("Set Contrast",0,1);
KaifK 16:1f196a0e12be 165 lcd.printString("using Pot 1",0,2);
KaifK 16:1f196a0e12be 166 lcd.printString("Hit B to Exit",0,5);
KaifK 16:1f196a0e12be 167 lcd.drawRect(0, 24, 84, 8, FILL_TRANSPARENT);
KaifK 16:1f196a0e12be 168 float pot_1_val = pad.read_pot1();
KaifK 16:1f196a0e12be 169 printf("POT 1 = %.2f \n",pot_1_val);
KaifK 16:1f196a0e12be 170 lcd.drawRect(0, 24, (int)(84*pot_1_val), 8, FILL_BLACK);
KaifK 16:1f196a0e12be 171 lcd.setContrast(pot_1_val);
KaifK 16:1f196a0e12be 172 lcd.refresh();
KaifK 16:1f196a0e12be 173 wait_ms(100);
KaifK 16:1f196a0e12be 174 lcd.clear();
KaifK 16:1f196a0e12be 175 }
KaifK 16:1f196a0e12be 176