IMU is outputting garbage value to the terminal

Dependencies:   mbed wave_player mbed-rtos 4DGL-uLCD-SE SDFileSystem

Committer:
Tito96
Date:
Fri Apr 23 01:03:49 2021 +0000
Revision:
2:ade842cb48e9
Parent:
1:618aa2c4ca6a
one player with IMU

Who changed what in which revision?

UserRevisionLine numberNew contents of line
michaeljson 0:3817adfaeb06 1 #include "mbed.h"
michaeljson 0:3817adfaeb06 2 #include "SDFileSystem.h"
michaeljson 0:3817adfaeb06 3 #include "wave_player.h"
michaeljson 0:3817adfaeb06 4 #include "enemy.h"
michaeljson 0:3817adfaeb06 5 #include "player.h"
michaeljson 0:3817adfaeb06 6 #include "missile.h"
michaeljson 0:3817adfaeb06 7 #include "globals.h"
michaeljson 0:3817adfaeb06 8 #include "rtos.h"
michaeljson 0:3817adfaeb06 9 #include <string>
Tito96 2:ade842cb48e9 10 #include "LSM9DS1.h"
michaeljson 0:3817adfaeb06 11
michaeljson 0:3817adfaeb06 12 /* ==== Navigation Switch ===== */
Tito96 2:ade842cb48e9 13
michaeljson 0:3817adfaeb06 14
michaeljson 0:3817adfaeb06 15 // Platform initialization
Tito96 2:ade842cb48e9 16
michaeljson 0:3817adfaeb06 17 uLCD_4DGL uLCD(p28,p27,p29); // LCD (serial tx, serial rx, reset pin;)
michaeljson 0:3817adfaeb06 18 AnalogOut DACout(p18); // speaker
michaeljson 0:3817adfaeb06 19 wave_player waver(&DACout); // wav player
michaeljson 0:3817adfaeb06 20 SDFileSystem sd(p5, p6, p7, p8, "sd"); // SD card and filesystem (mosi, miso, sck, cs)
Tito96 2:ade842cb48e9 21 //Nav_Switch myNav(p16, p10, p11, p9, p12); //pin order on Sparkfun breakout
Tito96 2:ade842cb48e9 22 DigitalIn pb(p15); // push button for player misisle fir
Tito96 2:ade842cb48e9 23 Serial pc(USBTX, USBRX);
Tito96 2:ade842cb48e9 24 PwmOut red(p21); // added to dim and brighten red LED -- Brice
Tito96 2:ade842cb48e9 25 PwmOut green(p22); // added to dim and brighten green LED -- Brice
Tito96 2:ade842cb48e9 26 PwmOut blue(p23); // added to dim and brighten blue LED -- Brice
michaeljson 0:3817adfaeb06 27
michaeljson 0:3817adfaeb06 28 // Initialize all global enemy objects
michaeljson 0:3817adfaeb06 29 enemy_t enemy_1;
michaeljson 0:3817adfaeb06 30 enemy_t enemy_2;
michaeljson 0:3817adfaeb06 31 enemy_t enemy_3;
michaeljson 0:3817adfaeb06 32 enemy_t enemy_4;
michaeljson 0:3817adfaeb06 33 enemy_t enemy_5;
michaeljson 0:3817adfaeb06 34 enemy_t enemy_6;
michaeljson 0:3817adfaeb06 35 enemy_t enemy_7;
michaeljson 0:3817adfaeb06 36 enemy_t enemy_8;
michaeljson 0:3817adfaeb06 37 enemy_t enemy_9;
michaeljson 0:3817adfaeb06 38 enemy_t enemy_10;
michaeljson 0:3817adfaeb06 39 enemy_t enemy_11;
michaeljson 0:3817adfaeb06 40 enemy_t enemy_12;
michaeljson 0:3817adfaeb06 41 enemy_t enemy_13;
michaeljson 0:3817adfaeb06 42 enemy_t enemy_14;
michaeljson 0:3817adfaeb06 43 enemy_t enemy_15;
michaeljson 0:3817adfaeb06 44
michaeljson 0:3817adfaeb06 45 // Initialize variables
michaeljson 0:3817adfaeb06 46 int numOfEnemies = 0;
michaeljson 0:3817adfaeb06 47 int ENEMY_MOVE = 1;
michaeljson 0:3817adfaeb06 48 int MOVE_DOWN = 0;
michaeljson 0:3817adfaeb06 49 int DIRECTION = 1;
michaeljson 0:3817adfaeb06 50 int firing_col = 0;
michaeljson 0:3817adfaeb06 51 int hit_player = 0;
michaeljson 0:3817adfaeb06 52 bool lose = false;
Tito96 2:ade842cb48e9 53 int lives1 = 3;
Tito96 2:ade842cb48e9 54 int lives2 = 2;
Tito96 2:ade842cb48e9 55 int lives3 = 1;
Tito96 2:ade842cb48e9 56 //int max_lives = 3;
michaeljson 0:3817adfaeb06 57 bool game_menu = false;
michaeljson 0:3817adfaeb06 58 bool begin_game = false;
michaeljson 0:3817adfaeb06 59 bool gameover = false;
Tito96 2:ade842cb48e9 60 int level = 1;
Tito96 2:ade842cb48e9 61 double inX,inY,inZ;
Tito96 2:ade842cb48e9 62 int pos = 0;
michaeljson 0:3817adfaeb06 63
michaeljson 0:3817adfaeb06 64 // Initialize global player object
michaeljson 0:3817adfaeb06 65 player_t player;
michaeljson 0:3817adfaeb06 66
michaeljson 0:3817adfaeb06 67 // Intialize global player and enemy missile
michaeljson 0:3817adfaeb06 68 missile_t missile; // player missile
michaeljson 0:3817adfaeb06 69 missile_t enemy_missile; // enemy missile
michaeljson 0:3817adfaeb06 70
michaeljson 0:3817adfaeb06 71 // Array of enemy objects
michaeljson 0:3817adfaeb06 72 enemy_t * enemyArray[15];
michaeljson 0:3817adfaeb06 73
michaeljson 0:3817adfaeb06 74 // Function Prototypes
michaeljson 0:3817adfaeb06 75 void move_enemy_down();
michaeljson 0:3817adfaeb06 76 void playstart(void const *args);
michaeljson 0:3817adfaeb06 77
michaeljson 0:3817adfaeb06 78 // Draws the enemies at the initial starting location
michaeljson 0:3817adfaeb06 79 void draw_enemies_level()
michaeljson 0:3817adfaeb06 80 {
michaeljson 0:3817adfaeb06 81 // Initialize local variables
michaeljson 0:3817adfaeb06 82 unsigned int start_x_pos = 12;
michaeljson 0:3817adfaeb06 83 unsigned int start_enemy_y_pos = 20;
michaeljson 0:3817adfaeb06 84 numOfEnemies = 15;
michaeljson 0:3817adfaeb06 85
michaeljson 0:3817adfaeb06 86 // First Row of Enemies
michaeljson 0:3817adfaeb06 87 enemy_init(&enemy_1,start_x_pos,start_enemy_y_pos,WHITE); // initialize x-pos and y-pos and color of enemy
michaeljson 0:3817adfaeb06 88 enemy_show(&enemy_1); // displays the enemy on uLCD
michaeljson 0:3817adfaeb06 89
michaeljson 0:3817adfaeb06 90 enemy_init(&enemy_2,start_x_pos+15,start_enemy_y_pos,WHITE);
michaeljson 0:3817adfaeb06 91 enemy_show(&enemy_2);
michaeljson 0:3817adfaeb06 92
michaeljson 0:3817adfaeb06 93 enemy_init(&enemy_3,start_x_pos+30,start_enemy_y_pos,WHITE);
michaeljson 0:3817adfaeb06 94 enemy_show(&enemy_3);
michaeljson 0:3817adfaeb06 95
michaeljson 0:3817adfaeb06 96 enemy_init(&enemy_4,start_x_pos+45,start_enemy_y_pos,WHITE);
michaeljson 0:3817adfaeb06 97 enemy_show(&enemy_4);
michaeljson 0:3817adfaeb06 98
michaeljson 0:3817adfaeb06 99 enemy_init(&enemy_5,start_x_pos+60,start_enemy_y_pos,WHITE);
michaeljson 0:3817adfaeb06 100 enemy_show(&enemy_5);
michaeljson 0:3817adfaeb06 101
michaeljson 0:3817adfaeb06 102 // Second Row of Enemies
michaeljson 0:3817adfaeb06 103 enemy_init(&enemy_6,start_x_pos,start_enemy_y_pos+12,WHITE);
michaeljson 0:3817adfaeb06 104 enemy_show(&enemy_6);
michaeljson 0:3817adfaeb06 105
michaeljson 0:3817adfaeb06 106 enemy_init(&enemy_7,start_x_pos+15,start_enemy_y_pos+12,WHITE);
michaeljson 0:3817adfaeb06 107 enemy_show(&enemy_7);
michaeljson 0:3817adfaeb06 108
michaeljson 0:3817adfaeb06 109 enemy_init(&enemy_8,start_x_pos+30,start_enemy_y_pos+12,WHITE);
michaeljson 0:3817adfaeb06 110 enemy_show(&enemy_8);
michaeljson 0:3817adfaeb06 111
michaeljson 0:3817adfaeb06 112 enemy_init(&enemy_9,start_x_pos+45,start_enemy_y_pos+12,WHITE);
michaeljson 0:3817adfaeb06 113 enemy_show(&enemy_9);
michaeljson 0:3817adfaeb06 114
michaeljson 0:3817adfaeb06 115 enemy_init(&enemy_10,start_x_pos+60,start_enemy_y_pos+12,WHITE);
michaeljson 0:3817adfaeb06 116 enemy_show(&enemy_10);
michaeljson 0:3817adfaeb06 117
michaeljson 0:3817adfaeb06 118 // Third Row of Enemies
michaeljson 0:3817adfaeb06 119 enemy_init(&enemy_11,start_x_pos,start_enemy_y_pos+24,WHITE);
michaeljson 0:3817adfaeb06 120 enemy_show(&enemy_11);
michaeljson 0:3817adfaeb06 121
michaeljson 0:3817adfaeb06 122 enemy_init(&enemy_12,start_x_pos+15,start_enemy_y_pos+24,WHITE);
michaeljson 0:3817adfaeb06 123 enemy_show(&enemy_12);
michaeljson 0:3817adfaeb06 124
michaeljson 0:3817adfaeb06 125 enemy_init(&enemy_13,start_x_pos+30,start_enemy_y_pos+24,WHITE);
michaeljson 0:3817adfaeb06 126 enemy_show(&enemy_13);
michaeljson 0:3817adfaeb06 127
michaeljson 0:3817adfaeb06 128 enemy_init(&enemy_14,start_x_pos+45,start_enemy_y_pos+24,WHITE);
michaeljson 0:3817adfaeb06 129 enemy_show(&enemy_14);
michaeljson 0:3817adfaeb06 130
michaeljson 0:3817adfaeb06 131 enemy_init(&enemy_15,start_x_pos+60,start_enemy_y_pos+24,WHITE);
michaeljson 0:3817adfaeb06 132 enemy_show(&enemy_15);
michaeljson 0:3817adfaeb06 133
michaeljson 0:3817adfaeb06 134 // Put enemy objects into array
michaeljson 0:3817adfaeb06 135 enemyArray[0] = &enemy_1;
michaeljson 0:3817adfaeb06 136 enemyArray[1] = &enemy_2;
michaeljson 0:3817adfaeb06 137 enemyArray[2] = &enemy_3;
michaeljson 0:3817adfaeb06 138 enemyArray[3] = &enemy_4;
michaeljson 0:3817adfaeb06 139 enemyArray[4] = &enemy_5;
michaeljson 0:3817adfaeb06 140 enemyArray[5] = &enemy_6;
michaeljson 0:3817adfaeb06 141 enemyArray[6] = &enemy_7;
michaeljson 0:3817adfaeb06 142 enemyArray[7] = &enemy_8;
michaeljson 0:3817adfaeb06 143 enemyArray[8] = &enemy_9;
michaeljson 0:3817adfaeb06 144 enemyArray[9] = &enemy_10;
michaeljson 0:3817adfaeb06 145 enemyArray[10] = &enemy_11;
michaeljson 0:3817adfaeb06 146 enemyArray[11] = &enemy_12;
michaeljson 0:3817adfaeb06 147 enemyArray[12] = &enemy_13;
michaeljson 0:3817adfaeb06 148 enemyArray[13] = &enemy_14;
michaeljson 0:3817adfaeb06 149 enemyArray[14] = &enemy_15;
michaeljson 0:3817adfaeb06 150 }
michaeljson 0:3817adfaeb06 151
michaeljson 0:3817adfaeb06 152 // Draws the player at the initial starting location
michaeljson 0:3817adfaeb06 153 void draw_initial_player()
michaeljson 0:3817adfaeb06 154 {
michaeljson 0:3817adfaeb06 155 int start_x_pos = 59;
michaeljson 0:3817adfaeb06 156 int start_y_pos = 110;
michaeljson 0:3817adfaeb06 157
michaeljson 0:3817adfaeb06 158 player_init(&player,start_x_pos,start_y_pos,WHITE); // intialize x-pos and y-pos and color of player
michaeljson 0:3817adfaeb06 159 player_show(&player); // display player on uLCD
michaeljson 0:3817adfaeb06 160 }
michaeljson 0:3817adfaeb06 161
michaeljson 0:3817adfaeb06 162 // Checks all enemies in row 1
michaeljson 0:3817adfaeb06 163 void check_hit_enemy_row1()
michaeljson 0:3817adfaeb06 164 {
michaeljson 0:3817adfaeb06 165 int hit_enemy;
michaeljson 0:3817adfaeb06 166
michaeljson 0:3817adfaeb06 167 // First Row of Enemies
michaeljson 0:3817adfaeb06 168 hit_enemy = check_enemy(&enemy_1, &missile); // checks if the missile hits the enemy and returns 1 if hit, 0 if not hit
michaeljson 0:3817adfaeb06 169 numOfEnemies = numOfEnemies - hit_enemy; // updates the number of enemies left
michaeljson 0:3817adfaeb06 170 hit_enemy = check_enemy(&enemy_2, &missile);
michaeljson 0:3817adfaeb06 171 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 172 hit_enemy = check_enemy(&enemy_3, &missile);
michaeljson 0:3817adfaeb06 173 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 174 hit_enemy = check_enemy(&enemy_4, &missile);
michaeljson 0:3817adfaeb06 175 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 176 hit_enemy = check_enemy(&enemy_5, &missile);
michaeljson 0:3817adfaeb06 177 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 178 }
michaeljson 0:3817adfaeb06 179
michaeljson 0:3817adfaeb06 180 // Same as check_hit_enemy_row1, but checks enemies at row 2
michaeljson 0:3817adfaeb06 181 void check_hit_enemy_row2()
michaeljson 0:3817adfaeb06 182 {
michaeljson 0:3817adfaeb06 183 int hit_enemy;
michaeljson 0:3817adfaeb06 184
michaeljson 0:3817adfaeb06 185 // Second Row of Enemies
michaeljson 0:3817adfaeb06 186 hit_enemy = check_enemy(&enemy_6, &missile);
michaeljson 0:3817adfaeb06 187 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 188 hit_enemy = check_enemy(&enemy_7, &missile);
michaeljson 0:3817adfaeb06 189 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 190 hit_enemy = check_enemy(&enemy_8, &missile);
michaeljson 0:3817adfaeb06 191 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 192 hit_enemy = check_enemy(&enemy_9, &missile);
michaeljson 0:3817adfaeb06 193 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 194 hit_enemy = check_enemy(&enemy_10, &missile);
michaeljson 0:3817adfaeb06 195 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 196 }
michaeljson 0:3817adfaeb06 197
michaeljson 0:3817adfaeb06 198 // Same as check_hit_enemy_row1, but checks enemies at row 3
michaeljson 0:3817adfaeb06 199 void check_hit_enemy_row3()
michaeljson 0:3817adfaeb06 200 {
michaeljson 0:3817adfaeb06 201 int hit_enemy;
michaeljson 0:3817adfaeb06 202
michaeljson 0:3817adfaeb06 203 // Third Row of Enemies
michaeljson 0:3817adfaeb06 204 hit_enemy = check_enemy(&enemy_11, &missile);
michaeljson 0:3817adfaeb06 205 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 206 hit_enemy = check_enemy(&enemy_12, &missile);
michaeljson 0:3817adfaeb06 207 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 208 hit_enemy = check_enemy(&enemy_13, &missile);
michaeljson 0:3817adfaeb06 209 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 210 hit_enemy = check_enemy(&enemy_14, &missile);
michaeljson 0:3817adfaeb06 211 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 212 hit_enemy = check_enemy(&enemy_15, &missile);
michaeljson 0:3817adfaeb06 213 numOfEnemies = numOfEnemies - hit_enemy;
michaeljson 0:3817adfaeb06 214 }
michaeljson 0:3817adfaeb06 215
michaeljson 0:3817adfaeb06 216 // Checks if player is hit
michaeljson 0:3817adfaeb06 217 void check_player_hit()
michaeljson 0:3817adfaeb06 218 {
michaeljson 0:3817adfaeb06 219 hit_player = check_player(&player, &enemy_missile); // checks if the missile hits the player and returns 1 if hit, 0 if not hit
michaeljson 0:3817adfaeb06 220 }
michaeljson 0:3817adfaeb06 221
michaeljson 0:3817adfaeb06 222 // Randomly selects an enemy to fire and updates the position of where the missile will fire from
michaeljson 0:3817adfaeb06 223 void random_attack_gen()
michaeljson 0:3817adfaeb06 224 {
michaeljson 0:3817adfaeb06 225 firing_col = rand() % 5; // selects a random column
michaeljson 0:3817adfaeb06 226
michaeljson 0:3817adfaeb06 227 // first checks if the 3rd row closest to the player is alive
michaeljson 0:3817adfaeb06 228 if (enemyArray[firing_col+10]->status == ENEMY_ALIVE)
michaeljson 0:3817adfaeb06 229 {
michaeljson 0:3817adfaeb06 230 // If alive, the enemy missile position is updated to the center of the enemy
michaeljson 0:3817adfaeb06 231 enemy_missile.missile_blk_x = enemyArray[firing_col+10]->enemy_blk_x + (enemyArray[firing_col+10]->enemy_width/2);
michaeljson 0:3817adfaeb06 232 enemy_missile.missile_blk_y = enemyArray[firing_col+10]->enemy_blk_y + enemyArray[firing_col+10]->enemy_height + 1;
michaeljson 0:3817adfaeb06 233 enemy_missile.status = ENEMY_MISSILE_ACTIVE; // sets the enemy missile as active
michaeljson 0:3817adfaeb06 234 }
michaeljson 0:3817adfaeb06 235 // if enemy at 3rd row is dead, checks the enemy in the 2nd row
michaeljson 0:3817adfaeb06 236 else if (enemyArray[firing_col+5]->status == ENEMY_ALIVE)
michaeljson 0:3817adfaeb06 237 {
michaeljson 0:3817adfaeb06 238 // If alive, the enemy missile position is updated to the center of the enemy
michaeljson 0:3817adfaeb06 239 enemy_missile.missile_blk_x = enemyArray[firing_col+5]->enemy_blk_x + (enemyArray[firing_col+5]->enemy_width/2);
michaeljson 0:3817adfaeb06 240 enemy_missile.missile_blk_y = enemyArray[firing_col+5]->enemy_blk_y + enemyArray[firing_col+5]->enemy_height + 1;
michaeljson 0:3817adfaeb06 241 enemy_missile.status = ENEMY_MISSILE_ACTIVE;
michaeljson 0:3817adfaeb06 242 }
michaeljson 0:3817adfaeb06 243 // if enemy at 2nd and 3rd row is dead, checks the enemy in the 1st row
michaeljson 0:3817adfaeb06 244 else if (enemyArray[firing_col]->status == ENEMY_ALIVE)
michaeljson 0:3817adfaeb06 245 {
michaeljson 0:3817adfaeb06 246 // If alive, the enemy missile position is updated to the center of the enemy
michaeljson 0:3817adfaeb06 247 enemy_missile.missile_blk_x = enemyArray[firing_col]->enemy_blk_x + (enemyArray[firing_col]->enemy_width/2);
michaeljson 0:3817adfaeb06 248 enemy_missile.missile_blk_y = enemyArray[firing_col]->enemy_blk_y + enemyArray[firing_col]->enemy_height + 1;
michaeljson 0:3817adfaeb06 249 enemy_missile.status = ENEMY_MISSILE_ACTIVE;
michaeljson 0:3817adfaeb06 250 }
michaeljson 0:3817adfaeb06 251 }
michaeljson 0:3817adfaeb06 252
michaeljson 0:3817adfaeb06 253 // moves the enemy
michaeljson 0:3817adfaeb06 254 void enemy_motion()
michaeljson 0:3817adfaeb06 255 {
michaeljson 0:3817adfaeb06 256 // will move the enemy every 6 loops
michaeljson 0:3817adfaeb06 257 if (ENEMY_MOVE % 6 == 0)
michaeljson 0:3817adfaeb06 258 {
michaeljson 0:3817adfaeb06 259 // FIrst Row of Enemies
michaeljson 0:3817adfaeb06 260 MOVE_DOWN = move_enemy(&enemy_1, MOVE_DOWN, DIRECTION); // moves the enemy based on the DIRECTION passed in and also sends global MOVE_DOWN to update in enemy.cpp
michaeljson 0:3817adfaeb06 261 MOVE_DOWN = move_enemy(&enemy_2, MOVE_DOWN, DIRECTION); // MOVE_DOWN will be 1 enemies reach the left or right edge of the screen
michaeljson 0:3817adfaeb06 262 MOVE_DOWN = move_enemy(&enemy_3, MOVE_DOWN, DIRECTION); // MOVE_DOWN will be 2 if enemies reach the player, otherwise 0
michaeljson 0:3817adfaeb06 263 MOVE_DOWN = move_enemy(&enemy_4, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 264 MOVE_DOWN = move_enemy(&enemy_5, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 265
michaeljson 0:3817adfaeb06 266 // Second Row of Enemies
michaeljson 0:3817adfaeb06 267 MOVE_DOWN = move_enemy(&enemy_6, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 268 MOVE_DOWN = move_enemy(&enemy_7, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 269 MOVE_DOWN = move_enemy(&enemy_8, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 270 MOVE_DOWN = move_enemy(&enemy_9, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 271 MOVE_DOWN = move_enemy(&enemy_10, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 272
michaeljson 0:3817adfaeb06 273 // Third Row of Enemies
michaeljson 0:3817adfaeb06 274 MOVE_DOWN = move_enemy(&enemy_11, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 275 MOVE_DOWN = move_enemy(&enemy_12, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 276 MOVE_DOWN = move_enemy(&enemy_13, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 277 MOVE_DOWN = move_enemy(&enemy_14, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 278 MOVE_DOWN = move_enemy(&enemy_15, MOVE_DOWN, DIRECTION);
michaeljson 0:3817adfaeb06 279
michaeljson 0:3817adfaeb06 280 // if MOVE_DOWN is 2, then the enemies have reached the player
michaeljson 0:3817adfaeb06 281 if (MOVE_DOWN == 2)
michaeljson 0:3817adfaeb06 282 {
michaeljson 0:3817adfaeb06 283 lose = true; // set global lose to true to go to gameover screen
michaeljson 0:3817adfaeb06 284 }
michaeljson 0:3817adfaeb06 285
michaeljson 0:3817adfaeb06 286 // if MOVE_DOWN is 1, update the y-pos of the enemies
michaeljson 0:3817adfaeb06 287 if (MOVE_DOWN == 1)
michaeljson 0:3817adfaeb06 288 {
michaeljson 0:3817adfaeb06 289 move_enemy_down(); // updates the y-pos of the enemies
michaeljson 0:3817adfaeb06 290
michaeljson 0:3817adfaeb06 291 // Flips the direction when the enemy moves down
michaeljson 0:3817adfaeb06 292 if (DIRECTION == 1)
michaeljson 0:3817adfaeb06 293 {
michaeljson 0:3817adfaeb06 294 DIRECTION = 2;
michaeljson 0:3817adfaeb06 295 }
michaeljson 0:3817adfaeb06 296 else
michaeljson 0:3817adfaeb06 297 {
michaeljson 0:3817adfaeb06 298 DIRECTION = 1;
michaeljson 0:3817adfaeb06 299 }
michaeljson 0:3817adfaeb06 300 MOVE_DOWN = 0; // sets MOVE_DOWN back to 0 to stop down movement until
michaeljson 0:3817adfaeb06 301 }
michaeljson 0:3817adfaeb06 302
michaeljson 0:3817adfaeb06 303 ENEMY_MOVE += 1;
michaeljson 0:3817adfaeb06 304 }
michaeljson 0:3817adfaeb06 305 else
michaeljson 0:3817adfaeb06 306 {
michaeljson 0:3817adfaeb06 307 ENEMY_MOVE += 1;
michaeljson 0:3817adfaeb06 308 }
michaeljson 0:3817adfaeb06 309 }
michaeljson 0:3817adfaeb06 310
michaeljson 0:3817adfaeb06 311 // moves all the enemies down in the y-direction
michaeljson 0:3817adfaeb06 312 void move_enemy_down()
michaeljson 0:3817adfaeb06 313 {
michaeljson 0:3817adfaeb06 314 // First Row of Enemies
michaeljson 0:3817adfaeb06 315 enemy_erase(&enemy_1);
michaeljson 0:3817adfaeb06 316 enemy_erase(&enemy_2);
michaeljson 0:3817adfaeb06 317 enemy_erase(&enemy_3);
michaeljson 0:3817adfaeb06 318 enemy_erase(&enemy_4);
michaeljson 0:3817adfaeb06 319 enemy_erase(&enemy_5);
michaeljson 0:3817adfaeb06 320
michaeljson 0:3817adfaeb06 321 enemy_1.enemy_blk_y += enemy_1.enemy_height+4;
michaeljson 0:3817adfaeb06 322 enemy_2.enemy_blk_y += enemy_2.enemy_height+4;
michaeljson 0:3817adfaeb06 323 enemy_3.enemy_blk_y += enemy_3.enemy_height+4;
michaeljson 0:3817adfaeb06 324 enemy_4.enemy_blk_y += enemy_4.enemy_height+4;
michaeljson 0:3817adfaeb06 325 enemy_5.enemy_blk_y += enemy_5.enemy_height+4;
michaeljson 0:3817adfaeb06 326
michaeljson 0:3817adfaeb06 327 // Second Row of Enemies
michaeljson 0:3817adfaeb06 328 enemy_erase(&enemy_6);
michaeljson 0:3817adfaeb06 329 enemy_erase(&enemy_7);
michaeljson 0:3817adfaeb06 330 enemy_erase(&enemy_8);
michaeljson 0:3817adfaeb06 331 enemy_erase(&enemy_9);
michaeljson 0:3817adfaeb06 332 enemy_erase(&enemy_10);
michaeljson 0:3817adfaeb06 333
michaeljson 0:3817adfaeb06 334 enemy_6.enemy_blk_y += enemy_6.enemy_height+4;
michaeljson 0:3817adfaeb06 335 enemy_7.enemy_blk_y += enemy_7.enemy_height+4;
michaeljson 0:3817adfaeb06 336 enemy_8.enemy_blk_y += enemy_8.enemy_height+4;
michaeljson 0:3817adfaeb06 337 enemy_9.enemy_blk_y += enemy_9.enemy_height+4;
michaeljson 0:3817adfaeb06 338 enemy_10.enemy_blk_y += enemy_10.enemy_height+4;
michaeljson 0:3817adfaeb06 339
michaeljson 0:3817adfaeb06 340 // Third Row of Enemies
michaeljson 0:3817adfaeb06 341 enemy_erase(&enemy_11);
michaeljson 0:3817adfaeb06 342 enemy_erase(&enemy_12);
michaeljson 0:3817adfaeb06 343 enemy_erase(&enemy_13);
michaeljson 0:3817adfaeb06 344 enemy_erase(&enemy_14);
michaeljson 0:3817adfaeb06 345 enemy_erase(&enemy_15);
michaeljson 0:3817adfaeb06 346
michaeljson 0:3817adfaeb06 347 enemy_11.enemy_blk_y += enemy_11.enemy_height+4;
michaeljson 0:3817adfaeb06 348 enemy_12.enemy_blk_y += enemy_12.enemy_height+4;
michaeljson 0:3817adfaeb06 349 enemy_13.enemy_blk_y += enemy_13.enemy_height+4;
michaeljson 0:3817adfaeb06 350 enemy_14.enemy_blk_y += enemy_14.enemy_height+4;
michaeljson 0:3817adfaeb06 351 enemy_15.enemy_blk_y += enemy_15.enemy_height+4;
michaeljson 0:3817adfaeb06 352 }
Tito96 2:ade842cb48e9 353 /*
michaeljson 0:3817adfaeb06 354 // thread that plays sounds during game
michaeljson 0:3817adfaeb06 355 void playstart(void const *args)//Th
michaeljson 0:3817adfaeb06 356 { //Depending on the state of the game,
michaeljson 0:3817adfaeb06 357 //queue either screen music or play sound effect upon fire
michaeljson 0:3817adfaeb06 358 while(true) {
michaeljson 0:3817adfaeb06 359 FILE *wave_file;
michaeljson 0:3817adfaeb06 360
michaeljson 0:3817adfaeb06 361 // plays intro music during menu screen
michaeljson 0:3817adfaeb06 362 while(game_menu)
michaeljson 0:3817adfaeb06 363 {
michaeljson 0:3817adfaeb06 364 wave_file=fopen("/sd/wavfiles/spacey_sound.wav","r");
michaeljson 0:3817adfaeb06 365
michaeljson 0:3817adfaeb06 366 waver.play(wave_file);
michaeljson 0:3817adfaeb06 367 fclose(wave_file);
michaeljson 0:3817adfaeb06 368 }
michaeljson 0:3817adfaeb06 369
michaeljson 0:3817adfaeb06 370 // Checks in game sound conditions
michaeljson 0:3817adfaeb06 371 while(begin_game)
michaeljson 0:3817adfaeb06 372 {
michaeljson 0:3817adfaeb06 373 // play firing sound when the player fires
michaeljson 0:3817adfaeb06 374 if(!pb && missile.status == PLAYER_MISSILE_INACTIVE) {
michaeljson 0:3817adfaeb06 375
michaeljson 0:3817adfaeb06 376 wave_file=fopen("/sd/wavfiles/4.wav","r");
michaeljson 0:3817adfaeb06 377
michaeljson 0:3817adfaeb06 378
michaeljson 0:3817adfaeb06 379 waver.play(wave_file);
michaeljson 0:3817adfaeb06 380 fclose(wave_file);
michaeljson 0:3817adfaeb06 381 }
michaeljson 0:3817adfaeb06 382
michaeljson 0:3817adfaeb06 383 // if player hit, play hit sound
michaeljson 0:3817adfaeb06 384 if (hit_player)
michaeljson 0:3817adfaeb06 385 {
michaeljson 0:3817adfaeb06 386 wave_file=fopen("/sd/wavfiles/1.wav","r");
michaeljson 0:3817adfaeb06 387
michaeljson 0:3817adfaeb06 388 waver.play(wave_file);
michaeljson 0:3817adfaeb06 389 fclose(wave_file);
michaeljson 0:3817adfaeb06 390 }
michaeljson 0:3817adfaeb06 391 }
michaeljson 0:3817adfaeb06 392
michaeljson 0:3817adfaeb06 393 // players gameover voice if player loses
michaeljson 0:3817adfaeb06 394 while(gameover)
michaeljson 0:3817adfaeb06 395 {
michaeljson 0:3817adfaeb06 396 wave_file=fopen("/sd/wavfiles/game_over.wav","r");
michaeljson 0:3817adfaeb06 397
michaeljson 0:3817adfaeb06 398 waver.play(wave_file);
michaeljson 0:3817adfaeb06 399 fclose(wave_file);
michaeljson 0:3817adfaeb06 400 }
michaeljson 0:3817adfaeb06 401 }
michaeljson 0:3817adfaeb06 402 }
Tito96 2:ade842cb48e9 403 */
Tito96 2:ade842cb48e9 404
Tito96 2:ade842cb48e9 405 void ledEffects(void const *args)//Th
Tito96 2:ade842cb48e9 406 { //Depending on the state of the game,
Tito96 2:ade842cb48e9 407 //generate different patterns/colors of lighting
Tito96 2:ade842cb48e9 408 while(1) {
Tito96 2:ade842cb48e9 409 // gradually increases and decreases each color independently. (A chill build up effect?)
Tito96 2:ade842cb48e9 410 while(game_menu)
Tito96 2:ade842cb48e9 411 {
Tito96 2:ade842cb48e9 412 red = 0.0;
Tito96 2:ade842cb48e9 413 green = 0.0;
Tito96 2:ade842cb48e9 414 blue = 0.0;
Tito96 2:ade842cb48e9 415 for (float i = 0.0; i < 0.5; i = i + 0.05) {
Tito96 2:ade842cb48e9 416 red = i;
Tito96 2:ade842cb48e9 417 Thread::wait(10);
Tito96 2:ade842cb48e9 418 }
Tito96 2:ade842cb48e9 419 red = 0.5;
Tito96 2:ade842cb48e9 420 Thread::wait(300);
Tito96 2:ade842cb48e9 421 for (float i = 0.5; i > 0.0; i = i - 0.05) {
Tito96 2:ade842cb48e9 422 red = i;
Tito96 2:ade842cb48e9 423 Thread::wait(10);
Tito96 2:ade842cb48e9 424 }
Tito96 2:ade842cb48e9 425 red = 0.0;
Tito96 2:ade842cb48e9 426 for (float i = 0.0; i < 0.25; i = i + 0.05) {
Tito96 2:ade842cb48e9 427 green = i;
Tito96 2:ade842cb48e9 428 Thread::wait(10);
Tito96 2:ade842cb48e9 429 }
Tito96 2:ade842cb48e9 430 green = 0.25;
Tito96 2:ade842cb48e9 431 Thread::wait(300);
Tito96 2:ade842cb48e9 432 for (float i = 0.25; i > 0.0; i = i - 0.05) {
Tito96 2:ade842cb48e9 433 green = i;
Tito96 2:ade842cb48e9 434 Thread::wait(10);
Tito96 2:ade842cb48e9 435 }
Tito96 2:ade842cb48e9 436 green = 0.0;
Tito96 2:ade842cb48e9 437 for (float i = 0.0; i < 0.5; i = i + 0.05) {
Tito96 2:ade842cb48e9 438 blue = i;
Tito96 2:ade842cb48e9 439 Thread::wait(10);
Tito96 2:ade842cb48e9 440 }
Tito96 2:ade842cb48e9 441 blue = 0.5;
Tito96 2:ade842cb48e9 442 Thread::wait(300);
Tito96 2:ade842cb48e9 443 for (float i = 0.5; i > 0.0; i = i - 0.05) {
Tito96 2:ade842cb48e9 444 blue = i;
Tito96 2:ade842cb48e9 445 Thread::wait(10);
Tito96 2:ade842cb48e9 446 }
Tito96 2:ade842cb48e9 447 blue = 0.0;
Tito96 2:ade842cb48e9 448 }
Tito96 2:ade842cb48e9 449 // Checks in game sound conditions
Tito96 2:ade842cb48e9 450 while(begin_game) // added OR begin_game2 so that music/sounds play during one-player or two-player
Tito96 2:ade842cb48e9 451 {
Tito96 2:ade842cb48e9 452 // play firing sound when the player fires
Tito96 2:ade842cb48e9 453 if(!pb && missile.status == PLAYER_MISSILE_INACTIVE) {
Tito96 2:ade842cb48e9 454 red = 0.0;
Tito96 2:ade842cb48e9 455 green = 0.0;
Tito96 2:ade842cb48e9 456 blue = 0.0;
Tito96 2:ade842cb48e9 457 red = 0.5;
Tito96 2:ade842cb48e9 458 Thread::wait(200);
Tito96 2:ade842cb48e9 459 green = 0.15;
Tito96 2:ade842cb48e9 460 Thread::wait(200);
Tito96 2:ade842cb48e9 461 red = 0.0;
Tito96 2:ade842cb48e9 462 green = 0.0;
Tito96 2:ade842cb48e9 463 }
Tito96 2:ade842cb48e9 464
Tito96 2:ade842cb48e9 465 // if player hit, play hit sound
Tito96 2:ade842cb48e9 466 if (hit_player)
Tito96 2:ade842cb48e9 467 {
Tito96 2:ade842cb48e9 468 red = 0.0;
Tito96 2:ade842cb48e9 469 green = 0.0;
Tito96 2:ade842cb48e9 470 blue = 0.0;
Tito96 2:ade842cb48e9 471 red = 0.5;
Tito96 2:ade842cb48e9 472 Thread::wait(60);
Tito96 2:ade842cb48e9 473 red = 0.0;
Tito96 2:ade842cb48e9 474 Thread::wait(60);
Tito96 2:ade842cb48e9 475 red = 0.5;
Tito96 2:ade842cb48e9 476 Thread::wait(60);
Tito96 2:ade842cb48e9 477 red = 0.0;
Tito96 2:ade842cb48e9 478 Thread::wait(60);
Tito96 2:ade842cb48e9 479 red = 0.5;
Tito96 2:ade842cb48e9 480 Thread::wait(60);
Tito96 2:ade842cb48e9 481 red = 0.0;
Tito96 2:ade842cb48e9 482 }
Tito96 2:ade842cb48e9 483 Thread::wait(500);
Tito96 2:ade842cb48e9 484 }
Tito96 2:ade842cb48e9 485
Tito96 2:ade842cb48e9 486 // players gameover voice if player loses
Tito96 2:ade842cb48e9 487 while(gameover)
Tito96 2:ade842cb48e9 488 {
Tito96 2:ade842cb48e9 489 for (float i = 0.0; i < 0.25; i = i + 0.05) {
Tito96 2:ade842cb48e9 490 red = i;
Tito96 2:ade842cb48e9 491 Thread::wait(10);
Tito96 2:ade842cb48e9 492 }
Tito96 2:ade842cb48e9 493 red = 0.25;
Tito96 2:ade842cb48e9 494 Thread::wait(300);
Tito96 2:ade842cb48e9 495 for (float i = 0.25; i > 0.0; i = i - 0.05) {
Tito96 2:ade842cb48e9 496 red = i;
Tito96 2:ade842cb48e9 497 Thread::wait(10);
Tito96 2:ade842cb48e9 498 }
Tito96 2:ade842cb48e9 499 red = 0.0;
Tito96 2:ade842cb48e9 500 Thread::wait(500);
Tito96 2:ade842cb48e9 501 }
Tito96 2:ade842cb48e9 502 }
Tito96 2:ade842cb48e9 503 }
Tito96 2:ade842cb48e9 504
Tito96 2:ade842cb48e9 505
Tito96 2:ade842cb48e9 506
michaeljson 0:3817adfaeb06 507
michaeljson 0:3817adfaeb06 508 int main() {
Tito96 2:ade842cb48e9 509 LSM9DS1 lol(p9, p10, 0xD6, 0x3C);
michaeljson 0:3817adfaeb06 510 // Initialization of Setup variables
michaeljson 0:3817adfaeb06 511 int blk_x, blk_y;
michaeljson 0:3817adfaeb06 512 pb.mode(PullUp);
Tito96 2:ade842cb48e9 513 lol.begin();
Tito96 2:ade842cb48e9 514 if (!lol.begin()) {
Tito96 2:ade842cb48e9 515 pc.printf("Failed to communicate with LSM9DS1.\n");
Tito96 2:ade842cb48e9 516 }
Tito96 2:ade842cb48e9 517 lol.calibrate();
Tito96 2:ade842cb48e9 518 // Thread thread(playstart); // intializes the thread to play sound
Tito96 2:ade842cb48e9 519 Thread thread2(ledEffects);
michaeljson 0:3817adfaeb06 520 uLCD.baudrate(500000); // set to 500000 to increase smooth gameplay
michaeljson 0:3817adfaeb06 521
Tito96 2:ade842cb48e9 522
michaeljson 0:3817adfaeb06 523 // Initialization of Game Menu variables
Tito96 2:ade842cb48e9 524
michaeljson 0:3817adfaeb06 525 const int title_x_pos = 2; // initial x-pos of title
michaeljson 0:3817adfaeb06 526 const int title_y_pos = 3; // initial y-pos of title
michaeljson 0:3817adfaeb06 527 int start_label_x_pos = 7; // start label x-pos
michaeljson 0:3817adfaeb06 528 int start_label_y_pos = 7; // start label y-pos
michaeljson 0:3817adfaeb06 529 int level_cursor_x_pos = 5; // level cursor x-position
michaeljson 0:3817adfaeb06 530 int level_cursor_y_pos = 7; // level cursor y-position
michaeljson 0:3817adfaeb06 531 int gameover_x_pos = 5; // gameover label x-position
michaeljson 0:3817adfaeb06 532 int gameover_y_pos = 5; // gameover label y-position
michaeljson 0:3817adfaeb06 533 int win_x_pos = 2; // congratulations label x-position
michaeljson 0:3817adfaeb06 534 int win_y_pos = 5; // congratulations label y-position
michaeljson 0:3817adfaeb06 535 int startover_x_pos = 3; // startover label x-position
michaeljson 0:3817adfaeb06 536 int startover_y_pos = 8; // startover label y-position
Tito96 2:ade842cb48e9 537 int level_cursor_y_pos_end = 11; // BOTTOM CURSOR POS -- Added by Brice for more menu options
Tito96 2:ade842cb48e9 538 int level_cursor_y_pos_start = 7; //--Added by Brice
michaeljson 0:3817adfaeb06 539
michaeljson 0:3817adfaeb06 540 // intialize temporary score and current score
michaeljson 0:3817adfaeb06 541 int temp = 0;
michaeljson 0:3817adfaeb06 542 int score = 0;
Tito96 2:ade842cb48e9 543
Tito96 2:ade842cb48e9 544 uLCD.cls();
michaeljson 0:3817adfaeb06 545 // Begin game loop
michaeljson 0:3817adfaeb06 546 while(1)
michaeljson 0:3817adfaeb06 547 {
michaeljson 0:3817adfaeb06 548 game_menu = true; // defaults to display menu screen
michaeljson 0:3817adfaeb06 549 ENEMY_MOVE = true; // defaults to move enemy
michaeljson 0:3817adfaeb06 550 DIRECTION = 1; // default to move right
michaeljson 0:3817adfaeb06 551 hit_player = 0; // default to not player hit
michaeljson 0:3817adfaeb06 552 MOVE_DOWN = 0; // default to not move down
michaeljson 0:3817adfaeb06 553 lose = false; // default to not lose
Tito96 2:ade842cb48e9 554 lives1 = 3; // defaults to 3 lives
Tito96 2:ade842cb48e9 555 lives2 = 2;
Tito96 2:ade842cb48e9 556 lives3 = 1;
michaeljson 0:3817adfaeb06 557 score = 0; // default to score of 0
michaeljson 0:3817adfaeb06 558
michaeljson 0:3817adfaeb06 559 uLCD.cls();
Tito96 2:ade842cb48e9 560 while(!lol.accelAvailable()) {
Tito96 2:ade842cb48e9 561 pos = 0;
Tito96 2:ade842cb48e9 562 }
Tito96 2:ade842cb48e9 563 lol.readAccel();
Tito96 2:ade842cb48e9 564
Tito96 2:ade842cb48e9 565 inX = lol.calcAccel(lol.ax);
Tito96 2:ade842cb48e9 566 inY = lol.calcAccel(lol.ay);
Tito96 2:ade842cb48e9 567 inZ = lol.calcAccel(lol.az);
Tito96 2:ade842cb48e9 568
Tito96 2:ade842cb48e9 569 // pc.printf("gyro: %d %d %d\n\r", lol.gx, imu.gy, imu.gz);
Tito96 2:ade842cb48e9 570 pc.printf("accel: %d %d %d\n\r", inX, inY, inZ);
Tito96 2:ade842cb48e9 571 //pc.printf("mag: %d %d %d\n\n\r", imu.mx, imu.my, imu.mz);
Tito96 2:ade842cb48e9 572 if (inY >= .5)
Tito96 2:ade842cb48e9 573 {
Tito96 2:ade842cb48e9 574 pc.printf("up\n\r");
Tito96 2:ade842cb48e9 575 pos = 1;
Tito96 2:ade842cb48e9 576 // wireless.sendDirection(DIR_LEFT);
Tito96 2:ade842cb48e9 577 }
Tito96 2:ade842cb48e9 578 else if( inY <= -.5)
Tito96 2:ade842cb48e9 579 {
Tito96 2:ade842cb48e9 580 pc.printf("down\n\r");
Tito96 2:ade842cb48e9 581 pos = 2;
Tito96 2:ade842cb48e9 582 //wireless.sendDirection(DIR_RIGHT);
Tito96 2:ade842cb48e9 583 }
Tito96 2:ade842cb48e9 584 else if (inX >= .5)
Tito96 2:ade842cb48e9 585 {
Tito96 2:ade842cb48e9 586 pc.printf("right\n\r");
Tito96 2:ade842cb48e9 587 pos = 3;
Tito96 2:ade842cb48e9 588 // wireless.sendDirection(DIR_UP);
Tito96 2:ade842cb48e9 589 }
Tito96 2:ade842cb48e9 590 else if (inX <= -.5)
Tito96 2:ade842cb48e9 591 {
Tito96 2:ade842cb48e9 592 pc.printf("left\n\r");
Tito96 2:ade842cb48e9 593 pos = 4;
Tito96 2:ade842cb48e9 594 //wireless.sendDirection(DIR_DOWN);
Tito96 2:ade842cb48e9 595 }
Tito96 2:ade842cb48e9 596 else
Tito96 2:ade842cb48e9 597 {
Tito96 2:ade842cb48e9 598 pc.printf("error");
Tito96 2:ade842cb48e9 599 pos = 0;
Tito96 2:ade842cb48e9 600 // wireless.sendDirection(DIR_NONE);
Tito96 2:ade842cb48e9 601 }
Tito96 2:ade842cb48e9 602 wait(0.5);
michaeljson 0:3817adfaeb06 603
michaeljson 0:3817adfaeb06 604 // Implementation of Game Menu
Tito96 2:ade842cb48e9 605 // Implementation of Game Menu
michaeljson 0:3817adfaeb06 606 while(game_menu)
michaeljson 0:3817adfaeb06 607 {
Tito96 2:ade842cb48e9 608 // Brice added this in order to move the cursor through the menu options
Tito96 2:ade842cb48e9 609 uLCD.locate(level_cursor_x_pos, level_cursor_y_pos);
Tito96 2:ade842cb48e9 610 uLCD.printf(" ");
Tito96 2:ade842cb48e9 611 if (pos == 2 && level_cursor_y_pos < level_cursor_y_pos_end) {
Tito96 2:ade842cb48e9 612 level_cursor_y_pos += 2;
Tito96 2:ade842cb48e9 613 } else if (pos == 1 && level_cursor_y_pos > level_cursor_y_pos_start) {
Tito96 2:ade842cb48e9 614 level_cursor_y_pos -= 2;
Tito96 2:ade842cb48e9 615 }
Tito96 2:ade842cb48e9 616
michaeljson 0:3817adfaeb06 617 uLCD.locate(level_cursor_x_pos,level_cursor_y_pos); // draws cursor next to "START" label
michaeljson 0:3817adfaeb06 618 uLCD.printf("->");
michaeljson 0:3817adfaeb06 619
michaeljson 0:3817adfaeb06 620 uLCD.locate(title_x_pos,title_y_pos); // "SPACE INVADERS" title position
michaeljson 0:3817adfaeb06 621 uLCD.printf("SPACE INVADERS"); // Title
michaeljson 0:3817adfaeb06 622
michaeljson 0:3817adfaeb06 623 uLCD.locate(start_label_x_pos,start_label_y_pos); // "START" label position
Tito96 2:ade842cb48e9 624 uLCD.printf("LEVEL 1");
Tito96 2:ade842cb48e9 625
Tito96 2:ade842cb48e9 626 uLCD.locate(start_label_x_pos,start_label_y_pos + 2); // "START" label position
Tito96 2:ade842cb48e9 627 uLCD.printf("LEVEL 2");
Tito96 2:ade842cb48e9 628
Tito96 2:ade842cb48e9 629 uLCD.locate(start_label_x_pos,start_label_y_pos + 4); // "START" label position
Tito96 2:ade842cb48e9 630 uLCD.printf("LEVEL 3");
michaeljson 0:3817adfaeb06 631
michaeljson 0:3817adfaeb06 632 // if pushbutton is pressed, game menu is exited and game begins
michaeljson 0:3817adfaeb06 633 if(!pb)
michaeljson 0:3817adfaeb06 634 {
michaeljson 0:3817adfaeb06 635 game_menu = false;
Tito96 2:ade842cb48e9 636 if (level_cursor_y_pos == start_label_y_pos) {
Tito96 2:ade842cb48e9 637 level = 1;
Tito96 2:ade842cb48e9 638 } else if (level_cursor_y_pos == start_label_y_pos + 2) {
Tito96 2:ade842cb48e9 639 level = 2;
Tito96 2:ade842cb48e9 640 // pc.printf("num players: 2");
Tito96 2:ade842cb48e9 641 } else if (level_cursor_y_pos == start_label_y_pos + 4){
Tito96 2:ade842cb48e9 642 level = 3;
Tito96 2:ade842cb48e9 643 }
Tito96 2:ade842cb48e9 644
Tito96 2:ade842cb48e9 645
michaeljson 0:3817adfaeb06 646 wait(0.5);
Tito96 2:ade842cb48e9 647
michaeljson 0:3817adfaeb06 648 }
michaeljson 0:3817adfaeb06 649 }
michaeljson 0:3817adfaeb06 650
Tito96 2:ade842cb48e9 651
michaeljson 0:3817adfaeb06 652 begin_game = true; // defaults begin_game to true
michaeljson 0:3817adfaeb06 653
michaeljson 0:3817adfaeb06 654 uLCD.cls();
michaeljson 0:3817adfaeb06 655
michaeljson 0:3817adfaeb06 656 // Draw the enemies
michaeljson 0:3817adfaeb06 657 draw_enemies_level();
michaeljson 0:3817adfaeb06 658
michaeljson 0:3817adfaeb06 659 // Draw the player
michaeljson 0:3817adfaeb06 660 draw_initial_player();
michaeljson 0:3817adfaeb06 661
michaeljson 0:3817adfaeb06 662 // sets the initial position of player missile and enemy missile (later updated)
michaeljson 0:3817adfaeb06 663 blk_x = player.player_blk_x+(player.player_width/2);
michaeljson 0:3817adfaeb06 664 blk_y = player.player_blk_y;
michaeljson 0:3817adfaeb06 665 missile_init(&missile, blk_x, blk_y, WHITE);
michaeljson 1:618aa2c4ca6a 666 int e_blk_x = 0;
michaeljson 1:618aa2c4ca6a 667 int e_blk_y = 2;
michaeljson 1:618aa2c4ca6a 668 enemy_missile_init(&enemy_missile, e_blk_x, e_blk_y, WHITE);
Tito96 2:ade842cb48e9 669
michaeljson 0:3817adfaeb06 670 // prints lives
Tito96 2:ade842cb48e9 671 if (level == 1){
michaeljson 0:3817adfaeb06 672 uLCD.locate(0,0);
Tito96 2:ade842cb48e9 673 uLCD.printf("Lives:%i", 3);
Tito96 2:ade842cb48e9 674 }
Tito96 2:ade842cb48e9 675
Tito96 2:ade842cb48e9 676 else if (level == 2){
Tito96 2:ade842cb48e9 677 uLCD.locate(0,0);
Tito96 2:ade842cb48e9 678 uLCD.printf("Lives:%i", 2);
Tito96 2:ade842cb48e9 679 }
michaeljson 0:3817adfaeb06 680
Tito96 2:ade842cb48e9 681 else if (level == 3){
Tito96 2:ade842cb48e9 682 uLCD.locate(0,0);
Tito96 2:ade842cb48e9 683 uLCD.printf("Lives:%i", 1);
Tito96 2:ade842cb48e9 684 }
michaeljson 0:3817adfaeb06 685 // prints score
michaeljson 0:3817adfaeb06 686 uLCD.locate(9,0);
michaeljson 0:3817adfaeb06 687 uLCD.printf("Score:%i", score);
michaeljson 0:3817adfaeb06 688
michaeljson 0:3817adfaeb06 689 // game play loop
michaeljson 0:3817adfaeb06 690 while(begin_game)
michaeljson 0:3817adfaeb06 691 {
michaeljson 0:3817adfaeb06 692 // updates score
michaeljson 0:3817adfaeb06 693 temp = score;
michaeljson 0:3817adfaeb06 694 score = (15-numOfEnemies)*15;
michaeljson 0:3817adfaeb06 695
michaeljson 0:3817adfaeb06 696 // prints score if score changes
michaeljson 0:3817adfaeb06 697 if (score != temp)
michaeljson 0:3817adfaeb06 698 {
michaeljson 0:3817adfaeb06 699 uLCD.locate(9,0);
michaeljson 0:3817adfaeb06 700 uLCD.printf("Score:%i", score);
michaeljson 0:3817adfaeb06 701 }
michaeljson 0:3817adfaeb06 702
michaeljson 0:3817adfaeb06 703 // move enemy
michaeljson 0:3817adfaeb06 704 enemy_motion();
michaeljson 0:3817adfaeb06 705
michaeljson 0:3817adfaeb06 706 // checks if player missile passes y-pos of row1
michaeljson 0:3817adfaeb06 707 if (missile.missile_blk_y+1-missile.missile_height <= enemy_1.enemy_blk_y
michaeljson 0:3817adfaeb06 708 && missile.missile_blk_y+1-missile.missile_height >= enemy_1.enemy_blk_y-enemy_1.enemy_height)
michaeljson 0:3817adfaeb06 709 {
michaeljson 0:3817adfaeb06 710 check_hit_enemy_row1();
michaeljson 0:3817adfaeb06 711 }
michaeljson 0:3817adfaeb06 712
michaeljson 0:3817adfaeb06 713 // checks if player missile passes y-pos of row2
michaeljson 0:3817adfaeb06 714 if (missile.missile_blk_y+1-missile.missile_height <= enemy_6.enemy_blk_y
michaeljson 0:3817adfaeb06 715 && missile.missile_blk_y+1-missile.missile_height >= enemy_6.enemy_blk_y-enemy_6.enemy_height)
michaeljson 0:3817adfaeb06 716 {
michaeljson 0:3817adfaeb06 717 check_hit_enemy_row2();
michaeljson 0:3817adfaeb06 718 }
michaeljson 0:3817adfaeb06 719
michaeljson 0:3817adfaeb06 720 // checks if player missile passes y-pos of row3
michaeljson 0:3817adfaeb06 721 if (missile.missile_blk_y+1-missile.missile_height <= enemy_11.enemy_blk_y
michaeljson 0:3817adfaeb06 722 && missile.missile_blk_y+1-missile.missile_height >= enemy_11.enemy_blk_y-enemy_11.enemy_height)
michaeljson 0:3817adfaeb06 723 {
michaeljson 0:3817adfaeb06 724 check_hit_enemy_row3();
michaeljson 0:3817adfaeb06 725 }
michaeljson 0:3817adfaeb06 726
michaeljson 0:3817adfaeb06 727 // Random Enemy Fire
michaeljson 0:3817adfaeb06 728 if (enemy_missile.status == ENEMY_MISSILE_INACTIVE)
michaeljson 0:3817adfaeb06 729 {
michaeljson 0:3817adfaeb06 730 random_attack_gen();
michaeljson 0:3817adfaeb06 731 }
michaeljson 0:3817adfaeb06 732
michaeljson 0:3817adfaeb06 733 // checks if enemy missile passes y-pos of player
michaeljson 0:3817adfaeb06 734 if (enemy_missile.missile_blk_y >= player.player_blk_y
michaeljson 0:3817adfaeb06 735 && enemy_missile.missile_blk_y <= player.player_blk_y+player.player_height)
michaeljson 0:3817adfaeb06 736 {
michaeljson 0:3817adfaeb06 737 check_player_hit();
michaeljson 0:3817adfaeb06 738 }
michaeljson 0:3817adfaeb06 739
michaeljson 0:3817adfaeb06 740 update_missile_pos(&missile); // updates player missile position
michaeljson 0:3817adfaeb06 741 update_enemy_missile_pos(&enemy_missile); // updates enemy missile position
michaeljson 0:3817adfaeb06 742
michaeljson 0:3817adfaeb06 743 // Player Movement checked with navigation switch
Tito96 2:ade842cb48e9 744 if (pos == 4 && ((player.player_blk_x-3) > 0))
michaeljson 0:3817adfaeb06 745 {
michaeljson 0:3817adfaeb06 746 player_erase(&player);
michaeljson 0:3817adfaeb06 747 player.player_blk_x -= 3;
michaeljson 0:3817adfaeb06 748 player_show(&player);
michaeljson 0:3817adfaeb06 749 }
Tito96 2:ade842cb48e9 750 else if (pos == 3 && ((player.player_blk_x+3) < (128-player.player_width)))
michaeljson 0:3817adfaeb06 751 {
michaeljson 0:3817adfaeb06 752 player_erase(&player);
michaeljson 0:3817adfaeb06 753 player.player_blk_x += 3;
michaeljson 0:3817adfaeb06 754 player_show(&player);
michaeljson 0:3817adfaeb06 755 }
michaeljson 0:3817adfaeb06 756
michaeljson 0:3817adfaeb06 757 // Player Fire
michaeljson 0:3817adfaeb06 758 if (pb == 0 && missile.status == PLAYER_MISSILE_INACTIVE)
michaeljson 0:3817adfaeb06 759 {
michaeljson 0:3817adfaeb06 760 missile.missile_blk_x = player.player_blk_x+(player.player_width/2);
michaeljson 0:3817adfaeb06 761 missile.missile_blk_y = player.player_blk_y;
michaeljson 0:3817adfaeb06 762 missile.status = PLAYER_MISSILE_ACTIVE;
michaeljson 0:3817adfaeb06 763 }
michaeljson 0:3817adfaeb06 764
michaeljson 0:3817adfaeb06 765 // checks if player destroyed all enemies
michaeljson 0:3817adfaeb06 766 if (numOfEnemies == 0)
michaeljson 0:3817adfaeb06 767 {
michaeljson 0:3817adfaeb06 768 uLCD.cls();
michaeljson 0:3817adfaeb06 769
michaeljson 0:3817adfaeb06 770 bool win = true; // sets win to true, for win screen
michaeljson 0:3817adfaeb06 771 begin_game = false;
michaeljson 0:3817adfaeb06 772
michaeljson 0:3817adfaeb06 773 // displays video clip
michaeljson 0:3817adfaeb06 774 uLCD.cls();
michaeljson 0:3817adfaeb06 775 uLCD.media_init();
michaeljson 0:3817adfaeb06 776 uLCD.set_sector_address(0x00, 0x00);
michaeljson 0:3817adfaeb06 777 uLCD.display_video(0,0);
michaeljson 0:3817adfaeb06 778 wait(1);
michaeljson 0:3817adfaeb06 779
michaeljson 0:3817adfaeb06 780 uLCD.cls();
michaeljson 0:3817adfaeb06 781
michaeljson 0:3817adfaeb06 782 // prints "Congratulations" on uLCD
michaeljson 0:3817adfaeb06 783 uLCD.locate(win_x_pos,win_y_pos);
michaeljson 0:3817adfaeb06 784 uLCD.printf("CONGRATULATIONS!");
michaeljson 0:3817adfaeb06 785
michaeljson 0:3817adfaeb06 786 // prints "Play Again?" and "Press pb..."
michaeljson 0:3817adfaeb06 787 uLCD.locate(startover_x_pos, startover_y_pos);
michaeljson 0:3817adfaeb06 788 uLCD.printf("Play Again?");
michaeljson 0:3817adfaeb06 789 uLCD.locate(startover_x_pos, startover_y_pos+1);
michaeljson 0:3817adfaeb06 790 uLCD.printf("Press pb...");
michaeljson 0:3817adfaeb06 791
michaeljson 0:3817adfaeb06 792 // waits at win screen until pushbutton is pressed
michaeljson 0:3817adfaeb06 793 while (win)
michaeljson 0:3817adfaeb06 794 {
michaeljson 0:3817adfaeb06 795 // if pb is pressed, reset game to start menu
michaeljson 0:3817adfaeb06 796 if (!pb)
michaeljson 0:3817adfaeb06 797 {
michaeljson 0:3817adfaeb06 798 win = false;
michaeljson 0:3817adfaeb06 799 wait(0.5);
michaeljson 0:3817adfaeb06 800 }
michaeljson 0:3817adfaeb06 801 }
michaeljson 0:3817adfaeb06 802
michaeljson 0:3817adfaeb06 803 }
michaeljson 0:3817adfaeb06 804
michaeljson 0:3817adfaeb06 805 // checks if player was hit
Tito96 2:ade842cb48e9 806 // checks if player was hit
michaeljson 0:3817adfaeb06 807 if (hit_player)
michaeljson 0:3817adfaeb06 808 {
Tito96 2:ade842cb48e9 809 if (level == 1){
Tito96 2:ade842cb48e9 810 //lives = max_lives;
Tito96 2:ade842cb48e9 811 lives1 -= 1;
michaeljson 0:3817adfaeb06 812 wait(0.5);
michaeljson 0:3817adfaeb06 813 hit_player = 0;
michaeljson 0:3817adfaeb06 814 player_show(&player);
michaeljson 0:3817adfaeb06 815 player.status = PLAYER_ALIVE;
michaeljson 0:3817adfaeb06 816
michaeljson 0:3817adfaeb06 817 // prints updated lives number
michaeljson 0:3817adfaeb06 818 uLCD.locate(0,0);
Tito96 2:ade842cb48e9 819 uLCD.printf("Lives:%i", lives1);
Tito96 2:ade842cb48e9 820 }
Tito96 2:ade842cb48e9 821 else if (level == 2){
Tito96 2:ade842cb48e9 822 // lives = max_lives - 1;
Tito96 2:ade842cb48e9 823 lives2 -= 1;
Tito96 2:ade842cb48e9 824 wait(0.5);
Tito96 2:ade842cb48e9 825 hit_player = 0;
Tito96 2:ade842cb48e9 826 player_show(&player);
Tito96 2:ade842cb48e9 827 player.status = PLAYER_ALIVE;
Tito96 2:ade842cb48e9 828
Tito96 2:ade842cb48e9 829 // prints updated lives number
Tito96 2:ade842cb48e9 830 uLCD.locate(0,0);
Tito96 2:ade842cb48e9 831 uLCD.printf("Lives:%i", lives2);
Tito96 2:ade842cb48e9 832 }
Tito96 2:ade842cb48e9 833
Tito96 2:ade842cb48e9 834 else if (level == 3){
Tito96 2:ade842cb48e9 835 //lives = max_lives - 2;
Tito96 2:ade842cb48e9 836 lives3 -= 1;
Tito96 2:ade842cb48e9 837 wait(0.5);
Tito96 2:ade842cb48e9 838 hit_player = 0;
Tito96 2:ade842cb48e9 839 player_show(&player);
Tito96 2:ade842cb48e9 840 player.status = PLAYER_ALIVE;
Tito96 2:ade842cb48e9 841
Tito96 2:ade842cb48e9 842 // prints updated lives number
Tito96 2:ade842cb48e9 843 uLCD.locate(0,0);
Tito96 2:ade842cb48e9 844 uLCD.printf("Lives:%i", lives3);
Tito96 2:ade842cb48e9 845 }
Tito96 2:ade842cb48e9 846
Tito96 2:ade842cb48e9 847 }
michaeljson 0:3817adfaeb06 848 // if player loses all lives or enemy reaches the player
Tito96 2:ade842cb48e9 849 if (lose || lives1 == 0 || lives2 == 0 || lives3 == 0)
michaeljson 0:3817adfaeb06 850 {
michaeljson 0:3817adfaeb06 851 begin_game = false; // set to false to end game
michaeljson 0:3817adfaeb06 852 uLCD.cls();
michaeljson 0:3817adfaeb06 853
michaeljson 0:3817adfaeb06 854 gameover = true; // set to go to display gameover screen
michaeljson 0:3817adfaeb06 855
michaeljson 0:3817adfaeb06 856 // prints "GAMEOVER" to uLCD
michaeljson 0:3817adfaeb06 857 uLCD.locate(gameover_x_pos, gameover_y_pos);
michaeljson 0:3817adfaeb06 858 uLCD.printf("GAMEOVER");
michaeljson 0:3817adfaeb06 859 wait(1);
michaeljson 0:3817adfaeb06 860
michaeljson 0:3817adfaeb06 861 // prints "Play Again?" and "Press pb..."
michaeljson 0:3817adfaeb06 862 uLCD.locate(startover_x_pos, startover_y_pos);
michaeljson 0:3817adfaeb06 863 uLCD.printf("Play Again?");
michaeljson 0:3817adfaeb06 864 uLCD.locate(startover_x_pos, startover_y_pos+1);
michaeljson 0:3817adfaeb06 865 uLCD.printf("Press pb...");
michaeljson 0:3817adfaeb06 866
michaeljson 0:3817adfaeb06 867 // stays in gameover screen until pb is pressed
michaeljson 0:3817adfaeb06 868 while (gameover)
michaeljson 0:3817adfaeb06 869 {
michaeljson 0:3817adfaeb06 870 // if pb is pressed, game is reset to the game menu screen
michaeljson 0:3817adfaeb06 871 if (!pb)
michaeljson 0:3817adfaeb06 872 {
michaeljson 0:3817adfaeb06 873 gameover = false;
michaeljson 0:3817adfaeb06 874 game_menu = true;
michaeljson 0:3817adfaeb06 875 wait(0.5);
michaeljson 0:3817adfaeb06 876 }
michaeljson 0:3817adfaeb06 877 }
michaeljson 0:3817adfaeb06 878 }
michaeljson 0:3817adfaeb06 879
michaeljson 0:3817adfaeb06 880 }
michaeljson 0:3817adfaeb06 881 }
michaeljson 0:3817adfaeb06 882 }