Pong With Java

PONG.JAVA
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package pong;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.util.Random;
  6.  
  7. public class Ball
  8. {
  9.  
  10.     public int x, y, width = 25, height = 25;
  11.  
  12.     public int motionX, motionY;
  13.  
  14.     public Random random;
  15.  
  16.     private Pong pong;
  17.  
  18.     public int amountOfHits;
  19.  
  20.     public Ball(Pong pong)
  21.     {
  22.         this.pong = pong;
  23.  
  24.         this.random = new Random();
  25.  
  26.         spawn();
  27.     }
  28.  
  29.     public void update(Paddle paddle1, Paddle paddle2)
  30.     {
  31.         int speed = 5;
  32.  
  33.         this.x += motionX * speed;
  34.         this.y += motionY * speed;
  35.  
  36.         if (this.y + height - motionY > pong.height || this.y + motionY < 0)
  37.         {
  38.             if (this.motionY < 0)
  39.             {
  40.                 this.y = 0;
  41.                 this.motionY = random.nextInt(4);
  42.  
  43.                 if (motionY == 0)
  44.                 {
  45.                     motionY = 1;
  46.                 }
  47.             }
  48.             else
  49.             {
  50.                 this.motionY = -random.nextInt(4);
  51.                 this.y = pong.height - height;
  52.  
  53.                 if (motionY == 0)
  54.                 {
  55.                     motionY = -1;
  56.                 }
  57.             }
  58.         }
  59.  
  60.         if (checkCollision(paddle1) == 1)
  61.         {
  62.             this.motionX = 1 + (amountOfHits / 5);
  63.             this.motionY = -2 + random.nextInt(4);
  64.  
  65.             if (motionY == 0)
  66.             {
  67.                 motionY = 1;
  68.             }
  69.  
  70.             amountOfHits++;
  71.         }
  72.         else if (checkCollision(paddle2) == 1)
  73.         {
  74.             this.motionX = -1 - (amountOfHits / 5);
  75.             this.motionY = -2 + random.nextInt(4);
  76.  
  77.             if (motionY == 0)
  78.             {
  79.                 motionY = 1;
  80.             }
  81.  
  82.             amountOfHits++;
  83.         }
  84.  
  85.         if (checkCollision(paddle1) == 2)
  86.         {
  87.             paddle2.score++;
  88.             spawn();
  89.         }
  90.         else if (checkCollision(paddle2) == 2)
  91.         {
  92.             paddle1.score++;
  93.             spawn();
  94.         }
  95.     }
  96.  
  97.     public void spawn()
  98.     {
  99.         this.amountOfHits = 0;
  100.         this.x = pong.width / 2 - this.width / 2;
  101.         this.y = pong.height / 2 - this.height / 2;
  102.  
  103.         this.motionY = -2 + random.nextInt(4);
  104.  
  105.         if (motionY == 0)
  106.         {
  107.             motionY = 1;
  108.         }
  109.  
  110.         if (random.nextBoolean())
  111.         {
  112.             motionX = 1;
  113.         }
  114.         else
  115.         {
  116.             motionX = -1;
  117.         }
  118.     }
  119.  
  120.     public int checkCollision(Paddle paddle)
  121.     {
  122.         if (this.x < paddle.x + paddle.width && this.x + width > paddle.x && this.y < paddle.y + paddle.height && this.y + height > paddle.y)
  123.         {
  124.             return 1; //bounce
  125.         }
  126.         else if ((paddle.x > x && paddle.paddleNumber == 1) || (paddle.x < x - width && paddle.paddleNumber == 2))
  127.         {
  128.             return 2; //score
  129.         }
  130.  
  131.         return 0; //nothing
  132.     }
  133.  
  134.     public void render(Graphics g)
  135.     {
  136.         g.setColor(Color.WHITE);
  137.         g.fillOval(x, y, width, height);
  138.     }
  139.  
  140. }
RENDERER.JAVA
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package pong;
  2.  
  3. import java.awt.Graphics;
  4. import java.awt.Graphics2D;
  5.  
  6. import javax.swing.JPanel;
  7.  
  8. public class Renderer extends JPanel
  9. {
  10.  
  11.     private static final long serialVersionUID = 1L;
  12.  
  13.     @Override
  14.     protected void paintComponent(Graphics g)
  15.     {
  16.         super.paintComponent(g);
  17.  
  18.         Pong.pong.render((Graphics2D) g);
  19.     }
  20.  
  21. }
