飛翔的鳥游戲

一.準備工作
首先創建一個新的Java項目命名為“飛翔的鳥”,并在src中創建一個包命名為“com.qiku.bird",在這個包內分別創建4個類命名為“Bird”、“BirdGame”、“Column”、“Ground”,并向需要的圖片素材導入到包內。

二.代碼呈現
package com.qiku.bird;
?
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
?
/*
?* 小鳥類
?* */
public class Bird {
? ? int x;// 坐標
? ? int y;
? ? int width; // 寬高
? ? int height;
? ? BufferedImage image; // 圖片
? ? BufferedImage[] images; // 小鳥所有圖片
?
? ? public Bird() {
? ? ? ? // 初始化數組 保存八張圖片
? ? ? ? images = new BufferedImage[8];
? ? ? ? // 使用循環結構 將小鳥所有圖片 存入數組
? ? ? ? for (int i = 0; i < images.length; i++) {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? images[i] = ImageIO.read(Bird.class.getResourceAsStream(i + ".png"));
? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? image = BirdGame.bird_image;
? ? ? ? width = image.getWidth();
? ? ? ? height = image.getHeight();
? ? ? ? x = 120;
? ? ? ? y = 240;
? ? }
?
? ? // 小鳥飛翔的方法
? ? int index = 0;
?
? ? public void fly() {
? ? ? ? image = images[index % images.length];
? ? ? ? index++;
? ? }
?
? ? // h = v * t + g * t * t / 2
? ? int g = 6; //重力加速度
? ? double t = 0.15; // 下落時間
? ? double v = 0; // 初速度
? ? double h = 0; // 下落距離
?
? ? //小鳥下落一次
? ? public void down() {
? ? ? ? h = v * t + g * t * t / 2; // 具體下落的距離
? ? ? ? v = v + g * t; // 末速度 = 當前速度 + 重力加速度 * 時間
? ? ? ? y += (int) h;
? ? }
?
? ? // 小鳥向上飛
? ? public void up() {
? ? ? ? // 給一個 負方向的初速度
? ? ? ? v = -30;
? ? }
? ? /*
? ? ?* 小鳥撞地面
? ? ?* */
? ? public boolean hitGround(Ground ground) {
? ? ? ? boolean isHit = this.y + this.height >= ground.y;
? ? ? ? return isHit;
? ? }
?
? ? // 小鳥撞天花板
? ? public boolean hitCeiling() {
? ? ? ? boolean isHit = this.y <= 0;
? ? ? ? return isHit;
? ? }
?
? ? // 小鳥撞柱子
? ? public boolean hitColumn(Column c) {
? ? ? ? boolean b1 = this.x + this.width >= c.x;
? ? ? ? boolean b2 = this.x <= c.x + c.width;
? ? ? ? boolean b3 = this.y <= c.y + c.height / 2 - c.gap / 2;
? ? ? ? boolean b4 = this.y + this.height >= c.y + c.height / 2 + c.gap / 2;
? ? ? ? // 滿足b1 b2表示水平方向 相撞 b1 b2 b3 同時滿足 撞上柱子 b1 b2 b4 同時滿足撞下柱子
? ? ? ? return b1 && b2 && (b3 || b4);
?
? ? }
?
}

package com.qiku.bird;
import javax.imageio.ImageIO;
import javax.swing.*;
?
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
?
/**
?* 游戲啟動類
?* 使用extends 關鍵字 繼承JPanel 畫板類 ==> 于是BirdGame 就具備了畫板類的功能
?*/
public class BirdGame extends JPanel {
? ? // ? ?定義游戲狀態
? ? public static final int START = 0; ?// 開始
? ? public static final int RUNNING = 1; ?// 運行
? ? public static final int GAME_OVER = 2; ?// 結束
? ? // 游戲當前狀態 默認0 開始狀態
? ? int state = START;
? ? int score = 0; //玩家得分
?
? ? static BufferedImage bg = null; // 背景圖片
? ? static BufferedImage start = null; //開始圖片
? ? static BufferedImage ground_image = null; // 地面
? ? static BufferedImage bird_image = null; // 小鳥
? ? static BufferedImage column_image = null; // 柱子
? ? static BufferedImage gameOver_image = null; // game游戲
?
? ? // 靜態代碼塊 一般用于加載靜態資源(視頻,音頻,圖片等)
? ? static {
? ? ? ? // 將本地的圖片bg.png讀取到程序中的bg
? ? ? ? try {
? ? ? ? ? ? bg = ImageIO.read(BirdGame.class.getResourceAsStream("bg.png"));
? ? ? ? ? ? start = ImageIO.read(BirdGame.class.getResourceAsStream("start.png"));
? ? ? ? ? ? ground_image = ImageIO.read(BirdGame.class.getResourceAsStream("ground.png"));
? ? ? ? ? ? column_image = ImageIO.read(BirdGame.class.getResourceAsStream("column.png"));
? ? ? ? ? ? bird_image = ImageIO.read(BirdGame.class.getResourceAsStream("0.png"));
? ? ? ? ? ? gameOver_image = ImageIO.read(BirdGame.class.getResourceAsStream("gameover.png"));
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
?
? ? Ground ground;//聲明地面
? ? Bird bird;
? ? Column column1;
? ? Column column2;
?
? ? // BirdGame 的構造方法
? ? public BirdGame() {
? ? ? ? bird = new Bird();
? ? ? ? ground = new Ground();
? ? ? ? column1 = new Column();
? ? ? ? column2 = new Column();
? ? ? ? // 柱子2的x坐標 = 柱子1的坐標基礎上+244保持水平間距
? ? ? ? column2.x = column1.x + column1.distance;
?
? ? }
?
? ? /*
? ? ?* 用于在畫板上繪制內容的方法
? ? ?* 想在畫板上顯示什么 在這個方法寫就行了
? ? ?* @param g 畫筆
? ? ?* ?*/
? ? @Override
?
? ? public void paint(Graphics g) {
? ? ? ? // g.fillRect(0,0,100,200); // 設置顏色落筆點 寬高
? ? ? ? g.drawImage(bg, 0, 0, null); // 畫背景
? ? ? ? if (state == START) {
? ? ? ? ? ? g.drawImage(start, 0, 0, null); ?// 開始圖片
? ? ? ? }
? ? ? ? g.drawImage(column1.image, column1.x, column1.y, null); // 畫柱子
? ? ? ? g.drawImage(column2.image, column2.x, column2.y, null); // 畫柱子2
? ? ? ? g.drawImage(bird.image, bird.x, bird.y, null); //小鳥圖片
? ? ? ? g.drawImage(ground.image, ground.x, ground.y, null); ?// 地面圖片
? ? ? ? if (state == GAME_OVER) {
? ? ? ? ? ? g.drawImage(gameOver_image, 0, 0, null); // 結束圖片
?
? ? ? ? }
? ? ? ? // 畫分數
? ? ? ? Font font = new Font("微軟雅黑", Font.BOLD, 25); // 創建字體
? ? ? ? g.setFont(font); ?// 給畫筆設置字體
? ? ? ? g.setColor(Color.BLACK); ?// 設置字體黑色顏色
? ? ? ? g.drawString("分數: ?" + score, 30, 50);
? ? ? ? g.setColor(Color.WHITE); ?// 設置字體白色顏色
? ? ? ? g.drawString("分數: ?" + score, 28, 48);
? ? }
?
? ? // 判斷小鳥與柱子是否相撞 游戲結束
? ? public boolean isGameOver() {
? ? ? ? boolean isHit = bird.hitGround(ground) || bird.hitCeiling() || bird.hitColumn(column1) || bird.hitColumn(column2);
? ? ? ? return isHit;
? ? }
?
?
? ? // 游戲流程控制的方法
? ? public void action() throws Exception {
? ? ? ? frame.addKeyListener(new KeyAdapter() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void keyPressed(KeyEvent e) {
? ? ? ? ? ? ? ? System.out.println(e.getKeyCode());
? ? ? ? ? ? ? ? if(e.getKeyCode() == 32){
? ? ? ? ? ? ? ? ? ? if (state == START) { ?// 如果是開始狀態 單擊轉換運行
? ? ? ? ? ? ? ? ? ? ? ? state = RUNNING;
? ? ? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? ? ? ? ? if (state == RUNNING) {
? ? ? ? ? ? ? ? ? ? ? ? bird.up(); //小鳥上升
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? if (state == GAME_OVER) {
? ? ? ? ? ? ? ? ? ? ? ? bird = new Bird();
? ? ? ? ? ? ? ? ? ? ? ? column1 = new Column();
? ? ? ? ? ? ? ? ? ? ? ? column2 = new Column();
? ? ? ? ? ? ? ? ? ? ? ? column2.x = column1.x + column1.distance;
? ? ? ? ? ? ? ? ? ? ? ? score = 0;
? ? ? ? ? ? ? ? ? ? ? ? state = START;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
?
?
? ? ? ? // 給當前對象()添加鼠標單擊事件
? ? ? ? this.addMouseListener(new MouseAdapter() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void mouseClicked(MouseEvent e) { // 鼠標單擊執行代碼
? ? ? ? ? ? ? ? if (state == START) { ?// 如果是開始狀態 單擊轉換運行
? ? ? ? ? ? ? ? ? ? state = RUNNING;
? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? ? ? if (state == RUNNING) {
? ? ? ? ? ? ? ? ? ? bird.up(); //小鳥上升
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if (state == GAME_OVER) {
? ? ? ? ? ? ? ? ? ? bird = new Bird();
? ? ? ? ? ? ? ? ? ? column1 = new Column();
? ? ? ? ? ? ? ? ? ? column2 = new Column();
? ? ? ? ? ? ? ? ? ? column2.x = column1.x + column1.distance;
? ? ? ? ? ? ? ? ? ? score = 0;
? ? ? ? ? ? ? ? ? ? state = START;
? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? }
? ? ? ? });
?
? ? ? ? // 死循環 {}的代碼會一直反復執行
? ? ? ? while (true) {
? ? ? ? ? ? if (state == START) {
? ? ? ? ? ? ? ? ground.step(); // 地面移動
? ? ? ? ? ? ? ? bird.fly(); // 小鳥飛翔
? ? ? ? ? ? } else if (state == RUNNING) {
? ? ? ? ? ? ? ? ground.step(); // 地面移動
? ? ? ? ? ? ? ? column1.step(); // 柱子1移動
? ? ? ? ? ? ? ? column2.step(); // 柱子2移動
? ? ? ? ? ? ? ? bird.fly(); // 小鳥飛翔
? ? ? ? ? ? ? ? bird.down(); // 小鳥下落
? ? ? ? ? ? ? ? if (isGameOver() == true) {
? ? ? ? ? ? ? ? ? ? state = GAME_OVER;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? // 設置增加分數
? ? ? ? ? ? ? ? if (bird.x == column1.x + column1.width + 1 || bird.x == column2.x + column2.width + 1) {
? ? ? ? ? ? ? ? ? ? score +=5;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
?
? ? ? ? ? ? repaint(); //重畫 即重新執行paint 方法
? ? ? ? ? ? Thread.sleep(10); //每隔10毫秒,讓程序休眠一次
? ? ? ? }
? ? }
? ? static ?JFrame frame = new JFrame();
? ? // main方法 - 程序的入口(即:有main方法 程序才能運行)
? ? public static void main(String[] args) throws Exception {
? ? ? ? BirdGame game = new BirdGame(); // 創建畫板對象
? ? ? ? frame.setSize(432, 644);//設置寬高
? ? ? ? frame.setLocationRelativeTo(null); // 居中顯示
? ? ? ? frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 設置關閉窗口,同時使程序結束
? ? ? ? frame.setVisible(true); //設置可見性
? ? ? ? frame.add(game); // 將畫板放到畫框上
? ? ? ? frame.setTitle("飛翔的小鳥");// 設置標題
? ? ? ? frame.setResizable(false);// 設置不允許玩家拖動界面
?
? ? ? ? // 調用action
? ? ? ? game.action();
? ? }
?
}

package com.qiku.bird;
?
import java.awt.image.BufferedImage;
?
/*
?* 柱子類
?* */
public class Column {
? ? int x;// 坐標
? ? int y;
? ? int width; // 寬高
? ? int height;
? ? BufferedImage image; // 圖片
? ? int gap; //上下柱子之間的間隙
? ? int distance; //水平方向柱子之間的距離
? ? int min = -(1200 / 2 - 144 / 2);
? ? int max = 644 - 146 - 144 / 2 - 1200 / 2;
?
? ? public Column() {
? ? ? ? gap = 144;
? ? ? ? distance = 244;
? ? ? ? image = BirdGame.column_image;
? ? ? ? width = image.getWidth();
? ? ? ? height = image.getHeight();
? ? ? ? x = BirdGame.bg.getWidth();
? ? ? ? y = (int) (Math.random() * (max - min) + min);
?
? ? }
?
? ? public void step() {
? ? ? ? x--;
? ? ? ? if (x <= -width) {
? ? ? ? ? ? x = BirdGame.bg.getWidth();
? ? ? ? ? ? y = (int) (Math.random() * (max - min) + min);
? ? ? ? }
? ? }
}
?

package com.qiku.bird;
?
import java.awt.image.BufferedImage;
?
/*
* 地面類
* */
public class Ground {
? ? int x ;// 地面坐標
? ? int y ;
? ? int width ; // 地面的寬高
? ? int height;
? ? BufferedImage image; // 地面圖片
?
? ? public Ground(){
? ? ? ? image = BirdGame.ground_image;
? ? ? ? x = 0;
? ? ? ? y = BirdGame.bg.getHeight() - image.getHeight();
?
? ? ? ? width = image.getWidth();
? ? ? ? height = image.getHeight();
? ? }
? ? /*
? ? * 地面走一步的方法
? ? * */
? ? public void step(){
? ? ? ? x--;
? ? ? ? if(x <= 432 - width){
? ? ? ? ? ? x=0;
? ? ? ? }
? ? }
}

三.結果呈現

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/166214.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/166214.shtml
英文地址,請注明出處:http://en.pswp.cn/news/166214.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【醫學圖像處理】超詳細!PET圖像批量預處理

目錄 一、單個PET圖像預處理1、使用[MRIConvert](https://pan.baidu.com/s/1cn3kgeVRir8HvP6HHm0M0Q?pwd5rt5)處理DCM2、MRI和PET數據預處理過程1&#xff09; 打開matlab命令行輸入spm pet&#xff0c;打開SMP12&#xff0c;界面如下2&#xff09; Realign&#xff0c;只需要…

【Vue】插值表達式

作用&#xff1a;利用表達式進行插值渲染 語法&#xff1a;{ { 表達式 } } 目錄 案例一&#xff1a; 案例二&#xff1a; 案例三&#xff1a; ?編輯 注意&#xff1a; 案例一&#xff1a; <!DOCTYPE html> <html lang"en"> <head><me…

項目中如何配置數據可視化展現

在現今數據驅動的時代&#xff0c;可視化已逐漸成為數據分析的主要途徑&#xff0c;可視化大屏的廣泛使用便應運而生。很多公司及政務機構&#xff0c;常利用大屏的手段展現其實力或演示業務&#xff0c;可視化的效果能讓觀者更快速的理解結果并直觀的看到數據展現。因此&#…

加速軟件開發:自動化測試在持續集成中的重要作用!

持續集成的自動化測試 如今互聯網軟件的開發、測試和發布&#xff0c;已經形成了一套非常標準的流程&#xff0c;最重要的組成部分就是持續集成&#xff08;Continuous integration&#xff0c;簡稱CI&#xff0c;目前主要的持續集成系統是Jenkins&#xff09;。 那么什么是持…

教育+AIGC開局之年:教育派作業幫、科技派科大訊飛同路不同道

配圖來自Canva可畫 與往年相比&#xff0c;今年的雙11顯得格外冷清&#xff0c;GMV&#xff08;商品交易總額&#xff09;數據和增長數據無人提及&#xff0c;京東、淘寶天貓、抖音、快手等平臺的火藥味都淡了。一片祥和有序的雙11氛圍中&#xff0c;昔日的K12教育企業與科技企…

sqlserver寫入中文亂碼問題

sqlserver寫入中文亂碼問題解決方案 首先查看sqlserver數據庫編碼 首先查看sqlserver數據庫編碼 查詢語句&#xff1a;SELECT COLLATIONPROPERTY(Chinese_PRC_Stroke_CI_AI_KS_WS, CodePage)&#xff1b; 對應的編碼&#xff1a; 936 簡體中文GBK 950 繁體中文BIG5 437 美國/加…

算法的10大排序

10大排序算法--python 一顆星--選擇排序一顆星--冒泡排序一顆星--插入排序兩顆星--歸并排序&#xff08;遞歸-難&#xff09;三顆星--桶排序三顆星--計數排序四顆星--基數排序四顆星--快速排序&#xff0c;尋找標志位&#xff08;遞歸-難&#xff09;四顆星--又是比較難的希爾排…

Python工具箱系列(四十六)

PDF&#xff08;Portable Document Format&#xff09;是一種便攜文檔格式&#xff0c;它基于PostScripty這種腳本語言。 ?? PDF文檔操作 PDF&#xff08;Portable Document Format&#xff09;是一種便攜文檔格式&#xff0c;它基于PostScripty這種腳本語言&#xff0c;它…

清華大學提出全新加速訓練大模型方法SoT

近日&#xff0c;微軟研究和清華大學的研究人員共同提出了一種名為“Skeleton-of-Thought&#xff08;SoT&#xff09;”的全新人工智能方法&#xff0c;旨在解決大型語言模型(LLMs)生成速度較慢的問題。 盡管像GPT-4和LLaMA等LLMs在技術領域產生了深遠影響&#xff0c;但其處…

提供電商數據|帶你簡單認識天貓API接口相關參數文檔調用說明

什么是API接口 API接口(Application Programming Interface Interface)是應用程序與開發人員或其他程序互相通信的方式。它允許開發者訪問應用程序的數據和功能。 API接口,軟件的“握手”與“交流”之道,軟件世界的“好基友”。想讓軟件聊得來?想開發App卻無從下手?API來相救…

【騰訊云云上實驗室-向量數據庫】Tencent Cloud VectorDB為非結構化數據查詢插上飛翔的翅膀——以企業知識庫為例

前言 以前我曾疑惑&#xff0c;對于非結構化的內容&#xff0c;如一張圖片或一段視頻&#xff0c;如何實現搜索呢&#xff1f;圖片或視頻作為二進制文件&#xff0c;我們如何將其轉化為可搜索的數據并存儲起來&#xff0c;然后在搜索時將其還原呢&#xff1f; 后來我發現&…

5個高防CDN的特點

1. 支持泛解析自定義HTTPS/SSL隱藏源IP。 2. 支持緩存加速永久在線SEO優化。當網站原服務器宕機時&#xff0c;如果開啟了此功能&#xff0c;用戶仍然可以訪問網站&#xff08;用戶看到的是 緩存數據&#xff09;&#xff0c;從而達到了網站永不中斷服務的效果&#xff0c;可以…

Minio8版本沖突問題

今天在配置minio的時候遇到了一個報錯 Error starting ApplicationContext. To display the conditions report re-run your application with debug enabled. 2023-11-24 10:31:42.897 ERROR 14312 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *******************…

blk_mq_init_queue函數學習記錄

blk-mq編程&#xff0c;主要要調用兩個函數進行初始化工作&#xff0c;blk_mq_init_queue這是第二個。該函數先是申請了struct request_queue結構&#xff0c;這個請求隊列后面用于賦值給磁盤那個結構體的相應成員。 struct request_queue *blk_mq_init_queue(struct blk_mq_t…

python3到文件的讀取以及輸出

excel表格的讀取和輸入輸出 python中txt的讀取和輸入輸出 txt輸出報錯&#x1f447; UnicodeEncodeError: ascii codec cant encode characters in position 154-157: ordinal not in range(128)解決方法

Tomcat 配置

1&#xff1a; 打開 2&#xff1a;選擇版本號&#xff0c;我這邊是 1.7 3&#xff1a;添加 web 4: 添加jar包 5&#xff1a;添加 6&#xff1a;添加 Tomcat

【每日一題】1410. HTML實體解析器-2023.11.23

題目&#xff1a; 1410. HTML 實體解析器 「HTML 實體解析器」 是一種特殊的解析器&#xff0c;它將 HTML 代碼作為輸入&#xff0c;并用字符本身替換掉所有這些特殊的字符實體。 HTML 里這些特殊字符和它們對應的字符實體包括&#xff1a; 雙引號&#xff1a;字符實體為 &…

vim翻頁快捷鍵

Vim翻頁 整頁 Ctrlf向下翻頁&#xff0c;下一頁&#xff0c;相當于Page DownCtrlb向上翻頁&#xff0c;上一頁&#xff0c;相當于Page Up 半頁 Ctrld向下半頁&#xff0c;下一半頁&#xff0c;光標下移Ctrlu向上半頁&#xff0c;上衣半頁&#xff0c;光標上移 按行 Ctrle…

vue2【組件的構成】

目錄 1&#xff1a;什么是組件化開發 2&#xff1a;vue中的組件化開發 3&#xff1a;vue組件的三個組成部分 4&#xff1a;組件中定義方法&#xff0c;監聽器&#xff0c;過濾器&#xff0c;計算屬性節點。 5&#xff1a;template中只允許唯一根節點&#xff0c;style默認…

OpenMLDB SQL 開發調試神器 - OpenMLDB SQL Emulator

今天為大家介紹一款來自 OpenMLDB 社區的優秀獨立工具 - OpenMLDB SQL Simulator&#xff08;https://github.com/vagetablechicken/OpenMLDBSQLEmulator&#xff09; &#xff0c;可以讓你更加高效方便的開發、調試 OpenMLDB SQL。 為了高效的實現時序特征計算&#xff0c;Op…