quhluh

Jump

Dec 9th, 2023
736
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.KeyEvent;
  6. import java.awt.event.KeyListener;
  7.  
  8. public class JumpingGame extends JFrame {
  9.  
  10.     private static final int WIDTH = 400;
  11.     private static final int HEIGHT = 400;
  12.     private static final int GROUND_HEIGHT = 300;
  13.  
  14.     private int characterY = GROUND_HEIGHT;
  15.  
  16.     public JumpingGame() {
  17.         setTitle("Jumping Game");
  18.         setSize(WIDTH, HEIGHT);
  19.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  20.  
  21.         JumpingPanel jumpingPanel = new JumpingPanel();
  22.         add(jumpingPanel);
  23.  
  24.         addKeyListener(new JumpingKeyListener());
  25.  
  26.         Timer timer = new Timer(10, new ActionListener() {
  27.             @Override
  28.             public void actionPerformed(ActionEvent e) {
  29.                 jumpingPanel.update();
  30.             }
  31.         });
  32.         timer.start();
  33.     }
  34.  
  35.     private class JumpingPanel extends JPanel {
  36.  
  37.         @Override
  38.         protected void paintComponent(Graphics g) {
  39.             super.paintComponent(g);
  40.             g.setColor(Color.BLUE);
  41.             g.fillRect(50, characterY, 30, 30);
  42.         }
  43.  
  44.         public void update() {
  45.             if (characterY < GROUND_HEIGHT) {
  46.                 characterY += 2; // Simulating gravity
  47.                 repaint();
  48.             }
  49.         }
  50.     }
  51.  
  52.     private class JumpingKeyListener implements KeyListener {
  53.  
  54.         @Override
  55.         public void keyPressed(KeyEvent e) {
  56.             if (e.getKeyCode() == KeyEvent.VK_SPACE && characterY == GROUND_HEIGHT) {
  57.                 jump();
  58.             }
  59.         }
  60.  
  61.         @Override
  62.         public void keyTyped(KeyEvent e) {
  63.             // Not used
  64.         }
  65.  
  66.         @Override
  67.         public void keyReleased(KeyEvent e) {
  68.             // Not used
  69.         }
  70.  
  71.         private void jump() {
  72.             // Simulate jumping by moving the character upwards
  73.             characterY -= 50;
  74.         }
  75.     }
  76.  
  77.     public static void main(String[] args) {
  78.         SwingUtilities.invokeLater(new Runnable() {
  79.             @Override
  80.             public void run() {
  81.                 new JumpingGame().setVisible(true);
  82.             }
  83.         });
  84.     }
  85. }
  86. This code creates a simple jumping game where the character jumps when you press the space bar. The JumpingGame class extends JFrame and contains a JumpingPanel class that extends JPanel. The character is represented by a blue rectangle, and jumping is simulated by changing the character's vertical position. The JumpingKeyListener class listens for the space bar key press and triggers the jump.
  87.  
  88. Note: This is a basic example for educational purposes. In a real game, you would likely use a game engine or a more sophisticated graphics library for better performance and flexibility.
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
Advertisement
Add Comment
Please, Sign In to add comment