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: SpaceInvader/SpaceInvader.h
- Revision:
- 16:3cb5c59ae7e8
- Parent:
- 5:0da65740cd5e
- Child:
- 20:477d2ee5e461
diff -r 286dde533aba -r 3cb5c59ae7e8 SpaceInvader/SpaceInvader.h
--- a/SpaceInvader/SpaceInvader.h Thu May 09 10:37:13 2019 +0000
+++ b/SpaceInvader/SpaceInvader.h Thu May 09 11:07:26 2019 +0000
@@ -5,15 +5,124 @@
#include "N5110.h"
#include "Gamepad.h"
+/** SpaceInvader class
+
+@brief Class for Spaceinvader
+
+@version 1.0
+
+@author Henri Rigby
+
+@date May 2019
+
+@code
+
+#include "SpaceInvader.h"
+SpaceInvader::SpaceInvader()
+{
+
+}
+
+SpaceInvader::~SpaceInvader()
+{
+
+}
+
+void SpaceInvader::init(int height,int width)
+{
+ _x = WIDTH/2 - width/2 - 2; //sets x position to be in centre of lcd display
+ _y = HEIGHT/2 - height/2 + 1; //sets y position to be in centre of lcd display
+ _height = height;
+ _width = width;
+ _speed = 1; //sets _speed to equal 1
+}
+
+void SpaceInvader::draw(N5110 &lcd)
+{
+ int invader[8][11] = { //sprite for spaceinvader
+ { 0,0,1,0,0,0,0,0,1,0,0 },
+ { 0,0,0,1,0,0,0,1,0,0,0 },
+ { 0,0,1,1,1,1,1,1,1,0,0 },
+ { 0,1,1,0,1,1,1,0,1,1,0 },
+ { 1,1,1,1,1,1,1,1,1,1,1 },
+ { 1,0,1,1,1,1,1,1,1,0,1 },
+ { 1,0,1,0,0,0,0,0,1,0,1 },
+ { 0,0,0,1,1,0,1,1,0,0,0 },
+};
+ lcd.drawSprite(_x,_y,_width,_height,(int *)invader); //draws spaceinvader sprite
+}
+
+void SpaceInvader::update(Direction d,float mag)
+{
+ _speed = int(mag*7.0f); //sets _speed to equal 7 * the magnitude of joystick
+ if (d == N) {
+ _y-=_speed; //moves spaceinvader up
+ } else if (d == S) {
+ _y+=_speed; //moves spaceinvader down
+ } else if (d == W) {
+ _x-=_speed; //moves spaceinvader left
+ } else if (d == E) {
+ _x+=_speed; //moves spaceinvader right
+ } else if (d == NW) {
+ _y-=((2*_speed)^1/2)/2; //equation prevents spaceinvader strafing when moving diagonally
+ _x-=((2*_speed)^1/2)/2; //moves spaceinvader up and to the left
+ } else if (d == SW) {
+ _y+=((2*_speed)^1/2)/2;
+ _x-=((2*_speed)^1/2)/2; //moves spaceinvader down and to the left
+ } else if (d == NE) {
+ _y-=((2*_speed)^1/2)/2;
+ _x+=((2*_speed)^1/2)/2; //moves spaceinvader up and to the right
+ } else if (d == SE) {
+ _y+=((2*_speed)^1/2)/2;
+ _x+=((2*_speed)^1/2)/2; //moves spaceinvader down and to the right
+ }
+ if (_x < 1) {
+ _x = 1; //prevents spaceinvader going offscreen to the left
+ }
+ if (_x > WIDTH - _width - 4) {
+ _x = WIDTH - _width - 4; //prevents spaceinvader going offscreen to the right
+ }
+ if (_y < 1) {
+ _y = 1; //prevents spaceinvader going offscreen at the top
+ }
+ if (_y > HEIGHT - _height + 2) {
+ _y = HEIGHT - _height + 2; //prevents spaceinvader going offscreen at the bottom
+ }
+}
+
+Vector2D SpaceInvader::get_pos() {
+ Vector2D p = {_x,_y}; //sets the value of position by taking both x and y coordinates
+ return p;
+}
+
+@endcode
+*/
+
class SpaceInvader
{
public:
SpaceInvader();
~SpaceInvader();
+ /**
+ * @brief Sets spaceinvader starting position and speed
+ * @param sets position and speed @details Sets starting position and speed of the spaceinvader
+ */
void init(int height,int width);
+ /**
+ * @brief Draw the spaceinvader
+ * @return draws spaceinvader @details Uses designed sprite to draw a spaceinvader
+ */
void draw(N5110 &lcd);
+ /**
+ * @brief Updates the position of the spaceinvader
+ * @param sets position @details Uses movement of joystick to set position of spaceinvader
+ */
void update(Direction d,float mag);
+ /**
+ * @brief Gets the position
+ * @return position @details returns the position of both the x and y coordinates
+ */
Vector2D get_pos();
private: