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
- {
- 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 javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- 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(25, 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();
- }
- public void paint(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.gray);
- graphics.drawLine(0, 30, Pong.winWidth, 30);
- graphics.drawLine(200, 30, Pong.winWidth, 30);
- }
- public Ball getBall()
- {
- return ball;
- }
- public void actionPerformed(ActionEvent e)
- {
- update();
- repaint();
- }
- public void keyPressed(KeyEvent e)
- {
- if (e.getKeyCode()==KeyEvent.VK_UP)
- {
- player.setYVel(-5);
- if(player.returnY()<30)player.setYVel(0);
- }
- else if(e.getKeyCode()==KeyEvent.VK_DOWN)
- {
- player.setYVel(5);
- if(player.returnY()+40>Pong.winHeight-80)player.setYVel(0);
- }
- }
- public void keyReleased(KeyEvent e)
- {
- if (e.getKeyCode()==KeyEvent.VK_UP||e.getKeyCode()==KeyEvent.VK_DOWN)player.setYVel(0);
- }
- //Unused
- public void keyTyped(KeyEvent e) {
- }
- }
- -
- package pong;
- import java.awt.*;
- public class Ball
- {
- int x=Pong.winWidth/2;
- int y=Pong.winHeight/2;
- int xVel=-5;
- int yVel=5;
- int score=0;
- int compScore=0;
- public void update()
- {
- x+=xVel;
- y+=yVel;
- if (x<=-1)
- {
- xVel=5;
- compScore++;
- }
- else if ((x+20)>Pong.winWidth-6)
- {
- xVel=-5;
- score++;
- }
- if (y<=-1)yVel=5;
- else if ((y+20)>Pong.winHeight-28)yVel=-5;
- }
- private void reverseX()
- {
- xVel=-xVel;
- }
- private void reverseY()
- {
- yVel=-yVel;
- }
- public void checkCollisionWith(Player player)
- {
- if(x>player.returnX()&&x<player.returnX()+20)
- {
- if(y>player.returnY()&&y<player.returnY()+80)reverseX();
- }
- }
- public void checkCollisionWith(Computer computer)
- {
- if(x>computer.returnX()&&x<computer.returnX()+20)
- {
- if(y>computer.returnY()&&y<computer.returnY()+80)reverseX();
- }
- }
- public void wallHit()
- {
- if(y<30)reverseY();
- }
- public void color(Graphics graphics)
- {
- graphics.setColor(Color.white);
- graphics.fillOval(x, y, 20, 20);
- graphics.drawString("Your score: "+score, 5, 20);
- graphics.drawString("Computer's Score: "+compScore, 760, 20);
- }
- public int getX()
- {
- return x;
- }
- public int getY()
- {
- return y;
- }
- }
- -
- package pong;
- import java.awt.*;
- public class Player
- {
- int y=Pong.winHeight/2;
- int yVel=0;
- public void update()
- {
- y+=yVel;
- }
- public void color(Graphics graphics)
- {
- graphics.setColor(Color.red);
- graphics.fillRect(35, y, 20, 80);
- }
- public void setYVel(int speed)
- {
- yVel=speed;
- }
- public int returnX()
- {
- return 35;
- }
- public int returnY()
- {
- return y;
- }
- }
- -
- package pong;
- import java.awt.*;
- public class Computer
- {
- Window window;
- int y=Pong.winHeight/2;
- int yVel=0;
- public Computer(Window window)
- {
- this.window=window;
- }
- public void update()
- {
- if(window.getBall().getY()<y)yVel=-3;
- else if(window.getBall().getY()>y)yVel=3;
- y+=yVel;
- }
- public int returnX()
- {
- return Pong.winWidth-61;
- }
- public int returnY() {
- return y;
- }
- void color(Graphics graphics)
- {
- graphics.setColor(Color.blue);
- graphics.fillRect(Pong.winWidth-55, y, 20, 80);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement