Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pong;
- import javax.swing.JFrame;
- public class Pong extends JFrame
- {
- final public static int windowWidth = 900;
- final public static int windowHeight = 600;
- public Pong()
- {
- add(new GamePanel());
- setSize(windowWidth,windowHeight);
- setResizable(false);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setVisible(true);
- }
- public static void main(String[] args) {
- Pong Pong=new Pong();
- }
- }
- -------------------------------------------------------------
- package pong;
- import java.awt.Color;
- import java.awt.Graphics;
- public class Ball
- {
- private int x = Pong.windowWidth / 2;
- private int y = Pong.windowHeight / 2;
- private int xVelocity = -5;
- private int yVelocity = 5;
- private final int size = 20;
- private int playerScore = 0;
- private int computerScore = 0;
- public void update() {
- x = x + xVelocity;
- y = y + yVelocity;
- if (x < 0) {
- xVelocity = 5;
- computerScore = computerScore + 1;
- } else if (x + size > Pong.windowWidth - 6) {
- xVelocity = -5;
- playerScore = playerScore + 1;
- }
- if (y < 0) {
- yVelocity = 5;
- } else if (y + size > Pong.windowHeight - 28) {
- yVelocity = -5;
- }
- }
- public void paint(Graphics g) {
- g.setColor(Color.white);
- 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;
- }
- }
- -----------------------------------------------------------------
- package pong;
- import java.awt.Color;
- import java.awt.Graphics;
- public class Computer
- {
- private final GamePanel field;
- private int y = Pong.windowHeight / 2;
- private int yVelocity = 0;
- private final int width = 20;
- private final int height = 80;
- public Computer(GamePanel game) {
- this.field = game;
- }
- public void update() {
- if (field.getBall().getY() < this.y) {
- //ball is above the computer
- yVelocity = -3;
- } else if (field.getBall().getY() > this.y) {
- yVelocity = 3;
- }
- y+=yVelocity;
- }
- public void paint(Graphics g) {
- g.setColor(Color.gray);
- g.fillRect(Pong.windowWidth - (35 + width), y, width, height);
- }
- public int getX() {
- return Pong.windowWidth - 6 - (35 + width);
- }
- public int getY() {
- return y;
- }
- public int getWidth() {
- return width;
- }
- public int getHeight() {
- return height;
- }
- }
- ------------------------------------------------------------------------------
- package pong;
- import java.awt.Color;
- 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 javax.swing.JPanel;
- import javax.swing.Timer;
- 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.black);
- g.fillRect(0, 0, Pong.windowWidth, Pong.windowHeight);
- player.paint(g);
- ball.paint(g);
- computer.paint(g);
- g.setColor(Color.blue);
- g.drawLine(0, 30, Pong.windowWidth, 30);
- g.drawLine(Pong.windowWidth / 2, 30, Pong.windowWidth / 2, Pong.windowHeight);
- 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.windowHeight - 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) {
- }
- }
- ---------------------------------------------------------------------------
- package pong;
- import java.awt.Color;
- import java.awt.Graphics;
- public class Player {
- private int y = Pong.windowHeight / 2;
- private int yVelocity = 0;
- private final int width = 20;
- private final int height = 80;
- public void update() {
- y = y + yVelocity;
- }
- public void paint(Graphics g) {
- g.setColor(Color.gray);
- 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;
- }
- }
- =======================================================================
- package pong;
- import javax.swing.JFrame;
- public class Pong extends JFrame
- {
- public static int winWidth=900;
- public static int winHeight=600;
- public Pong()
- {
- add(new Window());
- setSize(winWidth, winHeight);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setVisible(true);
- }
- public static void main(String[] args)
- {
- Pong pong=new Pong();
- }
- }
- -----------------------------------------------------------------------
- package pong;
- import java.awt.Color;
- import java.awt.Graphics;
- public class Ball
- {
- int x=Pong.winWidth/2;
- int y=Pong.winHeight/2;
- int xVel=-5;
- int yVel=5;
- int size=20;
- int score=0;
- int compScore=0;
- public void update()
- {
- x+=xVel;
- y+=yVel;
- if (x<=-1)
- {
- xVel=5;
- compScore++;
- }
- else if ((x+size)>Pong.winWidth-6)
- {
- xVel=-5;
- score++;
- }
- if (y<=-1)
- {
- yVel=5;
- }
- else if ((y+size)>Pong.winHeight-28)
- {
- yVel=-5;
- }
- }
- private void reverseX()
- {
- xVel=-xVel;
- }
- private void reverseY()
- {
- yVel=-yVel;
- }
- public void checkCollisionWith(Player player)
- {
- if(this.x>player.returnX()&&this.x<player.returnX()+player.returnWidth())
- {
- if(this.y >player.returnY()&&this.y<player.returnY()+player.returnHeight())reverseX();
- }
- }
- public void wallHit()
- {
- if(this.y < 30)
- {
- reverseY();
- }
- }
- public void checkCollisionWith(Computer computer)
- {
- if(this.x>computer.returnX()&&this.x<computer.returnX()+computer.returnWidth())
- {
- if(this.y>computer.returnY()&&this.y<computer.returnY()+computer.returnHeight())reverseX();
- }
- }
- public void color(Graphics graphics)
- {
- graphics.setColor(Color.white);
- graphics.fillOval(x, y, size, size);
- graphics.drawString(showScore(), 5, 15);
- graphics.drawString(showCompScore(), 280, 15);
- }
- public int getX()
- {
- return x;
- }
- public int getY()
- {
- return y;
- }
- public int getPlayerScore()
- {
- return score;
- }
- public int getComputerScore()
- {
- return compScore;
- }
- public String showScore()
- {
- return "Player Score: "+score;
- }
- public String showCompScore()
- {
- return "Computer Score: "+compScore;
- }
- }
- -----------------------------------------------------------------------
- package pong;
- import java.awt.Color;
- import java.awt.Graphics;
- public class Player
- {
- int y=Pong.winHeight/2;
- int yVel=0;
- int w=20;
- int h=80;
- public void update()
- {
- y+=yVel;
- }
- public void color(Graphics graphics)
- {
- graphics.setColor(Color.gray);
- graphics.fillRect(35, y, w, h);
- }
- public void setYVel(int speed)
- {
- yVel=speed;
- }
- public int returnX()
- {
- return 35;
- }
- public int returnY()
- {
- return y;
- }
- public int returnWidth()
- {
- return w;
- }
- public int returnHeight()
- {
- return h;
- }
- }
- ---------------------------------------------------------------------
- package pong;
- import java.awt.Color;
- 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 javax.swing.JPanel;
- import javax.swing.Timer;
- public class Window extends JPanel implements ActionListener, KeyListener
- {
- Player player=new Player();
- Ball ball=new Ball();
- Computer computer=new Computer(this);
- public Window()
- {
- Timer timer=new Timer(50, this);
- timer.start();
- this.addKeyListener(this);
- this.setFocusable(true);
- }
- private void update() {
- player.update();
- ball.update();
- computer.update();
- ball.checkCollisionWith(player);
- ball.checkCollisionWith(computer);
- ball.wallHit();
- }
- @Override
- public void paintComponent(Graphics graphics)
- {
- graphics.setColor(Color.black);
- graphics.fillRect(0, 0, Pong.winWidth, Pong.winHeight);
- player.color(graphics);
- ball.color(graphics);
- computer.color(graphics);
- graphics.setColor(Color.blue);
- graphics.drawLine(0, 30, Pong.winWidth, 30);
- graphics.drawLine(Pong.winWidth / 2, 30, Pong.winWidth / 2, Pong.winHeight);
- graphics.setColor(Color.yellow);
- }
- public Ball getBall()
- {
- return ball;
- }
- public void action(ActionEvent aEvent) {
- update();
- repaint();
- }
- public void keyPress(KeyEvent kEvent)
- {
- if (kEvent.getKeyCode()==KeyEvent.VK_UP)
- {
- player.setYVel(-5);
- if (player.returnY()<30)player.setYVel(0);
- }
- else if (kEvent.getKeyCode()==KeyEvent.VK_DOWN)
- {
- player.setYVel(5);
- if (player.returnY()+40>Pong.winHeight-28)
- {
- player.setYVel(0);
- }
- }
- }
- public void keyRelease(KeyEvent kEvent)
- {
- if (kEvent.getKeyCode()==KeyEvent.VK_UP||kEvent.getKeyCode()==KeyEvent.VK_DOWN)player.setYVel(0);
- }
- @Override
- public void actionPerformed(ActionEvent ae) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- @Override
- public void keyTyped(KeyEvent ke) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- @Override
- public void keyPressed(KeyEvent ke) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- @Override
- public void keyReleased(KeyEvent ke) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- }
- -----------------------------------------------------------------------
- package pong;
- import java.awt.Color;
- import java.awt.Graphics;
- public class Computer
- {
- Window window;
- int y=Pong.winHeight / 2;
- int yVel=0;
- int w=20;
- int h=80;
- public Computer(Window window)
- {
- this.window=window;
- }
- public void update()
- {
- if (window.getBall().getY() < this.y)
- {
- yVel=-3;
- }
- else if (window.getBall().getY() > this.y)
- {
- yVel=3;
- }
- y+=yVel;
- }
- public int returnX()
- {
- return Pong.winWidth-6-(35+w);
- }
- public int returnY() {
- return y;
- }
- public int returnWidth()
- {
- return w;
- }
- public int returnHeight()
- {
- return h;
- }
- void color(Graphics graphics)
- {
- graphics.setColor(Color.gray);
- graphics.fillRect(Pong.winWidth-(35+w), y, w, h);
- }
- }
- -= END =-
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement