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 =1L;
static int livesp1; // Player 1's remaining lives
static int livesp2; // Player 2's remaining lives
private Color color;
static Paddle p1; // Player 1's paddle
static Paddle p2; // Player 2's paddle
private Ball ball;
private Power powerUp;
// Constructor initializes the game with players, paddle positions, and a Swing timer
public PingPong(){
super("PingPong",770,600);
this.setFocusable(true);
this.requestFocus();
powerUp = new Power();
livesp1=10; // Player 1 starts with 10 lives
livesp2=10; // Player 2 starts with 10 lives
// Initialize paddles for both players
color = Color.RED;
p1= new Paddle(color,600,100, true); // Player 1's paddle on the right side
color = Color.BLUE;
p2= new Paddle(color,30,100, false); // Player 2's paddle on the left side
// Initialize the ball
Point ballPosition = new Point(385,290); // Adjust the starting position as needed
Point[] ballShape ={new Point(0,0)};
ball = new Ball(ballShape, ballPosition, 0, width, height, p1, p2);
// Add key listeners to handle paddle movement
this.addKeyListener(p1);
this.addKeyListener(p2);
// Create and start a Swing Timer to update and repaint the game
Timer timer = new Timer(1000/60, new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
update(); // Update game logic
repaint(); // Repaint the game window
}
});
timer.start();
}
// Update method to handle game logic
public void update(){
// Update paddles' positions
p1.getChange();
p2.getChange();
//Update all's position
ball.move();
//Checks for collision with paddles
ball.checksCollision(p1);
ball.checksCollision(p2);
//if (collidesWithPowerUp(p1)|| collidesWithPowerUp(p2)){
// applyPowerUp(p1); // Apply power-up to player 1
// applyPowerUp(p2); // Apply power-up to player 2
}
//}
// private void applyPowerUp(Paddle paddle){
// int power = powerUp.getPower();
// switch (power){
// case 1:
//// Power-up type 1: Increase paddle height
// paddle.increaseHeight();
// break;
// case 2:
//// Power-up type 2: Decrease paddle height
// paddle.decreaseHeight();
// break;
// default:
//// Unknown power-up type or no power-up effect
// break;
//}
//// Reset the power-up to its initial state after applying
// powerUp.resetPower();
//}
private boolean collidesWithPowerUp(Paddle paddle){
return powerUp.getHit(Ball.getX(), 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 paint(Graphics brush){
brush.setColor(Color.BLACK);
brush.fillRect(0,0, width, height);
// Display players' remaining lives
if (livesp1>0|| livesp2>0){
brush.setColor(Color.WHITE);
brush.drawString("Player 1 lives left: "+ livesp1,60,20);
brush.drawString("PingPong",325,20);
brush.drawString("Player 2 lives left: "+ livesp2,500,20);
// Paint paddles for both players
p1.paint(brush);
p2.paint(brush);
ball.draw(brush);
// powerUp.powerball(brush);
} else {
// If one player runs out of lives, display the winner
brush.setColor(Color.BLACK);
brush.fillRect(0,0,700,600);
brush.setColor(Color.GREEN);
Font font = new Font("Arial", Font.BOLD, 30);
brush.setFont(font);
if (livesp1<=0){
brush.drawString("Winner: Player 1!!!!",10,20);
} else {
brush.drawString("Winner: Player 2!!!!",10,20);
}
}
}
// Main method to start the game
public static void main(String[] 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!