Advertisement
Ramdan51-062

Pong

Nov 30th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.62 KB | None | 0 0
  1.  
  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.     public static Pong pong;
  20.     public int width = 1024, height = 768;
  21.     public Paddle player1;
  22.     public Paddle player2;
  23.     public Ball ball;
  24.     public boolean bot = false, selectDifficulty;
  25.     public boolean w, s, up, down;
  26.     public int gameStatus = 0, scoreLimit = 7, gameWon;
  27.     public int botMove, botDifficulty, botCooldown;
  28.     public Renderer renderer;
  29.     public Random random;
  30.     public JFrame jframe;
  31.    
  32.     public Pong()
  33.     {
  34.         Timer timer = new Timer(20, this);
  35.         random = new Random();
  36.         jframe = new JFrame("MyPong");
  37.         renderer = new Renderer();
  38.        
  39.         jframe.setSize(width + 15, height + 35);
  40.         jframe.setVisible(true);
  41.         jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  42.         jframe.add(renderer);
  43.         jframe.addKeyListener(this);
  44.        
  45.         timer.start();
  46.     }
  47.    
  48.     public void start()
  49.     {
  50.         gameStatus = 2;
  51.         player1 = new Paddle(this, 1);
  52.         player2 = new Paddle(this, 2);
  53.         ball = new Ball(this);
  54.     }
  55.    
  56.     public void update()
  57.     {
  58.         //Win condition
  59.         if(player1.score >= scoreLimit)
  60.         {
  61.             gameWon = 1;
  62.             gameStatus = 3;
  63.         }
  64.        
  65.         if(player2.score >= scoreLimit)
  66.         {
  67.             gameWon = 2;
  68.             gameStatus = 3;
  69.         }
  70.         //Movement
  71.         if(w)
  72.         {
  73.             player1.move(true);
  74.         }
  75.         if(s)
  76.         {
  77.             player1.move(false);
  78.         }
  79.         if(!bot)
  80.         {
  81.             if(up)
  82.             {
  83.                 player2.move(true);
  84.             }
  85.             if(down)
  86.             {
  87.                 player2.move(false);
  88.             }
  89.         }
  90.         //Bot control
  91.         else
  92.         {
  93.             if (botCooldown > 0)
  94.             {
  95.                 botCooldown--;
  96.  
  97.                 if (botCooldown == 0)
  98.                 {
  99.                     botMove = 0;
  100.                 }
  101.             }
  102.  
  103.             if (botMove < 10)
  104.             {
  105.                 if (player2.y + player2.height / 2 < ball.y)
  106.                 {
  107.                     player2.move(false);
  108.                     botMove++;
  109.                 }
  110.  
  111.                 if (player2.y + player2.height / 2 > ball.y)
  112.                 {
  113.                     player2.move(true);
  114.                     botMove++;
  115.                 }
  116.  
  117.                 if (botDifficulty == 0)
  118.                 {
  119.                     botCooldown = 20;
  120.                 }
  121.                 if (botDifficulty == 1)
  122.                 {
  123.                     botCooldown = 15;
  124.                 }
  125.                 if (botDifficulty == 2)
  126.                 {
  127.                     botCooldown = 10;
  128.                 }
  129.             }
  130.         }
  131.         ball.update(player1, player2);
  132.     }
  133.    
  134.     public void render(Graphics2D g)
  135.     {
  136.         g.setColor(Color.WHITE);
  137.         g.fillRect(0, 0, width, height);
  138.         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  139.        
  140.         if(gameStatus == 0)
  141.         {
  142.             g.setColor(Color.BLACK);
  143.             g.setFont(new Font("Arial", 1, 50));
  144.             g.drawString("Pong", width/2 -  75, 50);
  145.            
  146.             if(!selectDifficulty)
  147.             {
  148.                 g.setFont(new Font("Arial", 1, 30));
  149.                
  150.                 g.drawString("Press Space to Play", width / 2 - 150, height / 2 - 25);
  151.         g.drawString("Press Shift to Play with Bot", width / 2 - 200, height / 2 + 25);
  152.         g.drawString("<< Score Limit: " + scoreLimit + " >>", width / 2 - 150, height / 2 + 75);
  153.             }
  154.         }
  155.         if(selectDifficulty)
  156.         {
  157.             String string = botDifficulty == 0? "Easy" : (botDifficulty == 1? "Medium" : "Hard");
  158.             g.setFont(new Font("Arial", 1, 30));
  159.             g.drawString("<< Bot Difficulty: " + string + " >>", width / 2 - 180, height / 2 - 25);
  160.             g.drawString("Press Space to Play", width / 2 - 150, height / 2 + 25);
  161.         }
  162.         if (gameStatus == 1)
  163.         {
  164.             g.setColor(Color.WHITE);
  165.             g.setFont(new Font("Arial", 1, 50));
  166.             g.drawString("PAUSED", width / 2 - 103, height / 2 - 25);
  167.         }
  168.  
  169.         if (gameStatus == 1 || gameStatus == 2)
  170.         {
  171.             g.setColor(Color.WHITE);
  172.  
  173.             g.setStroke(new BasicStroke(5f));
  174.  
  175.             g.drawLine(width / 2, 0, width / 2, height);
  176.  
  177.             g.setStroke(new BasicStroke(2f));
  178.  
  179.             g.drawOval(width / 2 - 150, height / 2 - 150, 300, 300);
  180.  
  181.             g.setFont(new Font("Arial", 1, 50));
  182.  
  183.             g.drawString(String.valueOf(player1.score), width / 2 - 90, 50);
  184.             g.drawString(String.valueOf(player2.score), width / 2 + 65, 50);
  185.  
  186.             player1.render(g);
  187.             player2.render(g);
  188.             ball.render(g);
  189.         }
  190.  
  191.         if (gameStatus == 3)
  192.         {
  193.             g.setColor(Color.WHITE);
  194.             g.setFont(new Font("Arial", 1, 50));
  195.  
  196.             g.drawString("PONG", width / 2 - 75, 50);
  197.  
  198.             if (bot && gameWon == 2)
  199.             {
  200.                 g.drawString("The Bot Wins!", width / 2 - 170, 200);
  201.             }
  202.             else
  203.             {
  204.                 g.drawString("Player " + gameWon + " Wins!", width / 2 - 165, 200);
  205.             }
  206.  
  207.             g.setFont(new Font("Arial", 1, 30));
  208.  
  209.             g.drawString("Press Space to Play Again", width / 2 - 185, height / 2 - 25);
  210.             g.drawString("Press ESC for Menu", width / 2 - 140, height / 2 + 25);
  211.         }
  212.     }
  213.        
  214.     @Override
  215.     public void actionPerformed(ActionEvent e)
  216.     {
  217.         if (gameStatus == 2)
  218.         {
  219.             update();
  220.         }
  221.  
  222.         renderer.repaint();
  223.     }
  224.  
  225.     public static void main(String[] args)
  226.     {
  227.         pong = new Pong();
  228.     }
  229.  
  230.     @Override
  231.     public void keyPressed(KeyEvent e)
  232.     {
  233.         int id = e.getKeyCode();
  234.  
  235.         if (id == KeyEvent.VK_W)
  236.         {
  237.             w = true;
  238.         }
  239.         else if (id == KeyEvent.VK_S)
  240.         {
  241.             s = true;
  242.         }
  243.         else if (id == KeyEvent.VK_UP)
  244.         {
  245.             up = true;
  246.         }
  247.         else if (id == KeyEvent.VK_DOWN)
  248.         {
  249.             down = true;
  250.         }
  251.         else if (id == KeyEvent.VK_RIGHT)
  252.         {
  253.             if (selectDifficulty)
  254.             {
  255.                 if (botDifficulty < 2)
  256.                 {
  257.                     botDifficulty++;
  258.                 }
  259.                 else
  260.                 {
  261.                     botDifficulty = 0;
  262.                 }
  263.             }
  264.             else if (gameStatus == 0)
  265.             {
  266.                 scoreLimit++;
  267.             }
  268.         }
  269.         else if (id == KeyEvent.VK_LEFT)
  270.         {
  271.             if (selectDifficulty)
  272.             {
  273.                 if (botDifficulty > 0)
  274.                 {
  275.                     botDifficulty--;
  276.                 }
  277.                 else
  278.                 {
  279.                     botDifficulty = 2;
  280.                 }
  281.             }
  282.             else if (gameStatus == 0 && scoreLimit > 1)
  283.             {
  284.                 scoreLimit--;
  285.             }
  286.         }
  287.         else if (id == KeyEvent.VK_ESCAPE && (gameStatus == 2 || gameStatus == 3))
  288.         {
  289.             gameStatus = 0;
  290.         }
  291.         else if (id == KeyEvent.VK_SHIFT && gameStatus == 0)
  292.         {
  293.             bot = true;
  294.             selectDifficulty = true;
  295.         }
  296.         else if (id == KeyEvent.VK_SPACE)
  297.         {
  298.             if (gameStatus == 0 || gameStatus == 3)
  299.             {
  300.                 if (!selectDifficulty)
  301.                 {
  302.                     bot = false;
  303.                 }
  304.                 else
  305.                 {
  306.                     selectDifficulty = false;
  307.                 }
  308.  
  309.                 start();
  310.             }
  311.             else if (gameStatus == 1)
  312.             {
  313.                 gameStatus = 2;
  314.             }
  315.             else if (gameStatus == 2)
  316.             {
  317.                 gameStatus = 1;
  318.             }
  319.         }
  320.     }
  321.  
  322.     @Override
  323.     public void keyReleased(KeyEvent e)
  324.     {
  325.         int id = e.getKeyCode();
  326.  
  327.         if (id == KeyEvent.VK_W)
  328.         {
  329.             w = false;
  330.         }
  331.         else if (id == KeyEvent.VK_S)
  332.         {
  333.             s = false;
  334.         }
  335.         else if (id == KeyEvent.VK_UP)
  336.         {
  337.             up = false;
  338.         }
  339.         else if (id == KeyEvent.VK_DOWN)
  340.         {
  341.             down = false;
  342.         }
  343.     }
  344.  
  345.     @Override
  346.     public void keyTyped(KeyEvent e)
  347.     {
  348.  
  349.     }
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement