This code contains the game Starship designed for the STM32F429i-DISC1 board. It requires a keyboard to play.
Dependencies: Starship LCD_DISCO_F429ZI USBHost mbed
Bullet.c@0:c9afe145b57b, 2017-09-17 (annotated)
- Committer:
- Oshyrath
- Date:
- Sun Sep 17 17:05:27 2017 +0000
- Revision:
- 0:c9afe145b57b
- Child:
- 1:527a11035e0b
This is the first push for Starship game for STM32F429i-DISC1.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Oshyrath | 0:c9afe145b57b | 1 | #include "Bullet.h" |
Oshyrath | 0:c9afe145b57b | 2 | void addEnemyBullet(uint16_t x_pos, uint16_t y_pos) |
Oshyrath | 0:c9afe145b57b | 3 | { |
Oshyrath | 0:c9afe145b57b | 4 | struct Bullet * newBullet = (struct Bullet *) malloc(sizeof(struct Bullet)); |
Oshyrath | 0:c9afe145b57b | 5 | newBullet->x_pos = x_pos; |
Oshyrath | 0:c9afe145b57b | 6 | newBullet->y_pos = y_pos; |
Oshyrath | 0:c9afe145b57b | 7 | newBullet->x_vel = 0; |
Oshyrath | 0:c9afe145b57b | 8 | newBullet->y_vel = DEFAULT_BULLET_SPEED; |
Oshyrath | 0:c9afe145b57b | 9 | newBullet->isPlayerBullet = 0; |
Oshyrath | 0:c9afe145b57b | 10 | enemyBulletCount++; |
Oshyrath | 0:c9afe145b57b | 11 | if(enemyBulletCount==1) |
Oshyrath | 0:c9afe145b57b | 12 | { |
Oshyrath | 0:c9afe145b57b | 13 | newBullet->prev = newBullet; |
Oshyrath | 0:c9afe145b57b | 14 | newBullet->next = newBullet; |
Oshyrath | 0:c9afe145b57b | 15 | enemyBulletPTR = newBullet; |
Oshyrath | 0:c9afe145b57b | 16 | return; |
Oshyrath | 0:c9afe145b57b | 17 | } |
Oshyrath | 0:c9afe145b57b | 18 | struct Bullet * tempNext = enemyBulletPTR->next; |
Oshyrath | 0:c9afe145b57b | 19 | enemyBulletPTR->next = newBullet; |
Oshyrath | 0:c9afe145b57b | 20 | newBullet->prev = enemyBulletPTR; |
Oshyrath | 0:c9afe145b57b | 21 | tempNext->prev = newBullet; |
Oshyrath | 0:c9afe145b57b | 22 | newBullet->next = tempNext; |
Oshyrath | 0:c9afe145b57b | 23 | } |
Oshyrath | 0:c9afe145b57b | 24 | void addPlayerBullet(uint16_t x_pos, uint16_t y_pos, uint16_t x_vel, uint16_t y_vel, uint8_t power) |
Oshyrath | 0:c9afe145b57b | 25 | { |
Oshyrath | 0:c9afe145b57b | 26 | struct Bullet * newBullet = (struct Bullet *) malloc(sizeof(struct Bullet)); |
Oshyrath | 0:c9afe145b57b | 27 | newBullet->x_pos = x_pos; |
Oshyrath | 0:c9afe145b57b | 28 | newBullet->y_pos = y_pos; |
Oshyrath | 0:c9afe145b57b | 29 | newBullet->x_vel = x_vel; |
Oshyrath | 0:c9afe145b57b | 30 | newBullet->y_vel = y_vel; |
Oshyrath | 0:c9afe145b57b | 31 | newBullet->powerShot = power; |
Oshyrath | 0:c9afe145b57b | 32 | newBullet->isPlayerBullet = 1; |
Oshyrath | 0:c9afe145b57b | 33 | playerBulletCount++; |
Oshyrath | 0:c9afe145b57b | 34 | if(playerBulletCount==1) |
Oshyrath | 0:c9afe145b57b | 35 | { |
Oshyrath | 0:c9afe145b57b | 36 | newBullet->prev = newBullet; |
Oshyrath | 0:c9afe145b57b | 37 | newBullet->next = newBullet; |
Oshyrath | 0:c9afe145b57b | 38 | playerBulletPTR = newBullet; |
Oshyrath | 0:c9afe145b57b | 39 | return; |
Oshyrath | 0:c9afe145b57b | 40 | } |
Oshyrath | 0:c9afe145b57b | 41 | struct Bullet * tempNext = playerBulletPTR->next; |
Oshyrath | 0:c9afe145b57b | 42 | playerBulletPTR->next = newBullet; |
Oshyrath | 0:c9afe145b57b | 43 | newBullet->prev = playerBulletPTR; |
Oshyrath | 0:c9afe145b57b | 44 | tempNext->prev = newBullet; |
Oshyrath | 0:c9afe145b57b | 45 | newBullet->next = tempNext; |
Oshyrath | 0:c9afe145b57b | 46 | } |
Oshyrath | 0:c9afe145b57b | 47 | void removeBullet(struct Bullet * removePTR) |
Oshyrath | 0:c9afe145b57b | 48 | { |
Oshyrath | 0:c9afe145b57b | 49 | if(removePTR->isPlayerBullet) |
Oshyrath | 0:c9afe145b57b | 50 | { |
Oshyrath | 0:c9afe145b57b | 51 | if(playerBulletCount <= 0) |
Oshyrath | 0:c9afe145b57b | 52 | return; |
Oshyrath | 0:c9afe145b57b | 53 | playerBulletCount--; |
Oshyrath | 0:c9afe145b57b | 54 | if(playerBulletCount == 1) |
Oshyrath | 0:c9afe145b57b | 55 | playerBulletPTR = (struct Bullet *) 0; |
Oshyrath | 0:c9afe145b57b | 56 | else |
Oshyrath | 0:c9afe145b57b | 57 | { |
Oshyrath | 0:c9afe145b57b | 58 | playerBulletPTR = removePTR->prev; |
Oshyrath | 0:c9afe145b57b | 59 | struct Bullet * tempNext = removePTR->next; |
Oshyrath | 0:c9afe145b57b | 60 | playerBulletPTR->next = tempNext; |
Oshyrath | 0:c9afe145b57b | 61 | tempNext->prev = playerBulletPTR; |
Oshyrath | 0:c9afe145b57b | 62 | } |
Oshyrath | 0:c9afe145b57b | 63 | } |
Oshyrath | 0:c9afe145b57b | 64 | else |
Oshyrath | 0:c9afe145b57b | 65 | { |
Oshyrath | 0:c9afe145b57b | 66 | if(enemyBulletCount <= 0) |
Oshyrath | 0:c9afe145b57b | 67 | return; |
Oshyrath | 0:c9afe145b57b | 68 | enemyBulletCount--; |
Oshyrath | 0:c9afe145b57b | 69 | if(enemyBulletCount == 1) |
Oshyrath | 0:c9afe145b57b | 70 | enemyBulletPTR = (struct Bullet *) 0; |
Oshyrath | 0:c9afe145b57b | 71 | else |
Oshyrath | 0:c9afe145b57b | 72 | { |
Oshyrath | 0:c9afe145b57b | 73 | enemyBulletPTR = removePTR->prev; |
Oshyrath | 0:c9afe145b57b | 74 | struct Bullet * tempNext = removePTR->next; |
Oshyrath | 0:c9afe145b57b | 75 | enemyBulletPTR->next = tempNext; |
Oshyrath | 0:c9afe145b57b | 76 | tempNext->prev = enemyBulletPTR; |
Oshyrath | 0:c9afe145b57b | 77 | } |
Oshyrath | 0:c9afe145b57b | 78 | } |
Oshyrath | 0:c9afe145b57b | 79 | free(removePTR); |
Oshyrath | 0:c9afe145b57b | 80 | } |
Oshyrath | 0:c9afe145b57b | 81 | void clearBullets() |
Oshyrath | 0:c9afe145b57b | 82 | { |
Oshyrath | 0:c9afe145b57b | 83 | while(enemyBulletCount) |
Oshyrath | 0:c9afe145b57b | 84 | removeBullet(enemyBulletPTR); |
Oshyrath | 0:c9afe145b57b | 85 | while(playerBulletCount) |
Oshyrath | 0:c9afe145b57b | 86 | removeBullet(playerBulletPTR); |
Oshyrath | 0:c9afe145b57b | 87 | } |
Oshyrath | 0:c9afe145b57b | 88 | |
Oshyrath | 0:c9afe145b57b | 89 | //Specific to 1 Bullet |
Oshyrath | 0:c9afe145b57b | 90 | void bulletMovement(struct Bullet * bullet) |
Oshyrath | 0:c9afe145b57b | 91 | { |
Oshyrath | 0:c9afe145b57b | 92 | bullet->x_pos+=bullet->x_vel; |
Oshyrath | 0:c9afe145b57b | 93 | bullet->y_pos+=bullet->y_vel; |
Oshyrath | 0:c9afe145b57b | 94 | } |
Oshyrath | 0:c9afe145b57b | 95 | void bulletMovement_ALL() |
Oshyrath | 0:c9afe145b57b | 96 | { |
Oshyrath | 0:c9afe145b57b | 97 | int i; |
Oshyrath | 0:c9afe145b57b | 98 | for(i = playerBulletCount; i; i--) |
Oshyrath | 0:c9afe145b57b | 99 | { |
Oshyrath | 0:c9afe145b57b | 100 | bulletMovement(playerBulletPTR); |
Oshyrath | 0:c9afe145b57b | 101 | playerBulletPTR = playerBulletPTR->next; |
Oshyrath | 0:c9afe145b57b | 102 | } |
Oshyrath | 0:c9afe145b57b | 103 | for(i = enemyBulletCount; i; i--) |
Oshyrath | 0:c9afe145b57b | 104 | { |
Oshyrath | 0:c9afe145b57b | 105 | bulletMovement(enemyBulletPTR); |
Oshyrath | 0:c9afe145b57b | 106 | enemyBulletPTR = enemyBulletPTR->next; |
Oshyrath | 0:c9afe145b57b | 107 | } |
Oshyrath | 0:c9afe145b57b | 108 | } |
Oshyrath | 0:c9afe145b57b | 109 | void removeBulletIfOOB(struct Bullet * bullet) |
Oshyrath | 0:c9afe145b57b | 110 | { |
Oshyrath | 0:c9afe145b57b | 111 | int xp = bullet->x_pos; |
Oshyrath | 0:c9afe145b57b | 112 | int yp = bullet->y_pos; |
Oshyrath | 0:c9afe145b57b | 113 | if(bullet->isPlayerBullet) |
Oshyrath | 0:c9afe145b57b | 114 | { |
Oshyrath | 0:c9afe145b57b | 115 | if((xp <= -BULLET_SIZE)||(xp>=LCD_WIDTH)||(yp <= -BULLET_SIZE)||(yp>=LCD_HEIGHT)) |
Oshyrath | 0:c9afe145b57b | 116 | removeBullet(bullet); |
Oshyrath | 0:c9afe145b57b | 117 | } |
Oshyrath | 0:c9afe145b57b | 118 | else |
Oshyrath | 0:c9afe145b57b | 119 | { |
Oshyrath | 0:c9afe145b57b | 120 | if(yp >= LCD_HEIGHT) |
Oshyrath | 0:c9afe145b57b | 121 | removeBullet(bullet); |
Oshyrath | 0:c9afe145b57b | 122 | } |
Oshyrath | 0:c9afe145b57b | 123 | } |
Oshyrath | 0:c9afe145b57b | 124 | void removeBulletIfOOB_ALL() |
Oshyrath | 0:c9afe145b57b | 125 | { |
Oshyrath | 0:c9afe145b57b | 126 | int i; |
Oshyrath | 0:c9afe145b57b | 127 | for(i = enemyBulletCount; i; i--) |
Oshyrath | 0:c9afe145b57b | 128 | { |
Oshyrath | 0:c9afe145b57b | 129 | enemyBulletPTR = enemyBulletPTR->next; |
Oshyrath | 0:c9afe145b57b | 130 | removeBulletIfOOB(enemyBulletPTR); |
Oshyrath | 0:c9afe145b57b | 131 | } |
Oshyrath | 0:c9afe145b57b | 132 | for(i = playerBulletCount; i; i--) |
Oshyrath | 0:c9afe145b57b | 133 | { |
Oshyrath | 0:c9afe145b57b | 134 | playerBulletPTR = playerBulletPTR->next; |
Oshyrath | 0:c9afe145b57b | 135 | removeBulletIfOOB(playerBulletPTR); |
Oshyrath | 0:c9afe145b57b | 136 | } |
Oshyrath | 0:c9afe145b57b | 137 | } |
Oshyrath | 0:c9afe145b57b | 138 | void callMultiFuncBullet() |
Oshyrath | 0:c9afe145b57b | 139 | { |
Oshyrath | 0:c9afe145b57b | 140 | bulletMovement_ALL(); |
Oshyrath | 0:c9afe145b57b | 141 | removeBulletIfOOB_ALL(); |
Oshyrath | 0:c9afe145b57b | 142 | } |