Advertisement
inhuman_Arif

gamepanel

Aug 19th, 2021
1,451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.02 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.util.Random;
  5.  
  6. public class GamePanel extends JPanel implements ActionListener {
  7.  
  8.     static final int SCREEN_WIDTH = 1300;
  9.     static final int SCREEN_HEIGHT = 750;
  10.     static final int UNIT_SIZE = 40;
  11.     static final int GAME_UNITS = (SCREEN_WIDTH * SCREEN_HEIGHT) / UNIT_SIZE;
  12.     static final int DELAY = 150;
  13.     int x[] = new int[GAME_UNITS];
  14.     int y[] = new int[GAME_UNITS];
  15.     int bodyParts = 4;
  16.     int appleEaten;
  17.     int appleX;
  18.     int appleY;
  19.     char direction = 'R';
  20.     boolean running = false;
  21.     Timer timer;
  22.     Random random;
  23.  
  24.     GamePanel(){
  25.         random = new Random();
  26.         this.setPreferredSize(new Dimension(SCREEN_WIDTH,SCREEN_HEIGHT));
  27.         this.setBackground(new Color(26,27,37));
  28.         this.setFocusable(true);
  29.         this.addKeyListener(new MyKeyAdapter());
  30.         startGame();
  31.     }
  32.     public void startGame(){
  33.         newApple();
  34.         running = true;
  35.         timer = new Timer(DELAY,this);
  36.         timer.start();
  37.     }
  38.     public void paintComponent(Graphics g){
  39.         super.paintComponent(g);
  40.         draw(g);
  41.     }
  42.     public void draw(Graphics g) {
  43.         if (running) {
  44.             g.setColor(Color.red);
  45.             g.fillOval(appleX, appleY, UNIT_SIZE-8, UNIT_SIZE-8);
  46.             int intensity=255;
  47.             for (int i = 0; i < bodyParts; i++) {
  48.                 if (i == 0) {
  49.                     g.setColor(new Color(27,200,139,255));
  50.                     g.fillRect(x[i], y[i], UNIT_SIZE-1, UNIT_SIZE-1);
  51.                 } else {
  52.                     g.setColor(new Color(27, 153, 139,intensity));
  53.                     intensity -= 2;
  54.                     g.fillRect(x[i], y[i], UNIT_SIZE-1, UNIT_SIZE-1);
  55.                 }
  56.             }
  57.             g.setColor(new Color(120,153,212));
  58.             g.setFont(new Font("Fira Sans Regular",Font.PLAIN,35));
  59.             FontMetrics metrics = getFontMetrics(g.getFont());
  60.             g.drawString("Score: "+appleEaten,(SCREEN_WIDTH-metrics.stringWidth("Score: "+appleEaten))/2,g.getFont().getSize());
  61.         }
  62.         else{
  63.             gameOver(g);
  64.         }
  65.     }
  66.     public void newApple(){
  67.         appleX = random.nextInt((int)(SCREEN_WIDTH/UNIT_SIZE))*UNIT_SIZE;
  68.         appleY = random.nextInt((int)(SCREEN_HEIGHT/UNIT_SIZE))*UNIT_SIZE;
  69.     }
  70.     public void move(){
  71.         for(int i=bodyParts;i>0;i--){
  72.             x[i] = x[i-1];
  73.             y[i] = y[i-1];
  74.         }
  75.         switch (direction){
  76.             case 'U':
  77.                 y[0] = y[0]-UNIT_SIZE;
  78.                 break;
  79.             case 'D':
  80.                 y[0] = y[0]+UNIT_SIZE;
  81.                 break;
  82.             case 'L':
  83.                 x[0] = x[0]-UNIT_SIZE;
  84.                 break;
  85.             case 'R':
  86.                 x[0] = x[0]+UNIT_SIZE;
  87.                 break;
  88.         }
  89.     }
  90.     public void checkApple(){
  91.         if((x[0]==appleX)&&(y[0]==appleY)){
  92.             bodyParts++;
  93.             appleEaten++;
  94.             newApple();
  95.         }
  96.     }
  97.     public void checkCollisions(){
  98.         //check if head collides with the body
  99.         for(int i=bodyParts;i>0;i--)
  100.             if ((x[0] == x[i]) && (y[0] == y[i]))
  101.                 running = false;
  102.  
  103.         //check if head touches left borders
  104.         if(x[0]<0)
  105.             running = false;
  106.         //if head touches right border
  107.         if(x[0]>SCREEN_WIDTH)
  108.             running = false;
  109.         //if head touches top border
  110.         if(y[0]<0)
  111.             running = false;
  112.         //if head touches bottom border;
  113.         if(y[0]>SCREEN_HEIGHT)
  114.             running = false;
  115.         if(!running)
  116.             timer.stop();
  117.     }
  118.     public void gameOver(Graphics g){
  119.         //score
  120.         g.setColor(new Color(120,153,212));
  121.         g.setFont(new Font("Fira Sans Regular",Font.PLAIN,35));
  122.         FontMetrics metrics1 = getFontMetrics(g.getFont());
  123.         g.drawString("Score: "+appleEaten,(SCREEN_WIDTH-metrics1.stringWidth("Score: "+appleEaten))/2,g.getFont().getSize());
  124.         //game over text
  125.         g.setColor(new Color(255,0,0,200));
  126.         g.setFont(new Font("Monospace Regular",Font.PLAIN,60));
  127.         FontMetrics metrics = getFontMetrics(g.getFont());
  128.         g.drawString("Game Over",(SCREEN_WIDTH-metrics.stringWidth("Game Over"))/2,(int)((double)SCREEN_HEIGHT/2.5));
  129.         //restart button
  130.         g.setColor(new Color(248,241,255));
  131.         g.setFont(new Font("Monospace Regular",Font.ITALIC,50));
  132.         FontMetrics metrics2 = getFontMetrics(g.getFont());
  133.         g.drawString("Press SPACE to play again",(SCREEN_WIDTH-metrics2.stringWidth("Press SPACE to play again"))/2,(int) ((double)SCREEN_HEIGHT/1.8));
  134.     }
  135.     public void restart(){
  136.         startGame();
  137.         repaint();
  138.         bodyParts = 4;
  139.         direction = 'R';
  140.         appleEaten = 0;
  141.         for(int i=0;i<4;i++){
  142.             x[i]=0;
  143.             y[i]=0;
  144.         }
  145.     }
  146.     @Override
  147.     public void actionPerformed(ActionEvent e) {
  148.         if(running){
  149.             move();
  150.             checkApple();
  151.             checkCollisions();
  152.         }
  153.         repaint();
  154.     }
  155.     public class MyKeyAdapter extends KeyAdapter{
  156.         @Override
  157.         public void keyPressed(KeyEvent e){
  158.             switch (e.getKeyCode()){
  159.                 case KeyEvent.VK_LEFT:
  160.                     if(direction != 'R')
  161.                         direction = 'L';
  162.                     break;
  163.                 case KeyEvent.VK_RIGHT:
  164.                     if(direction != 'L')
  165.                         direction = 'R';
  166.                     break;
  167.                 case KeyEvent.VK_UP:
  168.                     if(direction != 'D')
  169.                         direction = 'U';
  170.                     break;
  171.                 case KeyEvent.VK_DOWN:
  172.                     if(direction != 'U')
  173.                         direction = 'D';
  174.                     break;
  175.                 case KeyEvent.VK_SPACE:
  176.                     if(running == false)
  177.                         restart();
  178.                     break;
  179.             }
  180.         }
  181.     }
  182. }
  183.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement