Advertisement
mnaufaldillah

Pong Tugas 9

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