ELEC2645 (2018/19) / Mbed 2 deprecated henririgby98

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SpaceInvader.cpp Source File

SpaceInvader.cpp

00001 #include "SpaceInvader.h"
00002 SpaceInvader::SpaceInvader()
00003 {
00004 
00005 }
00006 
00007 SpaceInvader::~SpaceInvader()
00008 {
00009 
00010 }
00011     
00012 void SpaceInvader::init(int height,int width)
00013 {
00014     _x = WIDTH/2 - width/2 - 2;  //sets x position to be in centre of lcd display
00015     _y = HEIGHT/2 - height/2 + 1;  //sets y position to be in centre of lcd display
00016     _height = height;
00017     _width = width;
00018     _speed = 1; //sets _speed to equal 1
00019 }
00020 
00021 void SpaceInvader::draw(N5110 &lcd)
00022 {
00023     int invader[8][11] =   {  //sprite for spaceinvader
00024     { 0,0,1,0,0,0,0,0,1,0,0 },
00025     { 0,0,0,1,0,0,0,1,0,0,0 },
00026     { 0,0,1,1,1,1,1,1,1,0,0 },
00027     { 0,1,1,0,1,1,1,0,1,1,0 },
00028     { 1,1,1,1,1,1,1,1,1,1,1 },
00029     { 1,0,1,1,1,1,1,1,1,0,1 },
00030     { 1,0,1,0,0,0,0,0,1,0,1 },
00031     { 0,0,0,1,1,0,1,1,0,0,0 },
00032 };
00033     lcd.drawSprite(_x,_y,_width,_height,(int *)invader);  //draws spaceinvader sprite
00034 }
00035 
00036 void SpaceInvader::update(Direction d,float mag)
00037 {
00038     _speed = int(mag*7.0f);  //sets _speed to equal 7 * the magnitude of joystick 
00039     if (d == N) {
00040         _y-=_speed;  //moves spaceinvader up
00041     } else if (d == S) {
00042         _y+=_speed;  //moves spaceinvader down
00043     } else if (d == W) {
00044         _x-=_speed;  //moves spaceinvader left
00045     } else if (d == E) {
00046         _x+=_speed;  //moves spaceinvader right
00047     } else if (d == NW) {
00048         _y-=((2*_speed)^1/2)/2;  //equation prevents spaceinvader strafing when moving diagonally
00049         _x-=((2*_speed)^1/2)/2;  //moves spaceinvader up and to the left
00050     } else if (d == SW) {
00051         _y+=((2*_speed)^1/2)/2;
00052         _x-=((2*_speed)^1/2)/2;  //moves spaceinvader down and to the left
00053     } else if (d == NE) {
00054         _y-=((2*_speed)^1/2)/2;
00055         _x+=((2*_speed)^1/2)/2;  //moves spaceinvader up and to the right
00056     } else if (d == SE) {
00057         _y+=((2*_speed)^1/2)/2;
00058         _x+=((2*_speed)^1/2)/2;  //moves spaceinvader down and to the right
00059     }
00060     if (_x < 1) {
00061         _x = 1;  //prevents spaceinvader going offscreen to the left
00062     }
00063     if (_x > WIDTH - _width - 4) {
00064         _x = WIDTH - _width - 4;  //prevents spaceinvader going offscreen to the right
00065     }
00066     if (_y < 1) {
00067         _y = 1;  //prevents spaceinvader going offscreen at the top
00068     }
00069     if (_y > HEIGHT - _height + 2) {
00070         _y = HEIGHT - _height + 2;  //prevents spaceinvader going offscreen at the bottom
00071     }
00072 }
00073 
00074 Vector2D SpaceInvader::get_pos() {
00075     Vector2D p = {_x,_y};  //sets the value of position by taking both x and y coordinates
00076     return p;    
00077 }