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.
Revision 2:fe4b9d530a8b, committed 2020-04-29
- Comitter:
- godv
- Date:
- Wed Apr 29 04:56:07 2020 +0000
- Parent:
- 1:a321707cc6df
- Commit message:
- 111
Changed in this revision
--- a/Ball/Ball.cpp Mon Apr 27 15:20:40 2020 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,72 +0,0 @@ -#include "Ball.h" - -Ball::Ball() -{ - -} - -Ball::~Ball() -{ - -} - -void Ball::init(int size,int speed) -{ - _size = size; - - _x = WIDTH/2 - _size/2; - _y = HEIGHT/2 - _size/2; - - 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 Ball::draw(N5110 &lcd) -{ - lcd.drawRect(_x,_y,_size,_size,FILL_BLACK); -} - -void Ball::update() -{ - _x += _velocity.x; - _y += _velocity.y; -} - -void Ball::set_velocity(Vector2D v) -{ - _velocity.x = v.x; - _velocity.y = v.y; -} - -Vector2D Ball::get_velocity() -{ - Vector2D v = {_velocity.x,_velocity.y}; - return v; -} - -Vector2D Ball::get_pos() -{ - Vector2D p = {_x,_y}; - return p; -} - -void Ball::set_pos(Vector2D p) -{ - _x = p.x; - _y = p.y; -} \ No newline at end of file
--- a/Ball/Ball.h Mon Apr 27 15:20:40 2020 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,36 +0,0 @@ -#ifndef BALL_H -#define BALL_H - -#include "mbed.h" -#include "N5110.h" -#include "Gamepad.h" -#include "Paddle.h" - -/** Ball Class -@author Dr Craig A. Evans, University of Leeds -@brief Controls the ball in the Pong game -@date Febraury 2017 -*/ -class Ball -{ - -public: - Ball(); - ~Ball(); - void init(int size,int speed); - void draw(N5110 &lcd); - void update(); - /// accessors and mutators - void set_velocity(Vector2D v); - Vector2D get_velocity(); - Vector2D get_pos(); - void set_pos(Vector2D p); - -private: - - Vector2D _velocity; - int _size; - int _x; - int _y; -}; -#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EscapeEngine/EscapeEngine.cpp Wed Apr 29 04:56:07 2020 +0000 @@ -0,0 +1,205 @@ +#include "EscapeEngine.h" + +EscapeEngine::EscapeEngine() +{ + +} + +EscapeEngine::~EscapeEngine() +{ + +} + +void EscapeEngine::init(int redspider_width,int redspider_height,int robot_width, int robot_height, int missile_width, int missile_height, int allspark_size, int speed) +{ + // initialise the game parameters + _redspider_width = redspider_width; + _redspider_height = redspider_height; + _robot_width = robot_width; + _robot_height = robot_height; + _missile_width = missile_width; + _missile_height = missile_height; + _allspark_size = allspark_size; + _speed = speed; + + // x position on screen - WIDTH is defined in N5110.h + _redspiderx = GAP; + + // Initializing Human,Zombie,Worm,Bat and Bullet + _redspider.init(_redspiderx,_redspider_height,_redspider_width); + _robot.init(_robot_height,_robot_width,_speed); + _missile.init(_missile_height,_missile_width,_speed); + _allspark.init(_allspark_size,_speed); +} + +void EscapeEngine::read_input(Gamepad &pad) +{ + _d = pad.get_direction(); + _mag = pad.get_mag(); +} + + + +void EscapeEngine::draw(N5110 &lcd) +{ + // draw the elements in the LCD buffer + lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); + lcd.drawLine(WIDTH/4,0,WIDTH/4,HEIGHT-1,2); + //score + print_scores(lcd); + // Human + _redspider.draw(lcd); + // Zombie + _robot.draw(lcd); + // Bullet + _missile.draw(lcd); + _allspark.draw(lcd); + +} + +void EscapeEngine::update(Gamepad &pad) +{ + check_goal(pad); + _redspider.update(_d,_mag); + _robot.update(); + _missile.update(); + _allspark.update(); + check_redspider_collisions(pad); + check_wall_collisions(pad); + +} + + + +void EscapeEngine::check_redspider_collisions(Gamepad &pad) +{ + // read current Zombie attributes + Vector2D allspark_pos = _allspark.get_pos(); + Vector2D allspark_velocity = _allspark.get_velocity(); + Vector2D redspider_pos = _redspider.get_pos(); + Vector2D redspider_velocity = _redspider.get_velocity(); + + // see if Zombie has hit the area of Human by checking for overlaps + + if ((redspider_pos.x <= allspark_pos.x)&& + (redspider_pos.y >= allspark_pos.y )&& + (redspider_pos.y <= allspark_pos.y + 8 )) + { + _redspider.add_score(); + _allspark.init(_allspark_size,_speed); + pad.tone(1500.0,0.5); + pad.leds_on(); + wait(0.5); + pad.leds_off(); + } + + + + + + + + // write new attributes + _redspider.set_velocity(redspider_velocity); + _redspider.set_pos(redspider_pos); +} + +void EscapeEngine::check_wall_collisions(Gamepad &pad) +{ + // read current Bat attributes + Vector2D allspark_pos = _allspark.get_pos(); + Vector2D allspark_velocity = _allspark.get_velocity(); + + + // see if Bat has hit the area of Human by checking for overlaps + if (allspark_pos.y <= 1) { // 1 due to 1 pixel boundary + allspark_pos.y = 1; // bounce off ceiling without going off screen + allspark_velocity.y = -allspark_velocity.y; + // audio feedback + pad.tone(750.0,0.1); + } + // check if hit bottom wall + else if (allspark_pos.y + _allspark_size >= (HEIGHT-1) ) { // bottom pixel is 47 + // hit bottom + allspark_pos.y = (HEIGHT-1) - _allspark_size; // stops ball going off screen + allspark_velocity.y = -allspark_velocity.y; + // audio feedback + pad.tone(750.0,0.1); + } + if (allspark_pos.x <= 1) { // 1 due to 1 pixel boundary + allspark_pos.x = 1; // bounce off ceiling without going off screen + allspark_velocity.x = -allspark_velocity.x; + // audio feedback + pad.tone(750.0,0.1); + } + // check if hit bottom wall + else if (allspark_pos.x + _allspark_size >= (WIDTH-1) ) { // bottom pixel is 47 + // hit bottom + allspark_pos.x = (WIDTH-1) - _allspark_size; // stops ball going off screen + allspark_velocity.x = -allspark_velocity.x; + // audio feedback + pad.tone(750.0,0.1); + } + // update ball parameters + _allspark.set_velocity(allspark_velocity); + _allspark.set_pos(allspark_pos); +} + + + +void EscapeEngine::check_goal(Gamepad &pad) +{ + Vector2D robot_pos = _robot.get_pos(); + Vector2D missile_pos = _missile.get_pos(); + Vector2D redspider_pos = _redspider.get_pos(); + Vector2D robot_velocity = _robot.get_velocity(); + Vector2D missile_velocity = _missile.get_velocity(); + Vector2D allspark_pos = _allspark.get_pos(); + + + // Human has scored + if ((redspider_pos.x <= allspark_pos.x)&& + (redspider_pos.y >= allspark_pos.y ) + + ){ + _redspider.add_score(); + robot_pos.x = WIDTH ; + robot_pos.y = rand() % 32; + missile_pos.x = robot_pos.x + 20; + missile_pos.y = robot_pos.y + 4; + _allspark.init(_allspark_size,_speed); + pad.tone(1000.0,0.25); + wait(0.1); + } + + + + else if((missile_pos.x > redspider_pos.x) + ){ missile_pos.x = redspider_pos.x + 20; + missile_pos.y = redspider_pos.y + 4; + } + + + + _robot.set_velocity(robot_velocity); + _robot.set_pos(robot_pos); + _missile.set_velocity(missile_velocity); + _missile.set_pos(missile_pos); + +} + +void EscapeEngine::print_scores(N5110 &lcd) +{ + + int redspider_score = _redspider.get_score(); + + + // print to LCD + char buffer1[14]; + sprintf(buffer1,"%2d",redspider_score); + lcd.printString("SCORE",WIDTH/2 - 25,0); + lcd.printString(buffer1,WIDTH/2 - 20,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits + + + + } \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EscapeEngine/EscapeEngine.h Wed Apr 29 04:56:07 2020 +0000 @@ -0,0 +1,77 @@ +#ifndef ESCAPEENGINE_H +#define ESCAPEENGINE_H + +#include "mbed.h" +#include "N5110.h" +#include "Gamepad.h" +#include "robot.h" +#include "redspider.h" +#include "missile.h" +#include "allspark.h" + + +#define GAP 2 + + +class EscapeEngine +{ + +public: + + /** Constructor */ + EscapeEngine(); + + /** Destructor */ + ~EscapeEngine(); + + /** Initialization + *@param the values of height,width and speed(int) + */ + void init(int redspider_width,int redspider_height,int robot_width, int robot_height,int missile_width, int missile_height , int allspark_size, int speed); + + /** read input of Gamepad */ + void read_input(Gamepad &pad); + + /** Update the values */ + void update(Gamepad &pad); + + /** draw patterns on lcd*/ + void draw(N5110 &lcd); + + int redspider_score; + +private: + + void check_redspider_collisions(Gamepad &pad); + void check_wall_collisions(Gamepad &pad); + void check_goal(Gamepad &pad); + void print_scores(N5110 &lcd); + + redspider _redspider; + + int _redspider_width; + int _redspider_height; + int _speed; + + // x positions of the Human + int _redspiderx; + + robot _robot; + int _robot_width; + int _robot_height; + + missile _missile; + int _missile_width; + int _missile_height; + + allspark _allspark; + int _allspark_size; + + + + Direction _d; + float _mag; + +}; + +#endif \ No newline at end of file
--- a/Paddle/Paddle.cpp Mon Apr 27 15:20:40 2020 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,64 +0,0 @@ -#include "Paddle.h" - -// nothing doing in the constructor and destructor -Paddle::Paddle() -{ - -} - -Paddle::~Paddle() -{ - -} - -void Paddle::init(int x,int height,int width) -{ - _x = x; // x value on screen is fixed - _y = HEIGHT/2 - height/2; // y depends on height of screen and height of paddle - _height = height; - _width = width; - _speed = 1; // default speed - _score = 0; // start score from zero - -} - -void Paddle::draw(N5110 &lcd) -{ - // draw paddle in screen buffer. - lcd.drawRect(_x,_y,_width,_height,FILL_BLACK); -} - -void Paddle::update(Direction d,float mag) -{ - _speed = int(mag*10.0f); // scale is arbitrary, could be changed in future - - // update y value depending on direction of movement - // North is decrement as origin is at the top-left so decreasing moves up - if (d == N) { - _y-=_speed; - } else if (d == S) { - _y+=_speed; - } - - // check the y origin to ensure that the paddle doesn't go off screen - if (_y < 1) { - _y = 1; - } - if (_y > HEIGHT - _height - 1) { - _y = HEIGHT - _height - 1; - } -} - -void Paddle::add_score() -{ - _score++; -} -int Paddle::get_score() -{ - return _score; -} - -Vector2D Paddle::get_pos() { - Vector2D p = {_x,_y}; - return p; -} \ No newline at end of file
--- a/Paddle/Paddle.h Mon Apr 27 15:20:40 2020 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,31 +0,0 @@ -#ifndef PADDLE_H -#define PADDLE_H - -#include "mbed.h" -#include "N5110.h" -#include "Gamepad.h" - -class Paddle -{ -public: - - Paddle(); - ~Paddle(); - void init(int x,int height,int width); - void draw(N5110 &lcd); - void update(Direction d,float mag); - void add_score(); - int get_score(); - Vector2D get_pos(); - -private: - - int _height; - int _width; - int _x; - int _y; - int _speed; - int _score; - -}; -#endif \ No newline at end of file
--- a/PongEngine/PongEngine.cpp Mon Apr 27 15:20:40 2020 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,174 +0,0 @@ -#include "PongEngine.h" - -PongEngine::PongEngine() -{ - -} - -PongEngine::~PongEngine() -{ - -} - -void PongEngine::init(int paddle_width,int paddle_height,int ball_size,int speed) -{ - // initialise the game parameters - _paddle_width = paddle_width; - _paddle_height = paddle_height; - _ball_size = ball_size; - _speed = speed; - - // x position on screen - WIDTH is defined in N5110.h - _p1x = GAP; - _p2x = WIDTH - GAP - _paddle_width; - - // puts paddles and ball in middle - _p1.init(_p1x,_paddle_height,_paddle_width); - _p2.init(_p2x,_paddle_height,_paddle_width); - _ball.init(_ball_size,_speed); -} - -void PongEngine::read_input(Gamepad &pad) -{ - _d = pad.get_direction(); - _mag = pad.get_mag(); -} - -void PongEngine::draw(N5110 &lcd) -{ - // draw the elements in the LCD buffer - // pitch - lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); - lcd.drawLine(WIDTH/2,0,WIDTH/2,HEIGHT-1,2); - //score - print_scores(lcd); - // paddles - _p1.draw(lcd); - _p2.draw(lcd); - // ball - _ball.draw(lcd); -} - -void PongEngine::update(Gamepad &pad) -{ - check_goal(pad); - // important to update paddles and ball before checking collisions so can - // correct for it before updating the display - _p1.update(_d,_mag); - _p2.update(_d,_mag); - _ball.update(); - - check_wall_collision(pad); - check_paddle_collisions(pad); -} - -void PongEngine::check_wall_collision(Gamepad &pad) -{ - // read current ball attributes - Vector2D ball_pos = _ball.get_pos(); - Vector2D ball_velocity = _ball.get_velocity(); - - // check if hit top wall - if (ball_pos.y <= 1) { // 1 due to 1 pixel boundary - ball_pos.y = 1; // bounce off ceiling without going off screen - ball_velocity.y = -ball_velocity.y; - // audio feedback - pad.tone(750.0,0.1); - } - // check if hit bottom wall - else if (ball_pos.y + _ball_size >= (HEIGHT-1) ) { // bottom pixel is 47 - // hit bottom - ball_pos.y = (HEIGHT-1) - _ball_size; // stops ball going off screen - ball_velocity.y = -ball_velocity.y; - // audio feedback - pad.tone(750.0,0.1); - } - - // update ball parameters - _ball.set_velocity(ball_velocity); - _ball.set_pos(ball_pos); -} - -void PongEngine::check_paddle_collisions(Gamepad &pad) -{ - // read current ball attributes - Vector2D ball_pos = _ball.get_pos(); - Vector2D ball_velocity = _ball.get_velocity(); - - // check p1 first - Vector2D p1_pos = _p1.get_pos(); - - // see if ball has hit the paddle by checking for overlaps - if ( - (ball_pos.y >= p1_pos.y) && //top - (ball_pos.y <= p1_pos.y + _paddle_height) && //bottom - (ball_pos.x >= _p1x) && //left - (ball_pos.x <= _p1x + _paddle_width) //right - ) { - // if it has, fix position and reflect x velocity - ball_pos.x = _p1x + _paddle_width; - ball_velocity.x = -ball_velocity.x; - // audio feedback - pad.tone(1000.0,0.1); - } - - // check p2 next - Vector2D p2_pos = _p2.get_pos(); - - // see if ball has hit the paddle by checking for overlaps - if ( - (ball_pos.y >= p2_pos.y) && //top - (ball_pos.y <= p2_pos.y + _paddle_height) && //bottom - (ball_pos.x + _ball_size >= _p2x) && //left - (ball_pos.x + _ball_size <= _p2x + _paddle_width) //right - ) { - // if it has, fix position and reflect x velocity - ball_pos.x = _p2x - _ball_size; - ball_velocity.x = -ball_velocity.x; - // audio feedback - pad.tone(1000.0,0.1); - } - - // write new attributes - _ball.set_velocity(ball_velocity); - _ball.set_pos(ball_pos); -} - -void PongEngine::check_goal(Gamepad &pad) -{ - Vector2D ball_pos = _ball.get_pos(); - // P2 has scored - if (ball_pos.x + _ball_size < 0) { - _p2.add_score(); - _ball.init(_ball_size,_speed); - pad.tone(1500.0,0.5); - pad.leds_on(); - wait(0.5); - pad.leds_off(); - } - - // P1 has scored - if (ball_pos.x > WIDTH) { - _p1.add_score(); - _ball.init(_ball_size,_speed); - pad.tone(1500.0,0.5); - pad.leds_on(); - wait(0.5); - pad.leds_off(); - } -} - -void PongEngine::print_scores(N5110 &lcd) -{ - // get scores from paddles - int p1_score = _p1.get_score(); - int p2_score = _p2.get_score(); - - // print to LCD i - char buffer1[14]; - sprintf(buffer1,"%2d",p1_score); - lcd.printString(buffer1,WIDTH/2 - 20,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits - char buffer2[14]; - sprintf(buffer2,"%2d",p2_score); - lcd.printString(buffer2,WIDTH/2 + 4,1); -} \ No newline at end of file
--- a/PongEngine/PongEngine.h Mon Apr 27 15:20:40 2020 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,51 +0,0 @@ -#ifndef PONGENGINE_H -#define PONGENGINE_H - -#include "mbed.h" -#include "N5110.h" -#include "Gamepad.h" -#include "Ball.h" -#include "Paddle.h" - -// gap from edge of screen -#define GAP 2 - -class PongEngine -{ - -public: - PongEngine(); - ~PongEngine(); - - void init(int paddle_width,int paddle_height,int ball_size,int speed); - void read_input(Gamepad &pad); - void update(Gamepad &pad); - void draw(N5110 &lcd); - -private: - - void check_wall_collision(Gamepad &pad); - void check_paddle_collisions(Gamepad &pad); - void check_goal(Gamepad &pad); - void print_scores(N5110 &lcd); - - Paddle _p1; - Paddle _p2; - - int _paddle_width; - int _paddle_height; - int _ball_size; - int _speed; - - // x positions of the paddles - int _p1x; - int _p2x; - - Ball _ball; - - Direction _d; - float _mag; - -}; - -#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/allspark/allspark.cpp Wed Apr 29 04:56:07 2020 +0000 @@ -0,0 +1,72 @@ +#include "allspark.h" + +allspark::allspark() +{ + +} + +allspark::~allspark() +{ + +} + +void allspark::init(int size,int speed) +{ + _size = size; + + _x = WIDTH/2 - _size/2; + _y = HEIGHT/2 - _size/2; + + 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 allspark::draw(N5110 &lcd) +{ + lcd.drawRect(_x,_y,_size,_size,FILL_BLACK); +} + +void allspark::update() +{ + _x += _velocity.x; + _y += _velocity.y; +} + +void allspark::set_velocity(Vector2D v) +{ + _velocity.x = v.x; + _velocity.y = v.y; +} + +Vector2D allspark::get_velocity() +{ + Vector2D v = {_velocity.x,_velocity.y}; + return v; +} + +Vector2D allspark::get_pos() +{ + Vector2D p = {_x,_y}; + return p; +} + +void allspark::set_pos(Vector2D p) +{ + _x = p.x; + _y = p.y; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/allspark/allspark.h Wed Apr 29 04:56:07 2020 +0000 @@ -0,0 +1,36 @@ +#ifndef ALLSPARK_H +#define ALLSPARK_H + +#include "mbed.h" +#include "N5110.h" +#include "Gamepad.h" +#include "redspider.h" + +/** allspark Class +@author Gao Dawei +@brief allspark +@date 27/4/2020 +*/ +class allspark +{ + +public: + allspark(); + ~allspark(); + void init(int size,int speed); + void draw(N5110 &lcd); + void update(); + /// accessors and mutators + void set_velocity(Vector2D v); + Vector2D get_velocity(); + Vector2D get_pos(); + void set_pos(Vector2D p); + +private: + + Vector2D _velocity; + int _size; + int _x; + int _y; +}; +#endif \ No newline at end of file
--- a/main.cpp Mon Apr 27 15:20:40 2020 +0000 +++ b/main.cpp Wed Apr 29 04:56:07 2020 +0000 @@ -1,18 +1,28 @@ +/* +ELEC2645 Embedded Systems Project +School of Electronic & Electrical Engineering +University of Leeds +Name: Gao Dawei +Username: godv +Student ID Number: 201199417 +Date: 27/04/2020 + */ + ///////// pre-processor directives //////// #include "mbed.h" #include "Gamepad.h" #include "N5110.h" -#include "PongEngine.h" +#include "EscapeEngine.h" -#ifdef WITH_TESTING -# include "tests.h" -#endif -#define PADDLE_WIDTH 2 -#define PADDLE_HEIGHT 8 -#define BALL_SIZE 2 -#define BALL_SPEED 3 - +#define REDSPIDER_WIDTH 5 +#define REDSPIDER_HEIGHT 7 +#define ROBOT_WIDTH 8 +#define ROBOT_HEIGHT 8 +#define ROBOT_SPEED 2 +#define MISSILE_HEIGHT 2 +#define MISSILE_WIDTH 4 +#define ALLSPARK_SIZE 1 /////////////// structs ///////////////// struct UserInput { Direction d; @@ -21,41 +31,47 @@ /////////////// objects /////////////// N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); Gamepad pad; -PongEngine pong; - +EscapeEngine Escape; + ///////////// prototypes /////////////// void init(); void update_game(UserInput input); void render(); void welcome(); - +void alive(); +void intro(); +void gameover(); +int redspider_score; ///////////// functions //////////////// int main() { -#ifdef WITH_TESTING - int number_of_failures = run_all_tests(); - - if(number_of_failures > 0) return number_of_failures; -#endif - int fps = 8; // frames per second - - init(); // initialise and then display welcome screen... - welcome(); // waiting for the user to start + lcd.setContrast(0.5); + lcd.setBrightness(0.7); + L1:init(); + intro(); + welcome(); // waiting for the user to start //Introduction the background render(); // first draw the initial frame wait(1.0f/fps); // and wait for one frame period // game loop - read input, update the game state and render the display - while (1) { - pong.read_input(pad); - pong.update(pad); + do{ + + Escape.read_input(pad); + Escape.update(pad); render(); wait(1.0f/fps); - } + if(1){ + alive(); + } + }while(1); + wait(0.5); + gameover(); + goto L1; + } - // initialies all classes and libraries void init() { @@ -63,8 +79,8 @@ lcd.init(); pad.init(); - // initialise the game with correct ball and paddle sizes - pong.init(PADDLE_WIDTH,PADDLE_HEIGHT,BALL_SIZE,BALL_SPEED); + // initialise the game with correct bullet zombie and human sizes + Escape.init(REDSPIDER_WIDTH,REDSPIDER_HEIGHT,ROBOT_WIDTH,ROBOT_HEIGHT,MISSILE_WIDTH,MISSILE_HEIGHT,ROBOT_SPEED,ALLSPARK_SIZE); } @@ -73,23 +89,57 @@ { // clear screen, re-draw and refresh lcd.clear(); - pong.draw(lcd); + Escape.draw(lcd); + lcd.refresh(); +} + +void intro() +{ + lcd.clear(); + lcd.printString("escape",0,2); lcd.refresh(); + wait(1.5); + + + } + +void alive() +{ + + lcd.printString("Megatron was pleased",18,4); + lcd.refresh(); } // simple splash screen displayed on start-up void welcome() { + while ( pad.check_event(Gamepad::START_PRESSED) == false ) { + lcd.printString("for your life and glory",0,2); + wait(1.5); + lcd.printString("press start",0,4); + lcd.refresh(); + pad.leds_on(); + wait(0.1); + pad.leds_off(); + wait(0.1); +} + lcd.refresh(); +} + +void gameover() +{ + lcd.clear(); - lcd.printString(" Pong! ",0,1); - lcd.printString(" Press Start ",0,4); - lcd.refresh(); - - // wait flashing LEDs until start button is pressed - while ( pad.check_event(Gamepad::START_PRESSED) == false) { - pad.leds_on(); - wait(0.1); - pad.leds_off(); - wait(0.1); + while ( pad.check_event(Gamepad::START_PRESSED) == false && pad.check_event(Gamepad::BACK_PRESSED) == false) { + pad.leds_on(); + wait(0.1); + pad.leds_off(); + wait(0.1); } + lcd.printString(" escape from Cybertron ",35,0); + lcd.printString(" START ",25,5); + + + lcd.refresh(); + } \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/missile/missile.cpp Wed Apr 29 04:56:07 2020 +0000 @@ -0,0 +1,64 @@ +#include "missile.h" + +missile::missile() +{ + +} + +missile::~missile() +{ + +} + + +void missile::init(int height,int width,int speed) +{ + Vector2D robot_pos = _robot.get_pos(); + _width = width; + _height = height; + _x = robot_pos.x + 20; + _y = robot_pos.y + 4; + + srand(time(NULL)); + + + _velocity.x = 2*speed; + _velocity.y = 0; +} + + +void missile::draw(N5110 &lcd) +{ + lcd.drawRect(_x,_y,_width,_height,FILL_BLACK); + +} + +void missile::update() +{ + _x += _velocity.x; + _y += _velocity.y; +} + +void missile::set_velocity(Vector2D v) +{ + _velocity.x = v.x; + _velocity.y = v.y; +} + +Vector2D missile::get_velocity() +{ + Vector2D v = {_velocity.x,_velocity.y}; + return v; +} + +Vector2D missile::get_pos() +{ + Vector2D p = {_x,_y}; + return p; +} + +void missile::set_pos(Vector2D p) +{ + _x = p.x; + _y = p.y; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/missile/missile.h Wed Apr 29 04:56:07 2020 +0000 @@ -0,0 +1,56 @@ +#ifndef MISSILE_H +#define MISSILE_H + +#include "mbed.h" +#include "N5110.h" +#include "Gamepad.h" +#include "robot.h" + +/** missie Class +* @brief missile +* @author Gao Dawei +* @date 27/4/2020 +*/ + +class missile +{ + +public: + /** Constructor */ + missile(); + + /** Destructor */ + ~missile(); + + /** Initialization */ + void init(int height,int width,int speed); + + /** draw pattern on lcd*/ + void draw(N5110 &lcd); + + /** Update the values */ + void update(); + /// accessors and mutators + + /**Set velocity */ + void set_velocity(Vector2D v); + + /**get velocity */ + Vector2D get_velocity(); + + /**get position */ + Vector2D get_pos(); + + /**Set position */ + void set_pos(Vector2D p); + +private: + + Vector2D _velocity; + robot _robot; + int _height; + int _width; + int _x; + int _y; +}; +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/redspider/redspider.cpp Wed Apr 29 04:56:07 2020 +0000 @@ -0,0 +1,121 @@ +#include "redspider.h" + +// nothing doing in the constructor and destructor +redspider::redspider() +{ + +} + +redspider::~redspider() +{ + +} + +int redspider1[5][7] = { + { 0,1,1,1,1,0,0 }, + { 0,0,1,1,0,0,0 }, + { 1,1,1,1,1,1,1 }, + { 0,0,1,1,0,0,0 }, + { 0,1,1,1,1,0,0 }, + + + +}; + + +void redspider::init(int x,int height,int width) +{ + _x = x; // x value on screen is fixed + _y = HEIGHT/2 - height/2; // y depends on height of screen and height of Human + _height = height; + _width = width; + _speed = 1; // default speed + _score = 1; // start score from zero + + +} + +void redspider::draw(N5110 &lcd) +{ + // draw Human in screen buffer. + lcd.drawSprite(_x,_y,_height,_width,(int*)redspider1); +} + +void redspider::update(Direction d,float mag) +{ + _speed = int(mag*10.0f); // scale is arbitrary, could be changed in future + + + // update y value depending on direction of movement + // North is decrement as origin is at the top-left so decreasing moves up + if (d == N) { + _y-=_speed; + } + if (d == S) { + _y+=_speed; + } + + if (d == W) { + _x-=_speed; + } + else if (d == E) { + _x+=_speed; + } + + // check the x,y origin to ensure that the Human doesn't go off screen + if (_y < 1) { + _y = 1; + } + if (_y > HEIGHT - _height - 1) { + _y = HEIGHT - _height - 1; + } + + if (_x < 1) { + _x = 1; + } + if (_x > WIDTH - _width - 1) { + _x = WIDTH - _width - 1; + } +} + +void redspider::add_score() +{ + _score++; +} + + + +void redspider::minus_score() +{ + _score--; +} + +int redspider::get_score() +{ + return _score; +} + + + + +void redspider::set_velocity(Vector2D v) +{ + _velocity.x = v.x; + _velocity.y = v.y; +} + +Vector2D redspider::get_velocity() +{ + Vector2D v = {_velocity.x,_velocity.y}; + return v; +} + +Vector2D redspider::get_pos() { + Vector2D p = {_x,_y}; + return p; +} +void redspider::set_pos(Vector2D p) +{ + _x = p.x; + _y = p.y; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/redspider/redspider.h Wed Apr 29 04:56:07 2020 +0000 @@ -0,0 +1,69 @@ +#ifndef REDSPIDER_H +#define REDSPIDER_H + +#include "mbed.h" +#include "N5110.h" +#include "Gamepad.h" + +/** redspider Class +* @brief redspider +* @author Gao Dawei +* @date 27/4/2020 +*/ + + +class redspider +{ +public: + /** Constructor */ + redspider(); + + /** Destructor */ + ~redspider(); + + /** Initialization */ + void init(int x,int height,int width); + + /** draw pattern on lcd*/ + void draw(N5110 &lcd); + + /** Update the values */ + void update(Direction d,float mag); + + /** add the score */ + void add_score(); + + void minus_score(); + /** Get the score */ + int get_score(); + + + + /** Get the position */ + + void set_velocity(Vector2D v); + + /** Get velocity*/ + Vector2D get_velocity(); + + /** Get position*/ + Vector2D get_pos(); + + /** Set position */ + void set_pos(Vector2D p); + +private: + + Vector2D _velocity; + int _height; + int _width; + int _x; + int _y; + int _speed; + int _score; + + + + +}; +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/robot/robot.cpp Wed Apr 29 04:56:07 2020 +0000 @@ -0,0 +1,79 @@ + #include "robot.h" + +robot::robot() +{ + +} + +robot::~robot() +{ + +} + +int robot1[8][8] = { + { 0,0,1,1,0,0,1,1 }, + { 1,1,0,0,1,1,1,1 }, + { 0,1,1,0,1,0,0,0 }, + { 1,0,1,1,1,1,1,0 }, + { 1,0,1,1,1,1,1,0 }, + { 0,1,1,0,1,1,1,0 }, + { 1,1,0,0,1,1,1,1 }, + { 0,0,1,1,0,0,1,1 }, + + + + +}; + +void robot::init(int height,int width,int x) +{ + _height = height; + _width = width; + _x = x ; + _y = rand() % 32; ; + + srand(time(NULL)); + + + _velocity.x = 0; + _velocity.y = 0; +} + + +void robot::draw(N5110 &lcd) +{ + lcd.drawSprite(_x,_y,_height,_width,(int*)robot1); +} + +void robot::update() +{ + _x -= _velocity.x; + _y += _velocity.y; +} + +void robot::set_velocity(Vector2D v) +{ + _velocity.x = v.x; + _velocity.y = v.y; +} + +Vector2D robot::get_velocity() +{ + Vector2D v = {_velocity.x,_velocity.y}; + return v; +} + +Vector2D robot::get_pos() +{ + Vector2D p = {_x,_y}; + return p; +} + +void robot::set_pos(Vector2D p) +{ + _x = p.x; + _y = p.y; +} + + + \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/robot/robot.h Wed Apr 29 04:56:07 2020 +0000 @@ -0,0 +1,55 @@ +#ifndef ROBOT_H +#define ROBOT_H + +#include "mbed.h" +#include "N5110.h" +#include "Gamepad.h" +#include "redspider.h" + +/** robot Class +* @brief robot +* @author Gao Dawei +* @date 27/4/2020 +*/ + +class robot +{ + +public: + /** Constructor */ + robot(); + + /** Destructor */ + ~robot(); + + /** Initialization */ + void init(int height,int width,int x); + + /** draw pattern on lcd*/ + void draw(N5110 &lcd); + + /** Update the values */ + void update(); + /// accessors and mutators + + /** Set velocity */ + void set_velocity(Vector2D v); + + /** Get velocity*/ + Vector2D get_velocity(); + + /** Get position*/ + Vector2D get_pos(); + + /** Set position */ + void set_pos(Vector2D p); + +private: + + Vector2D _velocity; + int _height; + int _width; + int _x; + int _y; +}; +#endif \ No newline at end of file