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
Revision 7:f61ac963eb07, committed 2020-05-29
- Comitter:
- ChenZirui
- Date:
- Fri May 29 02:55:37 2020 +0000
- Parent:
- 6:b393cfe4e0a7
- Child:
- 8:5f0190b282f7
- Commit message:
- not final
Changed in this revision
--- a/Board/Board.cpp Thu May 28 01:11:47 2020 +0000
+++ b/Board/Board.cpp Fri May 29 02:55:37 2020 +0000
@@ -1,105 +1,86 @@
#include "Board.h"
-
-// nothing doing in the constructor and destructor
-Board::Board()
-{
-
-}
-
-Board::~Board()
-{
+#define LENGTH 84
-}
//borad initial function
-void Board::init(int x,int y,int height,int width)
+void Board::init(int x,int y,int length,int width)
{
- _x=x ; // horizontal coordinate of Board
- _y=y ;// vertical coordinate of board
- _height =height;
- _width = width;
- _speed = 1; // default speed
- _score = 0;
- //lcd.drawRect(0,0,84,24,FILL_BLACK);
+ _x=x ; // horizontal coordinate of Board
+ _y=y ; // vertical coordinate of board
+ _length =length; // length
+ _width = width; // width
+ _speed = 1; // default speed
+ _score = 0; // initi8al socre
- }
- void Board::draw(N5110 &lcd)
- {
- //lcd.drawRect(_x+(_height/2)-(_width/2),_y,_width,_height,FILL_BLACK);
- lcd.drawRect(_x,_y,_height,_width,FILL_BLACK);
- // lcd.drawRect(0,0,84,24,FILL_BLACK);
- // lcd.drawRect(41,24,10,10,FILL_TRANSPARENT);
- // lcd.clearPixel(10,24);
-
-
- }
- void Board::update(Direction d,float mag)
- {
- _speed = int(mag*10.0f); // scale is arbitrary, could be changed in future
- //if((ball_pos.y >= 0)&&(ball_pos.y <= 24))
- // {
- // lcd.drawRect(24,41,10,10,FILL_TRANSPARENT);
- // }
- // lcd.clearPixel(10,24);
-
- // update y value depending on direction of movement
- // North is decrement as origin is at the top-left so decreasing moves up
+
+}
+void Board::draw(N5110 &lcd) // use N5110 series function to draw a board
+{
+ lcd.drawRect(_x,_y,_length,_width,FILL_BLACK);
+}
+void Board::update(Direction d,float mag) // update position and speed increment information
+{
+ _speed = int(mag*10.0f); // speed variable
+
+ // eight directions control
if (d == N)
{
- _y-=_speed;
+ _y-=_speed; // North-up
}else if (d == S)
{
- _y+=_speed;
- }else if(d == W)
+ _y+=_speed; // South-down
+ }else if(d == W)
{
- _x-=_speed;
- }else if(d == E)
+ _x-=_speed; // West-left
+ }else if(d == E)
{
- _x+=_speed;
+ _x+=_speed; // East-right
}else if(d == NE)
{
_y+=_speed;
- _x+=_speed;
+ _x+=_speed; //Northeast-upright
}else if(d == NW)
{
_y+=_speed;
- _x-=_speed;
+ _x-=_speed; //Northwest-upleft
}else if(d == SE)
{
_y-=_speed;
- _x+=_speed;
+ _x+=_speed; //Southeast-downright
}else if(d == SW)
{
_y-=_speed;
- _x-=_speed;
- }
- if (_y < 1)
- {
- _y = 1;
+ _x-=_speed; //Southwest-downleft
}
- if (_y > HEIGHT - _height - 1)
+ // boundary judging and dealing
+
+ if (_y <= 24) //up
{
- _y = HEIGHT - _height - 1;
+ _y = 24;
}
- if (_x < 1)
+ if (_y >= HEIGHT - _width - 1) //bottom
+ {
+ _y = HEIGHT - _width - 1;
+ }
+ if (_x < 1) //left
{
_x = 1;
}
- if (_x > WIDTH - _width - 1)
+ if (_x > WIDTH - _length - 1) //right
{
- _x = WIDTH - _width - 1;
+ _x = WIDTH - _length - 1;
}
}
- void Board::add_score()
- {
+/*void Board::add_score()
+{
_score++;
- }
- int Board::get_score()
- {
- return _score;
- }
+}
+int Board::get_score()
+{
+ return _score;
+}*/
- Vector2D Board::get_pos()
- {
- Vector2D p = {_x,_y};
- return p;
- }
\ No newline at end of file
+Vector2D Board::get_pos() //position data
+{
+ Vector2D p = {_x,_y};
+ return p;
+}
\ No newline at end of file
--- a/Board/Board.h Thu May 28 01:11:47 2020 +0000
+++ b/Board/Board.h Fri May 29 02:55:37 2020 +0000
@@ -1,33 +1,34 @@
#ifndef BOARD_H
#define BOARD_H
-
+#define LENGTH 84
#include "mbed.h"
#include "N5110.h"
#include "Gamepad.h"
#include "Bullet.h"
-
+/** Board Class
+@author Chen Zirui, University of Leeds
+@brief Board data and its functions list
+@date May 2020
+*/
class Board
{
public:
- Board();
- ~Board();//constructor and destructor
- void init(int x,int y,int height,int width);//initial the first data of board
- void draw(N5110 &lcd);//use screeen function to draw a board
- void update(Direction d,float mag);//update borad to position to realise the effect of speed
- void add_score();//score generation function
- int get_score();
- Vector2D get_pos();
+ void init(int x,int y,int length,int width); //initial the first data of board
+ void draw(N5110 &lcd); //use screeen function to draw a board
+ void update(Direction d,float mag); //update borad to position to realise the effect of speed
+ void add_score(); //score generation function
+ int get_score(); //score reading function
+ Vector2D get_pos(); //position reading function
private:
-
- int _height;
+ //all parameters about board
+ int _length;
int _width;
int _x;
int _y;
int _speed;
int _score;
- //bullet _bullet;
-
+
};
#endif
\ No newline at end of file
--- a/Bullet/Bullet.cpp Thu May 28 01:11:47 2020 +0000
+++ b/Bullet/Bullet.cpp Fri May 29 02:55:37 2020 +0000
@@ -1,62 +1,50 @@
#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;
+ _x = 24;
+ _y = 28;
srand(time(NULL));
- int direction = rand() % 4; // randomise initial direction.
+ int direction = rand() % 2; // randomise initial direction.
// 4 possibilities. Get random modulo and set velocities accordingly
if (direction == 0) {
_velocity.x = speed;
- _velocity.y = 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))
+ /* if((_y >= 0)&&(_y <= 24))
{
- // lcd.clearPixel(_x,_y-1);
+ X=_x;
+ Y=_y;
}
- //lcd.drawRect(0,0,84,24,FILL_BLACK);
- // lcd.clearPixel(_x,_y);
+ lcd.clearPixel(_x,_y);*/
}
-void Bullet::update()
+void Bullet::update(N5110 &lcd)
{
_x += _velocity.x;
_y += _velocity.y;
- if((_y >= 0)&&(_y <= 24))
+ /* if(( _y >= 0)&&( _y <= 24))
{
-// lcd.clearPixel(_x,_y-1);
-
+
+ // lcd.clearPixel(_x,_y-1);
+ X=_x;
+ Y=_y;
}
+ lcd.clearPixel(X,Y-1);*/
}
void Bullet::set_velocity(Vector2D v)
--- a/Bullet/Bullet.h Thu May 28 01:11:47 2020 +0000
+++ b/Bullet/Bullet.h Fri May 29 02:55:37 2020 +0000
@@ -7,32 +7,31 @@
#include "Board.h"
/** Bullet Class
-@author Dr Craig A. Evans, University of Leeds
-@brief Controls the Bullet in the Pong game
-@date Febraury 2017
+@author Chen Zirui, University of Leeds
+@brief Bullet data and its functions list,initialisation,drawing,updating,positing setting and speed reading
+@date May 2020
*/
class Bullet
{
public:
- Bullet();
- ~Bullet();
- void init(int x,int size,int speed,int height);
- 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);
+
+ void init(int x,int size,int speed,int height); //initial datas of bullet
+ void draw(N5110 &lcd); //draw a bullet
+ void update(N5110 &lcd); //ubdate bullet position
+ void set_velocity(Vector2D v); //velocity setting
+ Vector2D get_velocity(); //speed data reading
+ Vector2D get_pos(); //position reading
+ void set_pos(Vector2D p); //position setting
private:
-
+ // all parameters about bullet
Vector2D _velocity;
int _size;
int _x;
int _y;
- //Bullet Bullet;
+ float X;
+ float Y;
};
#endif
\ No newline at end of file
--- a/Touch/Touch.cpp Thu May 28 01:11:47 2020 +0000
+++ b/Touch/Touch.cpp Fri May 29 02:55:37 2020 +0000
@@ -1,67 +1,70 @@
#include "Touch.h"
-Touch::Touch()
-{
-}
-
-Touch::~Touch()
-{
-
-}
-
-void Touch::init(int board_width,int board_height,int bullet_size,int speed)
+//initialisation part
+void Touch::init(int Board_width,int Board_length,int bullet_size,int speed,N5110 &lcd)
{
// initialise the game parameters
- _Board_width = board_width;
- _Board_height = board_height;
- _bullet_size = bullet_size;
- _speed = speed;
-
- // x position on screen - WIDTH is defined in N5110.h
- _p1x = GAP;
- _p2x = WIDTH - GAP - _Board_width;
- _p1y=48-board_height*0.5;
- // puts boards and ball in middle
- _p1.init(_p1x,_p1y,_Board_height,_Board_width);
- //lcd.drawRect(0,0,84,24,FILL_BLACK);
- _bullet.init(_p1x,board_height,_speed,_Board_height);
+ _Board_width = Board_width; //width of board
+ _Board_length = Board_length; //length of board
+ _bullet_size = bullet_size; //bullet size
+ _speed = speed; //speed
+ _leds=0;//led number
+
+ _board.init(_board_x,_board_y,_Board_length,_Board_width); // draw board
+ _bullet.init(_board_x,Board_length,_speed,_Board_length); //draw bullet
+
}
-void Touch::read_input(Gamepad &pad)
+void Touch::reading(Gamepad &pad)
{
- _d = pad.get_direction();
- _mag = pad.get_mag();
+ _d = pad.get_direction(); // joystick direction
+ _mag = pad.get_mag(); // joystick direction movement parameter
}
void Touch::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);
- lcd.drawRect(0,0,84,24,FILL_BLACK);
- //lcd.drawRect(0,0,84,24,FILL_BLACK);
- //score
+
+ Vector2D bullet_pos = _bullet.get_pos(); //bullet position
+ /* s=0;
+ if((bullet_pos.y >= 0)&&(bullet_pos.y <= 24))
+ {
+ //s++;
+ Y[s]= bullet_pos.y ;
+ X[s]= bullet_pos.x;
+ s++;
+ }*/
+ for(int r=1;r<84;r++)
+ for(int c=1;c<24;c++)
+ {
+ lcd.setPixel(r,c);
+ if((bullet_pos.y >= 0)&&(bullet_pos.y <= 24))
+ {
+
+ Y= bullet_pos.y;
+ X= bullet_pos.x;
+ lcd.clearPixel(X,Y-1);
+ /* for(int i=0;i<s;i++)
+ lcd.clearPixel(X[i],Y[i]-1);*/
+ }
+ }
+ lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); // draw a platform for game
print_scores(lcd);
- // boards
- _p1.draw(lcd);
- _p2.draw(lcd);
- // ball
+ _board.draw(lcd);
_bullet.draw(lcd);
}
-void Touch::update(Gamepad &pad)
+void Touch::update(Gamepad &pad,N5110 &lcd)
{
check_goal(pad);
- // important to update boards and ball before checking collisions so can
+ // important to update Boards and ball before checking collisions so can
// correct for it before updating the display
- _p1.update(_d,_mag);
+ _board.update(_d,_mag);
//_p2.update(_d,_mag);
- _bullet.update();
+ _bullet.update(lcd);
check_wall_collision(pad);
- check_Board_collisions(pad);
+ check_Board_collisions(pad,lcd);
}
void Touch::check_wall_collision(Gamepad &pad)
@@ -77,68 +80,55 @@
// audio feedback
pad.tone(750.0,0.1);
}
- // check if hit bottom wall
- else if (bullet_pos.y + _bullet_size >= (HEIGHT-1) ) { // bottom pixel is 47
+ // check if hit right wall
+ else if (bullet_pos.x+_bullet_size >= 83 ) { // right wall is 83
// hit bottom
- bullet_pos.y = (HEIGHT-1) - _bullet_size; // stops ball going off screen
- bullet_velocity.y = -bullet_velocity.y;
+ bullet_pos.x = (WIDTH-1) - _bullet_size; // stops ball going off screen
+ bullet_velocity.x = -bullet_velocity.x;
+ // audio feedback
+ pad.tone(750.0,0.1);
+ }// check if hit left wall
+ else if (bullet_pos.x <= 1 ) { // left wall pixel is 1
+ // hit bottom
+ bullet_pos.x = 1; // stops ball going off screen
+ bullet_velocity.x = -bullet_velocity.x;
// audio feedback
pad.tone(750.0,0.1);
}
-
// update ball parameters
_bullet.set_velocity(bullet_velocity);
_bullet.set_pos(bullet_pos);
}
-void Touch::check_Board_collisions(Gamepad &pad)
+void Touch::check_Board_collisions(Gamepad &pad,N5110 &lcd)
{
- // read current ball attributes
Vector2D bullet_pos = _bullet.get_pos();
Vector2D bullet_velocity = _bullet.get_velocity();
-
- // check p1 first
- Vector2D p1_pos = _p1.get_pos();
- Vector2D p2_pos = _p2.get_pos();
- // see if ball has hit the board by checking for overlaps
- //lcd.drawRect(0,0,84,24,FILL_BLACK);
+ Vector2D p1_pos = _board.get_pos();
if (
- (bullet_pos.y >= p1_pos.y) && //top
- (bullet_pos.y <= p1_pos.y + _Board_height) && //bottom
- (bullet_pos.x >= _p1x) && //left
- (bullet_pos.x <= _p1x + _Board_width) //right
+ (bullet_pos.x >= p1_pos.x) && //top
+ (bullet_pos.x <= p1_pos.x + _Board_length) && //bottom
+ (bullet_pos.y+_bullet_size >=p1_pos.y) //left
+ //right
){
// if it has, fix position and reflect x velocity
- bullet_pos.x = _p1x + _Board_width;
- bullet_velocity.x = -bullet_velocity.x;
+ // bullet_pos.y = _boardy - _bullet_size;
+ bullet_velocity.y = -bullet_velocity.y;
// audio feedback
pad.tone(1000.0,0.1);
}
if((bullet_pos.y >= 0)&&(bullet_pos.y <= 24))
{
- // lcd.drawRect(bullet_pos.x,bullet_pos.y-4,4,4,FILL_TRANSPARENT);
+ // Y= bullet_pos.y ;
+ // X= bullet_pos.x;
+ // lcd.clearPixel(X,_Y-1);
bullet_pos.y = bullet_pos.y + _Board_width;
bullet_velocity.y = -bullet_velocity.y;
- }
- // check p2 next
- /* Vector2D p2_pos = _p2.get_pos();
-
- // see if ball has hit the board by checking for overlaps
- if (
- (ball_pos.y >= p2_pos.y) && //top
- (ball_pos.y <= p2_pos.y + _Board_height) && //bottom
- (ball_pos.x + _bullet_size >= _p2x) && //left
- (ball_pos.x + _bullet_size <= _p2x + _Board_width) //right
- ) {
- // if it has, fix position and reflect x velocity
- ball_pos.x = _p2x - _bullet_size;
- ball_velocity.x = -ball_velocity.x;
- // audio feedback
- pad.tone(1000.0,0.1);
- }*/
-
+ // lcd.clearPixel(_x,_y-1);
+ }
+ // lcd.clearPixel(X,Y-1);*/
// write new attributes
_bullet.set_velocity(bullet_velocity);
_bullet.set_pos(bullet_pos);
@@ -147,32 +137,30 @@
void Touch::check_goal(Gamepad &pad)
{
Vector2D bullet_pos = _bullet.get_pos();
- // P2 has scored
- if (bullet_pos.x + _bullet_size < 0) {
- _p2.add_score();
- _bullet.init(_p1x,_bullet_size,_speed,_Board_height);
- pad.tone(1500.0,0.5);
- pad.leds_on();
- wait(0.5);
- pad.leds_off();
- }
+
+
// P1 has scored
- if (bullet_pos.x > WIDTH) {
- _p1.add_score();
- _bullet.init(_p1x,_bullet_size,_speed,_Board_height);
+ if (bullet_pos.y + _bullet_size> 47) {
+ _board.add_score();
+ _bullet.init(_board_x,_bullet_size,_speed,_Board_length);
+ _leds++;
+ if(_leds>6)
+ {
+ _leds=6;
+ }
+ pad.led(_leds,0);
pad.tone(1500.0,0.5);
- pad.leds_on();
+ // pad.leds_on();
wait(0.5);
- pad.leds_off();
+ // pad.leds_off();
}
}
void Touch::print_scores(N5110 &lcd)
{
- // get scores from boards
- int p1_score = _p1.get_score();
- int p2_score = _p2.get_score();
+ // get scores from Boards
+ int p1_score = _board.get_score();
// print to LCD i
char buffer1[14];
--- a/Touch/Touch.h Thu May 28 01:11:47 2020 +0000
+++ b/Touch/Touch.h Fri May 29 02:55:37 2020 +0000
@@ -14,33 +14,35 @@
{
public:
- Touch();
- ~Touch();
- void init(int Board_width,int Board_height,int bullet_size,int speed);
- void read_input(Gamepad &pad);
- void update(Gamepad &pad);
+ void init(int Board_width,int Board_length,int bullet_size,int speed,N5110 &lcd);
+ void reading(Gamepad &pad);
+ void update(Gamepad &pad,N5110 &lcd);
void draw(N5110 &lcd);
+ int _leds;
private:
void check_wall_collision(Gamepad &pad);
- void check_Board_collisions(Gamepad &pad);
+ void check_Board_collisions(Gamepad &pad,N5110 &lcd);
void check_goal(Gamepad &pad);
void print_scores(N5110 &lcd);
- Board _p1;
- Board _p2;
+ Board _board;
int _Board_width;
- int _Board_height;
+ int _Board_length;
int _bullet_size;
int _speed;
// x positions of the Boards
- int _p1x;
- int _p2x;
+ int _board_x;
+ int _board_y;
int _p1y;
+ //int _leds;
+ float X;//[83];
+ float Y;//[24];
+ int s;
Bullet _bullet;
Direction _d;
--- a/main.cpp Thu May 28 01:11:47 2020 +0000
+++ b/main.cpp Fri May 29 02:55:37 2020 +0000
@@ -8,10 +8,10 @@
# include "tests.h"
#endif
-#define PADDLE_WIDTH 2
-#define PADDLE_HEIGHT 8
-#define BALL_SIZE 2
-#define BALL_SPEED 3
+#define BOARD_WIDTH 2
+#define BOARD_LENGTH 8
+#define BULLET_SIZE 2
+#define BULLET_SPEED 3
/////////////// structs /////////////////
struct UserInput {
@@ -33,52 +33,51 @@
{
#ifdef WITH_TESTING
int number_of_failures = run_all_tests();
-
+ //test part
if(number_of_failures > 0) return number_of_failures;
#endif
- int fps = 6; // frames per second
- init(); // initialise and then display welcome screen...
- welcome(); // waiting for the user to start
- //lcd.drawRect(0,0,84,24,FILL_BLACK);
- render(); // first draw the initial frame
- //lcd.drawRect(0,0,84,24,FILL_BLACK);
- wait(1.0f/fps); // and wait for one frame period
-
- //lcd.drawRect(0,0,84,24,FILL_BLACK);
- // game loop - read input, update the game state and render the display
- while (1) {
- // pong.read_input(pad);
- // pong.read_input(pad);
- touch.read_input(pad);
- touch.update(pad);
+ int fps = 6; // frames per second
+ init(); ` // initialise
+ welcome(); // welcome screen for user to start
+ render(); // first draw the initial frame
+ wait(1.0f/fps); // and wait for one frame period
+ pad.leds_on(); //turn on all leds as 'lives' showing
+
+
+ while (1) //while loop, execute reading, updating, drawing,juding part frequentrly
+ {
+ touch.reading(pad);
+ touch.update(pad,lcd);
render();
- //lcd.drawRect(0,0,84,24,FILL_BLACK);
+ if(touch._leds>=6)
+ {
+ break; // if all lives finish , you will lsot game
+ }
wait(1.0f/fps);
}
+ lcd.init();
+ pad.init();
+ lcd.printString(" You fail ",0,1);
+ lcd.printString(" Press reset ",0,4); // the reminder of game lost
+ lcd.refresh(); //function to refresh the display
}
// initialies all classes and libraries
void init()
{
- // need to initialise LCD and Gamepad
lcd.init();
pad.init();
-
- // initialise the game with correct ball and paddle sizes
- touch.init(PADDLE_WIDTH,PADDLE_HEIGHT,BALL_SIZE,BALL_SPEED);
-
+ touch.init(BOARD_WIDTH,BOARD_LENGTH,BULLET_SIZE,BULLET_SPEED,lcd);
}
-
-// this function draws each frame on the LCD
+//clearing record before and drawing frames
void render()
{
- // clear screen, re-draw and refresh
+
lcd.clear();
touch.draw(lcd);
lcd.refresh();
}
-
-// simple splash screen displayed on start-up
+//welcome screen
void welcome() {
lcd.printString(" touch! ",0,1);
--- a/mbed.bld Thu May 28 01:11:47 2020 +0000 +++ b/mbed.bld Fri May 29 02:55:37 2020 +0000 @@ -1,1 +1,1 @@ -https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file +https://os.mbed.com/users/mbed_official/code/mbed/builds/3a7713b1edbc \ No newline at end of file