BenitoDannes

uas-brickbreaker-gameplay

Jun 6th, 2017
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.13 KB | None | 0 0
  1. import javax.swing.JPanel;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.awt.event.KeyEvent;
  5. import java.awt.event.KeyListener;
  6. import java.awt.Graphics;
  7. import java.awt.Color;
  8. import javax.swing.Timer;
  9. import java.awt.Rectangle;
  10. import java.awt.Graphics2D;
  11. import java.awt.Font;
  12.  
  13. public class Gameplay extends JPanel implements KeyListener, ActionListener
  14. {
  15.     private boolean play = false;
  16.     private int score = 0;
  17.     private int totalBricks = 21;
  18.     private Timer timer;
  19.     private int delay = 8;
  20.     private int playerX = 310;
  21.     private int ballposX = 120;
  22.     private int ballposY = 350;
  23.     private int ballXdir = -1;
  24.     private int ballYdir = -2;
  25.    
  26.     private MapGenerator map;
  27.  
  28.     public Gameplay()
  29.     {
  30.         map = new MapGenerator (3, 7);
  31.         addKeyListener(this);
  32.         setFocusable(true);
  33.         setFocusTraversalKeysEnabled(false);
  34.        
  35.         timer = new Timer(delay, this);
  36.         timer.start();
  37.     }
  38.    
  39.     public void paint (Graphics g)
  40.     {
  41.         // Background
  42.         g.setColor(Color.black);
  43.         g.fillRect (1,1, 692, 592);
  44.                
  45.         // Drawing Map
  46.         map.draw ((Graphics2D)g);
  47.        
  48.         // Borders
  49.         g.setColor (Color.yellow);
  50.         g.fillRect (0,0, 3, 592);
  51.         g.fillRect (0,0, 692, 3);
  52.         g.fillRect (691,0, 3, 592);
  53.                
  54.         // scores
  55.         g.setColor (Color.WHITE);
  56.         g.setFont (new Font("sans-serif", Font.BOLD, 25));
  57.         g.drawString (""+score, 520, 30);
  58.        
  59.         // Paddle
  60.         g.setColor (Color.green);
  61.         g.fillRect (playerX, 525, 80, 40); //550, 100, 8
  62.        
  63.         // Ball
  64.         g.setColor (Color.yellow);
  65.         g.fillOval (ballposX, ballposY, 20, 20);
  66.        
  67.         if (totalBricks <= 0)
  68.         {
  69.             play = false;
  70.             ballXdir = 0;
  71.             ballYdir = 0;
  72.             g.setColor (Color.yellow);
  73.             g.setFont (new Font("serif", Font.BOLD, 25));
  74.             g.drawString ("You won! ", 190, 300);
  75.             g.drawString ("Press Enter to Restart", 230, 350);
  76.         }
  77.        
  78.         // game Over
  79.         if (ballposY > 570)
  80.         {
  81.             play = false;
  82.             ballXdir = 0;
  83.             ballYdir = 0;
  84.             g.setColor (Color.RED);
  85.             g.setFont (new Font("serif", Font.BOLD, 25));
  86.             g.drawString ("Game Over, Scores: ", 190, 300);
  87.             g.drawString ("Press Enter to Restart", 230, 350);
  88.         }
  89.        
  90.         g.dispose();
  91.     }
  92.    
  93.     @Override
  94.     public void actionPerformed (ActionEvent e)
  95.     {
  96.         timer.start();
  97.        
  98.         if (play)
  99.         {
  100.             if (new Rectangle (ballposX, ballposY, 20, 20).intersects (new Rectangle(playerX, 525, 80, 40)))
  101.             {
  102.                 ballYdir = -ballYdir;
  103.             }
  104.            
  105.             for (int i = 0; i < map.map.length; i++)
  106.             {
  107.                 for (int j = 0; j < map.map[0].length; j++)
  108.                 {
  109.                     if (map.map[i][j] > 0)
  110.                     {
  111.                         int brickX = j* map.brickWidth + 80;
  112.                         int brickY = i * map.brickHeight + 50;
  113.                         int brickWidth = map.brickWidth;
  114.                         int brickHeight = map.brickHeight;
  115.                        
  116.                         Rectangle rect = new Rectangle(brickX, brickY, brickWidth, brickHeight);
  117.                         Rectangle ballRect = new Rectangle (ballposX, ballposY, 20, 20);
  118.                         Rectangle brickRect = rect;
  119.                        
  120.                         if (ballRect.intersects (brickRect))
  121.                         {
  122.                             map.setBrickValue (0, i, j);
  123.                             totalBricks--;
  124.                             score += 5;
  125.                            
  126.                             if (ballposX + 19 <= brickRect.x || ballposX + 1 >= brickRect.x + brickRect.width)
  127.                             {
  128.                                 ballXdir = -ballXdir;
  129.                             }
  130.                             else
  131.                             {
  132.                                 ballYdir = -ballYdir;
  133.                             }
  134.                         }
  135.                     }
  136.                 }
  137.             }
  138.             ballposX += ballXdir;
  139.             ballposY += ballYdir;
  140.            
  141.             if (ballposX < 0)
  142.             {
  143.                 ballXdir = -ballXdir;
  144.             }
  145.            
  146.             if (ballposY < 0)
  147.             {
  148.                 ballYdir = -ballYdir;
  149.             }
  150.            
  151.             if (ballposX > 670)
  152.             {
  153.                 ballXdir = -ballXdir;
  154.             }
  155.         }
  156.        
  157.         repaint();
  158.     }
  159.    
  160.     @Override
  161.     public void keyTyped (KeyEvent e)
  162.     {
  163.     }
  164.    
  165.     @Override
  166.     public void keyPressed (KeyEvent e)
  167.     {
  168.         if (e.getKeyCode() == KeyEvent.VK_RIGHT)
  169.         {
  170.             if (playerX >= 600)
  171.             {
  172.                 playerX = 600;
  173.             }
  174.             else
  175.             {
  176.                 moveRight();
  177.             }
  178.         }
  179.        
  180.         if (e.getKeyCode() == KeyEvent.VK_LEFT)
  181.         {
  182.             if (playerX < 10)
  183.             {
  184.                 playerX = 10;
  185.             }
  186.             else
  187.             {
  188.                 moveLeft();
  189.             }
  190.         }
  191.        
  192.         if (e.getKeyCode() == KeyEvent.VK_ENTER)
  193.         {
  194.             if (!play)
  195.             {
  196.                 play = true;
  197.                 ballposX = 120;
  198.                 ballposY = 350;
  199.                 ballXdir = -1;
  200.                 ballYdir = -2;
  201.                 playerX = 310;
  202.                 score = 0;
  203.                 totalBricks = 21;
  204.                 map = new MapGenerator(3, 7);
  205.                
  206.                 repaint();
  207.             }
  208.         }
  209.     }
  210.    
  211.     @Override
  212.     public void keyReleased (KeyEvent e)
  213.     {
  214.     }
  215.    
  216.     public void moveRight()
  217.     {
  218.         play = true;
  219.         playerX += 20;
  220.     }
  221.    
  222.     public void moveLeft()
  223.     {
  224.         play = true;
  225.         playerX -= 20;
  226.     }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment