久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

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

共計(jì) 2704 個(gè)字符,預(yù)計(jì)需要花費(fèi) 7 分鐘才能閱讀完成。

以下是一個(gè)簡(jiǎn)單的 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;
        }
    }
}

這是一個(gè)簡(jiǎn)單的涂鴉跳躍游戲,玩家通過(guò)控制跳躍的小涂鴉在不斷生成的平臺(tái)上往上跳躍。玩家按下空格鍵時(shí),小涂鴉會(huì)向上跳躍,玩家需要控制小涂鴉跳到平臺(tái)上,如果沒(méi)有跳到平臺(tái)上就會(huì)掉下來(lái),游戲結(jié)束。平臺(tái)會(huì)不斷從上面生成,玩家需要不斷跳躍上去,直到無(wú)法繼續(xù)跳躍

丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!

正文完
 
丸趣
版權(quán)聲明:本站原創(chuàng)文章,由 丸趣 2023-12-13發(fā)表,共計(jì)2704字。
轉(zhuǎn)載說(shuō)明:除特殊說(shuō)明外本站除技術(shù)相關(guān)以外文章皆由網(wǎng)絡(luò)搜集發(fā)布,轉(zhuǎn)載請(qǐng)注明出處。
評(píng)論(沒(méi)有評(píng)論)
主站蜘蛛池模板: 青神县| 弥勒县| 靖西县| 沈阳市| 灵山县| 昌平区| 嘉义市| 鄂州市| 称多县| 泽库县| 河间市| 铜山县| 乌拉特前旗| 葫芦岛市| 湘乡市| 望都县| 满洲里市| 五大连池市| 南木林县| 太保市| 大余县| 开封市| 罗定市| 定襄县| 吐鲁番市| 竹溪县| 仁化县| 公安县| 全州县| 永仁县| 泊头市| 兰州市| 邹平县| 米易县| 秦安县| 灌阳县| 武清区| 西青区| 融水| 南川市| 那坡县|