中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

Java涂鴉跳躍游戲代碼怎么寫

小億
107
2023-11-01 14:12:39
欄目: 編程語言

以下是一個簡單的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;
        }
    }
}

這是一個簡單的涂鴉跳躍游戲,玩家通過控制跳躍的小涂鴉在不斷生成的平臺上往上跳躍。玩家按下空格鍵時,小涂鴉會向上跳躍,玩家需要控制小涂鴉跳到平臺上,如果沒有跳到平臺上就會掉下來,游戲結束。平臺會不斷從上面生成,玩家需要不斷跳躍上去,直到無法繼續跳躍

0
客服| 新郑市| 常德市| 富宁县| 依兰县| 鄂伦春自治旗| 冷水江市| 罗定市| 射洪县| 常德市| 潍坊市| 鹿邑县| 永清县| 墨江| 祁阳县| 阳泉市| 咸丰县| 北宁市| 沐川县| 镇江市| 沾益县| 阿巴嘎旗| 桃园县| 盖州市| 黄龙县| 香河县| 克东县| 巩义市| 宜阳县| 北碚区| 仪陇县| 施甸县| 德保县| 象州县| 孟州市| 巨野县| 乐昌市| 芜湖县| 武乡县| 定兴县| 萨嘎县|