This is test version of Pokemongo game. ELEC 2645 final project.

Dependencies:   Tone

Committer:
shalwego
Date:
Thu Apr 15 15:35:12 2021 +0000
Revision:
0:819c2d6a69ac
Issue about music playing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shalwego 0:819c2d6a69ac 1
shalwego 0:819c2d6a69ac 2 ///////////// includes /////////////////////
shalwego 0:819c2d6a69ac 3 #include "mbed.h"
shalwego 0:819c2d6a69ac 4 #include "platform/mbed_thread.h"
shalwego 0:819c2d6a69ac 5 #include "Joystick.h"
shalwego 0:819c2d6a69ac 6 #include "N5110.h"
shalwego 0:819c2d6a69ac 7 #include "ShiftReg.h"
shalwego 0:819c2d6a69ac 8 #include "PokeEngine.h"
shalwego 0:819c2d6a69ac 9 #include "Utils.h"
shalwego 0:819c2d6a69ac 10 //#include <cstdlib>
shalwego 0:819c2d6a69ac 11 #include "Tone.h"
shalwego 0:819c2d6a69ac 12 ///////////// defines /////////////////////
shalwego 0:819c2d6a69ac 13 #define PADDLE_WIDTH 2
shalwego 0:819c2d6a69ac 14 #define PADDLE_HEIGHT 8
shalwego 0:819c2d6a69ac 15 #define BALL_SIZE 2
shalwego 0:819c2d6a69ac 16 #define BALL_SPEED 3
shalwego 0:819c2d6a69ac 17 ///////////// objects ///////////////////
shalwego 0:819c2d6a69ac 18 N5110 lcd(p14,p8,p9,p10,p11,p13,p21);
shalwego 0:819c2d6a69ac 19 Joystick joystick(p20,p19);
shalwego 0:819c2d6a69ac 20 DigitalIn buttonA(p29);
shalwego 0:819c2d6a69ac 21 BusOut leds(LED4,LED3,LED2,LED1);
shalwego 0:819c2d6a69ac 22 ShiftReg seven_seg;
shalwego 0:819c2d6a69ac 23 AnalogIn pot(p17);
shalwego 0:819c2d6a69ac 24 PokeEngine poke;
shalwego 0:819c2d6a69ac 25 Tone dac(p18);
shalwego 0:819c2d6a69ac 26 ///////////// prototypes ///////////////////
shalwego 0:819c2d6a69ac 27 void init();
shalwego 0:819c2d6a69ac 28 void welcome();
shalwego 0:819c2d6a69ac 29 void render();
shalwego 0:819c2d6a69ac 30 void gameover();
shalwego 0:819c2d6a69ac 31 void display_lives(int lives);
shalwego 0:819c2d6a69ac 32
shalwego 0:819c2d6a69ac 33 const int note_background []= {
shalwego 0:819c2d6a69ac 34 NOTE_FS3, NOTE_FS3, NOTE_CS4, NOTE_B3, NOTE_A3,
shalwego 0:819c2d6a69ac 35 NOTE_GS3, NOTE_GS3, NOTE_GS3, NOTE_B3, NOTE_A3, NOTE_GS3,
shalwego 0:819c2d6a69ac 36 NOTE_A3, NOTE_A4, NOTE_GS4, NOTE_A4, NOTE_GS4, NOTE_A4,
shalwego 0:819c2d6a69ac 37 NOTE_A3, NOTE_A4, NOTE_GS4, NOTE_A4, NOTE_GS4, NOTE_A4,
shalwego 0:819c2d6a69ac 38
shalwego 0:819c2d6a69ac 39 NOTE_FS3, NOTE_FS3, NOTE_CS4, NOTE_B3, NOTE_A3,
shalwego 0:819c2d6a69ac 40 NOTE_GS3, NOTE_GS3, NOTE_GS3, NOTE_B3, NOTE_A3, NOTE_GS3,
shalwego 0:819c2d6a69ac 41 NOTE_A3, NOTE_A4, NOTE_GS4, NOTE_A4, NOTE_GS4, NOTE_A4,
shalwego 0:819c2d6a69ac 42 NOTE_A3, NOTE_A4, NOTE_GS4, NOTE_A4, NOTE_GS4, NOTE_A4,
shalwego 0:819c2d6a69ac 43
shalwego 0:819c2d6a69ac 44 NOTE_FS3, NOTE_FS3, NOTE_CS4, NOTE_B3, NOTE_A3,
shalwego 0:819c2d6a69ac 45 NOTE_GS3, NOTE_GS3, NOTE_GS3, NOTE_B3, NOTE_A3, NOTE_GS3,
shalwego 0:819c2d6a69ac 46 NOTE_FS3, NOTE_FS3, NOTE_A4, NOTE_GS4, NOTE_A4, NOTE_GS4, NOTE_A4,
shalwego 0:819c2d6a69ac 47 NOTE_FS3, NOTE_FS3, NOTE_A4, NOTE_GS4, NOTE_A4, NOTE_GS4, NOTE_A4,
shalwego 0:819c2d6a69ac 48
shalwego 0:819c2d6a69ac 49 NOTE_FS3, NOTE_FS3, NOTE_CS4, NOTE_B3, NOTE_A3,
shalwego 0:819c2d6a69ac 50 NOTE_GS3, NOTE_GS3, NOTE_GS3, NOTE_B3, NOTE_A3, NOTE_GS3,
shalwego 0:819c2d6a69ac 51 NOTE_FS3, NOTE_FS3, NOTE_A4, NOTE_GS4, NOTE_A4, NOTE_GS4, NOTE_A4,
shalwego 0:819c2d6a69ac 52 NOTE_FS3, NOTE_FS3, NOTE_A4, NOTE_GS4, NOTE_A4, NOTE_GS4, NOTE_A4,
shalwego 0:819c2d6a69ac 53
shalwego 0:819c2d6a69ac 54 NOTE_FS3, NOTE_FS3, NOTE_CS4, NOTE_B3, NOTE_A3,
shalwego 0:819c2d6a69ac 55 NOTE_GS3, NOTE_GS3, NOTE_GS3, NOTE_B3, NOTE_A3, NOTE_GS3,
shalwego 0:819c2d6a69ac 56 NOTE_FS3, NOTE_FS3, NOTE_A4, NOTE_GS4, NOTE_A4, NOTE_GS4, NOTE_A4,
shalwego 0:819c2d6a69ac 57 };
shalwego 0:819c2d6a69ac 58
shalwego 0:819c2d6a69ac 59 const int duration_background[] = {
shalwego 0:819c2d6a69ac 60 4,8,8,4,4,
shalwego 0:819c2d6a69ac 61 4,8,8,4,4,
shalwego 0:819c2d6a69ac 62 4,4,8,8,8,8,
shalwego 0:819c2d6a69ac 63 4,4,8,8,8,8,
shalwego 0:819c2d6a69ac 64
shalwego 0:819c2d6a69ac 65 4,8,8,4,4,
shalwego 0:819c2d6a69ac 66 4,8,8,4,4,
shalwego 0:819c2d6a69ac 67 4,4,8,8,8,8,
shalwego 0:819c2d6a69ac 68 4,4,8,8,8,8,
shalwego 0:819c2d6a69ac 69
shalwego 0:819c2d6a69ac 70 4,8,8,4,4,
shalwego 0:819c2d6a69ac 71 4,8,8,4,4,
shalwego 0:819c2d6a69ac 72 4,4,8,8,8,8,
shalwego 0:819c2d6a69ac 73 4,4,8,8,8,8,
shalwego 0:819c2d6a69ac 74
shalwego 0:819c2d6a69ac 75 4,8,8,4,4,
shalwego 0:819c2d6a69ac 76 4,8,8,4,4,
shalwego 0:819c2d6a69ac 77 4,4,8,8,8,8,
shalwego 0:819c2d6a69ac 78 4,4,8,8,8,8,
shalwego 0:819c2d6a69ac 79
shalwego 0:819c2d6a69ac 80 4,8,8,4,4,
shalwego 0:819c2d6a69ac 81 4,8,8,4,4,
shalwego 0:819c2d6a69ac 82 4,4,8,8,8,8,
shalwego 0:819c2d6a69ac 83 };
shalwego 0:819c2d6a69ac 84 /*
shalwego 0:819c2d6a69ac 85 const int duration_array[] = {
shalwego 0:819c2d6a69ac 86 4,8,8,4,4,
shalwego 0:819c2d6a69ac 87 4,8,8,4,4,
shalwego 0:819c2d6a69ac 88 4,4,8,8,8,8,
shalwego 0:819c2d6a69ac 89 4,4,8,8,8,8,
shalwego 0:819c2d6a69ac 90
shalwego 0:819c2d6a69ac 91 4,8,8,4,4,
shalwego 0:819c2d6a69ac 92 4,8,8,4,4,
shalwego 0:819c2d6a69ac 93 4,4,8,8,8,8,
shalwego 0:819c2d6a69ac 94 4,4,8,8,8,8,
shalwego 0:819c2d6a69ac 95
shalwego 0:819c2d6a69ac 96 4,8,8,4,4,
shalwego 0:819c2d6a69ac 97 4,8,8,4,4,
shalwego 0:819c2d6a69ac 98 4,4,8,8,8,8,
shalwego 0:819c2d6a69ac 99 4,4,8,8,8,8,
shalwego 0:819c2d6a69ac 100
shalwego 0:819c2d6a69ac 101 4,8,8,4,4,
shalwego 0:819c2d6a69ac 102 4,8,8,4,4,
shalwego 0:819c2d6a69ac 103 4,4,8,8,8,8,
shalwego 0:819c2d6a69ac 104 4,4,8,8,8,8,
shalwego 0:819c2d6a69ac 105
shalwego 0:819c2d6a69ac 106 4,8,8,4,4,
shalwego 0:819c2d6a69ac 107 4,8,8,4,4,
shalwego 0:819c2d6a69ac 108 4,4,8,8,8,8,
shalwego 0:819c2d6a69ac 109 };
shalwego 0:819c2d6a69ac 110
shalwego 0:819c2d6a69ac 111 const int note_array[] = {
shalwego 0:819c2d6a69ac 112 NOTE_FS3, NOTE_FS3, NOTE_CS4, NOTE_B3, NOTE_A3,
shalwego 0:819c2d6a69ac 113 NOTE_GS3, NOTE_GS3, NOTE_GS3, NOTE_B3, NOTE_A3, NOTE_GS3,
shalwego 0:819c2d6a69ac 114 NOTE_A3, NOTE_A4, NOTE_GS4, NOTE_A4, NOTE_GS4, NOTE_A4,
shalwego 0:819c2d6a69ac 115 NOTE_A3, NOTE_A4, NOTE_GS4, NOTE_A4, NOTE_GS4, NOTE_A4,
shalwego 0:819c2d6a69ac 116
shalwego 0:819c2d6a69ac 117 NOTE_FS3, NOTE_FS3, NOTE_CS4, NOTE_B3, NOTE_A3,
shalwego 0:819c2d6a69ac 118 NOTE_GS3, NOTE_GS3, NOTE_GS3, NOTE_B3, NOTE_A3, NOTE_GS3,
shalwego 0:819c2d6a69ac 119 NOTE_A3, NOTE_A4, NOTE_GS4, NOTE_A4, NOTE_GS4, NOTE_A4,
shalwego 0:819c2d6a69ac 120 NOTE_A3, NOTE_A4, NOTE_GS4, NOTE_A4, NOTE_GS4, NOTE_A4,
shalwego 0:819c2d6a69ac 121
shalwego 0:819c2d6a69ac 122 NOTE_FS3, NOTE_FS3, NOTE_CS4, NOTE_B3, NOTE_A3,
shalwego 0:819c2d6a69ac 123 NOTE_GS3, NOTE_GS3, NOTE_GS3, NOTE_B3, NOTE_A3, NOTE_GS3,
shalwego 0:819c2d6a69ac 124 NOTE_FS3, NOTE_FS3, NOTE_A4, NOTE_GS4, NOTE_A4, NOTE_GS4, NOTE_A4,
shalwego 0:819c2d6a69ac 125 NOTE_FS3, NOTE_FS3, NOTE_A4, NOTE_GS4, NOTE_A4, NOTE_GS4, NOTE_A4,
shalwego 0:819c2d6a69ac 126
shalwego 0:819c2d6a69ac 127 NOTE_FS3, NOTE_FS3, NOTE_CS4, NOTE_B3, NOTE_A3,
shalwego 0:819c2d6a69ac 128 NOTE_GS3, NOTE_GS3, NOTE_GS3, NOTE_B3, NOTE_A3, NOTE_GS3,
shalwego 0:819c2d6a69ac 129 NOTE_FS3, NOTE_FS3, NOTE_A4, NOTE_GS4, NOTE_A4, NOTE_GS4, NOTE_A4,
shalwego 0:819c2d6a69ac 130 NOTE_FS3, NOTE_FS3, NOTE_A4, NOTE_GS4, NOTE_A4, NOTE_GS4, NOTE_A4,
shalwego 0:819c2d6a69ac 131
shalwego 0:819c2d6a69ac 132 NOTE_FS3, NOTE_FS3, NOTE_CS4, NOTE_B3, NOTE_A3,
shalwego 0:819c2d6a69ac 133 NOTE_GS3, NOTE_GS3, NOTE_GS3, NOTE_B3, NOTE_A3, NOTE_GS3,
shalwego 0:819c2d6a69ac 134 NOTE_FS3, NOTE_FS3, NOTE_A4, NOTE_GS4, NOTE_A4, NOTE_GS4, NOTE_A4,
shalwego 0:819c2d6a69ac 135 };
shalwego 0:819c2d6a69ac 136 */
shalwego 0:819c2d6a69ac 137 //volatile int g_gameover_flag = 0;
shalwego 0:819c2d6a69ac 138 //void gameover_isr();
shalwego 0:819c2d6a69ac 139
shalwego 0:819c2d6a69ac 140 int main() {
shalwego 0:819c2d6a69ac 141
shalwego 0:819c2d6a69ac 142 init();
shalwego 0:819c2d6a69ac 143 welcome();
shalwego 0:819c2d6a69ac 144 render();
shalwego 0:819c2d6a69ac 145 int fps = 10;
shalwego 0:819c2d6a69ac 146 int lives = 4;
shalwego 0:819c2d6a69ac 147 //poke.check_lives(lives);
shalwego 0:819c2d6a69ac 148 display_lives(lives);
shalwego 0:819c2d6a69ac 149 while (lives != 0) { // keep looping while lives remain
shalwego 0:819c2d6a69ac 150 // read the joystick input and store in a struct
shalwego 0:819c2d6a69ac 151 UserInput input = {joystick.get_direction(),joystick.get_mag()};
shalwego 0:819c2d6a69ac 152 lives = poke.update(input); // update the game engine based on input
shalwego 0:819c2d6a69ac 153 printf("lives = %d ", lives);
shalwego 0:819c2d6a69ac 154 display_lives(lives); // display lives on LEDs
shalwego 0:819c2d6a69ac 155 render(); // draw frame on screen
shalwego 0:819c2d6a69ac 156 thread_sleep_for(1000/fps); // and wait for one frame period - ms
shalwego 0:819c2d6a69ac 157 }
shalwego 0:819c2d6a69ac 158 //int n = sizeof(note_array)/sizeof(int);
shalwego 0:819c2d6a69ac 159 //dac.play_melody(n,note_array,duration_array,72.0,true);
shalwego 0:819c2d6a69ac 160 gameover();
shalwego 0:819c2d6a69ac 161 /* while (1) {
shalwego 0:819c2d6a69ac 162
shalwego 0:819c2d6a69ac 163 // check if flag is set i.e. interrupt has occured
shalwego 0:819c2d6a69ac 164 if (g_timer_flag) {
shalwego 0:819c2d6a69ac 165 g_timer_flag = 0; // if it has, clear the flag
shalwego 0:819c2d6a69ac 166 printf("Tick \n");
shalwego 0:819c2d6a69ac 167 // DO TASK HERE
shalwego 0:819c2d6a69ac 168 }
shalwego 0:819c2d6a69ac 169
shalwego 0:819c2d6a69ac 170 // put the MCU to sleep until an interrupt wakes it up
shalwego 0:819c2d6a69ac 171 sleep();
shalwego 0:819c2d6a69ac 172
shalwego 0:819c2d6a69ac 173 }*/
shalwego 0:819c2d6a69ac 174
shalwego 0:819c2d6a69ac 175 }
shalwego 0:819c2d6a69ac 176
shalwego 0:819c2d6a69ac 177 void init() {
shalwego 0:819c2d6a69ac 178 lcd.clear();
shalwego 0:819c2d6a69ac 179 seven_seg.write(0x00); // turn of 7-seg display
shalwego 0:819c2d6a69ac 180 lcd.init();
shalwego 0:819c2d6a69ac 181 lcd.setContrast(0.4);
shalwego 0:819c2d6a69ac 182 joystick.init();
shalwego 0:819c2d6a69ac 183 dac.init();
shalwego 0:819c2d6a69ac 184
shalwego 0:819c2d6a69ac 185 poke.init(70,10,5,20,10,3,2);
shalwego 0:819c2d6a69ac 186 poke.init_rocket(50,40,4,2);
shalwego 0:819c2d6a69ac 187 }
shalwego 0:819c2d6a69ac 188
shalwego 0:819c2d6a69ac 189 void welcome() { // splash screen
shalwego 0:819c2d6a69ac 190 lcd.printString(" Pokemon go! ",0,1);
shalwego 0:819c2d6a69ac 191 lcd.printString(" Press A ",0,4);
shalwego 0:819c2d6a69ac 192 lcd.refresh();
shalwego 0:819c2d6a69ac 193 //t n_background = sizeof(note_background)/sizeof(int);
shalwego 0:819c2d6a69ac 194 //c.play_melody(n_background,note_background,duration_background,72.0,true);
shalwego 0:819c2d6a69ac 195 while (buttonA.read() == 0) {
shalwego 0:819c2d6a69ac 196 leds = 0b1111;
shalwego 0:819c2d6a69ac 197 thread_sleep_for(100);
shalwego 0:819c2d6a69ac 198 leds = 0b0000;
shalwego 0:819c2d6a69ac 199 thread_sleep_for(100);
shalwego 0:819c2d6a69ac 200 }
shalwego 0:819c2d6a69ac 201 }
shalwego 0:819c2d6a69ac 202
shalwego 0:819c2d6a69ac 203 void render() { // clear screen, re-draw and refresh
shalwego 0:819c2d6a69ac 204 lcd.clear();
shalwego 0:819c2d6a69ac 205 poke.draw(lcd);
shalwego 0:819c2d6a69ac 206 lcd.refresh();
shalwego 0:819c2d6a69ac 207 }
shalwego 0:819c2d6a69ac 208
shalwego 0:819c2d6a69ac 209 void display_lives(int lives) {
shalwego 0:819c2d6a69ac 210 if (lives == 4) {
shalwego 0:819c2d6a69ac 211 leds = 0b1111;
shalwego 0:819c2d6a69ac 212 seven_seg.write(0x66);
shalwego 0:819c2d6a69ac 213 } else if (lives == 3) {
shalwego 0:819c2d6a69ac 214 leds = 0b1110;
shalwego 0:819c2d6a69ac 215 seven_seg.write(0x4F);
shalwego 0:819c2d6a69ac 216 } else if (lives == 2) {
shalwego 0:819c2d6a69ac 217 leds = 0b1100;
shalwego 0:819c2d6a69ac 218 seven_seg.write(0x5B);
shalwego 0:819c2d6a69ac 219 } else if (lives == 1) {
shalwego 0:819c2d6a69ac 220 leds = 0b1000;
shalwego 0:819c2d6a69ac 221 seven_seg.write(0x06);
shalwego 0:819c2d6a69ac 222 } else {
shalwego 0:819c2d6a69ac 223 leds = 0b0000;
shalwego 0:819c2d6a69ac 224 seven_seg.write(0x3F);
shalwego 0:819c2d6a69ac 225 }
shalwego 0:819c2d6a69ac 226 }
shalwego 0:819c2d6a69ac 227
shalwego 0:819c2d6a69ac 228 void gameover() { // splash screen
shalwego 0:819c2d6a69ac 229 //for (int i = 1; i < 10; i++) {
shalwego 0:819c2d6a69ac 230 while(1){
shalwego 0:819c2d6a69ac 231 lcd.clear();
shalwego 0:819c2d6a69ac 232 int score = poke.get_score();
shalwego 0:819c2d6a69ac 233 char char_score = score + '0';
shalwego 0:819c2d6a69ac 234 // char *char_score = new char[2];
shalwego 0:819c2d6a69ac 235 //for (int i = 0; i < 2; i++){
shalwego 0:819c2d6a69ac 236 // char_score[i] = score;}
shalwego 0:819c2d6a69ac 237 lcd.printString(" Game Over ",0,0);
shalwego 0:819c2d6a69ac 238 lcd.printString(" Your score: ",0,2);
shalwego 0:819c2d6a69ac 239 lcd.printChar(char_score,42,4);
shalwego 0:819c2d6a69ac 240 lcd.refresh();
shalwego 0:819c2d6a69ac 241 leds = 0b1111;
shalwego 0:819c2d6a69ac 242 thread_sleep_for(250);
shalwego 0:819c2d6a69ac 243 lcd.clear();
shalwego 0:819c2d6a69ac 244 lcd.refresh();
shalwego 0:819c2d6a69ac 245 leds = 0b0000;
shalwego 0:819c2d6a69ac 246 thread_sleep_for(250);
shalwego 0:819c2d6a69ac 247 }
shalwego 0:819c2d6a69ac 248 }
shalwego 0:819c2d6a69ac 249
shalwego 0:819c2d6a69ac 250 /*void gameover_isr()
shalwego 0:819c2d6a69ac 251 {
shalwego 0:819c2d6a69ac 252 g_gameover_flag = 1; // set flag in ISR
shalwego 0:819c2d6a69ac 253 }*/