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
Touch/Touch.cpp@9:a8420b353bb0, 2020-05-29 (annotated)
- Committer:
- ChenZirui
- Date:
- Fri May 29 03:27:27 2020 +0000
- Revision:
- 9:a8420b353bb0
- Parent:
- 8:5f0190b282f7
- Child:
- 15:7ca2d1b2bd0e
not final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ChenZirui | 5:7207c9b70108 | 1 | #include "Touch.h" |
ChenZirui | 5:7207c9b70108 | 2 | |
ChenZirui | 5:7207c9b70108 | 3 | |
ChenZirui | 7:f61ac963eb07 | 4 | //initialisation part |
ChenZirui | 7:f61ac963eb07 | 5 | void Touch::init(int Board_width,int Board_length,int bullet_size,int speed,N5110 &lcd) |
ChenZirui | 5:7207c9b70108 | 6 | { |
ChenZirui | 5:7207c9b70108 | 7 | // initialise the game parameters |
ChenZirui | 7:f61ac963eb07 | 8 | _Board_width = Board_width; //width of board |
ChenZirui | 7:f61ac963eb07 | 9 | _Board_length = Board_length; //length of board |
ChenZirui | 7:f61ac963eb07 | 10 | _bullet_size = bullet_size; //bullet size |
ChenZirui | 7:f61ac963eb07 | 11 | _speed = speed; //speed |
ChenZirui | 7:f61ac963eb07 | 12 | _leds=0;//led number |
ChenZirui | 7:f61ac963eb07 | 13 | |
ChenZirui | 7:f61ac963eb07 | 14 | _board.init(_board_x,_board_y,_Board_length,_Board_width); // draw board |
ChenZirui | 7:f61ac963eb07 | 15 | _bullet.init(_board_x,Board_length,_speed,_Board_length); //draw bullet |
ChenZirui | 7:f61ac963eb07 | 16 | |
ChenZirui | 5:7207c9b70108 | 17 | } |
ChenZirui | 5:7207c9b70108 | 18 | |
ChenZirui | 7:f61ac963eb07 | 19 | void Touch::reading(Gamepad &pad) |
ChenZirui | 5:7207c9b70108 | 20 | { |
ChenZirui | 7:f61ac963eb07 | 21 | _d = pad.get_direction(); // joystick direction |
ChenZirui | 7:f61ac963eb07 | 22 | _mag = pad.get_mag(); // joystick direction movement parameter |
ChenZirui | 5:7207c9b70108 | 23 | } |
ChenZirui | 5:7207c9b70108 | 24 | |
ChenZirui | 5:7207c9b70108 | 25 | void Touch::draw(N5110 &lcd) |
ChenZirui | 5:7207c9b70108 | 26 | { |
ChenZirui | 7:f61ac963eb07 | 27 | |
ChenZirui | 8:5f0190b282f7 | 28 | Vector2D bullet_pos = _bullet.get_pos(); //bullet position data |
ChenZirui | 7:f61ac963eb07 | 29 | /* s=0; |
ChenZirui | 7:f61ac963eb07 | 30 | if((bullet_pos.y >= 0)&&(bullet_pos.y <= 24)) |
ChenZirui | 7:f61ac963eb07 | 31 | { |
ChenZirui | 7:f61ac963eb07 | 32 | //s++; |
ChenZirui | 7:f61ac963eb07 | 33 | Y[s]= bullet_pos.y ; |
ChenZirui | 7:f61ac963eb07 | 34 | X[s]= bullet_pos.x; |
ChenZirui | 7:f61ac963eb07 | 35 | s++; |
ChenZirui | 7:f61ac963eb07 | 36 | }*/ |
ChenZirui | 7:f61ac963eb07 | 37 | for(int r=1;r<84;r++) |
ChenZirui | 7:f61ac963eb07 | 38 | for(int c=1;c<24;c++) |
ChenZirui | 7:f61ac963eb07 | 39 | { |
ChenZirui | 7:f61ac963eb07 | 40 | lcd.setPixel(r,c); |
ChenZirui | 7:f61ac963eb07 | 41 | if((bullet_pos.y >= 0)&&(bullet_pos.y <= 24)) |
ChenZirui | 7:f61ac963eb07 | 42 | { |
ChenZirui | 7:f61ac963eb07 | 43 | |
ChenZirui | 7:f61ac963eb07 | 44 | Y= bullet_pos.y; |
ChenZirui | 7:f61ac963eb07 | 45 | X= bullet_pos.x; |
ChenZirui | 7:f61ac963eb07 | 46 | lcd.clearPixel(X,Y-1); |
ChenZirui | 7:f61ac963eb07 | 47 | /* for(int i=0;i<s;i++) |
ChenZirui | 7:f61ac963eb07 | 48 | lcd.clearPixel(X[i],Y[i]-1);*/ |
ChenZirui | 7:f61ac963eb07 | 49 | } |
ChenZirui | 7:f61ac963eb07 | 50 | } |
ChenZirui | 7:f61ac963eb07 | 51 | lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); // draw a platform for game |
ChenZirui | 5:7207c9b70108 | 52 | print_scores(lcd); |
ChenZirui | 8:5f0190b282f7 | 53 | _board.draw(lcd); // draw a board for game |
ChenZirui | 8:5f0190b282f7 | 54 | _bullet.draw(lcd); // draw a bullet for game |
ChenZirui | 5:7207c9b70108 | 55 | } |
ChenZirui | 5:7207c9b70108 | 56 | |
ChenZirui | 7:f61ac963eb07 | 57 | void Touch::update(Gamepad &pad,N5110 &lcd) |
ChenZirui | 5:7207c9b70108 | 58 | { |
ChenZirui | 8:5f0190b282f7 | 59 | drop(pad); |
ChenZirui | 7:f61ac963eb07 | 60 | _board.update(_d,_mag); |
ChenZirui | 7:f61ac963eb07 | 61 | _bullet.update(lcd); |
ChenZirui | 5:7207c9b70108 | 62 | |
ChenZirui | 8:5f0190b282f7 | 63 | Boundary_touch(pad); |
ChenZirui | 8:5f0190b282f7 | 64 | Board_touch(pad,lcd); |
ChenZirui | 5:7207c9b70108 | 65 | } |
ChenZirui | 5:7207c9b70108 | 66 | |
ChenZirui | 8:5f0190b282f7 | 67 | void Touch::Boundary_touch(Gamepad &pad) |
ChenZirui | 5:7207c9b70108 | 68 | { |
ChenZirui | 5:7207c9b70108 | 69 | // read current ball attributes |
ChenZirui | 5:7207c9b70108 | 70 | Vector2D bullet_pos = _bullet.get_pos(); |
ChenZirui | 5:7207c9b70108 | 71 | Vector2D bullet_velocity = _bullet.get_velocity(); |
ChenZirui | 5:7207c9b70108 | 72 | |
ChenZirui | 5:7207c9b70108 | 73 | // check if hit top wall |
ChenZirui | 5:7207c9b70108 | 74 | if (bullet_pos.y <= 1) { // 1 due to 1 pixel boundary |
ChenZirui | 5:7207c9b70108 | 75 | bullet_pos.y = 1; // bounce off ceiling without going off screen |
ChenZirui | 5:7207c9b70108 | 76 | bullet_velocity.y = -bullet_velocity.y; |
ChenZirui | 5:7207c9b70108 | 77 | // audio feedback |
ChenZirui | 5:7207c9b70108 | 78 | pad.tone(750.0,0.1); |
ChenZirui | 5:7207c9b70108 | 79 | } |
ChenZirui | 7:f61ac963eb07 | 80 | // check if hit right wall |
ChenZirui | 7:f61ac963eb07 | 81 | else if (bullet_pos.x+_bullet_size >= 83 ) { // right wall is 83 |
ChenZirui | 5:7207c9b70108 | 82 | // hit bottom |
ChenZirui | 7:f61ac963eb07 | 83 | bullet_pos.x = (WIDTH-1) - _bullet_size; // stops ball going off screen |
ChenZirui | 7:f61ac963eb07 | 84 | bullet_velocity.x = -bullet_velocity.x; |
ChenZirui | 7:f61ac963eb07 | 85 | // audio feedback |
ChenZirui | 7:f61ac963eb07 | 86 | pad.tone(750.0,0.1); |
ChenZirui | 7:f61ac963eb07 | 87 | }// check if hit left wall |
ChenZirui | 7:f61ac963eb07 | 88 | else if (bullet_pos.x <= 1 ) { // left wall pixel is 1 |
ChenZirui | 7:f61ac963eb07 | 89 | // hit bottom |
ChenZirui | 7:f61ac963eb07 | 90 | bullet_pos.x = 1; // stops ball going off screen |
ChenZirui | 7:f61ac963eb07 | 91 | bullet_velocity.x = -bullet_velocity.x; |
ChenZirui | 5:7207c9b70108 | 92 | // audio feedback |
ChenZirui | 5:7207c9b70108 | 93 | pad.tone(750.0,0.1); |
ChenZirui | 5:7207c9b70108 | 94 | } |
ChenZirui | 5:7207c9b70108 | 95 | // update ball parameters |
ChenZirui | 5:7207c9b70108 | 96 | _bullet.set_velocity(bullet_velocity); |
ChenZirui | 5:7207c9b70108 | 97 | _bullet.set_pos(bullet_pos); |
ChenZirui | 5:7207c9b70108 | 98 | } |
ChenZirui | 5:7207c9b70108 | 99 | |
ChenZirui | 8:5f0190b282f7 | 100 | void Touch::Board_touch(Gamepad &pad,N5110 &lcd) |
ChenZirui | 5:7207c9b70108 | 101 | { |
ChenZirui | 5:7207c9b70108 | 102 | Vector2D bullet_pos = _bullet.get_pos(); |
ChenZirui | 5:7207c9b70108 | 103 | Vector2D bullet_velocity = _bullet.get_velocity(); |
ChenZirui | 7:f61ac963eb07 | 104 | Vector2D p1_pos = _board.get_pos(); |
ChenZirui | 5:7207c9b70108 | 105 | if ( |
ChenZirui | 9:a8420b353bb0 | 106 | (bullet_pos.x >= p1_pos.x) && //left |
ChenZirui | 9:a8420b353bb0 | 107 | (bullet_pos.x <= p1_pos.x + _Board_length) && //right |
ChenZirui | 9:a8420b353bb0 | 108 | (bullet_pos.y+_bullet_size >=p1_pos.y)&& //down |
ChenZirui | 9:a8420b353bb0 | 109 | (bullet_pos.y-_Board_width<=p1_pos.y) //up |
ChenZirui | 5:7207c9b70108 | 110 | ){ |
ChenZirui | 5:7207c9b70108 | 111 | |
ChenZirui | 5:7207c9b70108 | 112 | // if it has, fix position and reflect x velocity |
ChenZirui | 9:a8420b353bb0 | 113 | bullet_velocity.y = -bullet_velocity.y; //fix position and reflect y velocity |
ChenZirui | 9:a8420b353bb0 | 114 | pad.tone(1000.0,0.1); // audio effect as reminder |
ChenZirui | 5:7207c9b70108 | 115 | |
ChenZirui | 5:7207c9b70108 | 116 | } |
ChenZirui | 5:7207c9b70108 | 117 | if((bullet_pos.y >= 0)&&(bullet_pos.y <= 24)) |
ChenZirui | 5:7207c9b70108 | 118 | { |
ChenZirui | 5:7207c9b70108 | 119 | bullet_pos.y = bullet_pos.y + _Board_width; |
ChenZirui | 5:7207c9b70108 | 120 | bullet_velocity.y = -bullet_velocity.y; |
ChenZirui | 9:a8420b353bb0 | 121 | |
ChenZirui | 7:f61ac963eb07 | 122 | } |
ChenZirui | 9:a8420b353bb0 | 123 | // write new datas |
ChenZirui | 5:7207c9b70108 | 124 | _bullet.set_velocity(bullet_velocity); |
ChenZirui | 5:7207c9b70108 | 125 | _bullet.set_pos(bullet_pos); |
ChenZirui | 5:7207c9b70108 | 126 | } |
ChenZirui | 5:7207c9b70108 | 127 | |
ChenZirui | 8:5f0190b282f7 | 128 | void Touch::drop(Gamepad &pad) |
ChenZirui | 5:7207c9b70108 | 129 | { |
ChenZirui | 5:7207c9b70108 | 130 | Vector2D bullet_pos = _bullet.get_pos(); |
ChenZirui | 7:f61ac963eb07 | 131 | if (bullet_pos.y + _bullet_size> 47) { |
ChenZirui | 7:f61ac963eb07 | 132 | _board.add_score(); |
ChenZirui | 7:f61ac963eb07 | 133 | _bullet.init(_board_x,_bullet_size,_speed,_Board_length); |
ChenZirui | 7:f61ac963eb07 | 134 | _leds++; |
ChenZirui | 7:f61ac963eb07 | 135 | if(_leds>6) |
ChenZirui | 7:f61ac963eb07 | 136 | { |
ChenZirui | 7:f61ac963eb07 | 137 | _leds=6; |
ChenZirui | 7:f61ac963eb07 | 138 | } |
ChenZirui | 7:f61ac963eb07 | 139 | pad.led(_leds,0); |
ChenZirui | 5:7207c9b70108 | 140 | pad.tone(1500.0,0.5); |
ChenZirui | 7:f61ac963eb07 | 141 | // pad.leds_on(); |
ChenZirui | 5:7207c9b70108 | 142 | wait(0.5); |
ChenZirui | 7:f61ac963eb07 | 143 | // pad.leds_off(); |
ChenZirui | 5:7207c9b70108 | 144 | } |
ChenZirui | 5:7207c9b70108 | 145 | } |
ChenZirui | 5:7207c9b70108 | 146 | |
ChenZirui | 5:7207c9b70108 | 147 | void Touch::print_scores(N5110 &lcd) |
ChenZirui | 5:7207c9b70108 | 148 | { |
ChenZirui | 7:f61ac963eb07 | 149 | // get scores from Boards |
ChenZirui | 7:f61ac963eb07 | 150 | int p1_score = _board.get_score(); |
ChenZirui | 5:7207c9b70108 | 151 | |
ChenZirui | 5:7207c9b70108 | 152 | // print to LCD i |
ChenZirui | 5:7207c9b70108 | 153 | char buffer1[14]; |
ChenZirui | 5:7207c9b70108 | 154 | sprintf(buffer1,"%2d",p1_score); |
ChenZirui | 9:a8420b353bb0 | 155 | lcd.printString(buffer1,WIDTH/2 - 20,1); |
ChenZirui | 5:7207c9b70108 | 156 | } |