PONG.JAVA
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package pong;
  2.  
  3. import java.awt.BasicStroke;
  4. import java.awt.Color;
  5. import java.awt.Font;
  6. import java.awt.Graphics2D;
  7. import java.awt.RenderingHints;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.KeyEvent;
  11. import java.awt.event.KeyListener;
  12. import java.util.Random;
  13.  
  14. import javax.swing.JFrame;
  15. import javax.swing.Timer;
  16.  
  17. public class Pong implements ActionListener, KeyListener
  18. {
  19.  
  20.     public static Pong pong;
  21.  
  22.     public int width = 700, height = 700;
  23.  
  24.     public Renderer renderer;
  25.  
  26.     public Paddle player1;
  27.  
  28.     public Paddle player2;
  29.  
  30.     public Ball ball;
  31.  
  32.     public boolean bot = false, selectingDifficulty;
  33.  
  34.     public boolean w, s, up, down;
  35.  
  36.     public int gameStatus = 0, scoreLimit = 7, playerWon; //0 = Menu, 1 = Paused, 2 = Playing, 3 = Over
  37.  
  38.     public int botDifficulty, botMoves, botCooldown = 0;
  39.  
  40.     public Random random;
  41.  
  42.     public JFrame jframe;
  43.  
  44.     public Pong()
  45.     {
  46.         Timer timer = new Timer(20, this);
  47.         random = new Random();
  48.  
  49.         jframe = new JFrame("Pong");
  50.  
  51.         renderer = new Renderer();
  52.  
  53.         jframe.setSize(width + 15, height + 35);
  54.         jframe.setVisible(true);
  55.         jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  56.         jframe.add(renderer);
  57.         jframe.addKeyListener(this);
  58.  
  59.         timer.start();
  60.     }
  61.  
  62.     public void start()
  63.     {
  64.         gameStatus = 2;
  65.         player1 = new Paddle(this, 1);
  66.         player2 = new Paddle(this, 2);
  67.         ball = new Ball(this);
  68.     }
  69.  
  70.     public void update()
  71.     {
  72.         if (player1.score >= scoreLimit)
  73.         {
  74.             playerWon = 1;
  75.             gameStatus = 3;
  76.         }
  77.  
  78.         if (player2.score >= scoreLimit)
  79.         {
  80.             gameStatus = 3;
  81.             playerWon = 2;
  82.         }
  83.  
  84.         if (w)
  85.         {
  86.             player1.move(true);
  87.         }
  88.         if (s)
  89.         {
  90.             player1.move(false);
  91.         }
  92.  
  93.         if (!bot)
  94.         {
  95.             if (up)
  96.             {
  97.                 player2.move(true);
  98.             }
  99.             if (down)
  100.             {
  101.                 player2.move(false);
  102.             }
  103.         }
  104.         else
  105.         {
  106.             if (botCooldown > 0)
  107.             {
  108.                 botCooldown--;
  109.  
  110.                 if (botCooldown == 0)
  111.                 {
  112.                     botMoves = 0;
  113.                 }
  114.             }
  115.  
  116.             if (botMoves < 10)
  117.             {
  118.                 if (player2.y + player2.height / 2 < ball.y)
  119.                 {
  120.                     player2.move(false);
  121.                     botMoves++;
  122.                 }
  123.  
  124.                 if (player2.y + player2.height / 2 > ball.y)
  125.                 {
  126.                     player2.move(true);
  127.                     botMoves++;
  128.                 }
  129.  
  130.                 if (botDifficulty == 0)
  131.                 {
  132.                     botCooldown = 20;
  133.                 }
  134.                 if (botDifficulty == 1)
  135.                 {
  136.                     botCooldown = 15;
  137.                 }
  138.                 if (botDifficulty == 2)
  139.                 {
  140.                     botCooldown = 10;
  141.                 }
  142.             }
  143.         }
  144.  
  145.         ball.update(player1, player2);
  146.     }
  147.  
  148.     public void render(Graphics2D g)
  149.     {
  150.         g.setColor(Color.BLACK);
  151.         g.fillRect(0, 0, width, height);
  152.         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  153.  
  154.         if (gameStatus == 0)
  155.         {
  156.             g.setColor(Color.WHITE);
  157.             g.setFont(new Font("Arial", 1, 50));
  158.  
  159.             g.drawString("PONG", width / 2 - 75, 50);
  160.  
  161.             if (!selectingDifficulty)
  162.             {
  163.                 g.setFont(new Font("Arial", 1, 30));
  164.  
  165.                 g.drawString("Press Space to Play", width / 2 - 150, height / 2 - 25);
  166.                 g.drawString("Press Shift to Play with Bot", width / 2 - 200, height / 2 + 25);
  167.                 g.drawString("<< Score Limit: " + scoreLimit + " >>", width / 2 - 150, height / 2 + 75);
  168.             }
  169.         }
  170.  
  171.         if (selectingDifficulty)
  172.         {
  173.             String string = botDifficulty == 0 ? "Easy" : (botDifficulty == 1 ? "Medium" : "Hard");
  174.  
  175.             g.setFont(new Font("Arial", 1, 30));
  176.  
  177.             g.drawString("<< Bot Difficulty: " + string + " >>", width / 2 - 180, height / 2 - 25);
  178.             g.drawString("Press Space to Play", width / 2 - 150, height / 2 + 25);
  179.         }
  180.  
  181.         if (gameStatus == 1)
  182.         {
  183.             g.setColor(Color.WHITE);
  184.             g.setFont(new Font("Arial", 1, 50));
  185.             g.drawString("PAUSED", width / 2 - 103, height / 2 - 25);
  186.         }
  187.  
  188.         if (gameStatus == 1 || gameStatus == 2)
  189.         {
  190.             g.setColor(Color.WHITE);
  191.  
  192.             g.setStroke(new BasicStroke(5f));
  193.  
  194.             g.drawLine(width / 2, 0, width / 2, height);
  195.  
  196.             g.setStroke(new BasicStroke(2f));
  197.  
  198.             g.drawOval(width / 2 - 150, height / 2 - 150, 300, 300);
  199.  
  200.             g.setFont(new Font("Arial", 1, 50));
  201.  
  202.             g.drawString(String.valueOf(player1.score), width / 2 - 90, 50);
  203.             g.drawString(String.valueOf(player2.score), width / 2 + 65, 50);
  204.  
  205.             player1.render(g);
  206.             player2.render(g);
  207.             ball.render(g);
  208.         }
  209.  
  210.         if (gameStatus == 3)
  211.         {
  212.             g.setColor(Color.WHITE);
  213.             g.setFont(new Font("Arial", 1, 50));
  214.  
  215.             g.drawString("PONG", width / 2 - 75, 50);
  216.  
  217.             if (bot && playerWon == 2)
  218.             {
  219.                 g.drawString("The Bot Wins!", width / 2 - 170, 200);
  220.             }
  221.             else
  222.             {
  223.                 g.drawString("Player " + playerWon + " Wins!", width / 2 - 165, 200);
  224.             }
  225.  
  226.             g.setFont(new Font("Arial", 1, 30));
  227.  
  228.             g.drawString("Press Space to Play Again", width / 2 - 185, height / 2 - 25);
  229.             g.drawString("Press ESC for Menu", width / 2 - 140, height / 2 + 25);
  230.         }
  231.     }
  232.  
  233.     @Override
  234.     public void actionPerformed(ActionEvent e)
  235.     {
  236.         if (gameStatus == 2)
  237.         {
  238.             update();
  239.         }
  240.  
  241.         renderer.repaint();
  242.     }
  243.  
  244.     public static void main(String[] args)
  245.     {
  246.         pong = new Pong();
  247.     }
  248.  
  249.     @Override
  250.     public void keyPressed(KeyEvent e)
  251.     {
  252.         int id = e.getKeyCode();
  253.  
  254.         if (id == KeyEvent.VK_W)
  255.         {
  256.             w = true;
  257.         }
  258.         else if (id == KeyEvent.VK_S)
  259.         {
  260.             s = true;
  261.         }
  262.         else if (id == KeyEvent.VK_UP)
  263.         {
  264.             up = true;
  265.         }
  266.         else if (id == KeyEvent.VK_DOWN)
  267.         {
  268.             down = true;
  269.         }
  270.         else if (id == KeyEvent.VK_RIGHT)
  271.         {
  272.             if (selectingDifficulty)
  273.             {
  274.                 if (botDifficulty < 2)
  275.                 {
  276.                     botDifficulty++;
  277.                 }
  278.                 else
  279.                 {
  280.                     botDifficulty = 0;
  281.                 }
  282.             }
  283.             else if (gameStatus == 0)
  284.             {
  285.                 scoreLimit++;
  286.             }
  287.         }
  288.         else if (id == KeyEvent.VK_LEFT)
  289.         {
  290.             if (selectingDifficulty)
  291.             {
  292.                 if (botDifficulty > 0)
  293.                 {
  294.                     botDifficulty--;
  295.                 }
  296.                 else
  297.                 {
  298.                     botDifficulty = 2;
  299.                 }
  300.             }
  301.             else if (gameStatus == 0 && scoreLimit > 1)
  302.             {
  303.                 scoreLimit--;
  304.             }
  305.         }
  306.         else if (id == KeyEvent.VK_ESCAPE && (gameStatus == 2 || gameStatus == 3))
  307.         {
  308.             gameStatus = 0;
  309.         }
  310.         else if (id == KeyEvent.VK_SHIFT && gameStatus == 0)
  311.         {
  312.             bot = true;
  313.             selectingDifficulty = true;
  314.         }
  315.         else if (id == KeyEvent.VK_SPACE)
  316.         {
  317.             if (gameStatus == 0 || gameStatus == 3)
  318.             {
  319.                 if (!selectingDifficulty)
  320.                 {
  321.                     bot = false;
  322.                 }
  323.                 else
  324.                 {
  325.                     selectingDifficulty = false;
  326.                 }
  327.  
  328.                 start();
  329.             }
  330.             else if (gameStatus == 1)
  331.             {
  332.                 gameStatus = 2;
  333.             }
  334.             else if (gameStatus == 2)
  335.             {
  336.                 gameStatus = 1;
  337.             }
  338.         }
  339.     }
  340.  
  341.     @Override
  342.     public void keyReleased(KeyEvent e)
  343.     {
  344.         int id = e.getKeyCode();
  345.  
  346.         if (id == KeyEvent.VK_W)
  347.         {
  348.             w = false;
  349.         }
  350.         else if (id == KeyEvent.VK_S)
  351.         {
  352.             s = false;
  353.         }
  354.         else if (id == KeyEvent.VK_UP)
  355.         {
  356.             up = false;
  357.         }
  358.         else if (id == KeyEvent.VK_DOWN)
  359.         {
  360.             down = false;
  361.         }
  362.     }
  363.  
  364.     @Override
  365.     public void keyTyped(KeyEvent e)
  366.     {
  367.  
  368.     }
  369. }
