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中,异常类本身不会对性能产生显著影响。异常处理机制是为了在程序运行时处理错误或异常情况,而不是为了优化性能。然而,在使用异常时,需要注意以下几点...

  • java中异常类如何避免

    在Java中,避免异常的最好方法是编写健壮的代码并遵循一些最佳实践。以下是一些建议: 预期和处理异常:使用try-catch语句来捕获和处理可能发生的异常。确保处理...

  • java中异常类有哪些常见类型

    Java中的异常类主要分为两大类:受检异常(Checked Exceptions)和非受检异常(Unchecked Exceptions)。以下是具体的异常类型:
    受检异常(Checked Except...

  • java中异常类怎么捕获

    在Java中,我们使用try-catch语句来捕获异常。当程序执行过程中遇到异常时,Java运行时系统会抛出一个异常对象。你可以使用try块来包含可能引发异常的代码,然后...

  • 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语言判断是否为回文串的方法是什么

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