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.
Touch.cpp
00001 #include "Touch.h" 00002 00003 00004 //initialisation part 00005 void Touch::init(int Board_width,int Board_length,int bullet_size,int speed,N5110 &lcd) 00006 { 00007 // initialise the game parameters 00008 _Board_width = Board_width; //width of board 00009 _Board_length = Board_length; //length of board 00010 _bullet_size = bullet_size; //bullet size 00011 _speed = speed; //speed 00012 _leds=0;//led number 00013 00014 _board.init(_board_x,_board_y,_Board_length,_Board_width); // draw board 00015 _bullet.init(_board_x,Board_length,_speed,_Board_length); //draw bullet 00016 00017 } 00018 00019 void Touch::reading(Gamepad &pad) 00020 { 00021 _d = pad.get_direction(); // joystick direction 00022 _mag = pad.get_mag(); // joystick direction movement parameter 00023 } 00024 00025 void Touch::draw(N5110 &lcd) 00026 { 00027 00028 Vector2D bullet_pos = _bullet.get_pos(); //bullet position data 00029 /* s=0; 00030 if((bullet_pos.y >= 0)&&(bullet_pos.y <= 24)) 00031 { 00032 //s++; 00033 Y[s]= bullet_pos.y ; 00034 X[s]= bullet_pos.x; 00035 s++; 00036 }*/ 00037 for(int r=1;r<84;r++) 00038 for(int c=1;c<24;c++) 00039 { 00040 lcd.setPixel(r,c); 00041 if((bullet_pos.y >= 0)&&(bullet_pos.y <= 24)) 00042 { 00043 00044 Y= bullet_pos.y; 00045 X= bullet_pos.x; 00046 lcd.clearPixel(X,Y-1); 00047 /* for(int i=0;i<s;i++) 00048 lcd.clearPixel(X[i],Y[i]-1);*/ 00049 } 00050 } 00051 lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); // draw a platform for game 00052 print_scores(lcd); 00053 _board.draw(lcd); // draw a board for game 00054 _bullet.draw(lcd); // draw a bullet for game 00055 } 00056 00057 void Touch::update(Gamepad &pad,N5110 &lcd) 00058 { 00059 drop(pad); 00060 _board.update(_d,_mag); 00061 _bullet.update(lcd); 00062 00063 Boundary_touch(pad); 00064 Board_touch(pad,lcd); 00065 } 00066 00067 void Touch::Boundary_touch(Gamepad &pad) 00068 { 00069 // read current ball attributes 00070 Vector2D bullet_pos = _bullet.get_pos(); 00071 Vector2D bullet_velocity = _bullet.get_velocity(); 00072 00073 // check if hit top wall 00074 if (bullet_pos.y <= 1) { // 1 due to 1 pixel boundary 00075 bullet_pos.y = 1; // bounce off ceiling without going off screen 00076 bullet_velocity.y = -bullet_velocity.y; 00077 // audio feedback 00078 pad.tone(750.0,0.1); 00079 } 00080 // check if hit right wall 00081 else if (bullet_pos.x+_bullet_size >= 83 ) { // right wall is 83 00082 // hit bottom 00083 bullet_pos.x = (WIDTH-1) - _bullet_size; // stops ball going off screen 00084 bullet_velocity.x = -bullet_velocity.x; 00085 // audio feedback 00086 pad.tone(750.0,0.1); 00087 }// check if hit left wall 00088 else if (bullet_pos.x <= 1 ) { // left wall pixel is 1 00089 // hit bottom 00090 bullet_pos.x = 1; // stops ball going off screen 00091 bullet_velocity.x = -bullet_velocity.x; 00092 // audio feedback 00093 pad.tone(750.0,0.1); 00094 } 00095 // update ball parameters 00096 _bullet.set_velocity(bullet_velocity); 00097 _bullet.set_pos(bullet_pos); 00098 } 00099 00100 void Touch::Board_touch(Gamepad &pad,N5110 &lcd) 00101 { 00102 Vector2D bullet_pos = _bullet.get_pos(); 00103 Vector2D bullet_velocity = _bullet.get_velocity(); 00104 Vector2D p1_pos = _board.get_pos(); 00105 if ( 00106 (bullet_pos.x >= p1_pos.x) && //top 00107 (bullet_pos.x <= p1_pos.x + _Board_length) && //bottom 00108 (bullet_pos.y+_bullet_size >=p1_pos.y) //left 00109 //right 00110 ){ 00111 00112 // if it has, fix position and reflect x velocity 00113 // bullet_pos.y = _boardy - _bullet_size; 00114 bullet_velocity.y = -bullet_velocity.y; 00115 // audio feedback 00116 pad.tone(1000.0,0.1); 00117 00118 } 00119 if((bullet_pos.y >= 0)&&(bullet_pos.y <= 24)) 00120 { 00121 // Y= bullet_pos.y ; 00122 // X= bullet_pos.x; 00123 // lcd.clearPixel(X,_Y-1); 00124 bullet_pos.y = bullet_pos.y + _Board_width; 00125 bullet_velocity.y = -bullet_velocity.y; 00126 // lcd.clearPixel(_x,_y-1); 00127 } 00128 // lcd.clearPixel(X,Y-1);*/ 00129 // write new attributes 00130 _bullet.set_velocity(bullet_velocity); 00131 _bullet.set_pos(bullet_pos); 00132 } 00133 00134 void Touch::drop(Gamepad &pad) 00135 { 00136 Vector2D bullet_pos = _bullet.get_pos(); 00137 00138 00139 00140 // P1 has scored 00141 if (bullet_pos.y + _bullet_size> 47) { 00142 _board.add_score(); 00143 _bullet.init(_board_x,_bullet_size,_speed,_Board_length); 00144 _leds++; 00145 if(_leds>6) 00146 { 00147 _leds=6; 00148 } 00149 pad.led(_leds,0); 00150 pad.tone(1500.0,0.5); 00151 // pad.leds_on(); 00152 wait(0.5); 00153 // pad.leds_off(); 00154 } 00155 } 00156 00157 void Touch::print_scores(N5110 &lcd) 00158 { 00159 // get scores from Boards 00160 int p1_score = _board.get_score(); 00161 00162 // print to LCD i 00163 char buffer1[14]; 00164 sprintf(buffer1,"%2d",p1_score); 00165 lcd.printString(buffer1,WIDTH/2 - 20,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits 00166 }
Generated on Mon Jul 18 2022 10:48:25 by
1.7.2