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@33:d17d71103d41, 2021-04-29 (annotated)
- Committer:
- wminix3
- Date:
- Thu Apr 29 05:41:40 2021 +0000
- Revision:
- 33:d17d71103d41
- Parent:
- 28:a2dac56af32f
Fixed score.
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" |
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 | |
wminix3 | 28:a2dac56af32f | 30 | // draw the player as a nicer looking sprite (closer to original game). Draw the sprite with the appropriately colored pixels. |
michaeljson | 0:3817adfaeb06 | 31 | void draw_player_object(int blk_x, int blk_y, int player_color, int p_width, int p_height) |
michaeljson | 0:3817adfaeb06 | 32 | { |
wminix3 | 21:a6b4c5598083 | 33 | char* colors; |
wminix3 | 20:ede4fa57d082 | 34 | //uLCD.filled_rectangle(blk_x,blk_y,blk_x+p_width,blk_y+p_height,player_color); |
wminix3 | 21:a6b4c5598083 | 35 | if (player_color == GREEN) { |
wminix3 | 21:a6b4c5598083 | 36 | colors = "000000G00000000000GGG0000000000GGG000000GGGGGGGGGGG0GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG"; |
wminix3 | 21:a6b4c5598083 | 37 | } else if (player_color == BLUE) { |
wminix3 | 21:a6b4c5598083 | 38 | colors = "000000B00000000000BBB0000000000BBB000000BBBBBBBBBBB0BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"; |
wminix3 | 21:a6b4c5598083 | 39 | } else if (player_color == PINK) { |
wminix3 | 21:a6b4c5598083 | 40 | colors = "000000P00000000000PPP0000000000PPP000000PPPPPPPPPPP0PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP"; |
wminix3 | 21:a6b4c5598083 | 41 | } else if (player_color == PURPLE) { |
wminix3 | 21:a6b4c5598083 | 42 | colors = "000000U00000000000UUU0000000000UUU000000UUUUUUUUUUU0UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU"; |
wminix3 | 22:a907eeb128a4 | 43 | } else if (player_color == RED) { |
wminix3 | 22:a907eeb128a4 | 44 | colors = "000000R00000000000RRR0000000000RRR000000RRRRRRRRRRR0RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR"; |
wminix3 | 21:a6b4c5598083 | 45 | } else { |
wminix3 | 21:a6b4c5598083 | 46 | colors = "000000Y00000000000YYY0000000000YYY000000YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"; |
wminix3 | 21:a6b4c5598083 | 47 | } |
wminix3 | 21:a6b4c5598083 | 48 | |
wminix3 | 21:a6b4c5598083 | 49 | /* "000000G000000 |
wminix3 | 21:a6b4c5598083 | 50 | 00000GGG00000 |
wminix3 | 21:a6b4c5598083 | 51 | 00000GGG00000 |
wminix3 | 21:a6b4c5598083 | 52 | 0GGGGGGGGGGG0 |
wminix3 | 21:a6b4c5598083 | 53 | GGGGGGGGGGGGG |
wminix3 | 21:a6b4c5598083 | 54 | GGGGGGGGGGGGG |
wminix3 | 21:a6b4c5598083 | 55 | GGGGGGGGGGGGG |
wminix3 | 21:a6b4c5598083 | 56 | GGGGGGGGGGGGG */ |
wminix3 | 21:a6b4c5598083 | 57 | draw_img(blk_x, blk_y, p_width, p_height, colors); |
michaeljson | 0:3817adfaeb06 | 58 | } |
michaeljson | 0:3817adfaeb06 | 59 | |
michaeljson | 0:3817adfaeb06 | 60 | void erase_player(int blk_x, int blk_y, int p_width, int p_height) |
michaeljson | 0:3817adfaeb06 | 61 | { |
michaeljson | 0:3817adfaeb06 | 62 | uLCD.filled_rectangle(blk_x,blk_y,blk_x+p_width,blk_y+p_height,BACKGROUND_COLOR); |
michaeljson | 0:3817adfaeb06 | 63 | } |
michaeljson | 0:3817adfaeb06 | 64 | |
michaeljson | 0:3817adfaeb06 | 65 | void player_init(player_t * g, int blk_x, int blk_y, int color) |
michaeljson | 0:3817adfaeb06 | 66 | { |
michaeljson | 0:3817adfaeb06 | 67 | g->player_blk_x = blk_x; |
michaeljson | 0:3817adfaeb06 | 68 | g->player_blk_y = blk_y; |
michaeljson | 0:3817adfaeb06 | 69 | g->player_color = 0x00FF00; |
wminix3 | 21:a6b4c5598083 | 70 | //g->player_height = 8; |
wminix3 | 21:a6b4c5598083 | 71 | //g->player_width = 8; |
michaeljson | 0:3817adfaeb06 | 72 | g->player_height = 8; |
wminix3 | 28:a2dac56af32f | 73 | g->player_width = 13; // change width of player to 13 for a nicer looking sprite |
michaeljson | 0:3817adfaeb06 | 74 | g->status = PLAYER_ALIVE; |
michaeljson | 0:3817adfaeb06 | 75 | } |
michaeljson | 0:3817adfaeb06 | 76 | |
michaeljson | 0:3817adfaeb06 | 77 | void player_show(player_t * g) |
michaeljson | 0:3817adfaeb06 | 78 | { |
michaeljson | 0:3817adfaeb06 | 79 | draw_player_object(g->player_blk_x, g->player_blk_y, g->player_color, g->player_width, g->player_height); |
michaeljson | 0:3817adfaeb06 | 80 | } |
michaeljson | 0:3817adfaeb06 | 81 | |
michaeljson | 0:3817adfaeb06 | 82 | void player_erase(player_t *g) |
michaeljson | 0:3817adfaeb06 | 83 | { |
michaeljson | 0:3817adfaeb06 | 84 | erase_player(g->player_blk_x, g->player_blk_y, g->player_width, g->player_height); |
michaeljson | 0:3817adfaeb06 | 85 | } |
michaeljson | 0:3817adfaeb06 | 86 | |
michaeljson | 0:3817adfaeb06 | 87 | int check_player(player_t * g, missile_t * h) |
michaeljson | 0:3817adfaeb06 | 88 | { |
michaeljson | 0:3817adfaeb06 | 89 | int player_died = 0; |
michaeljson | 0:3817adfaeb06 | 90 | if (g->status == PLAYER_ALIVE |
michaeljson | 0:3817adfaeb06 | 91 | && ((h->missile_blk_x >= g->player_blk_x) && (h->missile_blk_x <= (g->player_blk_x + g->player_width)))) |
michaeljson | 0:3817adfaeb06 | 92 | { |
michaeljson | 0:3817adfaeb06 | 93 | player_erase(g); |
michaeljson | 0:3817adfaeb06 | 94 | g->status = PLAYER_DEAD; |
michaeljson | 0:3817adfaeb06 | 95 | player_died = 1; |
michaeljson | 0:3817adfaeb06 | 96 | h->status = ENEMY_MISSILE_EXPLODED; |
michaeljson | 0:3817adfaeb06 | 97 | } |
michaeljson | 0:3817adfaeb06 | 98 | |
michaeljson | 0:3817adfaeb06 | 99 | return player_died; |
michaeljson | 0:3817adfaeb06 | 100 | } |