Craig Evans
/
Pong2
Pong for Gamepad2
Embed:
(wiki syntax)
Show/hide line numbers
PongEngine.cpp
00001 #include "PongEngine.h" 00002 00003 PongEngine::PongEngine() 00004 { 00005 00006 } 00007 00008 PongEngine::~PongEngine() 00009 { 00010 00011 } 00012 00013 void PongEngine::init(int paddle_width,int paddle_height,int ball_size,int speed) 00014 { 00015 // initialise the game parameters 00016 _paddle_width = paddle_width; 00017 _paddle_height = paddle_height; 00018 _ball_size = ball_size; 00019 _speed = speed; 00020 00021 // x position on screen - WIDTH is defined in N5110.h 00022 _p1x = GAP; 00023 _p2x = WIDTH - GAP - _paddle_width; 00024 00025 // puts paddles and ball in middle 00026 _p1.init(_p1x,_paddle_height,_paddle_width); 00027 _p2.init(_p2x,_paddle_height,_paddle_width); 00028 _ball.init(_ball_size,_speed); 00029 } 00030 00031 void PongEngine::read_input(Gamepad &pad) 00032 { 00033 _d = pad.get_direction(); 00034 _mag = pad.get_mag(); 00035 } 00036 00037 void PongEngine::draw(N5110 &lcd) 00038 { 00039 // draw the elements in the LCD buffer 00040 // pitch 00041 lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); 00042 lcd.drawLine(WIDTH/2,0,WIDTH/2,HEIGHT-1,2); 00043 //score 00044 print_scores(lcd); 00045 // paddles 00046 _p1.draw(lcd); 00047 _p2.draw(lcd); 00048 // ball 00049 _ball.draw(lcd); 00050 } 00051 00052 void PongEngine::update(Gamepad &pad) 00053 { 00054 check_goal(pad); 00055 // important to update paddles and ball before checking collisions so can 00056 // correct for it before updating the display 00057 _p1.update(_d,_mag); 00058 _p2.update(_d,_mag); 00059 _ball.update(); 00060 00061 check_wall_collision(pad); 00062 check_paddle_collisions(pad); 00063 } 00064 00065 void PongEngine::check_wall_collision(Gamepad &pad) 00066 { 00067 // read current ball attributes 00068 Vector2D ball_pos = _ball.get_pos(); 00069 Vector2D ball_velocity = _ball.get_velocity(); 00070 00071 // check if hit top wall 00072 if (ball_pos.y <= 1) { // 1 due to 1 pixel boundary 00073 ball_pos.y = 1; // bounce off ceiling without going off screen 00074 ball_velocity.y = -ball_velocity.y; 00075 // audio feedback 00076 pad.tone(750.0,0.1); 00077 } 00078 // check if hit bottom wall 00079 else if (ball_pos.y + _ball_size >= (HEIGHT-1) ) { // bottom pixel is 47 00080 // hit bottom 00081 ball_pos.y = (HEIGHT-1) - _ball_size; // stops ball going off screen 00082 ball_velocity.y = -ball_velocity.y; 00083 // audio feedback 00084 pad.tone(750.0,0.1); 00085 } 00086 00087 // update ball parameters 00088 _ball.set_velocity(ball_velocity); 00089 _ball.set_pos(ball_pos); 00090 } 00091 00092 void PongEngine::check_paddle_collisions(Gamepad &pad) 00093 { 00094 // read current ball attributes 00095 Vector2D ball_pos = _ball.get_pos(); 00096 Vector2D ball_velocity = _ball.get_velocity(); 00097 00098 // check p1 first 00099 Vector2D p1_pos = _p1.get_pos(); 00100 00101 // see if ball has hit the paddle by checking for overlaps 00102 if ( 00103 (ball_pos.y >= p1_pos.y) && //top 00104 (ball_pos.y <= p1_pos.y + _paddle_height) && //bottom 00105 (ball_pos.x >= _p1x) && //left 00106 (ball_pos.x <= _p1x + _paddle_width) //right 00107 ) { 00108 // if it has, fix position and reflect x velocity 00109 ball_pos.x = _p1x + _paddle_width; 00110 ball_velocity.x = -ball_velocity.x; 00111 // audio feedback 00112 pad.tone(1000.0,0.1); 00113 } 00114 00115 // check p2 next 00116 Vector2D p2_pos = _p2.get_pos(); 00117 00118 // see if ball has hit the paddle by checking for overlaps 00119 if ( 00120 (ball_pos.y >= p2_pos.y) && //top 00121 (ball_pos.y <= p2_pos.y + _paddle_height) && //bottom 00122 (ball_pos.x + _ball_size >= _p2x) && //left 00123 (ball_pos.x + _ball_size <= _p2x + _paddle_width) //right 00124 ) { 00125 // if it has, fix position and reflect x velocity 00126 ball_pos.x = _p2x - _ball_size; 00127 ball_velocity.x = -ball_velocity.x; 00128 // audio feedback 00129 pad.tone(1000.0,0.1); 00130 } 00131 00132 // write new attributes 00133 _ball.set_velocity(ball_velocity); 00134 _ball.set_pos(ball_pos); 00135 } 00136 00137 void PongEngine::check_goal(Gamepad &pad) 00138 { 00139 Vector2D ball_pos = _ball.get_pos(); 00140 // P2 has scored 00141 if (ball_pos.x + _ball_size < 0) { 00142 _p2.add_score(); 00143 _ball.init(_ball_size,_speed); 00144 pad.tone(1500.0,0.5); 00145 pad.leds_on(); 00146 wait(0.5); 00147 pad.leds_off(); 00148 } 00149 00150 // P1 has scored 00151 if (ball_pos.x > WIDTH) { 00152 _p1.add_score(); 00153 _ball.init(_ball_size,_speed); 00154 pad.tone(1500.0,0.5); 00155 pad.leds_on(); 00156 wait(0.5); 00157 pad.leds_off(); 00158 } 00159 } 00160 00161 void PongEngine::print_scores(N5110 &lcd) 00162 { 00163 // get scores from paddles 00164 int p1_score = _p1.get_score(); 00165 int p2_score = _p2.get_score(); 00166 00167 // print to LCD i 00168 char buffer1[14]; 00169 sprintf(buffer1,"%2d",p1_score); 00170 lcd.printString(buffer1,WIDTH/2 - 20,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits 00171 char buffer2[14]; 00172 sprintf(buffer2,"%2d",p2_score); 00173 lcd.printString(buffer2,WIDTH/2 + 4,1); 00174 }
Generated on Sat Jul 16 2022 18:01:16 by 1.7.2