Advertisement
KeeganT

stuff

Jun 17th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.71 KB | None | 0 0
  1. package pong;
  2.  
  3. import javax.swing.JFrame;
  4.  
  5. public class Pong extends JFrame
  6. {
  7.     final public static int windowWidth = 900;
  8.     final public static int windowHeight = 600;
  9.  
  10.     public Pong()
  11.     {
  12.         add(new GamePanel());
  13.        
  14.         setSize(windowWidth,windowHeight);
  15.         setResizable(false);
  16.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  17.         setVisible(true);
  18.     }
  19.  
  20.     public static void main(String[] args) {
  21.         Pong Pong=new Pong();
  22.     }
  23. }
  24.  
  25. -------------------------------------------------------------
  26.  
  27. package pong;
  28.  
  29. import java.awt.Color;
  30. import java.awt.Graphics;
  31.  
  32. public class Ball
  33. {
  34.  
  35.     private int x = Pong.windowWidth / 2;
  36.     private int y = Pong.windowHeight / 2;
  37.     private int xVelocity = -5;
  38.     private int yVelocity = 5;
  39.     private final int size = 20;
  40.     private int playerScore = 0;
  41.     private int computerScore = 0;
  42.  
  43.     public void update() {
  44.         x = x + xVelocity;
  45.         y = y + yVelocity;
  46.  
  47.         if (x < 0) {
  48.             xVelocity = 5;
  49.             computerScore = computerScore + 1;
  50.         } else if (x + size > Pong.windowWidth - 6) {
  51.             xVelocity = -5;
  52.             playerScore = playerScore + 1;
  53.         }
  54.  
  55.         if (y < 0) {
  56.             yVelocity = 5;
  57.         } else if (y + size > Pong.windowHeight - 28) {
  58.             yVelocity = -5;
  59.         }
  60.     }
  61.  
  62.     public void paint(Graphics g) {
  63.         g.setColor(Color.white);
  64.         g.fillOval(x, y, size, size);
  65.         g.drawString(toPlayer(), 5, 15);
  66.         g.drawString(toComputer(), 280, 15);
  67.     }
  68.  
  69.     private void reverseDirection() {
  70.         xVelocity = -xVelocity;
  71.     }
  72.  
  73.     private void reverseDirectionY() {
  74.         yVelocity = -yVelocity;
  75.     }
  76.  
  77.     public void checkCollisionWith(Player player) {
  78.         if (this.x > player.getX() && this.x < player.getX() + player.getWidth()) {
  79.             if (this.y > player.getY() && this.y < player.getY() + player.getHeight()) {
  80.                 //ball has collided with player
  81.                 reverseDirection();
  82.             }
  83.         }
  84.     }
  85.  
  86.     public void hitWall() {
  87.         if (this.y < 30) {
  88.             reverseDirectionY();
  89.         }
  90.     }
  91.  
  92.     public void checkCollisionWith(Computer computer) {
  93.         if (this.x > computer.getX() && this.x < computer.getX() + computer.getWidth()) {
  94.             if (this.y > computer.getY() && this.y < computer.getY() + computer.getHeight()) {
  95.                 //ball has collided with computer
  96.                 reverseDirection();
  97.             }
  98.         }
  99.     }
  100.  
  101.     public int getX() {
  102.         return x;
  103.     }
  104.  
  105.     public int getY() {
  106.         return y;
  107.     }
  108.  
  109.     public int getPlayerScore() {
  110.         return playerScore;
  111.     }
  112.  
  113.     public int getComputerScore() {
  114.         return computerScore;
  115.     }
  116.  
  117.     public String toPlayer() {
  118.         String retVal = "";
  119.         retVal = "Player Score: " + playerScore;
  120.         return retVal;
  121.     }
  122.  
  123.     public String toComputer() {
  124.         String retVal = "";
  125.         retVal = "Computer Score: " + computerScore;
  126.         return retVal;
  127.     }
  128. }
  129.  
  130. -----------------------------------------------------------------
  131.  
  132. package pong;
  133.  
  134. import java.awt.Color;
  135. import java.awt.Graphics;
  136.  
  137. public class Computer
  138. {
  139.  
  140.     private final GamePanel field;
  141.     private int y = Pong.windowHeight / 2;
  142.     private int yVelocity = 0;
  143.     private final int width = 20;
  144.     private final int height = 80;
  145.  
  146.     public Computer(GamePanel game) {
  147.         this.field = game;
  148.     }
  149.  
  150.     public void update() {
  151.         if (field.getBall().getY() < this.y) {
  152.             //ball is above the computer
  153.             yVelocity = -3;
  154.         } else if (field.getBall().getY() > this.y) {
  155.             yVelocity = 3;
  156.         }
  157.         y+=yVelocity;
  158.  
  159.     }
  160.  
  161.     public void paint(Graphics g) {
  162.         g.setColor(Color.gray);
  163.         g.fillRect(Pong.windowWidth - (35 + width), y, width, height);
  164.     }
  165.  
  166.     public int getX() {
  167.         return Pong.windowWidth - 6 - (35 + width);
  168.     }
  169.  
  170.     public int getY() {
  171.         return y;
  172.     }
  173.  
  174.     public int getWidth() {
  175.         return width;
  176.     }
  177.  
  178.     public int getHeight() {
  179.         return height;
  180.     }
  181. }
  182.  
  183. ------------------------------------------------------------------------------
  184.  
  185. package pong;
  186.  
  187. import java.awt.Color;
  188. import java.awt.Graphics;
  189. import java.awt.event.ActionEvent;
  190. import java.awt.event.ActionListener;
  191. import java.awt.event.KeyEvent;
  192. import java.awt.event.KeyListener;
  193. import javax.swing.JPanel;
  194. import javax.swing.Timer;
  195.  
  196. public class GamePanel extends JPanel implements ActionListener, KeyListener {
  197.  
  198.     Player player = new Player();
  199.     Ball ball = new Ball();
  200.     Computer computer = new Computer(this);
  201.  
  202.     public GamePanel() {
  203.         Timer time = new Timer(50, this);
  204.         time.start();
  205.  
  206.         this.addKeyListener(this);
  207.         this.setFocusable(true);
  208.     }
  209.  
  210.     private void update() {
  211.         player.update();
  212.         ball.update();
  213.         computer.update();
  214.  
  215.         ball.checkCollisionWith(player);
  216.         ball.checkCollisionWith(computer);
  217.         ball.hitWall();
  218.  
  219.     }
  220.  
  221.     public void paintComponent(Graphics g) {
  222.         g.setColor(Color.black);
  223.         g.fillRect(0, 0, Pong.windowWidth, Pong.windowHeight);
  224.  
  225.         player.paint(g);
  226.         ball.paint(g);
  227.         computer.paint(g);
  228.  
  229.         g.setColor(Color.blue);
  230.         g.drawLine(0, 30, Pong.windowWidth, 30);
  231.         g.drawLine(Pong.windowWidth / 2, 30, Pong.windowWidth / 2, Pong.windowHeight);
  232.         g.setColor(Color.yellow);
  233.  
  234.  
  235.  
  236.     }
  237.  
  238.     public Ball getBall() {
  239.         return ball;
  240.     }
  241.  
  242.     public void actionPerformed(ActionEvent e) {
  243.         update();
  244.         repaint();
  245.     }
  246.  
  247.     public void keyPressed(KeyEvent e) {
  248.         if (e.getKeyCode() == KeyEvent.VK_UP) {
  249.             player.setYVelocity(-5);
  250.             if (player.getY() < 30) {
  251.                 player.setYVelocity(0);
  252.             }
  253.         } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
  254.             player.setYVelocity(5);
  255.             if (player.getY() + 40 > Pong.windowHeight - 28) {
  256.                 player.setYVelocity(0);
  257.             }
  258.         }
  259.     }
  260.  
  261.     public void keyReleased(KeyEvent e) {
  262.         int keyCode = e.getKeyCode();
  263.         if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_DOWN) {
  264.             player.setYVelocity(0);
  265.  
  266.         }
  267.     }
  268.  
  269.     public void keyTyped(KeyEvent e) {
  270.     }
  271. }
  272.  
  273. ---------------------------------------------------------------------------
  274.  
  275. package pong;
  276.  
  277. import java.awt.Color;
  278. import java.awt.Graphics;
  279.  
  280. public class Player {
  281.  
  282.     private int y = Pong.windowHeight / 2;
  283.     private int yVelocity = 0;
  284.     private final int width = 20;
  285.     private final int height = 80;
  286.  
  287.     public void update() {
  288.         y = y + yVelocity;
  289.     }
  290.  
  291.     public void paint(Graphics g) {
  292.         g.setColor(Color.gray);
  293.         g.fillRect(35, y, width, height);
  294.     }
  295.  
  296.     public void setYVelocity(int speed) {
  297.         yVelocity=speed;
  298.     }
  299.  
  300.     public int getX() {
  301.         return 35;
  302.     }
  303.  
  304.     public int getY() {
  305.         return y;
  306.     }
  307.  
  308.     public int getWidth() {
  309.         return width;
  310.     }
  311.  
  312.     public int getHeight() {
  313.         return height;
  314.     }
  315. }
  316.  
  317. =======================================================================
  318.  
  319. package pong;
  320.  
  321. import javax.swing.JFrame;
  322.  
  323. public class Pong extends JFrame
  324. {
  325.     public static int winWidth=900;
  326.     public static int winHeight=600;
  327.    
  328.     public Pong()
  329.     {
  330.         add(new Window());
  331.        
  332.         setSize(winWidth, winHeight);
  333.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  334.         setVisible(true);
  335.     }
  336.    
  337.     public static void main(String[] args)
  338.     {
  339.         Pong pong=new Pong();
  340.     }
  341. }
  342.  
  343. -----------------------------------------------------------------------
  344.  
  345. package pong;
  346.  
  347. import java.awt.Color;
  348. import java.awt.Graphics;
  349.  
  350. public class Ball
  351. {
  352.  
  353.     int x=Pong.winWidth/2;
  354.     int y=Pong.winHeight/2;
  355.     int xVel=-5;
  356.     int yVel=5;
  357.     int size=20;
  358.     int score=0;
  359.     int compScore=0;
  360.  
  361.     public void update()
  362.     {
  363.         x+=xVel;
  364.         y+=yVel;
  365.  
  366.         if (x<=-1)
  367.         {
  368.             xVel=5;
  369.             compScore++;
  370.         }
  371.         else if ((x+size)>Pong.winWidth-6)
  372.         {
  373.             xVel=-5;
  374.             score++;
  375.         }
  376.  
  377.         if (y<=-1)
  378.         {
  379.             yVel=5;
  380.         }
  381.        
  382.         else if ((y+size)>Pong.winHeight-28)
  383.         {
  384.             yVel=-5;
  385.         }
  386.     }
  387.  
  388.     private void reverseX()
  389.     {
  390.         xVel=-xVel;
  391.     }
  392.  
  393.     private void reverseY()
  394.     {
  395.         yVel=-yVel;
  396.     }
  397.  
  398.     public void checkCollisionWith(Player player)
  399.     {
  400.         if(this.x>player.returnX()&&this.x<player.returnX()+player.returnWidth())
  401.         {
  402.             if(this.y >player.returnY()&&this.y<player.returnY()+player.returnHeight())reverseX();
  403.         }
  404.     }
  405.  
  406.     public void wallHit()
  407.     {
  408.         if(this.y < 30)
  409.         {
  410.             reverseY();
  411.         }
  412.     }
  413.  
  414.     public void checkCollisionWith(Computer computer)
  415.     {
  416.         if(this.x>computer.returnX()&&this.x<computer.returnX()+computer.returnWidth())
  417.         {
  418.             if(this.y>computer.returnY()&&this.y<computer.returnY()+computer.returnHeight())reverseX();
  419.         }
  420.     }
  421.    
  422.     public void color(Graphics graphics)
  423.     {
  424.         graphics.setColor(Color.white);
  425.         graphics.fillOval(x, y, size, size);
  426.         graphics.drawString(showScore(), 5, 15);
  427.         graphics.drawString(showCompScore(), 280, 15);
  428.     }
  429.  
  430.     public int getX()
  431.     {
  432.         return x;
  433.     }
  434.  
  435.     public int getY()
  436.     {
  437.         return y;
  438.     }
  439.  
  440.     public int getPlayerScore()
  441.     {
  442.         return score;
  443.     }
  444.  
  445.     public int getComputerScore()
  446.     {
  447.         return compScore;
  448.     }
  449.  
  450.     public String showScore()
  451.     {
  452.         return "Player Score: "+score;
  453.     }
  454.  
  455.     public String showCompScore()
  456.     {
  457.         return "Computer Score: "+compScore;
  458.     }
  459. }
  460.  
  461. -----------------------------------------------------------------------
  462.  
  463. package pong;
  464.  
  465. import java.awt.Color;
  466. import java.awt.Graphics;
  467.  
  468. public class Player
  469. {
  470.  
  471.     int y=Pong.winHeight/2;
  472.     int yVel=0;
  473.     int w=20;
  474.     int h=80;
  475.  
  476.     public void update()
  477.     {
  478.         y+=yVel;
  479.     }
  480.  
  481.     public void color(Graphics graphics)
  482.     {
  483.         graphics.setColor(Color.gray);
  484.         graphics.fillRect(35, y, w, h);
  485.     }
  486.  
  487.     public void setYVel(int speed)
  488.     {
  489.         yVel=speed;
  490.     }
  491.  
  492.     public int returnX()
  493.     {
  494.         return 35;
  495.     }
  496.  
  497.     public int returnY()
  498.     {
  499.         return y;
  500.     }
  501.  
  502.     public int returnWidth()
  503.     {
  504.         return w;
  505.     }
  506.  
  507.     public int returnHeight()
  508.     {
  509.         return h;
  510.     }
  511. }
  512.  
  513. ---------------------------------------------------------------------
  514.  
  515. package pong;
  516.  
  517. import java.awt.Color;
  518. import java.awt.Graphics;
  519. import java.awt.event.ActionEvent;
  520. import java.awt.event.ActionListener;
  521. import java.awt.event.KeyEvent;
  522. import java.awt.event.KeyListener;
  523. import javax.swing.JPanel;
  524. import javax.swing.Timer;
  525.  
  526. public class Window extends JPanel implements ActionListener, KeyListener
  527. {
  528.     Player player=new Player();
  529.     Ball ball=new Ball();
  530.     Computer computer=new Computer(this);
  531.    
  532.     public Window()
  533.     {
  534.         Timer timer=new Timer(50, this);
  535.         timer.start();
  536.  
  537.         this.addKeyListener(this);
  538.         this.setFocusable(true);
  539.     }
  540.  
  541.     private void update() {
  542.         player.update();
  543.         ball.update();
  544.         computer.update();
  545.  
  546.         ball.checkCollisionWith(player);
  547.         ball.checkCollisionWith(computer);
  548.         ball.wallHit();
  549.  
  550.     }
  551.  
  552.     @Override
  553.     public void paintComponent(Graphics graphics)
  554.     {
  555.         graphics.setColor(Color.black);
  556.         graphics.fillRect(0, 0, Pong.winWidth, Pong.winHeight);
  557.  
  558.         player.color(graphics);
  559.         ball.color(graphics);
  560.         computer.color(graphics);
  561.  
  562.         graphics.setColor(Color.blue);
  563.         graphics.drawLine(0, 30, Pong.winWidth, 30);
  564.         graphics.drawLine(Pong.winWidth / 2, 30, Pong.winWidth / 2, Pong.winHeight);
  565.         graphics.setColor(Color.yellow);
  566.     }
  567.  
  568.     public Ball getBall()
  569.     {
  570.         return ball;
  571.     }
  572.  
  573.     public void action(ActionEvent aEvent) {
  574.         update();
  575.         repaint();
  576.     }
  577.  
  578.     public void keyPress(KeyEvent kEvent)
  579.     {
  580.         if (kEvent.getKeyCode()==KeyEvent.VK_UP)
  581.         {
  582.             player.setYVel(-5);
  583.             if (player.returnY()<30)player.setYVel(0);
  584.         }
  585.        
  586.         else if (kEvent.getKeyCode()==KeyEvent.VK_DOWN)
  587.         {
  588.             player.setYVel(5);
  589.             if (player.returnY()+40>Pong.winHeight-28)
  590.             {
  591.                 player.setYVel(0);
  592.             }
  593.         }
  594.     }
  595.  
  596.     public void keyRelease(KeyEvent kEvent)
  597.     {
  598.         if (kEvent.getKeyCode()==KeyEvent.VK_UP||kEvent.getKeyCode()==KeyEvent.VK_DOWN)player.setYVel(0);
  599.     }
  600.  
  601.     @Override
  602.     public void actionPerformed(ActionEvent ae) {
  603.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  604.     }
  605.  
  606.     @Override
  607.     public void keyTyped(KeyEvent ke) {
  608.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  609.     }
  610.  
  611.     @Override
  612.     public void keyPressed(KeyEvent ke) {
  613.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  614.     }
  615.  
  616.     @Override
  617.     public void keyReleased(KeyEvent ke) {
  618.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  619.     }
  620. }
  621.  
  622. -----------------------------------------------------------------------
  623.  
  624. package pong;
  625.  
  626. import java.awt.Color;
  627. import java.awt.Graphics;
  628.  
  629. public class Computer
  630. {
  631.  
  632.     Window window;
  633.     int y=Pong.winHeight / 2;
  634.     int yVel=0;
  635.     int w=20;
  636.     int h=80;
  637.  
  638.     public Computer(Window window)
  639.     {
  640.         this.window=window;
  641.     }
  642.  
  643.     public void update()
  644.     {
  645.         if (window.getBall().getY() < this.y)
  646.         {
  647.             yVel=-3;
  648.         }
  649.         else if (window.getBall().getY() > this.y)
  650.         {
  651.             yVel=3;
  652.         }
  653.         y+=yVel;
  654.     }
  655.  
  656.     public int returnX()
  657.     {
  658.         return Pong.winWidth-6-(35+w);
  659.     }
  660.  
  661.     public int returnY() {
  662.         return y;
  663.     }
  664.  
  665.     public int returnWidth()
  666.     {
  667.         return w;
  668.     }
  669.  
  670.     public int returnHeight()
  671.     {
  672.         return h;
  673.     }
  674.  
  675.     void color(Graphics graphics)
  676.     {
  677.         graphics.setColor(Color.gray);
  678.         graphics.fillRect(Pong.winWidth-(35+w), y, w, h);
  679.     }
  680. }
  681.  
  682. -= END =-
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement