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
Diff: Touch/Touch.cpp
- Revision:
- 5:7207c9b70108
- Child:
- 6:b393cfe4e0a7
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Touch/Touch.cpp Wed May 27 21:13:59 2020 +0000
@@ -0,0 +1,184 @@
+#include "Touch.h"
+
+Touch::Touch()
+{
+
+}
+
+Touch::~Touch()
+{
+
+}
+
+void Touch::init(int board_width,int board_height,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);
+ //lcd.drawRect(0,0,84,24,FILL_BLACK);
+ _bullet.init(_p1x,board_height,_speed,_Board_height);
+}
+
+void Touch::read_input(Gamepad &pad)
+{
+ _d = pad.get_direction();
+ _mag = pad.get_mag();
+}
+
+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
+ print_scores(lcd);
+ // boards
+ _p1.draw(lcd);
+ _p2.draw(lcd);
+ // ball
+ _bullet.draw(lcd);
+}
+
+void Touch::update(Gamepad &pad,N5110 &lcd)
+{
+ check_goal(pad);
+ // important to update boards and ball before checking collisions so can
+ // correct for it before updating the display
+ _p1.update(_d,_mag,lcd);
+ //_p2.update(_d,_mag);
+ _bullet.update(lcd);
+
+ check_wall_collision(pad);
+ check_Board_collisions(pad,lcd);
+}
+
+void Touch::check_wall_collision(Gamepad &pad)
+{
+ // read current ball attributes
+ Vector2D bullet_pos = _bullet.get_pos();
+ Vector2D bullet_velocity = _bullet.get_velocity();
+
+ // check if hit top wall
+ if (bullet_pos.y <= 1) { // 1 due to 1 pixel boundary
+ bullet_pos.y = 1; // bounce off ceiling without going off screen
+ bullet_velocity.y = -bullet_velocity.y;
+ // 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
+ // hit bottom
+ bullet_pos.y = (HEIGHT-1) - _bullet_size; // stops ball going off screen
+ bullet_velocity.y = -bullet_velocity.y;
+ // 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,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);
+ 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
+ ){
+
+ // if it has, fix position and reflect x velocity
+ bullet_pos.x = _p1x + _Board_width;
+ bullet_velocity.x = -bullet_velocity.x;
+ // 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);
+ 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);
+ }*/
+
+ // write new attributes
+ _bullet.set_velocity(bullet_velocity);
+ _bullet.set_pos(bullet_pos);
+}
+
+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);
+ pad.tone(1500.0,0.5);
+ pad.leds_on();
+ wait(0.5);
+ 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();
+
+ // print to LCD i
+ char buffer1[14];
+ sprintf(buffer1,"%2d",p1_score);
+ lcd.printString(buffer1,WIDTH/2 - 20,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
+ char buffer2[14];
+ sprintf(buffer2,"%2d",p2_score);
+ lcd.printString(buffer2,WIDTH/2 + 4,1);
+}
\ No newline at end of file