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