微信推送怎么做購物網(wǎng)站360搜索引擎網(wǎng)址
Java實現(xiàn)飛翔的鳥小游戲
1.準(zhǔn)備工作
創(chuàng)建一個新的Java項目命名為“飛翔的鳥”,并在src中創(chuàng)建一個包命名為“com.qiku.bird",在這個包內(nèi)分別創(chuàng)建4個類命名為**“Bird”、“BirdGame”、“Column”、“Ground”,并向需要的圖片**素材導(dǎo)入到包內(nèi)。
2.代碼內(nèi)容
Bird類
package com.qiku.bird;import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;/** 小鳥類* */
public class Bird {int x;// 坐標(biāo)int y;int width; // 寬高int height;BufferedImage image; // 圖片BufferedImage[] images; // 小鳥所有圖片public Bird() {// 初始化數(shù)組 保存八張圖片images = new BufferedImage[8];// 使用循環(huán)結(jié)構(gòu) 將小鳥所有圖片 存入數(shù)組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 / 2int 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; // 末速度 = 當(dāng)前速度 + 重力加速度 * 時間y += (int) h;}// 小鳥向上飛public void up() {// 給一個 負(fù)方向的初速度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);}}
BirdGame類
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 關(guān)鍵字 繼承JPanel 畫板類 ==> 于是BirdGame 就具備了畫板類的功能*/
public class BirdGame extends JPanel {// 定義游戲狀態(tài)public static final int START = 0; // 開始public static final int RUNNING = 1; // 運行public static final int GAME_OVER = 2; // 結(jié)束// 游戲當(dāng)前狀態(tài) 默認(rèn)0 開始狀態(tài)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游戲// 靜態(tài)代碼塊 一般用于加載靜態(tài)資源(視頻,音頻,圖片等)static {// 將本地的圖片bg.png讀取到程序中的bgtry {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 的構(gòu)造方法public BirdGame() {bird = new Bird();ground = new Ground();column1 = new Column();column2 = new Column();// 柱子2的x坐標(biāo) = 柱子1的坐標(biāo)基礎(chǔ)上+244保持水平間距column2.x = column1.x + column1.distance;}/** 用于在畫板上繪制內(nèi)容的方法* 想在畫板上顯示什么 在這個方法寫就行了* @param g 畫筆* */@Overridepublic void paint(Graphics g) {// g.fillRect(0,0,100,200); // 設(shè)置顏色落筆點 寬高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); // 畫柱子2g.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); // 結(jié)束圖片}// 畫分?jǐn)?shù)Font font = new Font("微軟雅黑", Font.BOLD, 25); // 創(chuàng)建字體g.setFont(font); // 給畫筆設(shè)置字體g.setColor(Color.BLACK); // 設(shè)置字體黑色顏色g.drawString("分?jǐn)?shù): " + score, 30, 50);g.setColor(Color.WHITE); // 設(shè)置字體白色顏色g.drawString("分?jǐn)?shù): " + score, 28, 48);}// 判斷小鳥與柱子是否相撞 游戲結(jié)束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() {@Overridepublic void keyPressed(KeyEvent e) {System.out.println(e.getKeyCode());if(e.getKeyCode() == 32){if (state == START) { // 如果是開始狀態(tài) 單擊轉(zhuǎn)換運行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;}}}});// 給當(dāng)前對象()添加鼠標(biāo)單擊事件this.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) { // 鼠標(biāo)單擊執(zhí)行代碼if (state == START) { // 如果是開始狀態(tài) 單擊轉(zhuǎn)換運行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;}}});// 死循環(huán) {}的代碼會一直反復(fù)執(zhí)行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;}// 設(shè)置增加分?jǐn)?shù)if (bird.x == column1.x + column1.width + 1 || bird.x == column2.x + column2.width + 1) {score +=5;}}repaint(); //重畫 即重新執(zhí)行paint 方法Thread.sleep(10); //每隔10毫秒,讓程序休眠一次}}static JFrame frame = new JFrame();// main方法 - 程序的入口(即:有main方法 程序才能運行)public static void main(String[] args) throws Exception {BirdGame game = new BirdGame(); // 創(chuàng)建畫板對象frame.setSize(432, 644);//設(shè)置寬高frame.setLocationRelativeTo(null); // 居中顯示frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 設(shè)置關(guān)閉窗口,同時使程序結(jié)束frame.setVisible(true); //設(shè)置可見性frame.add(game); // 將畫板放到畫框上frame.setTitle("飛翔的小鳥");// 設(shè)置標(biāo)題frame.setResizable(false);// 設(shè)置不允許玩家拖動界面// 調(diào)用actiongame.action();}}
Column類
package com.qiku.bird;import java.awt.image.BufferedImage;/** 柱子類* */
public class Column {int x;// 坐標(biāo)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 = 150;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);}}
}
Ground類
package com.qiku.bird;import java.awt.image.BufferedImage;/*
* 地面類
* */
public class Ground {int x ;// 地面坐標(biāo)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;}}}