Dependencies: mbed
Bullet/Bullet.cpp@16:f8a6834a0289, 2020-05-29 (annotated)
- Committer:
- ChenZirui
- Date:
- Fri May 29 07:39:20 2020 +0000
- Revision:
- 16:f8a6834a0289
- Parent:
- 15:7ca2d1b2bd0e
Final submission . I have read and agreed with Statement of Academic Integrity
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ChenZirui | 5:7207c9b70108 | 1 | #include "Bullet.h" |
ChenZirui | 16:f8a6834a0289 | 2 | //Bullet part |
ChenZirui | 5:7207c9b70108 | 3 | |
ChenZirui | 15:7ca2d1b2bd0e | 4 | void Bullet::init(int x,int size,int speed,int y) |
ChenZirui | 5:7207c9b70108 | 5 | { |
ChenZirui | 15:7ca2d1b2bd0e | 6 | _size = size; // the size of bullet |
ChenZirui | 5:7207c9b70108 | 7 | |
ChenZirui | 15:7ca2d1b2bd0e | 8 | _x = x; //initial horizontal coordinate |
ChenZirui | 15:7ca2d1b2bd0e | 9 | _y = y; //initial vertical coordinate |
ChenZirui | 5:7207c9b70108 | 10 | |
ChenZirui | 5:7207c9b70108 | 11 | srand(time(NULL)); |
ChenZirui | 7:f61ac963eb07 | 12 | int direction = rand() % 2; // randomise initial direction. |
ChenZirui | 5:7207c9b70108 | 13 | |
ChenZirui | 15:7ca2d1b2bd0e | 14 | // 2 random direction |
ChenZirui | 5:7207c9b70108 | 15 | if (direction == 0) { |
ChenZirui | 5:7207c9b70108 | 16 | _velocity.x = speed; |
ChenZirui | 7:f61ac963eb07 | 17 | _velocity.y = -speed; |
ChenZirui | 5:7207c9b70108 | 18 | } else if (direction == 1) { |
ChenZirui | 5:7207c9b70108 | 19 | _velocity.x = -speed; |
ChenZirui | 5:7207c9b70108 | 20 | _velocity.y = -speed; |
ChenZirui | 7:f61ac963eb07 | 21 | } |
ChenZirui | 5:7207c9b70108 | 22 | } |
ChenZirui | 5:7207c9b70108 | 23 | |
ChenZirui | 15:7ca2d1b2bd0e | 24 | void Bullet::draw(N5110 &lcd) // bullet drawing function |
ChenZirui | 5:7207c9b70108 | 25 | { |
ChenZirui | 5:7207c9b70108 | 26 | lcd.drawRect(_x,_y,_size,_size,FILL_BLACK); |
ChenZirui | 5:7207c9b70108 | 27 | } |
ChenZirui | 5:7207c9b70108 | 28 | |
ChenZirui | 15:7ca2d1b2bd0e | 29 | void Bullet::update(N5110 &lcd) // bullet updating |
ChenZirui | 5:7207c9b70108 | 30 | { |
ChenZirui | 5:7207c9b70108 | 31 | _x += _velocity.x; |
ChenZirui | 5:7207c9b70108 | 32 | _y += _velocity.y; |
ChenZirui | 15:7ca2d1b2bd0e | 33 | |
ChenZirui | 5:7207c9b70108 | 34 | } |
ChenZirui | 15:7ca2d1b2bd0e | 35 | void Bullet::set_velocity(Vector2D v) //bullet velocity setting |
ChenZirui | 5:7207c9b70108 | 36 | { |
ChenZirui | 5:7207c9b70108 | 37 | _velocity.x = v.x; |
ChenZirui | 5:7207c9b70108 | 38 | _velocity.y = v.y; |
ChenZirui | 5:7207c9b70108 | 39 | } |
ChenZirui | 5:7207c9b70108 | 40 | |
ChenZirui | 15:7ca2d1b2bd0e | 41 | Vector2D Bullet::get_velocity() //bullet velocity reading |
ChenZirui | 5:7207c9b70108 | 42 | { |
ChenZirui | 5:7207c9b70108 | 43 | Vector2D v = {_velocity.x,_velocity.y}; |
ChenZirui | 5:7207c9b70108 | 44 | return v; |
ChenZirui | 5:7207c9b70108 | 45 | } |
ChenZirui | 5:7207c9b70108 | 46 | |
ChenZirui | 15:7ca2d1b2bd0e | 47 | Vector2D Bullet::get_pos() //bullet position reading |
ChenZirui | 5:7207c9b70108 | 48 | { |
ChenZirui | 5:7207c9b70108 | 49 | Vector2D p = {_x,_y}; |
ChenZirui | 5:7207c9b70108 | 50 | return p; |
ChenZirui | 5:7207c9b70108 | 51 | } |
ChenZirui | 5:7207c9b70108 | 52 | |
ChenZirui | 15:7ca2d1b2bd0e | 53 | void Bullet::set_pos(Vector2D p) // bullet position setting |
ChenZirui | 5:7207c9b70108 | 54 | { |
ChenZirui | 5:7207c9b70108 | 55 | _x = p.x; |
ChenZirui | 5:7207c9b70108 | 56 | _y = p.y; |
ChenZirui | 5:7207c9b70108 | 57 | } |