Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
main.cpp@6:5d57c758c31d, 2019-04-03 (annotated)
- Committer:
- el17mcd
- Date:
- Wed Apr 03 14:47:21 2019 +0000
- Revision:
- 6:5d57c758c31d
- Parent:
- 5:8a2e96f7fb4d
- Child:
- 7:a3ccabdebe2e
Working prototype of hit detection mechanic. A 5x5 pixel projectile and 6x10 pixel tank are given random x,y coordinates and assigned corresponding grid positions. If their hitboxes overlap (share the same grid position for any of their pixels); hit.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| el17mcd | 2:8382613c86a0 | 1 | /* |
| el17mcd | 2:8382613c86a0 | 2 | ELEC2645 Embedded Systems Project |
| el17mcd | 2:8382613c86a0 | 3 | School of Electronic & Electrical Engineering |
| el17mcd | 2:8382613c86a0 | 4 | University of Leeds |
| el17mcd | 2:8382613c86a0 | 5 | Name: Maxim C. Delacoe |
| el17mcd | 2:8382613c86a0 | 6 | Username: EL 17 MCD |
| el17mcd | 2:8382613c86a0 | 7 | Student ID Number: 2011 58344 |
| el17mcd | 2:8382613c86a0 | 8 | Date: 19/03/2019 |
| el17mcd | 2:8382613c86a0 | 9 | */ |
| el17mcd | 2:8382613c86a0 | 10 | ///////// pre-processor directives //////// |
| el17mcd | 2:8382613c86a0 | 11 | #include "mbed.h" |
| el17mcd | 2:8382613c86a0 | 12 | #include "Gamepad.h" |
| el17mcd | 2:8382613c86a0 | 13 | #include "N5110.h" |
| el17mcd | 2:8382613c86a0 | 14 | #include "Bitmap.h" |
| el17mcd | 2:8382613c86a0 | 15 | |
| el17mcd | 2:8382613c86a0 | 16 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
| el17mcd | 2:8382613c86a0 | 17 | |
| el17mcd | 2:8382613c86a0 | 18 | const int tank_left[6][10] = { |
| el17mcd | 2:8382613c86a0 | 19 | { 0,0,0,1,1,1,0,0,0,0 }, |
| el17mcd | 2:8382613c86a0 | 20 | { 0,0,1,1,1,1,1,0,0,0 }, |
| el17mcd | 2:8382613c86a0 | 21 | { 0,0,1,1,1,1,1,1,1,0 }, |
| el17mcd | 2:8382613c86a0 | 22 | { 1,1,1,1,1,1,1,1,1,1 }, |
| el17mcd | 2:8382613c86a0 | 23 | { 1,0,1,0,1,0,1,0,1,0 }, |
| el17mcd | 2:8382613c86a0 | 24 | { 0,1,0,1,0,1,0,1,0,0 }, |
| el17mcd | 2:8382613c86a0 | 25 | }; |
| el17mcd | 4:3b3a7f102250 | 26 | const int proj[5][5] = { |
| el17mcd | 4:3b3a7f102250 | 27 | { 0,0,1,0,0 }, |
| el17mcd | 4:3b3a7f102250 | 28 | { 0,0,1,0,0 }, |
| el17mcd | 4:3b3a7f102250 | 29 | { 1,1,1,1,1 }, |
| el17mcd | 4:3b3a7f102250 | 30 | { 0,0,1,0,0 }, |
| el17mcd | 4:3b3a7f102250 | 31 | { 0,0,1,0,0 }, |
| el17mcd | 4:3b3a7f102250 | 32 | }; |
| el17mcd | 4:3b3a7f102250 | 33 | // Spawning tank and projectile at random position |
| el17mcd | 4:3b3a7f102250 | 34 | // with visual feedback to build grid and hitbox system |
| el17mcd | 2:8382613c86a0 | 35 | void welcome() |
| el17mcd | 2:8382613c86a0 | 36 | { |
| el17mcd | 2:8382613c86a0 | 37 | lcd.clear(); |
| el17mcd | 2:8382613c86a0 | 38 | lcd.printString(" ELEC 2645",0,0); |
| el17mcd | 2:8382613c86a0 | 39 | lcd.printString(" Game ",0,1); |
| el17mcd | 2:8382613c86a0 | 40 | lcd.printString(" Project",0,2); |
| el17mcd | 2:8382613c86a0 | 41 | lcd.printString("Max C. Delacoe",0,4); |
| el17mcd | 2:8382613c86a0 | 42 | lcd.printString(" 2011 58344",0,5); |
| el17mcd | 2:8382613c86a0 | 43 | lcd.refresh(); |
| el17mcd | 2:8382613c86a0 | 44 | wait(0.2); |
| el17mcd | 2:8382613c86a0 | 45 | } |
| el17mcd | 2:8382613c86a0 | 46 | |
| el17mcd | 2:8382613c86a0 | 47 | int main() |
| el17mcd | 2:8382613c86a0 | 48 | { |
| el17mcd | 4:3b3a7f102250 | 49 | int tank_hb[40]; |
| el17mcd | 6:5d57c758c31d | 50 | int proj_hb[25]; |
| el17mcd | 2:8382613c86a0 | 51 | lcd.init(); |
| el17mcd | 3:087b28bf8b96 | 52 | // welcome(); // display welcome message |
| el17mcd | 2:8382613c86a0 | 53 | |
| el17mcd | 5:8a2e96f7fb4d | 54 | while(1) { // infinite loop |
| el17mcd | 4:3b3a7f102250 | 55 | |
| el17mcd | 6:5d57c758c31d | 56 | srand(time(NULL)); |
| el17mcd | 6:5d57c758c31d | 57 | int t_pos_x = rand() % (84-1-10); // Tank position |
| el17mcd | 6:5d57c758c31d | 58 | int t_pos_y = rand() % (48-1-6); |
| el17mcd | 6:5d57c758c31d | 59 | |
| el17mcd | 6:5d57c758c31d | 60 | int p_pos_x = rand() % (84-1-5); // projectile position |
| el17mcd | 6:5d57c758c31d | 61 | int p_pos_y = rand() % (48-1-5); |
| el17mcd | 5:8a2e96f7fb4d | 62 | |
| el17mcd | 5:8a2e96f7fb4d | 63 | //Takes the tanks x and y position (of its bottom left pixel of sprite) and |
| el17mcd | 5:8a2e96f7fb4d | 64 | //determines the area it cover in terms of grid values. |
| el17mcd | 4:3b3a7f102250 | 65 | int i = 0; |
| el17mcd | 5:8a2e96f7fb4d | 66 | for (int i0 = 0; i0 < 4; i0++) { |
| el17mcd | 4:3b3a7f102250 | 67 | |
| el17mcd | 4:3b3a7f102250 | 68 | for (int i1 = 1; i1 < 11; i1++) { |
| el17mcd | 4:3b3a7f102250 | 69 | tank_hb[i] = (i0 + t_pos_y) * 84 + t_pos_x + i1; |
| el17mcd | 5:8a2e96f7fb4d | 70 | i++; |
| el17mcd | 4:3b3a7f102250 | 71 | } |
| el17mcd | 4:3b3a7f102250 | 72 | } |
| el17mcd | 6:5d57c758c31d | 73 | |
| el17mcd | 6:5d57c758c31d | 74 | i = 0; |
| el17mcd | 6:5d57c758c31d | 75 | |
| el17mcd | 6:5d57c758c31d | 76 | for (int i0 = 0; i0 < 5; i0++) { |
| el17mcd | 5:8a2e96f7fb4d | 77 | |
| el17mcd | 6:5d57c758c31d | 78 | for (int i1 = 1; i1 < 6; i1++) { |
| el17mcd | 6:5d57c758c31d | 79 | proj_hb[i] = (i0 + p_pos_y) * 84 + p_pos_x + i1; |
| el17mcd | 6:5d57c758c31d | 80 | i++; |
| el17mcd | 6:5d57c758c31d | 81 | } |
| el17mcd | 6:5d57c758c31d | 82 | } |
| el17mcd | 6:5d57c758c31d | 83 | |
| el17mcd | 6:5d57c758c31d | 84 | bool hit = false; |
| el17mcd | 6:5d57c758c31d | 85 | for (int i0 = 0; i0 < 25; i0++) { |
| el17mcd | 6:5d57c758c31d | 86 | for (int i1 = 0; i1 < 40; i1++) { |
| el17mcd | 6:5d57c758c31d | 87 | if (proj_hb[i0] == tank_hb[i1]) {hit = true;} |
| el17mcd | 6:5d57c758c31d | 88 | } |
| el17mcd | 6:5d57c758c31d | 89 | if(hit == true) {break;} |
| el17mcd | 6:5d57c758c31d | 90 | } |
| el17mcd | 6:5d57c758c31d | 91 | |
| el17mcd | 6:5d57c758c31d | 92 | // bool hit = false; |
| el17mcd | 4:3b3a7f102250 | 93 | lcd.clear(); |
| el17mcd | 4:3b3a7f102250 | 94 | lcd.drawSprite(t_pos_x,42 - t_pos_y,6,10,(int *)tank_left); |
| el17mcd | 6:5d57c758c31d | 95 | lcd.drawSprite(p_pos_x,43 - p_pos_y,5,5,(int *)proj); |
| el17mcd | 6:5d57c758c31d | 96 | lcd.refresh(); |
| el17mcd | 6:5d57c758c31d | 97 | wait(0.5); |
| el17mcd | 6:5d57c758c31d | 98 | if (hit == true){ |
| el17mcd | 6:5d57c758c31d | 99 | lcd.clear(); |
| el17mcd | 6:5d57c758c31d | 100 | lcd.printString("HIT",0,1); |
| el17mcd | 6:5d57c758c31d | 101 | lcd.refresh(); |
| el17mcd | 6:5d57c758c31d | 102 | wait(2.0); |
| el17mcd | 6:5d57c758c31d | 103 | } |
| el17mcd | 6:5d57c758c31d | 104 | } |
| el17mcd | 2:8382613c86a0 | 105 | } |
| el17mcd | 6:5d57c758c31d | 106 | /* |
| el17mcd | 6:5d57c758c31d | 107 | char buffer[14]; |
| el17mcd | 6:5d57c758c31d | 108 | sprintf(buffer,"t_pos_x = %d ",t_pos_x); |
| el17mcd | 6:5d57c758c31d | 109 | lcd.printString(buffer,0,1); |
| el17mcd | 6:5d57c758c31d | 110 | */ |