在线精品99_中国九九盗摄偷拍偷看_91免费版在线观看_91.app_91高清视频在线_99热最新网站

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

143次阅读
没有评论

共计 2704 个字符,预计需要花费 7 分钟才能阅读完成。

以下是一个简单的 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;
        }
    }
}

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

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-13发表,共计2704字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)
主站蜘蛛池模板: 久久无码人妻精品一区二区三区 | 日本a级黄 | 深夜福利视频网站 | 国内国语一级毛片在线视频 | 隐私视频黄www小 | 亚洲av永久无码精品漫画 | 福利视频一二三在线视频免费观看 | 刘涛一级aa免费毛片视频 | 一级毛片一级毛片a毛片欧美 | 日本丰满的人妻hd高清在线 | 一本一道波多野结衣av中文 | 另类图片亚洲色图 | 久久久精品久久日韩一区综合 | 亚洲精品精华液一区二区 | 天堂成人 | 青青青国产在线观看免费网站 | 欧美午夜精品久久久久久浪潮 | 污全彩肉肉无遮挡彩色 | 一区二区在线观看视频 | 能看毛片的网址 | 天堂网在线www资源在线 | 在线观看成人无码中文av天堂 | 久久精品无码一区二区三区免费 | 特级毛片在线大全免费播放 | 中文字幕色av一区二区三区 | 少妇富婆高级按摩出水高潮 | 九九精品在线视频 | 国产精品麻豆传媒 | 免费特黄一级欧美大片 | 毛片网站在线 | 国产欧美中文字幕 | 亚洲av永久中文无码精品 | 91亚洲精品第一综合不卡播放 | 久久精品女人天堂av | 免费a级毛片18禁网站免费 | 国产欧美一区二区三区久久 | 忘穿内裤坐公交被挺进小说 | 高清中文字幕视频在线播 | 国产精品久久久久久久久 | 国产精品免费_区二区三区观看 | 免费激情网站 |