117.info
人生若只如初见

Java涂鸦跳跃游戏代码怎么写

以下是一个简单的Java涂鸦跳跃游戏代码示例:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DoodleJump extends JPanel implements ActionListener {
    private static final int WIDTH = 400;
    private static final int HEIGHT = 600;
    private static final int GROUND = 550;
    private static final int PLAYER_SIZE = 25;
    private static final int PLATFORM_WIDTH = 50;
    private static final int PLATFORM_HEIGHT = 10;
    private static final int PLATFORM_GAP = 200;
    private static final int PLATFORM_COUNT = 6;
    private static final int GRAVITY = 2;
    private static final int JUMP_HEIGHT = 30;

    private Timer timer;
    private int playerX;
    private int playerY;
    private int velocityY;
    private boolean isJumping;
    private Platform[] platforms;

    public DoodleJump() {
        setPreferredSize(new Dimension(WIDTH, HEIGHT));
        setBackground(Color.BLACK);
        setFocusable(true);
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                    if (!isJumping) {
                        isJumping = true;
                        velocityY = -JUMP_HEIGHT;
                    }
                }
            }
        });

        playerX = WIDTH / 2;
        playerY = GROUND;
        velocityY = 0;
        isJumping = false;

        platforms = new Platform[PLATFORM_COUNT];
        for (int i = 0; i < PLATFORM_COUNT; i++) {
            platforms[i] = new Platform((int) (Math.random() * (WIDTH - PLATFORM_WIDTH)), GROUND - i * PLATFORM_GAP);
        }

        timer = new Timer(10, this);
        timer.start();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.setColor(Color.WHITE);
        g.fillOval(playerX - PLAYER_SIZE / 2, playerY - PLAYER_SIZE / 2, PLAYER_SIZE, PLAYER_SIZE);

        for (Platform platform : platforms) {
            g.fillRect(platform.getX(), platform.getY(), PLATFORM_WIDTH, PLATFORM_HEIGHT);
        }
    }

    public void actionPerformed(ActionEvent e) {
        playerY += velocityY;

        if (isJumping) {
            velocityY += GRAVITY;
        }

        if (playerY > GROUND) {
            playerY = GROUND;
            velocityY = 0;
            isJumping = false;
        }

        for (Platform platform : platforms) {
            if (playerY + PLAYER_SIZE / 2 > platform.getY() && playerY + PLAYER_SIZE / 2 < platform.getY() + PLATFORM_HEIGHT
                    && playerX + PLAYER_SIZE / 2 > platform.getX() && playerX - PLAYER_SIZE / 2 < platform.getX() + PLATFORM_WIDTH) {
                playerY = platform.getY() - PLAYER_SIZE / 2;
                velocityY = 0;
                isJumping = false;
            }
        }

        for (Platform platform : platforms) {
            platform.setY(platform.getY() + GRAVITY);
            if (platform.getY() > HEIGHT) {
                platform.setY(platform.getY() - PLATFORM_GAP * PLATFORM_COUNT);
                platform.setX((int) (Math.random() * (WIDTH - PLATFORM_WIDTH)));
            }
        }

        repaint();
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Doodle Jump");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new DoodleJump());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private class Platform {
        private int x;
        private int y;

        public Platform(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public int getX() {
            return x;
        }

        public int getY() {
            return y;
        }

        public void setX(int x) {
            this.x = x;
        }

        public void setY(int y) {
            this.y = y;
        }
    }
}

这是一个简单的涂鸦跳跃游戏,玩家通过控制跳跃的小涂鸦在不断生成的平台上往上跳跃。玩家按下空格键时,小涂鸦会向上跳跃,玩家需要控制小涂鸦跳到平台上,如果没有跳到平台上就会掉下来,游戏结束。平台会不断从上面生成,玩家需要不断跳跃上去,直到无法继续跳跃

未经允许不得转载 » 本文链接:https://www.117.info/ask/fe859AzsLBQRUA1Y.html

推荐文章

  • java怎么读取文件并修改文件内容

    要读取和修改文件内容,可以使用Java中的File类和FileInputStream、FileOutputStream类。
    首先,使用File类来创建文件对象,指定要读取和修改的文件路径:<...

  • java常见内存泄露的情况有哪些

    Java常见的内存泄漏情况包括: 对象未被正确释放:当一个对象不再被使用时,如果没有正确释放它所占用的内存,那么该对象就会造成内存泄漏。 静态集合类导致的内...

  • java读取文件内存溢出的原因有哪些

    Java读取文件内存溢出的原因主要有以下几种: 文件过大:如果读取的文件过大,超过了JVM堆内存的限制,就会导致内存溢出。这通常发生在一次性将整个文件内容读入...

  • java怎么读取config中的配置文件

    在Java中,可以使用Properties类来读取配置文件。下面是一个简单的示例:
    import java.io.FileInputStream;
    import java.io.IOException;
    import...

  • Windows下操作POP3的技巧有哪些

    在Windows下操作POP3的技巧包括: 使用邮件客户端:Windows上有很多流行的邮件客户端,如Microsoft Outlook、Mozilla Thunderbird、Windows Mail等,这些客户端都...

  • win10桌面天气小插件如何安装

    要在Win10桌面上安装天气小插件,可以按照以下步骤进行操作: 打开微软商店,搜索并下载一个兼容的天气小插件应用程序,例如“Desktop Weather”或“Weather Vie...

  • idea中如何查看底层Java源码

    要查看Java源码,可以使用以下几种方法: 使用IDE:主流的Java集成开发环境(IDE)如Eclipse、IntelliJ IDEA等都提供了查看Java源码的功能。在IDE中,可以通过选...

  • c语言判断是否为回文串的方法是什么

    判断一个字符串是否为回文串的方法可以使用两个指针分别从字符串的开头和结尾向中间移动,每次比较指针位置上的字符是否相等,如果相等则继续移动指针,直到两个...