quhluh

Flipping Bottle Code for JAVA

Dec 15th, 2023
1,253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. Copy code
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6.  
  7. public class BottleFlipGame extends JFrame {
  8.     private static final int WIDTH = 800;
  9.     private static final int HEIGHT = 600;
  10.     private static final int BOTTLE_WIDTH = 50;
  11.     private static final int BOTTLE_HEIGHT = 100;
  12.  
  13.     private Timer timer;
  14.     private int bottleX;
  15.     private int bottleY;
  16.     private int bottleSpeedY;
  17.  
  18.     public BottleFlipGame() {
  19.         setTitle("Bottle Flip Game");
  20.         setSize(WIDTH, HEIGHT);
  21.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.         setLocationRelativeTo(null);
  23.  
  24.         bottleX = WIDTH / 2 - BOTTLE_WIDTH / 2;
  25.         bottleY = HEIGHT - BOTTLE_HEIGHT - 30;
  26.         bottleSpeedY = 0;
  27.  
  28.         timer = new Timer(10, new ActionListener() {
  29.             @Override
  30.             public void actionPerformed(ActionEvent e) {
  31.                 update();
  32.                 repaint();
  33.             }
  34.         });
  35.  
  36.         addKeyListener(new java.awt.event.KeyAdapter() {
  37.             public void keyPressed(java.awt.event.KeyEvent evt) {
  38.                 formKeyPressed(evt);
  39.             }
  40.         });
  41.  
  42.         setFocusable(true);
  43.     }
  44.  
  45.     private void formKeyPressed(java.awt.event.KeyEvent evt) {
  46.         if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_SPACE && bottleSpeedY == 0) {
  47.             bottleSpeedY = -5; // Jump when space is pressed
  48.         }
  49.     }
  50.  
  51.     private void update() {
  52.         // Update bottle position
  53.         bottleY += bottleSpeedY;
  54.         bottleSpeedY += 1; // Gravity effect
  55.  
  56.         // Check if the bottle hits the ground
  57.         if (bottleY > HEIGHT - BOTTLE_HEIGHT - 30) {
  58.             bottleY = HEIGHT - BOTTLE_HEIGHT - 30;
  59.             bottleSpeedY = 0;
  60.         }
  61.     }
  62.  
  63.     @Override
  64.     public void paint(Graphics g) {
  65.         super.paint(g);
  66.         // Draw the bottle
  67.         g.setColor(Color.BLUE);
  68.         g.fillRect(bottleX, bottleY, BOTTLE_WIDTH, BOTTLE_HEIGHT);
  69.  
  70.         // Draw ground
  71.         g.setColor(Color.GREEN);
  72.         g.fillRect(0, HEIGHT - 30, WIDTH, 30);
  73.     }
  74.  
  75.     public static void main(String[] args) {
  76.         SwingUtilities.invokeLater(new Runnable() {
  77.             @Override
  78.             public void run() {
  79.                 BottleFlipGame bottleFlipGame = new BottleFlipGame();
  80.                 bottleFlipGame.setVisible(true);
  81.                 bottleFlipGame.timer.start();
  82.             }
  83.         });
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment