Updated Space Invaders on the mbed. Improved upon Michael Son's "Mbed Space Invaders" at https://os.mbed.com/users/michaeljson/notebook/mbed-space-invaders/.

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

Fork of Two-PlayerSpaceInvaders by William Minix

test

Committer:
wminix3
Date:
Mon Apr 26 21:53:25 2021 +0000
Revision:
23:56f6a12aaebd
Parent:
22:a907eeb128a4
Child:
28:a2dac56af32f
Added barriers that both enemies and the player can damage. More true to original space invaders.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
michaeljson 0:3817adfaeb06 1 #include "player.h"
michaeljson 0:3817adfaeb06 2 #include "missile.h"
wminix3 23:56f6a12aaebd 3 /*
wminix3 20:ede4fa57d082 4 #define GREEN 0x00FF00
wminix3 20:ede4fa57d082 5 #define BLUE 0x0000FF
wminix3 20:ede4fa57d082 6 #define PINK 0xFFC0CB
wminix3 21:a6b4c5598083 7 #define PURPLE 0x800080
wminix3 20:ede4fa57d082 8 #define YELLOW 0xFFFF00
wminix3 22:a907eeb128a4 9 #define RED 0xFF0000
wminix3 23:56f6a12aaebd 10
wminix3 21:a6b4c5598083 11 // Modified from the RPG Game from ECE 2035. Draw more complex player object (with changing color).
wminix3 21:a6b4c5598083 12 void draw_img(int u, int v, int width, int height, const char* img)
wminix3 20:ede4fa57d082 13 {
wminix3 21:a6b4c5598083 14 int colors[width*height];
wminix3 21:a6b4c5598083 15 for (int i = 0; i < width*height; i++)
wminix3 20:ede4fa57d082 16 {
wminix3 21:a6b4c5598083 17 if (img[i] == 'G') colors[i] = GREEN;
wminix3 21:a6b4c5598083 18 else if (img[i] == 'B') colors[i] = BLUE;
wminix3 21:a6b4c5598083 19 else if (img[i] == 'P') colors[i] = PINK;
wminix3 21:a6b4c5598083 20 else if (img[i] == 'U') colors[i] = PURPLE;
wminix3 20:ede4fa57d082 21 else if (img[i] == 'Y') colors[i] = YELLOW;
wminix3 22:a907eeb128a4 22 else if (img[i] == 'R') colors[i] = RED;
wminix3 20:ede4fa57d082 23 else colors[i] = BLACK;
wminix3 20:ede4fa57d082 24 }
wminix3 21:a6b4c5598083 25 uLCD.BLIT(u, v, width, height, colors);
wminix3 20:ede4fa57d082 26 wait_us(250); // Recovery time!
wminix3 20:ede4fa57d082 27 }
wminix3 23:56f6a12aaebd 28 */
wminix3 20:ede4fa57d082 29
michaeljson 0:3817adfaeb06 30 void draw_player_object(int blk_x, int blk_y, int player_color, int p_width, int p_height)
michaeljson 0:3817adfaeb06 31 {
wminix3 21:a6b4c5598083 32 char* colors;
wminix3 20:ede4fa57d082 33 //uLCD.filled_rectangle(blk_x,blk_y,blk_x+p_width,blk_y+p_height,player_color);
wminix3 21:a6b4c5598083 34 if (player_color == GREEN) {
wminix3 21:a6b4c5598083 35 colors = "000000G00000000000GGG0000000000GGG000000GGGGGGGGGGG0GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG";
wminix3 21:a6b4c5598083 36 } else if (player_color == BLUE) {
wminix3 21:a6b4c5598083 37 colors = "000000B00000000000BBB0000000000BBB000000BBBBBBBBBBB0BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB";
wminix3 21:a6b4c5598083 38 } else if (player_color == PINK) {
wminix3 21:a6b4c5598083 39 colors = "000000P00000000000PPP0000000000PPP000000PPPPPPPPPPP0PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP";
wminix3 21:a6b4c5598083 40 } else if (player_color == PURPLE) {
wminix3 21:a6b4c5598083 41 colors = "000000U00000000000UUU0000000000UUU000000UUUUUUUUUUU0UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU";
wminix3 22:a907eeb128a4 42 } else if (player_color == RED) {
wminix3 22:a907eeb128a4 43 colors = "000000R00000000000RRR0000000000RRR000000RRRRRRRRRRR0RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR";
wminix3 21:a6b4c5598083 44 } else {
wminix3 21:a6b4c5598083 45 colors = "000000Y00000000000YYY0000000000YYY000000YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY";
wminix3 21:a6b4c5598083 46 }
wminix3 21:a6b4c5598083 47
wminix3 21:a6b4c5598083 48 /* "000000G000000
wminix3 21:a6b4c5598083 49 00000GGG00000
wminix3 21:a6b4c5598083 50 00000GGG00000
wminix3 21:a6b4c5598083 51 0GGGGGGGGGGG0
wminix3 21:a6b4c5598083 52 GGGGGGGGGGGGG
wminix3 21:a6b4c5598083 53 GGGGGGGGGGGGG
wminix3 21:a6b4c5598083 54 GGGGGGGGGGGGG
wminix3 21:a6b4c5598083 55 GGGGGGGGGGGGG */
wminix3 21:a6b4c5598083 56 draw_img(blk_x, blk_y, p_width, p_height, colors);
michaeljson 0:3817adfaeb06 57 }
michaeljson 0:3817adfaeb06 58
michaeljson 0:3817adfaeb06 59 void erase_player(int blk_x, int blk_y, int p_width, int p_height)
michaeljson 0:3817adfaeb06 60 {
michaeljson 0:3817adfaeb06 61 uLCD.filled_rectangle(blk_x,blk_y,blk_x+p_width,blk_y+p_height,BACKGROUND_COLOR);
michaeljson 0:3817adfaeb06 62 }
michaeljson 0:3817adfaeb06 63
michaeljson 0:3817adfaeb06 64 void player_init(player_t * g, int blk_x, int blk_y, int color)
michaeljson 0:3817adfaeb06 65 {
michaeljson 0:3817adfaeb06 66 g->player_blk_x = blk_x;
michaeljson 0:3817adfaeb06 67 g->player_blk_y = blk_y;
michaeljson 0:3817adfaeb06 68 g->player_color = 0x00FF00;
wminix3 21:a6b4c5598083 69 //g->player_height = 8;
wminix3 21:a6b4c5598083 70 //g->player_width = 8;
michaeljson 0:3817adfaeb06 71 g->player_height = 8;
wminix3 21:a6b4c5598083 72 g->player_width = 13;
michaeljson 0:3817adfaeb06 73 g->status = PLAYER_ALIVE;
michaeljson 0:3817adfaeb06 74 }
michaeljson 0:3817adfaeb06 75
michaeljson 0:3817adfaeb06 76 void player_show(player_t * g)
michaeljson 0:3817adfaeb06 77 {
michaeljson 0:3817adfaeb06 78 draw_player_object(g->player_blk_x, g->player_blk_y, g->player_color, g->player_width, g->player_height);
michaeljson 0:3817adfaeb06 79 }
michaeljson 0:3817adfaeb06 80
michaeljson 0:3817adfaeb06 81 void player_erase(player_t *g)
michaeljson 0:3817adfaeb06 82 {
michaeljson 0:3817adfaeb06 83 erase_player(g->player_blk_x, g->player_blk_y, g->player_width, g->player_height);
michaeljson 0:3817adfaeb06 84 }
michaeljson 0:3817adfaeb06 85
michaeljson 0:3817adfaeb06 86 int check_player(player_t * g, missile_t * h)
michaeljson 0:3817adfaeb06 87 {
michaeljson 0:3817adfaeb06 88 int player_died = 0;
michaeljson 0:3817adfaeb06 89 if (g->status == PLAYER_ALIVE
michaeljson 0:3817adfaeb06 90 && ((h->missile_blk_x >= g->player_blk_x) && (h->missile_blk_x <= (g->player_blk_x + g->player_width))))
michaeljson 0:3817adfaeb06 91 {
michaeljson 0:3817adfaeb06 92 player_erase(g);
michaeljson 0:3817adfaeb06 93 g->status = PLAYER_DEAD;
michaeljson 0:3817adfaeb06 94 player_died = 1;
michaeljson 0:3817adfaeb06 95 h->status = ENEMY_MISSILE_EXPLODED;
michaeljson 0:3817adfaeb06 96 }
michaeljson 0:3817adfaeb06 97
michaeljson 0:3817adfaeb06 98 return player_died;
michaeljson 0:3817adfaeb06 99 }