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@15:7ca2d1b2bd0e, 2020-05-29 (annotated)
- Committer:
- ChenZirui
- Date:
- Fri May 29 07:35:45 2020 +0000
- Revision:
- 15:7ca2d1b2bd0e
- Parent:
- 9:a8420b353bb0
Final submission . I have read and agreed with Statement of Academic Integrity
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 | 15:7ca2d1b2bd0e | 5 | void Touch::init(int Board_width,int Board_length,int bullet_size,int speed,N5110 &lcd,int _board_y1,int _board_x1) |
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 | 15:7ca2d1b2bd0e | 12 | _leds=0; //led number |
ChenZirui | 15:7ca2d1b2bd0e | 13 | _board_x=_board_x1; |
ChenZirui | 15:7ca2d1b2bd0e | 14 | _board_y=_board_y1; |
ChenZirui | 7:f61ac963eb07 | 15 | _board.init(_board_x,_board_y,_Board_length,_Board_width); // draw board |
ChenZirui | 15:7ca2d1b2bd0e | 16 | _bullet.init(_board_x,_bullet_size,_speed,_board_y); //draw bullet |
ChenZirui | 7:f61ac963eb07 | 17 | |
ChenZirui | 5:7207c9b70108 | 18 | } |
ChenZirui | 5:7207c9b70108 | 19 | |
ChenZirui | 7:f61ac963eb07 | 20 | void Touch::reading(Gamepad &pad) |
ChenZirui | 5:7207c9b70108 | 21 | { |
ChenZirui | 7:f61ac963eb07 | 22 | _d = pad.get_direction(); // joystick direction |
ChenZirui | 7:f61ac963eb07 | 23 | _mag = pad.get_mag(); // joystick direction movement parameter |
ChenZirui | 5:7207c9b70108 | 24 | } |
ChenZirui | 5:7207c9b70108 | 25 | |
ChenZirui | 5:7207c9b70108 | 26 | void Touch::draw(N5110 &lcd) |
ChenZirui | 5:7207c9b70108 | 27 | { |
ChenZirui | 7:f61ac963eb07 | 28 | lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); // draw a platform for game |
ChenZirui | 15:7ca2d1b2bd0e | 29 | lcd.drawRect(0,20,84,4,FILL_BLACK); // score board |
ChenZirui | 5:7207c9b70108 | 30 | print_scores(lcd); |
ChenZirui | 8:5f0190b282f7 | 31 | _board.draw(lcd); // draw a board for game |
ChenZirui | 8:5f0190b282f7 | 32 | _bullet.draw(lcd); // draw a bullet for game |
ChenZirui | 5:7207c9b70108 | 33 | } |
ChenZirui | 5:7207c9b70108 | 34 | |
ChenZirui | 7:f61ac963eb07 | 35 | void Touch::update(Gamepad &pad,N5110 &lcd) |
ChenZirui | 5:7207c9b70108 | 36 | { |
ChenZirui | 8:5f0190b282f7 | 37 | drop(pad); |
ChenZirui | 7:f61ac963eb07 | 38 | _board.update(_d,_mag); |
ChenZirui | 7:f61ac963eb07 | 39 | _bullet.update(lcd); |
ChenZirui | 5:7207c9b70108 | 40 | |
ChenZirui | 8:5f0190b282f7 | 41 | Boundary_touch(pad); |
ChenZirui | 8:5f0190b282f7 | 42 | Board_touch(pad,lcd); |
ChenZirui | 5:7207c9b70108 | 43 | } |
ChenZirui | 5:7207c9b70108 | 44 | |
ChenZirui | 8:5f0190b282f7 | 45 | void Touch::Boundary_touch(Gamepad &pad) |
ChenZirui | 5:7207c9b70108 | 46 | { |
ChenZirui | 5:7207c9b70108 | 47 | // read current ball attributes |
ChenZirui | 5:7207c9b70108 | 48 | Vector2D bullet_pos = _bullet.get_pos(); |
ChenZirui | 5:7207c9b70108 | 49 | Vector2D bullet_velocity = _bullet.get_velocity(); |
ChenZirui | 5:7207c9b70108 | 50 | |
ChenZirui | 5:7207c9b70108 | 51 | // check if hit top wall |
ChenZirui | 5:7207c9b70108 | 52 | if (bullet_pos.y <= 1) { // 1 due to 1 pixel boundary |
ChenZirui | 5:7207c9b70108 | 53 | bullet_pos.y = 1; // bounce off ceiling without going off screen |
ChenZirui | 5:7207c9b70108 | 54 | bullet_velocity.y = -bullet_velocity.y; |
ChenZirui | 5:7207c9b70108 | 55 | // audio feedback |
ChenZirui | 5:7207c9b70108 | 56 | pad.tone(750.0,0.1); |
ChenZirui | 5:7207c9b70108 | 57 | } |
ChenZirui | 7:f61ac963eb07 | 58 | // check if hit right wall |
ChenZirui | 7:f61ac963eb07 | 59 | else if (bullet_pos.x+_bullet_size >= 83 ) { // right wall is 83 |
ChenZirui | 5:7207c9b70108 | 60 | // hit bottom |
ChenZirui | 7:f61ac963eb07 | 61 | bullet_pos.x = (WIDTH-1) - _bullet_size; // stops ball going off screen |
ChenZirui | 7:f61ac963eb07 | 62 | bullet_velocity.x = -bullet_velocity.x; |
ChenZirui | 7:f61ac963eb07 | 63 | // audio feedback |
ChenZirui | 7:f61ac963eb07 | 64 | pad.tone(750.0,0.1); |
ChenZirui | 7:f61ac963eb07 | 65 | }// check if hit left wall |
ChenZirui | 7:f61ac963eb07 | 66 | else if (bullet_pos.x <= 1 ) { // left wall pixel is 1 |
ChenZirui | 7:f61ac963eb07 | 67 | // hit bottom |
ChenZirui | 7:f61ac963eb07 | 68 | bullet_pos.x = 1; // stops ball going off screen |
ChenZirui | 7:f61ac963eb07 | 69 | bullet_velocity.x = -bullet_velocity.x; |
ChenZirui | 5:7207c9b70108 | 70 | // audio feedback |
ChenZirui | 5:7207c9b70108 | 71 | pad.tone(750.0,0.1); |
ChenZirui | 5:7207c9b70108 | 72 | } |
ChenZirui | 5:7207c9b70108 | 73 | // update ball parameters |
ChenZirui | 5:7207c9b70108 | 74 | _bullet.set_velocity(bullet_velocity); |
ChenZirui | 5:7207c9b70108 | 75 | _bullet.set_pos(bullet_pos); |
ChenZirui | 5:7207c9b70108 | 76 | } |
ChenZirui | 5:7207c9b70108 | 77 | |
ChenZirui | 8:5f0190b282f7 | 78 | void Touch::Board_touch(Gamepad &pad,N5110 &lcd) |
ChenZirui | 5:7207c9b70108 | 79 | { |
ChenZirui | 5:7207c9b70108 | 80 | Vector2D bullet_pos = _bullet.get_pos(); |
ChenZirui | 5:7207c9b70108 | 81 | Vector2D bullet_velocity = _bullet.get_velocity(); |
ChenZirui | 15:7ca2d1b2bd0e | 82 | Vector2D b_pos = _board.get_pos(); |
ChenZirui | 5:7207c9b70108 | 83 | if ( |
ChenZirui | 15:7ca2d1b2bd0e | 84 | (bullet_pos.x >= b_pos.x) && //left |
ChenZirui | 15:7ca2d1b2bd0e | 85 | (bullet_pos.x <= b_pos.x + _Board_length) && //right |
ChenZirui | 15:7ca2d1b2bd0e | 86 | (bullet_pos.y+_bullet_size >=b_pos.y)&& //down |
ChenZirui | 15:7ca2d1b2bd0e | 87 | (bullet_pos.y-_Board_width<=b_pos.y) //up |
ChenZirui | 5:7207c9b70108 | 88 | ){ |
ChenZirui | 5:7207c9b70108 | 89 | |
ChenZirui | 5:7207c9b70108 | 90 | // if it has, fix position and reflect x velocity |
ChenZirui | 15:7ca2d1b2bd0e | 91 | _board.add_score(); |
ChenZirui | 9:a8420b353bb0 | 92 | bullet_velocity.y = -bullet_velocity.y; //fix position and reflect y velocity |
ChenZirui | 9:a8420b353bb0 | 93 | pad.tone(1000.0,0.1); // audio effect as reminder |
ChenZirui | 5:7207c9b70108 | 94 | |
ChenZirui | 5:7207c9b70108 | 95 | } |
ChenZirui | 5:7207c9b70108 | 96 | if((bullet_pos.y >= 0)&&(bullet_pos.y <= 24)) |
ChenZirui | 5:7207c9b70108 | 97 | { |
ChenZirui | 5:7207c9b70108 | 98 | bullet_pos.y = bullet_pos.y + _Board_width; |
ChenZirui | 5:7207c9b70108 | 99 | bullet_velocity.y = -bullet_velocity.y; |
ChenZirui | 9:a8420b353bb0 | 100 | |
ChenZirui | 7:f61ac963eb07 | 101 | } |
ChenZirui | 9:a8420b353bb0 | 102 | // write new datas |
ChenZirui | 5:7207c9b70108 | 103 | _bullet.set_velocity(bullet_velocity); |
ChenZirui | 5:7207c9b70108 | 104 | _bullet.set_pos(bullet_pos); |
ChenZirui | 5:7207c9b70108 | 105 | } |
ChenZirui | 5:7207c9b70108 | 106 | |
ChenZirui | 8:5f0190b282f7 | 107 | void Touch::drop(Gamepad &pad) |
ChenZirui | 5:7207c9b70108 | 108 | { |
ChenZirui | 5:7207c9b70108 | 109 | Vector2D bullet_pos = _bullet.get_pos(); |
ChenZirui | 7:f61ac963eb07 | 110 | if (bullet_pos.y + _bullet_size> 47) { |
ChenZirui | 15:7ca2d1b2bd0e | 111 | _bullet.init(_board_x,_bullet_size,_speed,_board_y); |
ChenZirui | 7:f61ac963eb07 | 112 | _leds++; |
ChenZirui | 7:f61ac963eb07 | 113 | if(_leds>6) |
ChenZirui | 7:f61ac963eb07 | 114 | { |
ChenZirui | 7:f61ac963eb07 | 115 | _leds=6; |
ChenZirui | 7:f61ac963eb07 | 116 | } |
ChenZirui | 7:f61ac963eb07 | 117 | pad.led(_leds,0); |
ChenZirui | 5:7207c9b70108 | 118 | pad.tone(1500.0,0.5); |
ChenZirui | 7:f61ac963eb07 | 119 | // pad.leds_on(); |
ChenZirui | 5:7207c9b70108 | 120 | wait(0.5); |
ChenZirui | 7:f61ac963eb07 | 121 | // pad.leds_off(); |
ChenZirui | 5:7207c9b70108 | 122 | } |
ChenZirui | 5:7207c9b70108 | 123 | } |
ChenZirui | 5:7207c9b70108 | 124 | |
ChenZirui | 5:7207c9b70108 | 125 | void Touch::print_scores(N5110 &lcd) |
ChenZirui | 5:7207c9b70108 | 126 | { |
ChenZirui | 7:f61ac963eb07 | 127 | // get scores from Boards |
ChenZirui | 15:7ca2d1b2bd0e | 128 | int b_score = _board.get_score(); |
ChenZirui | 5:7207c9b70108 | 129 | |
ChenZirui | 5:7207c9b70108 | 130 | // print to LCD i |
ChenZirui | 5:7207c9b70108 | 131 | char buffer1[14]; |
ChenZirui | 15:7ca2d1b2bd0e | 132 | sprintf(buffer1,"Score is %2d",b_score); |
ChenZirui | 15:7ca2d1b2bd0e | 133 | lcd.printString(buffer1,10,1); |
ChenZirui | 5:7207c9b70108 | 134 | } |