Create an applet and draw the pong board: a rectangle two ‘gutters’ a centre line Make sure the left and right gutters are stored as variables. Incorporate your bouncing ball code, make sure it bounces off the gutters, not the rectangle. Draw two paddles – left and right. Make sure their x- and y-values and their height are stored in variables. You may choose to create a Paddle object OR code them into the main class – it is your choice. Incorporate the KeyListener. Control the left paddles up movement with the ‘w’ key and down movement with the ‘s’ key. Control the right paddle with the up and down arrow keys. Make sure that the paddles do not go past the top or bottom edges of the rectangle. Write code to allow the ball to bounce off the paddle, but stop if it hits the gutter. Get collisions happening for the left paddle first, before moving on to the right paddle. See diagram. You need to test if the ball is between the top of the paddle and the bottom of the paddle, but you cannot test against the Y co-ordinate of the ball, you must test against the middle of the ball. Eg. if (ball_y + (ball_size/2) >= paddle_y) // the ball hit below the top edge // of the paddle Once the ball correctly stops when it hits either gutter, comment out the line that stops the ball, and add in a line that incorporates scoring and re-spawns the ball from the centre of the game. If the ball hits the right gutter, the left player should win a point and the ball should spawn in a way that will make it move upwards and to the left. If the ball hits the left gutter, the right player should win a point and the ball should spawn in a way that will make it move upwards and to the right. Write code to display these scores on the applet (see image). If you want to add in an additional level of difficulty, add code to increase the ball speed with each hit of the paddle.
Create an applet and draw the pong board: a rectangle two ‘gutters’ a centre line Make sure the left and right gutters are stored as variables. Incorporate your bouncing ball code, make sure it bounces off the gutters, not the rectangle. Draw two paddles – left and right. Make sure their x- and y-values and their height are stored in variables. You may choose to create a Paddle object OR code them into the main class – it is your choice. Incorporate the KeyListener. Control the left paddles up movement with the ‘w’ key and down movement with the ‘s’ key. Control the right paddle with the up and down arrow keys. Make sure that the paddles do not go past the top or bottom edges of the rectangle. Write code to allow the ball to bounce off the paddle, but stop if it hits the gutter. Get collisions happening for the left paddle first, before moving on to the right paddle. See diagram. You need to test if the ball is between the top of the paddle and the bottom of the paddle, but you cannot test against the Y co-ordinate of the ball, you must test against the middle of the ball. Eg. if (ball_y + (ball_size/2) >= paddle_y) // the ball hit below the top edge // of the paddle Once the ball correctly stops when it hits either gutter, comment out the line that stops the ball, and add in a line that incorporates scoring and re-spawns the ball from the centre of the game. If the ball hits the right gutter, the left player should win a point and the ball should spawn in a way that will make it move upwards and to the left. If the ball hits the left gutter, the right player should win a point and the ball should spawn in a way that will make it move upwards and to the right. Write code to display these scores on the applet (see image). If you want to add in an additional level of difficulty, add code to increase the ball speed with each hit of the paddle.
Prac-16-Timers-18qhuu0.docx