import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Random;
import java.util.Timer;
import javax.imageio.ImageIO;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class BallPanel extends JPanel {
public int hamx = 300;
public int hamy = 300;
public int peterx;
public int petery;
public int score = 0;
Random rand = new Random();
Random rand2 = new Random();
protected int ballX = 250;
public int count = 0;
protected int ballY = 250;
private int ballDiameter = 50;
private BufferedImage image1;
private BufferedImage image2;
private BufferedImage image3;
public BufferedImage image4;
private double ballXSpeed = 5;
private double ballYSpeed = 10;
public int life = 3;
public int time = 0;
Random r;
Random r2;
public void ResetGame(){
count = 0;
hamx = r.nextInt(600);
hamy = r2.nextInt(600);
peterx = rand.nextInt(600);
petery = rand2.nextInt(600);
score = 0;
life = 3;
repaint();
}
public int getLives() {
return life;
}
public void setLives(int lives) {
this.life = life;
}
public void resetIt(){
if(peterx <= ballX + 45 && petery <= ballY + 45 && peterx >= ballX - 45 && petery >= ballY - 45)
{
setLives(3);
getLives();
life--;
r = new Random();
r2 = new Random();
peterx=r.nextInt(600);
petery=r2.nextInt(600);
repaint();
}
if(peterx <= hamx + 45 && petery <= hamy + 45 && peterx >= hamx - 45 && petery >= hamy - 45)
{
score++;
hamx = r.nextInt(600);
hamy = r2.nextInt(600);
repaint();
}
if(life<=0){
Object[] possibleValues = { "Close", "Restart"};
Object selectedValue = JOptionPane.showInputDialog(null,
"Your score was:"+score, "You Lose!",
JOptionPane.INFORMATION_MESSAGE, null,
possibleValues, possibleValues[0]);
if (selectedValue == possibleValues[0]){
System.exit(1);
}
if (selectedValue == possibleValues[1]){
ResetGame();
}
}
}
KeyListener kw = new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
int k = e.getKeyCode();
if (k == KeyEvent.VK_UP) {
petery = petery - 10;
resetIt();
repaint();
}
if (k == KeyEvent.VK_DOWN) {
petery = petery + 10;
resetIt();
repaint();
}
if (k == KeyEvent.VK_LEFT) {
peterx = peterx - 10;
resetIt();
repaint();
}
if (k == KeyEvent.VK_RIGHT) {
peterx = peterx + 10;
resetIt();
repaint();
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
};
public BallPanel() {
setBackground(Color.BLACK);
try {
image1 = ImageIO.read(new File(
"/Users/MW/Downloads/petergriffin2.png"));
image2 = ImageIO.read(new File(
"/Users/MW/Downloads/cheeseburger2.png"));
image3 = ImageIO.read(new File(
"/Users/MW/Downloads/SpikeBall.png"));
image4 = ImageIO.read(new File(
"/Users/MW/Downloads/petericon.png"));
} catch (Exception e) {
System.out.println("could not load img");
}
}
// call this method from a Timer to move the ball!
public void step() {
// move the ball on the X axis
ballX += ballXSpeed;
// if the ball goes off the edge, bounce it by reversing its speed
if (ballX < 0 || ballX > getWidth() - ballDiameter) {
ballXSpeed *= -1;
}
// move the ball on the Y axis
ballY += ballYSpeed;
// if the ball goes off the edge, bounce it by reversing its speed
if (ballY < 0 || ballY > getHeight() - ballDiameter) {
ballYSpeed *= -1;
}
// tell this JPanel to repaint itself since the ball has moved
repaint();
}
// paint the ball
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.white);
g.drawString("Score:"+score,30,20);
g.drawString("lives:"+Integer.toString(life),600,20);
g.drawImage(image1, peterx, petery, null);
g.drawImage(image2,hamx, hamy, null);
g.drawImage(image3,ballX,ballY,null);
g.drawString(Integer.toString(time/30),20,40);
g.setColor(Color.BLUE);
g.setColor(Color.white);
repaint();
}
}