Question: what is wrong with my code package game; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing. * ; import javax.swing.JPanel; import
what is wrong with my code package game;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.;
import javax.swing.JPanel;
import java.awt.event.;
class PingPong extends Game
private static final long serialVersionUID L;
static int livesp; Player s remaining lives
static int livesp; Player s remaining lives
private Color color;
static Paddle p; Player s paddle
static Paddle p; Player s paddle
private Ball ball;
private Power powerUp;
Constructor initializes the game with players, paddle positions, and a Swing timer
public PingPong
superPingPong;
this.setFocusabletrue;
this.requestFocus;
powerUp new Power;
livesp; Player starts with lives
livesp; Player starts with lives
Initialize paddles for both players
color Color.RED;
p new Paddlecolor true; Player s paddle on the right side
color Color.BLUE;
p new Paddlecolor false; Player s paddle on the left side
Initialize the ball
Point ballPosition new Point; Adjust the starting position as needed
Point ballShape new Point;
ball new BallballShape ballPosition, width, height, p p;
Add key listeners to handle paddle movement
this.addKeyListenerp;
this.addKeyListenerp;
Create and start a Swing Timer to update and repaint the game
Timer timer new Timer new ActionListener
@Override
public void actionPerformedActionEvent e
update; Update game logic
repaint; Repaint the game window
;
timer.start;
Update method to handle game logic
public void update
Update paddles' positions
pgetChange;
pgetChange;
Update all's position
ball.move;
Checks for collision with paddles
ball.checksCollisionp;
ball.checksCollisionp;
if collidesWithPowerUpp collidesWithPowerUpp
applyPowerUpp; Apply powerup to player
applyPowerUpp; Apply powerup to player
private void applyPowerUpPaddle paddle
int power powerUp.getPower;
switch power
case :
Powerup type : Increase paddle height
paddle.increaseHeight;
break;
case :
Powerup type : Decrease paddle height
paddle.decreaseHeight;
break;
default:
Unknown powerup type or no powerup effect
break;
Reset the powerup to its initial state after applying
powerUp.resetPower;
private boolean collidesWithPowerUpPaddle paddle
return powerUp.getHitBallgetX Ball.getY &&
Ball.getX paddle.getLeftX && Ball.getX paddle.getRightX &&
Ball.getY paddle.getTopY && Ball.getY paddle.getBottomY;
Paint method to draw game elements on the screen
@Override
public void paintGraphics brush
brush.setColorColorBLACK;
brush.fillRect width, height;
Display players' remaining lives
if livesp livesp
brush.setColorColorWHITE;
brush.drawStringPlayer lives left: livesp;
brush.drawStringPingPong;
brush.drawStringPlayer lives left: livesp;
Paint paddles for both players
ppaintbrush;
ppaintbrush;
ball.drawbrush;
powerUp.powerballbrush;
else
If one player runs out of lives, display the winner
brush.setColorColorBLACK;
brush.fillRect;
brush.setColorColorGREEN;
Font font new FontArial Font.BOLD, ;
brush.setFontfont;
if livesp
brush.drawStringWinner: Player ;
else
brush.drawStringWinner: Player ;
Main method to start the game
public static void mainString args
PingPong game new PingPong;
game.repaint;
package game;
import java.awt.Graphics;
import java.util.Random;
public class Ball extends Polygon
private int yVelocity;
private static int xVelocity;
private Random random;
private static Poin
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