PADDLE.JAVA
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package pong;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5.  
  6. public class Paddle
  7. {
  8.  
  9.     public int paddleNumber;
  10.  
  11.     public int x, y, width = 50, height = 250;
  12.  
  13.     public int score;
  14.  
  15.     public Paddle(Pong pong, int paddleNumber)
  16.     {
  17.         this.paddleNumber = paddleNumber;
  18.  
  19.         if (paddleNumber == 1)
  20.         {
  21.             this.x = 0;
  22.         }
  23.  
  24.         if (paddleNumber == 2)
  25.         {
  26.             this.x = pong.width - width;
  27.         }
  28.  
  29.         this.y = pong.height / 2 - this.height / 2;
  30.     }
  31.  
  32.     public void render(Graphics g)
  33.     {
  34.         g.setColor(Color.WHITE);
  35.         g.fillRect(x, y, width, height);
  36.     }
  37.  
  38.     public void move(boolean up)
  39.     {
  40.         int speed = 15;
  41.  
  42.         if (up)
  43.         {
  44.             if (y - speed > 0)
  45.             {
  46.                 y -= speed;
  47.             }
  48.             else
  49.             {
  50.                 y = 0;
  51.             }
  52.         }
  53.         else
  54.         {
  55.             if (y + height + speed < Pong.pong.height)
  56.             {
  57.                 y += speed;
  58.             }
  59.             else
  60.             {
  61.                 y = Pong.pong.height - height;
  62.             }
  63.         }
  64.     }
  65.  
  66. }
STRUCTURE GAMEPLAY

Komentar

Postingan Populer