以下是一個簡單的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; } }}
這是一個簡單的涂鴉跳躍游戲,玩家通過控制跳躍的小涂鴉在不斷生成的平臺上往上跳躍。玩家按下空格鍵時,小涂鴉會向上跳躍,玩家需要控制小涂鴉跳到平臺上,如果沒有跳到平臺上就會掉下來,游戲結束。平臺會不斷從上面生成,玩家需要不斷跳躍上去,直到無法繼續跳躍
本文由 貴州做網站公司 整理發布,部分圖文來源于互聯網,如有侵權,請聯系我們刪除,謝謝!
c語言中正確的字符常量是用一對單引號將一個字符括起表示合法的字符常量。例如‘a’。數值包括整型、浮點型。整型可用十進制,八進制,十六進制。八進制前面要加0,后面...
2022年天津專場考試原定于3月19日舉行,受疫情影響確定延期,但目前延期后的考試時間推遲。 符合報名條件的考生,須在規定時間登錄招考資訊網(www.zha...
:喜歡聽,樂意看。指很受歡迎?!巴卣官Y料”喜聞樂見:[ xǐ wén lè jiàn ]詳細解釋1. 【解釋】:喜歡聽,樂意看。指很受歡迎。2. 【示例】:這是...
(資料圖)有關2020年5月14是什么情人節 5月14日情人節有什么寓意方面的知識,估計很多人不是太了解,今天就給大家詳細的介紹一下關于2020年5月14是什么情人節 5月14日情人節有什么寓意的相關內容。2020年5月14是什么情人節2020年5月14是黃色與玫瑰情人節。5月14日情人節有什么寓意5月是玫瑰發芽的時節,氣候宜人適合出游,因此許多情侶都選在這天相攜到戶外踏青,所以5月14日也有“玫...
余額寶轉出到賬時間是多久?余額寶轉出分為兩種情況,分別是快速到賬和普通到賬,具體情況如下:【1】快速到賬:當日轉出,當日到賬,預計是在2小時內到賬的,最快可以實現實時到賬。但是這種轉出方式下單日單戶限額1萬元?!?】普通到賬:這種情況是沒有轉出額度限制的,T日轉出,T+1日24:00前到賬,T日一般就是指基金交易日。而基金交易日一般就是指非節假日的周一到周五,如果碰上了節假日那么資金的到賬時間就會...
補稅是因為工資太高了嗎?不是因為工資太高。補稅和退稅主要取決于預繳稅款和實際繳納稅款是否有差異。如果有差異,會多退少補,不僅取決于工資。是否補稅主要取決于一個人的年度綜合收入是否達到起征點。如果達到了,之前沒有扣除的稅款需要在年底前補稅。另一方面,如果年收入沒有達到起征點,但扣除稅款,則需要在年底退稅。而在退稅的情況下,實際上會申請各種特殊的附加扣除,比如租房,需要提前申請,這樣到年底,租房的金額...