Advertisement
KeeganT

Pong

Jun 19th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.04 KB | None | 0 0
  1. package pong;
  2.  
  3. import javax.swing.JFrame;
  4.  
  5. public class Pong extends JFrame
  6. {
  7.     public static int winWidth=900;
  8.     public static int winHeight=600;
  9.    
  10.     public Pong()
  11.     {
  12.         add(new Window());
  13.        
  14.         setSize(winWidth, winHeight);
  15.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16.         setVisible(true);
  17.     }
  18.    
  19.     public static void main(String[] args)
  20.     {
  21.         Pong pong=new Pong();
  22.     }
  23. }
  24.  
  25. -
  26.  
  27. package pong;
  28.  
  29. import javax.swing.*;
  30. import java.awt.*;
  31. import java.awt.event.*;
  32.  
  33. public class Window extends JPanel implements ActionListener, KeyListener {
  34.  
  35.     Player player=new Player();
  36.     Ball ball=new Ball();
  37.     Computer computer=new Computer(this);
  38.  
  39.     public Window()
  40.     {
  41.         Timer timer=new Timer(25, this);
  42.         timer.start();
  43.  
  44.         this.addKeyListener(this);
  45.         this.setFocusable(true);
  46.     }
  47.  
  48.     private void update()
  49.     {
  50.         player.update();
  51.         ball.update();
  52.         computer.update();
  53.  
  54.         ball.checkCollisionWith(player);
  55.         ball.checkCollisionWith(computer);
  56.         ball.wallHit();
  57.  
  58.     }
  59.  
  60.     public void paint(Graphics graphics)
  61.     {
  62.         graphics.setColor(Color.black);
  63.         graphics.fillRect(0, 0, Pong.winWidth, Pong.winHeight);
  64.  
  65.         player.color(graphics);
  66.         ball.color(graphics);
  67.         computer.color(graphics);
  68.  
  69.         graphics.setColor(Color.gray);
  70.         graphics.drawLine(0, 30, Pong.winWidth, 30);
  71.         graphics.drawLine(200, 30, Pong.winWidth, 30);
  72.     }
  73.  
  74.     public Ball getBall()
  75.     {
  76.         return ball;
  77.     }
  78.  
  79.     public void actionPerformed(ActionEvent e)
  80.     {
  81.         update();
  82.         repaint();
  83.     }
  84.  
  85.     public void keyPressed(KeyEvent e)
  86.     {
  87.         if (e.getKeyCode()==KeyEvent.VK_UP)
  88.         {
  89.             player.setYVel(-5);
  90.             if(player.returnY()<30)player.setYVel(0);
  91.         }
  92.        
  93.         else if(e.getKeyCode()==KeyEvent.VK_DOWN)
  94.         {
  95.             player.setYVel(5);
  96.             if(player.returnY()+40>Pong.winHeight-80)player.setYVel(0);
  97.         }
  98.     }
  99.  
  100.     public void keyReleased(KeyEvent e)
  101.     {
  102.         if (e.getKeyCode()==KeyEvent.VK_UP||e.getKeyCode()==KeyEvent.VK_DOWN)player.setYVel(0);
  103.     }
  104.  
  105.     //Unused
  106.     public void keyTyped(KeyEvent e) {
  107.     }
  108. }
  109.  
  110. -
  111.  
  112. package pong;
  113.  
  114. import java.awt.*;
  115.  
  116. public class Ball
  117. {
  118.  
  119.     int x=Pong.winWidth/2;
  120.     int y=Pong.winHeight/2;
  121.     int xVel=-5;
  122.     int yVel=5;
  123.     int score=0;
  124.     int compScore=0;
  125.  
  126.     public void update()
  127.     {
  128.         x+=xVel;
  129.         y+=yVel;
  130.         if (x<=-1)
  131.         {
  132.             xVel=5;
  133.             compScore++;
  134.         }
  135.         else if ((x+20)>Pong.winWidth-6)
  136.         {
  137.             xVel=-5;
  138.             score++;
  139.         }
  140.         if (y<=-1)yVel=5;
  141.         else if ((y+20)>Pong.winHeight-28)yVel=-5;
  142.     }
  143.  
  144.     private void reverseX()
  145.     {
  146.         xVel=-xVel;
  147.     }
  148.  
  149.     private void reverseY()
  150.     {
  151.         yVel=-yVel;
  152.     }
  153.  
  154.     public void checkCollisionWith(Player player)
  155.     {
  156.         if(x>player.returnX()&&x<player.returnX()+20)
  157.         {
  158.             if(y>player.returnY()&&y<player.returnY()+80)reverseX();
  159.         }
  160.     }
  161.  
  162.     public void checkCollisionWith(Computer computer)
  163.     {
  164.         if(x>computer.returnX()&&x<computer.returnX()+20)
  165.         {
  166.             if(y>computer.returnY()&&y<computer.returnY()+80)reverseX();
  167.         }
  168.     }
  169.    
  170.     public void wallHit()
  171.     {
  172.         if(y<30)reverseY();
  173.     }
  174.    
  175.     public void color(Graphics graphics)
  176.     {
  177.         graphics.setColor(Color.white);
  178.         graphics.fillOval(x, y, 20, 20);
  179.         graphics.drawString("Your score: "+score, 5, 20);
  180.         graphics.drawString("Computer's Score: "+compScore, 760, 20);
  181.     }
  182.  
  183.     public int getX()
  184.     {
  185.         return x;
  186.     }
  187.  
  188.     public int getY()
  189.     {
  190.         return y;
  191.     }
  192. }
  193.  
  194. -
  195.  
  196. package pong;
  197.  
  198. import java.awt.*;
  199.  
  200. public class Player
  201. {
  202.  
  203.     int y=Pong.winHeight/2;
  204.     int yVel=0;
  205.  
  206.     public void update()
  207.     {
  208.         y+=yVel;
  209.     }
  210.  
  211.     public void color(Graphics graphics)
  212.     {
  213.         graphics.setColor(Color.red);
  214.         graphics.fillRect(35, y, 20, 80);
  215.     }
  216.  
  217.     public void setYVel(int speed)
  218.     {
  219.         yVel=speed;
  220.     }
  221.  
  222.     public int returnX()
  223.     {
  224.         return 35;
  225.     }
  226.  
  227.     public int returnY()
  228.     {
  229.         return y;
  230.     }
  231. }
  232.  
  233. -
  234.  
  235. package pong;
  236.  
  237. import java.awt.*;
  238.  
  239. public class Computer
  240. {
  241.  
  242.     Window window;
  243.     int y=Pong.winHeight/2;
  244.     int yVel=0;
  245.  
  246.     public Computer(Window window)
  247.     {
  248.         this.window=window;
  249.     }
  250.  
  251.     public void update()
  252.     {
  253.         if(window.getBall().getY()<y)yVel=-3;
  254.         else if(window.getBall().getY()>y)yVel=3;
  255.         y+=yVel;
  256.     }
  257.  
  258.     public int returnX()
  259.     {
  260.         return Pong.winWidth-61;
  261.     }
  262.  
  263.     public int returnY() {
  264.         return y;
  265.     }
  266.  
  267.     void color(Graphics graphics)
  268.     {
  269.         graphics.setColor(Color.blue);
  270.         graphics.fillRect(Pong.winWidth-55, y, 20, 80);
  271.     }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement