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@5:7207c9b70108, 2020-05-27 (annotated)
- Committer:
- ChenZirui
- Date:
- Wed May 27 21:13:59 2020 +0000
- Revision:
- 5:7207c9b70108
- Child:
- 6:b393cfe4e0a7
problems
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 | Touch::Touch() |
ChenZirui | 5:7207c9b70108 | 4 | { |
ChenZirui | 5:7207c9b70108 | 5 | |
ChenZirui | 5:7207c9b70108 | 6 | } |
ChenZirui | 5:7207c9b70108 | 7 | |
ChenZirui | 5:7207c9b70108 | 8 | Touch::~Touch() |
ChenZirui | 5:7207c9b70108 | 9 | { |
ChenZirui | 5:7207c9b70108 | 10 | |
ChenZirui | 5:7207c9b70108 | 11 | } |
ChenZirui | 5:7207c9b70108 | 12 | |
ChenZirui | 5:7207c9b70108 | 13 | void Touch::init(int board_width,int board_height,int bullet_size,int speed,N5110 &lcd) |
ChenZirui | 5:7207c9b70108 | 14 | { |
ChenZirui | 5:7207c9b70108 | 15 | // initialise the game parameters |
ChenZirui | 5:7207c9b70108 | 16 | _Board_width = board_width; |
ChenZirui | 5:7207c9b70108 | 17 | _Board_height = board_height; |
ChenZirui | 5:7207c9b70108 | 18 | _bullet_size = bullet_size; |
ChenZirui | 5:7207c9b70108 | 19 | _speed = speed; |
ChenZirui | 5:7207c9b70108 | 20 | |
ChenZirui | 5:7207c9b70108 | 21 | // x position on screen - WIDTH is defined in N5110.h |
ChenZirui | 5:7207c9b70108 | 22 | _p1x = GAP; |
ChenZirui | 5:7207c9b70108 | 23 | _p2x = WIDTH - GAP - _Board_width; |
ChenZirui | 5:7207c9b70108 | 24 | _p1y=48-board_height*0.5; |
ChenZirui | 5:7207c9b70108 | 25 | // puts boards and ball in middle |
ChenZirui | 5:7207c9b70108 | 26 | _p1.init(_p1x,_p1y,_Board_height,_Board_width,lcd); |
ChenZirui | 5:7207c9b70108 | 27 | //lcd.drawRect(0,0,84,24,FILL_BLACK); |
ChenZirui | 5:7207c9b70108 | 28 | _bullet.init(_p1x,board_height,_speed,_Board_height); |
ChenZirui | 5:7207c9b70108 | 29 | } |
ChenZirui | 5:7207c9b70108 | 30 | |
ChenZirui | 5:7207c9b70108 | 31 | void Touch::read_input(Gamepad &pad) |
ChenZirui | 5:7207c9b70108 | 32 | { |
ChenZirui | 5:7207c9b70108 | 33 | _d = pad.get_direction(); |
ChenZirui | 5:7207c9b70108 | 34 | _mag = pad.get_mag(); |
ChenZirui | 5:7207c9b70108 | 35 | } |
ChenZirui | 5:7207c9b70108 | 36 | |
ChenZirui | 5:7207c9b70108 | 37 | void Touch::draw(N5110 &lcd) |
ChenZirui | 5:7207c9b70108 | 38 | { |
ChenZirui | 5:7207c9b70108 | 39 | // draw the elements in the LCD buffer |
ChenZirui | 5:7207c9b70108 | 40 | // pitch |
ChenZirui | 5:7207c9b70108 | 41 | lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); |
ChenZirui | 5:7207c9b70108 | 42 | lcd.drawLine(WIDTH/2,0,WIDTH/2,HEIGHT-1,2); |
ChenZirui | 5:7207c9b70108 | 43 | lcd.drawRect(0,0,84,24,FILL_BLACK); |
ChenZirui | 5:7207c9b70108 | 44 | //lcd.drawRect(0,0,84,24,FILL_BLACK); |
ChenZirui | 5:7207c9b70108 | 45 | //score |
ChenZirui | 5:7207c9b70108 | 46 | print_scores(lcd); |
ChenZirui | 5:7207c9b70108 | 47 | // boards |
ChenZirui | 5:7207c9b70108 | 48 | _p1.draw(lcd); |
ChenZirui | 5:7207c9b70108 | 49 | _p2.draw(lcd); |
ChenZirui | 5:7207c9b70108 | 50 | // ball |
ChenZirui | 5:7207c9b70108 | 51 | _bullet.draw(lcd); |
ChenZirui | 5:7207c9b70108 | 52 | } |
ChenZirui | 5:7207c9b70108 | 53 | |
ChenZirui | 5:7207c9b70108 | 54 | void Touch::update(Gamepad &pad,N5110 &lcd) |
ChenZirui | 5:7207c9b70108 | 55 | { |
ChenZirui | 5:7207c9b70108 | 56 | check_goal(pad); |
ChenZirui | 5:7207c9b70108 | 57 | // important to update boards and ball before checking collisions so can |
ChenZirui | 5:7207c9b70108 | 58 | // correct for it before updating the display |
ChenZirui | 5:7207c9b70108 | 59 | _p1.update(_d,_mag,lcd); |
ChenZirui | 5:7207c9b70108 | 60 | //_p2.update(_d,_mag); |
ChenZirui | 5:7207c9b70108 | 61 | _bullet.update(lcd); |
ChenZirui | 5:7207c9b70108 | 62 | |
ChenZirui | 5:7207c9b70108 | 63 | check_wall_collision(pad); |
ChenZirui | 5:7207c9b70108 | 64 | check_Board_collisions(pad,lcd); |
ChenZirui | 5:7207c9b70108 | 65 | } |
ChenZirui | 5:7207c9b70108 | 66 | |
ChenZirui | 5:7207c9b70108 | 67 | void Touch::check_wall_collision(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 | 5:7207c9b70108 | 80 | // check if hit bottom wall |
ChenZirui | 5:7207c9b70108 | 81 | else if (bullet_pos.y + _bullet_size >= (HEIGHT-1) ) { // bottom pixel is 47 |
ChenZirui | 5:7207c9b70108 | 82 | // hit bottom |
ChenZirui | 5:7207c9b70108 | 83 | bullet_pos.y = (HEIGHT-1) - _bullet_size; // stops ball going off screen |
ChenZirui | 5:7207c9b70108 | 84 | bullet_velocity.y = -bullet_velocity.y; |
ChenZirui | 5:7207c9b70108 | 85 | // audio feedback |
ChenZirui | 5:7207c9b70108 | 86 | pad.tone(750.0,0.1); |
ChenZirui | 5:7207c9b70108 | 87 | } |
ChenZirui | 5:7207c9b70108 | 88 | |
ChenZirui | 5:7207c9b70108 | 89 | // update ball parameters |
ChenZirui | 5:7207c9b70108 | 90 | _bullet.set_velocity(bullet_velocity); |
ChenZirui | 5:7207c9b70108 | 91 | _bullet.set_pos(bullet_pos); |
ChenZirui | 5:7207c9b70108 | 92 | } |
ChenZirui | 5:7207c9b70108 | 93 | |
ChenZirui | 5:7207c9b70108 | 94 | void Touch::check_Board_collisions(Gamepad &pad,N5110 &lcd) |
ChenZirui | 5:7207c9b70108 | 95 | { |
ChenZirui | 5:7207c9b70108 | 96 | // read current ball attributes |
ChenZirui | 5:7207c9b70108 | 97 | Vector2D bullet_pos = _bullet.get_pos(); |
ChenZirui | 5:7207c9b70108 | 98 | Vector2D bullet_velocity = _bullet.get_velocity(); |
ChenZirui | 5:7207c9b70108 | 99 | |
ChenZirui | 5:7207c9b70108 | 100 | // check p1 first |
ChenZirui | 5:7207c9b70108 | 101 | Vector2D p1_pos = _p1.get_pos(); |
ChenZirui | 5:7207c9b70108 | 102 | Vector2D p2_pos = _p2.get_pos(); |
ChenZirui | 5:7207c9b70108 | 103 | // see if ball has hit the board by checking for overlaps |
ChenZirui | 5:7207c9b70108 | 104 | //lcd.drawRect(0,0,84,24,FILL_BLACK); |
ChenZirui | 5:7207c9b70108 | 105 | if ( |
ChenZirui | 5:7207c9b70108 | 106 | (bullet_pos.y >= p1_pos.y) && //top |
ChenZirui | 5:7207c9b70108 | 107 | (bullet_pos.y <= p1_pos.y + _Board_height) && //bottom |
ChenZirui | 5:7207c9b70108 | 108 | (bullet_pos.x >= _p1x) && //left |
ChenZirui | 5:7207c9b70108 | 109 | (bullet_pos.x <= _p1x + _Board_width) //right |
ChenZirui | 5:7207c9b70108 | 110 | ){ |
ChenZirui | 5:7207c9b70108 | 111 | |
ChenZirui | 5:7207c9b70108 | 112 | // if it has, fix position and reflect x velocity |
ChenZirui | 5:7207c9b70108 | 113 | bullet_pos.x = _p1x + _Board_width; |
ChenZirui | 5:7207c9b70108 | 114 | bullet_velocity.x = -bullet_velocity.x; |
ChenZirui | 5:7207c9b70108 | 115 | // audio feedback |
ChenZirui | 5:7207c9b70108 | 116 | pad.tone(1000.0,0.1); |
ChenZirui | 5:7207c9b70108 | 117 | |
ChenZirui | 5:7207c9b70108 | 118 | } |
ChenZirui | 5:7207c9b70108 | 119 | if((bullet_pos.y >= 0)&&(bullet_pos.y <= 24)) |
ChenZirui | 5:7207c9b70108 | 120 | { |
ChenZirui | 5:7207c9b70108 | 121 | lcd.drawRect(bullet_pos.x,bullet_pos.y-4,4,4,FILL_TRANSPARENT); |
ChenZirui | 5:7207c9b70108 | 122 | bullet_pos.y = bullet_pos.y + _Board_width; |
ChenZirui | 5:7207c9b70108 | 123 | bullet_velocity.y = -bullet_velocity.y; |
ChenZirui | 5:7207c9b70108 | 124 | } |
ChenZirui | 5:7207c9b70108 | 125 | // check p2 next |
ChenZirui | 5:7207c9b70108 | 126 | /* Vector2D p2_pos = _p2.get_pos(); |
ChenZirui | 5:7207c9b70108 | 127 | |
ChenZirui | 5:7207c9b70108 | 128 | // see if ball has hit the board by checking for overlaps |
ChenZirui | 5:7207c9b70108 | 129 | if ( |
ChenZirui | 5:7207c9b70108 | 130 | (ball_pos.y >= p2_pos.y) && //top |
ChenZirui | 5:7207c9b70108 | 131 | (ball_pos.y <= p2_pos.y + _Board_height) && //bottom |
ChenZirui | 5:7207c9b70108 | 132 | (ball_pos.x + _bullet_size >= _p2x) && //left |
ChenZirui | 5:7207c9b70108 | 133 | (ball_pos.x + _bullet_size <= _p2x + _Board_width) //right |
ChenZirui | 5:7207c9b70108 | 134 | ) { |
ChenZirui | 5:7207c9b70108 | 135 | // if it has, fix position and reflect x velocity |
ChenZirui | 5:7207c9b70108 | 136 | ball_pos.x = _p2x - _bullet_size; |
ChenZirui | 5:7207c9b70108 | 137 | ball_velocity.x = -ball_velocity.x; |
ChenZirui | 5:7207c9b70108 | 138 | // audio feedback |
ChenZirui | 5:7207c9b70108 | 139 | pad.tone(1000.0,0.1); |
ChenZirui | 5:7207c9b70108 | 140 | }*/ |
ChenZirui | 5:7207c9b70108 | 141 | |
ChenZirui | 5:7207c9b70108 | 142 | // write new attributes |
ChenZirui | 5:7207c9b70108 | 143 | _bullet.set_velocity(bullet_velocity); |
ChenZirui | 5:7207c9b70108 | 144 | _bullet.set_pos(bullet_pos); |
ChenZirui | 5:7207c9b70108 | 145 | } |
ChenZirui | 5:7207c9b70108 | 146 | |
ChenZirui | 5:7207c9b70108 | 147 | void Touch::check_goal(Gamepad &pad) |
ChenZirui | 5:7207c9b70108 | 148 | { |
ChenZirui | 5:7207c9b70108 | 149 | Vector2D bullet_pos = _bullet.get_pos(); |
ChenZirui | 5:7207c9b70108 | 150 | // P2 has scored |
ChenZirui | 5:7207c9b70108 | 151 | if (bullet_pos.x + _bullet_size < 0) { |
ChenZirui | 5:7207c9b70108 | 152 | _p2.add_score(); |
ChenZirui | 5:7207c9b70108 | 153 | _bullet.init(_p1x,_bullet_size,_speed,_Board_height); |
ChenZirui | 5:7207c9b70108 | 154 | pad.tone(1500.0,0.5); |
ChenZirui | 5:7207c9b70108 | 155 | pad.leds_on(); |
ChenZirui | 5:7207c9b70108 | 156 | wait(0.5); |
ChenZirui | 5:7207c9b70108 | 157 | pad.leds_off(); |
ChenZirui | 5:7207c9b70108 | 158 | } |
ChenZirui | 5:7207c9b70108 | 159 | |
ChenZirui | 5:7207c9b70108 | 160 | // P1 has scored |
ChenZirui | 5:7207c9b70108 | 161 | if (bullet_pos.x > WIDTH) { |
ChenZirui | 5:7207c9b70108 | 162 | _p1.add_score(); |
ChenZirui | 5:7207c9b70108 | 163 | _bullet.init(_p1x,_bullet_size,_speed,_Board_height); |
ChenZirui | 5:7207c9b70108 | 164 | pad.tone(1500.0,0.5); |
ChenZirui | 5:7207c9b70108 | 165 | pad.leds_on(); |
ChenZirui | 5:7207c9b70108 | 166 | wait(0.5); |
ChenZirui | 5:7207c9b70108 | 167 | pad.leds_off(); |
ChenZirui | 5:7207c9b70108 | 168 | } |
ChenZirui | 5:7207c9b70108 | 169 | } |
ChenZirui | 5:7207c9b70108 | 170 | |
ChenZirui | 5:7207c9b70108 | 171 | void Touch::print_scores(N5110 &lcd) |
ChenZirui | 5:7207c9b70108 | 172 | { |
ChenZirui | 5:7207c9b70108 | 173 | // get scores from boards |
ChenZirui | 5:7207c9b70108 | 174 | int p1_score = _p1.get_score(); |
ChenZirui | 5:7207c9b70108 | 175 | int p2_score = _p2.get_score(); |
ChenZirui | 5:7207c9b70108 | 176 | |
ChenZirui | 5:7207c9b70108 | 177 | // print to LCD i |
ChenZirui | 5:7207c9b70108 | 178 | char buffer1[14]; |
ChenZirui | 5:7207c9b70108 | 179 | sprintf(buffer1,"%2d",p1_score); |
ChenZirui | 5:7207c9b70108 | 180 | lcd.printString(buffer1,WIDTH/2 - 20,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits |
ChenZirui | 5:7207c9b70108 | 181 | char buffer2[14]; |
ChenZirui | 5:7207c9b70108 | 182 | sprintf(buffer2,"%2d",p2_score); |
ChenZirui | 5:7207c9b70108 | 183 | lcd.printString(buffer2,WIDTH/2 + 4,1); |
ChenZirui | 5:7207c9b70108 | 184 | } |