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
Collision/Collision.cpp@12:009895f6b6e4, 2020-05-11 (annotated)
- Committer:
- el19zf
- Date:
- Mon May 11 14:43:16 2020 +0000
- Revision:
- 12:009895f6b6e4
- Parent:
- 11:494cc44777fe
- Child:
- 13:eb60628db8bf
add a test file and optimzed code
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| el19zf | 6:dce38fe4e092 | 1 | #include "Collision.h" |
| el19zf | 6:dce38fe4e092 | 2 | |
| el19zf | 11:494cc44777fe | 3 | Collision::Collision() |
| el19zf | 11:494cc44777fe | 4 | { |
| el19zf | 6:dce38fe4e092 | 5 | |
| el19zf | 6:dce38fe4e092 | 6 | } |
| el19zf | 6:dce38fe4e092 | 7 | |
| el19zf | 11:494cc44777fe | 8 | Collision::~Collision() |
| el19zf | 11:494cc44777fe | 9 | { |
| el19zf | 6:dce38fe4e092 | 10 | |
| el19zf | 6:dce38fe4e092 | 11 | } |
| el19zf | 6:dce38fe4e092 | 12 | |
| el19zf | 11:494cc44777fe | 13 | void Collision::init() |
| el19zf | 11:494cc44777fe | 14 | { |
| el19zf | 8:8287d2ef965d | 15 | _health = 3; |
| el19zf | 8:8287d2ef965d | 16 | _check_index = 0; |
| el19zf | 11:494cc44777fe | 17 | _check_des_index = 0; |
| el19zf | 8:8287d2ef965d | 18 | } |
| el19zf | 8:8287d2ef965d | 19 | |
| el19zf | 11:494cc44777fe | 20 | bool Collision::check(N5110 &lcd) |
| el19zf | 11:494cc44777fe | 21 | { |
| el19zf | 8:8287d2ef965d | 22 | _check_index = 0; |
| el19zf | 9:62d6559f0d50 | 23 | //check every sprite set points |
| el19zf | 9:62d6559f0d50 | 24 | if(lcd.getPixel(_people_pos.x+1,_people_pos.y)) { _check_index = 1; } |
| el19zf | 8:8287d2ef965d | 25 | if(lcd.getPixel(_people_pos.x+2,_people_pos.y)) { _check_index = 1; } |
| el19zf | 8:8287d2ef965d | 26 | if(lcd.getPixel(_people_pos.x,_people_pos.y+1)) { _check_index = 1; } |
| el19zf | 8:8287d2ef965d | 27 | if(lcd.getPixel(_people_pos.x+1,_people_pos.y+1)) { _check_index = 1; } |
| el19zf | 8:8287d2ef965d | 28 | if(lcd.getPixel(_people_pos.x+2,_people_pos.y+1)) { _check_index = 1; } |
| el19zf | 8:8287d2ef965d | 29 | if(lcd.getPixel(_people_pos.x+3,_people_pos.y+1)) { _check_index = 1; } |
| el19zf | 8:8287d2ef965d | 30 | if(lcd.getPixel(_people_pos.x+1,_people_pos.y+2)) { _check_index = 1; } |
| el19zf | 8:8287d2ef965d | 31 | if(lcd.getPixel(_people_pos.x+2,_people_pos.y+2)) { _check_index = 1; } |
| el19zf | 8:8287d2ef965d | 32 | if(lcd.getPixel(_people_pos.x+1,_people_pos.y+3)) { _check_index = 1; } |
| el19zf | 8:8287d2ef965d | 33 | if(lcd.getPixel(_people_pos.x+2,_people_pos.y+3)) { _check_index = 1; } |
| el19zf | 8:8287d2ef965d | 34 | //printf("index = %d\n",_check_index); |
| el19zf | 8:8287d2ef965d | 35 | //printf("people_pos = %f,%f\n",_people_pos.x+2,_people_pos.y+3); |
| el19zf | 8:8287d2ef965d | 36 | if((_check_index == 1)&&(_health > 0)) { |
| el19zf | 8:8287d2ef965d | 37 | //_check_index = 0; |
| el19zf | 8:8287d2ef965d | 38 | _health = _health - 1; |
| el19zf | 8:8287d2ef965d | 39 | } |
| el19zf | 8:8287d2ef965d | 40 | return _check_index; |
| el19zf | 8:8287d2ef965d | 41 | //printf("health is %d\n",_health); |
| el19zf | 8:8287d2ef965d | 42 | } |
| el19zf | 8:8287d2ef965d | 43 | |
| el19zf | 11:494cc44777fe | 44 | bool Collision::check_des(N5110 &lcd) |
| el19zf | 11:494cc44777fe | 45 | { |
| el19zf | 11:494cc44777fe | 46 | _check_des_index = 0; |
| el19zf | 11:494cc44777fe | 47 | if(lcd.getPixel(82,24)||lcd.getPixel(82,25)||lcd.getPixel(82,26)|| |
| el19zf | 11:494cc44777fe | 48 | lcd.getPixel(82,27)||lcd.getPixel(82,23)||lcd.getPixel(82,22)|| |
| el19zf | 11:494cc44777fe | 49 | lcd.getPixel(82,21)||lcd.getPixel(82,20)||lcd.getPixel(82,28)) |
| el19zf | 11:494cc44777fe | 50 | { |
| el19zf | 11:494cc44777fe | 51 | _check_des_index = 1; |
| el19zf | 11:494cc44777fe | 52 | //printf("reach des\n"); |
| el19zf | 11:494cc44777fe | 53 | } |
| el19zf | 11:494cc44777fe | 54 | return _check_des_index; |
| el19zf | 11:494cc44777fe | 55 | } |
| el19zf | 11:494cc44777fe | 56 | |
| el19zf | 11:494cc44777fe | 57 | void Collision::draw(N5110 &lcd) |
| el19zf | 11:494cc44777fe | 58 | { |
| el19zf | 8:8287d2ef965d | 59 | if(_health > 0) |
| el19zf | 12:009895f6b6e4 | 60 | lcd.drawRect(1,1,_health*2,3,FILL_BLACK); |
| el19zf | 8:8287d2ef965d | 61 | } |
| el19zf | 8:8287d2ef965d | 62 | |
| el19zf | 11:494cc44777fe | 63 | int Collision::get_health() |
| el19zf | 11:494cc44777fe | 64 | { |
| el19zf | 8:8287d2ef965d | 65 | return _health; |
| el19zf | 8:8287d2ef965d | 66 | } |
| el19zf | 8:8287d2ef965d | 67 | |
| el19zf | 11:494cc44777fe | 68 | int Collision::get_des() |
| el19zf | 11:494cc44777fe | 69 | { |
| el19zf | 11:494cc44777fe | 70 | return _check_des_index; |
| el19zf | 11:494cc44777fe | 71 | } |
| el19zf | 11:494cc44777fe | 72 | |
| el19zf | 11:494cc44777fe | 73 | void Collision::set_pos(Vector2D pos) |
| el19zf | 11:494cc44777fe | 74 | { |
| el19zf | 8:8287d2ef965d | 75 | _people_pos = pos; |
| el19zf | 8:8287d2ef965d | 76 | } |