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
Diff: Bullet/Bullet.cpp
- Revision:
- 5:7207c9b70108
- Child:
- 6:b393cfe4e0a7
diff -r e46c295d4baf -r 7207c9b70108 Bullet/Bullet.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Bullet/Bullet.cpp Wed May 27 21:13:59 2020 +0000
@@ -0,0 +1,84 @@
+#include "Bullet.h"
+
+Bullet::Bullet()
+{
+
+}
+
+Bullet::~Bullet()
+{
+
+}
+
+void Bullet::init(int x,int size,int speed,int height)
+{
+ _size = size;
+
+ _x = x+(height)*0.5;
+ _y = HEIGHT - size;
+
+ srand(time(NULL));
+ int direction = rand() % 4; // randomise initial direction.
+
+ // 4 possibilities. Get random modulo and set velocities accordingly
+ if (direction == 0) {
+ _velocity.x = speed;
+ _velocity.y = speed;
+ } else if (direction == 1) {
+ _velocity.x = speed;
+ _velocity.y = -speed;
+ } else if (direction == 2) {
+ _velocity.x = speed;
+ _velocity.y = speed;
+ } else {
+ _velocity.x = -speed;
+ _velocity.y = -speed;
+ }
+}
+
+void Bullet::draw(N5110 &lcd)
+{
+ lcd.drawRect(_x,_y,_size,_size,FILL_BLACK);
+ if((_y >= 0)&&(_y <= 24))
+ {
+ // lcd.clearPixel(_x,_y-1);
+
+ }
+ //lcd.drawRect(0,0,84,24,FILL_BLACK);
+ // lcd.clearPixel(_x,_y);
+}
+
+void Bullet::update(N5110 &lcd)
+{
+ _x += _velocity.x;
+ _y += _velocity.y;
+ if((_y >= 0)&&(_y <= 24))
+ {
+// lcd.clearPixel(_x,_y-1);
+
+ }
+}
+
+void Bullet::set_velocity(Vector2D v)
+{
+ _velocity.x = v.x;
+ _velocity.y = v.y;
+}
+
+Vector2D Bullet::get_velocity()
+{
+ Vector2D v = {_velocity.x,_velocity.y};
+ return v;
+}
+
+Vector2D Bullet::get_pos()
+{
+ Vector2D p = {_x,_y};
+ return p;
+}
+
+void Bullet::set_pos(Vector2D p)
+{
+ _x = p.x;
+ _y = p.y;
+}
\ No newline at end of file