Source code
Ball.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pong;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JOptionPane;
/**
*
* @author Zuki
*/
public class Ball {
private int x = Pong.WINDOW_WIDTH / 2;
private int y = Pong.WINDOW_HEIGHT / 2;
private int xVelocity = -4;
private int yVelocity = 4;
private int size = 5;
private int playerScore = 0;
private int computerScore = 0;
public void update() {
x = x + xVelocity;
y = y + yVelocity;
if (x < 0) {
xVelocity = 4;
computerScore = computerScore + 1;
} else if (x + size > Pong.WINDOW_WIDTH - 6) {
xVelocity = -4;
playerScore = playerScore + 1;
}
if (y < 0) {
yVelocity = 4;
} else if (y + size > Pong.WINDOW_HEIGHT - 28) {
yVelocity = -4;
}
}
public void paint(Graphics g) {
g.setColor(Color.yellow);
g.fillOval(x, y, size, size);
g.drawString(toPlayer(), 5, 15);
g.drawString(toComputer(), 280, 15);
}
private void reverseDirection() {
xVelocity = -xVelocity;
}
private void reverseDirectionY() {
yVelocity = -yVelocity;
}
public void checkCollisionWith(Player player) {
if (this.x > player.getX() && this.x < player.getX() + player.getWidth()) {
if (this.y > player.getY() && this.y < player.getY() + player.getHeight()) {
//ball has collided with player
reverseDirection();
}
}
}
public void hitWall() {
if (this.y < 30) {
reverseDirectionY();
}
}
public void checkCollisionWith(Computer computer) {
if (this.x > computer.getX() && this.x < computer.getX() + computer.getWidth()) {
if (this.y > computer.getY() && this.y < computer.getY() + computer.getHeight()) {
//ball has collided with computer
reverseDirection();
}
}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getPlayerScore() {
return playerScore;
}
public int getComputerScore() {
return computerScore;
}
public String toPlayer() {
String retVal = "";
retVal = "Player Score: " + playerScore;
return retVal;
}
public String toComputer() {
String retVal = "";
retVal = "Computer Score: " + computerScore;
return retVal;
}
}
Computer.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pong;
import java.awt.Color;
import java.awt.Graphics;
/**
*
* @author Zuki
*/
public class Computer {
private GamePanel field;
private int y = Pong.WINDOW_HEIGHT / 2;
private int yVelocity = 0;
private int width = 10;
private int height = 40;
public Computer(GamePanel game) {
this.field = game;
}
public void update() {
if (field.getBall().getY() < this.y) {
//ball is above the computer
yVelocity = -4;
} else if (field.getBall().getY() > this.y) {
yVelocity = 4;
}
y = y + yVelocity;
}
public void paint(Graphics g) {
g.setColor(Color.red);
g.fillRect(Pong.WINDOW_WIDTH - (35 + width), y, width, height);
}
public int getX() {
return Pong.WINDOW_WIDTH - 6 - (35 + width);
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}
Gamepanel.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pong;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.Component;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import javax.swing.Timer;
/**
*
* @author Zuki
*/
public class GamePanel extends JPanel implements ActionListener, KeyListener {
Player player = new Player();
Ball ball = new Ball();
Computer computer = new Computer(this);
public GamePanel() {
Timer time = new Timer(50, this);
time.start();
this.addKeyListener(this);
this.setFocusable(true);
}
private void update() {
player.update();
ball.update();
computer.update();
ball.checkCollisionWith(player);
ball.checkCollisionWith(computer);
ball.hitWall();
}
public void paintComponent(Graphics g) {
g.setColor(Color.blue);
g.fillRect(0, 0, Pong.WINDOW_WIDTH, Pong.WINDOW_HEIGHT);
Toolkit t=Toolkit.getDefaultToolkit();
Image i=t.getImage("ponglogo.jpg");
g.drawImage(i, 120,100,this);
player.paint(g);
ball.paint(g);
computer.paint(g);
g.setColor(Color.white);
g.drawLine(0, 30, Pong.WINDOW_WIDTH, 30);
g.drawLine(Pong.WINDOW_WIDTH / 2, 30, Pong.WINDOW_WIDTH / 2, Pong.WINDOW_HEIGHT);
g.drawOval((Pong.WINDOW_WIDTH / 2) - 30, Pong.WINDOW_HEIGHT / 2 - 30, 60, 60);
g.setColor(Color.yellow);
}
public Ball getBall() {
return ball;
}
public void actionPerformed(ActionEvent e) {
update();
repaint();
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
player.setYVelocity(-5);
if (player.getY() < 30) {
player.setYVelocity(0);
}
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
player.setYVelocity(5);
if (player.getY() + 40 > Pong.WINDOW_HEIGHT - 28) {
player.setYVelocity(0);
}
}
}
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_DOWN) {
player.setYVelocity(0);
}
}
public void keyTyped(KeyEvent e) {
}
}
Player.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pong;
import java.awt.Color;
import java.awt.Graphics;
/**
*
* @author Zuki
*/
public class Player {
private int y = Pong.WINDOW_HEIGHT / 2;
private int yVelocity = 0;
private int width = 10;
private int height = 40;
public Player() {
}
public void update() {
y = y + yVelocity;
}
public void paint(Graphics g) {
g.setColor(Color.white);
g.fillRect(35, y, width, height);
}
public void setYVelocity(int speed) {
yVelocity = speed;
}
public int getX() {
return 35;
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}
Pong.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pong;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author Zuki
*/
public class Pong extends JFrame {
final static int WINDOW_WIDTH = 406;
final static int WINDOW_HEIGHT = 278;
public Pong() {
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new GamePanel());
setVisible(true);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Pong();
// TODO code application logic here
}
}
0 Comments