飞翔的小鸟小游戏

主类 

package APP;import 框架.GameFrame;public class GameApp {public static void main(String[] args) {//游戏的入口new GameFrame();}
}

场景实物

package 框架;import 图导.Constant;
import 图导.GameUtil;import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;/*** 障碍物类*/
public class Barrier {//矩形参数private Rectangle rect;private boolean mob = true;//障碍物移动速度private int speed = 3;//障碍物需要的三个图片private static BufferedImage[] img;//障碍物的状态private boolean visible;static {final int COUNT = 3;//类加载的时候将三个图片初始化img = new BufferedImage[COUNT];for (int i = 0; i < COUNT; i++) {img[i] = 图导.GameUtil.loadBufferedImage(Constant.BARRIER_IMG_PATH[i]);}}//位置private int x, y;//宽度和高度private int width, height;//障碍物的类型private int type;public static final int TYPE_TOP_NORMAL = 0;public static final int TYPE_BOTTOM_NORMAL = 2;public static final int TYPE_HOVER_NORMAL = 4;public static final int TYPE_MOBILE = 6;//获得障碍物的宽度和高度public static final int BARRIRE_WIDTH = img[0].getWidth();public static final int BARRIRE_HEIGHT = img[0].getHeight();public static final int BARRIRE_HEAD_WIDTH = img[1].getWidth();public static final int BARRIRE_HEAD_HEIGHT = img[1].getHeight();public Barrier() {rect = new Rectangle();}public Barrier(int x, int y, int height, int type) {this.x = x;this.y = y;this.height = height;this.type = type;this.width = BARRIRE_WIDTH;}//根据不同的类型绘制障碍物public void draw(Graphics g) {switch (type) {case TYPE_TOP_NORMAL:drawTopMormal(g);break;case TYPE_BOTTOM_NORMAL:drawNomalTop(g);break;case TYPE_HOVER_NORMAL:drawHoverNormal(g);break;case TYPE_MOBILE:drawMobile(g);break;}}//绘制从上向下的障碍物private void drawTopMormal(Graphics g) {//求出所需要的障碍物的块数int count = (height - BARRIRE_HEAD_HEIGHT) / BARRIRE_HEIGHT + 1;//for循坏绘制障碍物for (int i = 0; i < count; i++) {g.drawImage(img[0], x, y + i * BARRIRE_HEIGHT, null);}//绘制头int y = height - BARRIRE_HEAD_HEIGHT;g.drawImage(img[2], x - (BARRIRE_HEAD_WIDTH - BARRIRE_WIDTH) / 2, y, null);x -= speed;if (x < 50) {visible = false;}rect(g);}//绘制从下向上的障碍物private void drawNomalTop(Graphics g) {//求出所需要的障碍物的块数int count = height / BARRIRE_HEIGHT + 1;//for循环绘制障碍物for (int i = 0; i < count; i++) {g.drawImage(img[0], x, Constant.FRAM_HEIGNT - i * BARRIRE_HEIGHT, null);}//绘制头int y = Constant.FRAM_HEIGNT - height;g.drawImage(img[1], x - (BARRIRE_HEAD_WIDTH - BARRIRE_WIDTH) / 2, y, null);x -= speed;if (x < -50) {visible = false;}rect(g);}//绘制中间的障碍物private void drawHoverNormal(Graphics g) {//求出所需要的障碍物的块数int count = (height-BARRIRE_HEAD_HEIGHT)/BARRIRE_HEIGHT;//绘制上头g.drawImage(img[1],x,y,null);//for循环绘制障碍物for (int i = 0; i < count; i++) {g.drawImage(img[0], x, y+BARRIRE_HEAD_HEIGHT+i*BARRIRE_HEIGHT, null);}rect(g);//绘制下头int y11 = y+height-BARRIRE_HEAD_HEIGHT;g.drawImage(img[2],x,y11,null);x -= speed;if (x < -50) {visible = false;}}//绘制移动的障碍物private void drawMobile(Graphics g) {//求出所需要的障碍物的块数int count = (height-BARRIRE_HEAD_HEIGHT)/BARRIRE_HEIGHT;//绘制上头g.drawImage(img[1],x,y,null);//for循环绘制障碍物for (int i = 0; i < count; i++) {g.drawImage(img[0], x, y+BARRIRE_HEAD_HEIGHT+i*BARRIRE_HEIGHT, null);}rect(g);//绘制下头int y11 = y+height-BARRIRE_HEAD_HEIGHT;g.drawImage(img[2],x,y11,null);x -= speed;if (x < -50) {visible = false;}if (mob) {y+=5;if (y >= 250) {mob=false;}}else if (!mob){y-=5;if (y <= 100) {mob=true;}}}/*** 绘制障碍物碰撞矩形** @return*/public void rect(Graphics g) {int x1 = this.x;int y1 = this.y;int w1 = img[0].getWidth();
//        g.setColor(Color.blue);
//        g.drawRect(x1, y1, w1, height);setRecyangle(x1, y1, w1, height);}/*** 障碍物的碰撞矩形参数** @return*/public void setRecyangle(int x, int y, int width, int height) {rect.x = x;rect.y = y;rect.width = width;rect.height = height;}//判断什么时候绘制下一组障碍物public boolean isInFrame() {return 600 - x > 150;}public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}public int getType() {return type;}public void setType(int type) {this.type = type;}public boolean isVisible() {return visible;}public void setVisible(boolean visible) {this.visible = visible;}public Rectangle getRect() {return rect;}
}

 

package 框架;import java.util.ArrayList;
import java.util.List;/*** 为了避免反复的创建和销毁对象,使用对象池来提前创建好一些对象。* 使用的时候从池中获得,使用完毕后,归还*/
public class Barrierpool {//用于管理池中所有对象的容器private static List<Barrier> pool = new ArrayList<>();//池中初始的对象个数public static final int initCount = 16;//对象池中最大个数public static final int maxCOunt = 20;static {//初始化池中的对象for (int i = 0; i < initCount; i++) {pool.add(new Barrier());}}/*** 从池中获取一个对象*/public static Barrier getPool(){int size = pool.size();//如果池中有对象才可以拿if (size > 0) {//移除并返回对象System.out.println("拿走一个");return pool.remove(size-1);}else {//池中没有对象了 只能newSystem.out.println("新的对象");return new Barrier();}}/*** 将对象归还容器中*/public static void setPool(Barrier barrier){if (pool.size() < maxCOunt) {pool.add(barrier);System.out.println("容器归还了");}}}
package 框架;import static 图导.Constant.*;
import 图导.GameUtil;
import java.awt.*;
import java.awt.image.BufferedImage;/*** 小鸟类*/
public class Bird {//小鸟矩形对象private Rectangle rect;//小鸟加速度private int acceleration;//小鸟的生命public boolean life = true;//存放小鸟图片private BufferedImage[] images;public static final int BIRD_IMG_COUNT = 3;//鸟的状态private int state;public static final int STATE_NORMAR = 0;//平着飞public static final int STATE_UP = 1;//向上飞public static final int STATE_DOWN = 2;//向下飞//小鸟的位置private int x=200,y=200;//小鸟移动方向 上下private boolean up=false,down=false;//小鸟移动速度private int speed=4;//构造方法中对资源初始化public Bird() {images = new BufferedImage[BIRD_IMG_COUNT];for (int i = 0; i < BIRD_IMG_COUNT; i++) {images[i] = GameUtil.loadBufferedImage(BIRD_IMG[i]);}int w = images[0].getWidth();int h = images[0].getHeight();rect = new Rectangle(w,h);}//绘制小鸟public void draw(Graphics g) {flyLogic();g.drawImage(images [state], x, y, null);//绘制小鸟的矩形g.drawRect(x,y,(int)rect.getWidth(), rect.height);rect.x=this.x;rect.y=this.y;}//控制小鸟移动方向public void flyLogic(){if (up){acceleration--;y+=acceleration;if (acceleration < -10) {acceleration=-10;}if (y<20){y=20;acceleration=0;}}if (!up){acceleration++;y+=acceleration;if (acceleration > 10) {acceleration=10;}if (y>475){y=475;acceleration=0;}}}public void fly(int fly){switch (fly){case 1:state=1;up=true;break;case 5:state=2;up=false;break;}}public Rectangle getRect() {return rect;}/*** 重新绘制小鸟的位置*/
public void  restartDraw(){life=true;x=200;y=200;
}}
package 框架;import java.awt.*;
import java.awt.image.BufferedImage;/*** 云彩类*/
public class Cloud {//云彩图片private BufferedImage img;//云彩速度private  int speed;//云彩的位置private  int x,y;public Cloud(){}public Cloud(BufferedImage img, int speed, int x, int y) {this.img = img;this.speed = speed;this.x = x;this.y = y;}public void draw(Graphics g){x-=speed;g.drawImage(img,x,y,null);}/*** 用于判断云彩是否飞出屏幕以外*/public boolean isOutFrame(){if (x < -100) {return true;}return false;}}
package 框架;import static 图导.Constant.*;import 图导.Constant;
import 图导.GameUtil;import java.awt.*;
import java.awt.image.BufferedImage;/*** 游戏背景类*/
public class GameBackGround {//背景需要的资源图片private BufferedImage bkimg;//构造器初始化资源public GameBackGround(){bkimg = GameUtil.loadBufferedImage(Constant.BK_IMG_OATH);}//绘制图片public void draw(Graphics g){//填充背景色g.setColor(BK_COLOR);g.fillRect(0,0,FRAM_WIDTH,FRAM_HEIGNT);g.setColor(Color.black);//得到图片的高度和宽度int height = bkimg.getHeight();int weight = bkimg.getWidth();//循环的次数int count = Constant.FRAM_WIDTH/weight+1;for (int i = 0; i < count; i++) {g.drawImage(bkimg,weight*i,Constant.FRAM_HEIGNT-height,null);}}
}
package 框架;import 图导.Constant;import java.awt.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;/*** 游戏中障碍物层*/
public class GameBarrierLayer {private GameTime gameTime;private int txt;private Random random = new Random();private List<Barrier> barriers;public GameBarrierLayer() {barriers = new ArrayList<>();gameTime = new GameTime();}//绘制障碍物public void draw(Graphics g, Bird bird) {for (int i = 0; i < barriers.size(); i++) {Barrier barrier = barriers.get(i);if (barrier.isVisible()) {barrier.draw(g);} else {Barrier remove = barriers.remove(i);Barrierpool.setPool(remove);i--;}}collideBird(bird);logic(g);}public void logic(Graphics g) {if (barriers.size() == 0) {ran();gameTime.begin();insert(600, 0, numberTop, 0);insert(600, 500 - numberDown, numberDown, 2);} else {long differ = gameTime.differ();g.setColor(Color.white);g.setFont(new Font("微软雅黑", 1, 20));g.drawString("坚持了:" + differ + "秒", 30, 50);//显示最高成绩txt = getTxt();if (differ <= txt){g.drawString("最高成绩:" + txt , 200, 50);}else {setTxt(String.valueOf(differ));g.drawString("最高成绩:" + getTxt() , 200, 50);}//判断最后一个障碍物是否完全进入屏幕内Barrier last = barriers.get(barriers.size() - 1);if (last.isInFrame()) {ran();if (number < 50){insert(600,32,440,4);}else if (number>450){insert(600,125,200,6);}else {insert(600, 0, numberTop, 0);insert(600, 500 - numberDown, numberDown, 2);}}}}//用于储存游戏数据File file = new File("D:\\workspace3xlf\\小鸟\\bird.txt");/*** 从文件获取数据* @return*/public int getTxt()   {BufferedReader in = null;try {in = new BufferedReader(new FileReader(file));} catch (FileNotFoundException e) {e.printStackTrace();}int read = 0;try {read = Integer.parseInt(in.readLine());} catch (IOException e) {e.printStackTrace();}try {in.close();} catch (IOException e) {e.printStackTrace();}return read;}/*** 写入数据到文件* @param str*/public void setTxt(String str)   {FileWriter out = null;try {out = new FileWriter(file);} catch (IOException e) {e.printStackTrace();}try {out.write(str);} catch (IOException e) {e.printStackTrace();}try {out.close();} catch (IOException e) {e.printStackTrace();}}/*** 用于从池中获取对象,并吧参数封装成barrier 存入barriers数组中*/public void insert(int x, int y, int num, int type) {Barrier top = Barrierpool.getPool();top.setX(x);top.setY(y);top.setHeight(num);top.setType(type);top.setVisible(true);barriers.add(top);}//上方障碍物高度private int numberTop;//下方障碍物高度private int numberDown;private int number;//产生两个100-500之间的随机高度public void ran() {numberTop = random.nextInt(400) + 100;numberDown = random.nextInt(400) + 100;number = random.nextInt(500);//如果管道重合,则重新随机if (numberTop + numberDown > 450) {ran();}}/*** 判断障碍物和小鸟发生碰撞*/public boolean collideBird(Bird bird) {for (int i = 0; i < barriers.size(); i++) {Barrier barrier = barriers.get(i);//判断矩形是否相交if (barrier.getRect().intersects(bird.getRect())) {System.out.println("装上啦");bird.life = false;}}return false;}/*** 用于清空障碍物的池子*/public void restant(){barriers.clear();}}
package 框架;import static 图导.Constant.*;import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;/*** 游戏的主窗口类,所有的关于游戏中绘制的内容都在此类中完成。*/
public class GameFrame extends Frame {//实例化gamebackGround类private GameBackGround gameBackGround;//实例化Bird类private Bird bird;//实例化GameBarrierLayer类private GameBarrierLayer gameBarrierLayer;//实例化GameFrontGround类private GameFrontGround gameFrontGround;//存放图片的图片private BufferedImage buffimg = new BufferedImage(FRAM_WIDTH,FRAM_HEIGNT,BufferedImage.TYPE_4BYTE_ABGR);//构造方法中初始化一些参数public GameFrame(){//窗口是否可见setVisible(true);//窗口的大小setSize(FRAM_WIDTH,FRAM_HEIGNT);//窗口的标题setTitle(FRAM_TITLE);//窗口的初始化位置setLocation(FRAM_X,FRAM_Y);//窗口的大小不可改变setResizable(false);//窗口的关闭事件addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);//结束程序}});//初始化游戏对象initGamg();new run().start();//添加按键监听addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {add(e);}@Overridepublic void keyReleased(KeyEvent e) {minu(e);}});}//对游戏中的对象初始化public void initGamg(){gameBackGround = new GameBackGround();bird = new Bird();gameFrontGround = new GameFrontGround();gameBarrierLayer = new GameBarrierLayer();}class run extends Thread{@Overridepublic void run() {while (true) {repaint();try {Thread.sleep(33);} catch (InterruptedException e) {e.printStackTrace();}}}}/*** 所有的我们需要绘制的内容都在此方法中进行调用绘制*/@Overridepublic void update(Graphics g) {if(bird.life){//得到图片的画笔Graphics graphics = buffimg.getGraphics();gameBackGround.draw(graphics);bird.draw(graphics);gameFrontGround.draw(graphics);gameBarrierLayer.draw(graphics,bird);//一次性的将图片绘制到屏幕中g.drawImage(buffimg,0,0,null);}else {String over = "游戏结束";g.setColor(Color.red);g.setFont(new Font("微软雅黑",1,60));g.drawString(over,180,250);String reset = "Space Reset Game";g.drawString(reset,25,350);}}//按键public void add(KeyEvent e){switch (e.getKeyCode()){case KeyEvent.VK_UP:bird.fly(1);break;case KeyEvent.VK_SPACE:if (bird.life == false) {restart();}break;}}//抬键public void minu(KeyEvent e){switch (e.getKeyCode()){case KeyEvent.VK_UP:bird.fly(5);break;}}/*** 重置游戏*/public void restart(){gameBarrierLayer.restant();bird.restartDraw();}}
package 框架;import 图导.Constant;
import 图导.GameUtil;import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;/*** 游戏的前景类*/
public class GameFrontGround {//云彩的个数private static final int CLOUND_COUNT = 2;//存放云彩的容器private List<Cloud> clouds;//云彩的飞行速度private static final int CLOUNG_SPEED = 1;//使用到图片资源private BufferedImage[] img;//用于产生随机数private Random random;// 构造器初始化数据public GameFrontGround() {clouds = new ArrayList<>();img = new BufferedImage[CLOUND_COUNT];//容器中添加云彩的图片for (int i = 0; i < CLOUND_COUNT; i++) {img[i] = GameUtil.loadBufferedImage("img/cloud0" + i + ".png");}random = new Random();}//绘制云彩public void draw(Graphics g) {logic();for (int i = 0; i < clouds.size(); i++) {clouds.get(i).draw(g);}}/*** 用于云彩的个数控制*/private void logic() {if ((int) (500 * Math.random()) < 5) {Cloud cloud = new Cloud(img[random.nextInt(CLOUND_COUNT)], CLOUNG_SPEED, 600, random.nextInt(150));clouds.add(cloud);}for (int i = 0; i < clouds.size(); i++) {Cloud cloud = clouds.get(i);if (cloud.isOutFrame()){clouds.remove(i);i--;System.out.println("移除了"+cloud);}}}
}
package 框架;/*** 游戏计时器*/
public class GameTime {//开始private long beginTime;//结束private long endTime;//时间差private long differ;public GameTime(){}public void  begin(){beginTime = System.currentTimeMillis();}public long differ(){endTime = System.currentTimeMillis();return differ=(endTime-beginTime)/1000;}}

框架

package 图导;import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;/*** 游戏工具类*/
public class GameUtil {/***该方法传入一个图片路径 读取图片*/public static BufferedImage loadBufferedImage(String ImgPath){try {return ImageIO.read(new FileInputStream(ImgPath));} catch (IOException e) {e.printStackTrace();}return null;}
}
package 图导;import java.awt.*;/*** @author* @create*/
public class Constant {//窗口的大小public static final int FRAM_WIDTH= 600;public static final int FRAM_HEIGNT= 500;//窗口标题public static final String FRAM_TITLE= "飞翔的小鸟";//窗口的初始化位置public static final int FRAM_X= 200;public static final int FRAM_Y= 200;//背景图片public static final String BK_IMG_OATH = "src/img/bird_bk.png";//背景色public static final Color BK_COLOR = new Color(0x4BC4CF);//小鸟图片资源public static final String[] BIRD_IMG={"src/img/bird_normal.png","src/img/bird_up.png","src/img/bird_down.png"};//障碍物图片资源public static final String[] BARRIER_IMG_PATH ={"src/img/barrier.png","src/img/barrier_up.png","src/img/barrier_down.png"};
}

结果

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/211649.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【腾讯云云上实验室-向量数据库】Tencent Cloud VectorDB在实战项目中替换Milvus测试

为什么尝试使用Tencent Cloud VectorDB替换Milvus向量库&#xff1f; 亮点&#xff1a;Tencent Cloud VectorDB支持Embedding&#xff0c;免去自己搭建模型的负担&#xff08;搭建一个生产环境的模型实在耗费精力和体力&#xff09;。 腾讯云向量数据库是什么&#xff1f; 腾…

C++的new / delete 与 C语言的malloc/realloc/calloc / free 的讲解

在C语言中我们通常会使用malloc/realloc/calloc来动态开辟的空间&#xff0c;malloc是只会开辟你提供的空间大小&#xff0c;并不会初始化内容&#xff1b;calloc不但会开辟空间&#xff0c;还会初始化&#xff1b;realloc是专门来扩容的&#xff0c;当你第一次开辟的空间不够用…

Javascript每天一道算法题(十四)——合并数组区间_中等

文章目录 1、问题2、示例3、解决方法&#xff08;0&#xff09;方法0——双指针&#xff08;错误思路&#xff09;&#xff08;1&#xff09;方法1——双指针&#xff08;正确&#xff09; 总结 1、问题 以数组 intervals 表示若干个区间的集合&#xff0c;其中单个区间为 inte…

[点云分割] 基于法线差的分割

效果&#xff1a; 总体思路&#xff1a; 1、计算DoN特征 2、依据曲率进行过滤 3、依据欧式距离进行聚类 计算DoN特征的目的是为了提供准确的曲率信息。 其他&#xff1a; 计算DoN特征&#xff0c;这个算法是一种基于法线差异的尺度滤波器&#xff0c;用于点云数据。对于点…

【uniapp】uniapp开发小程序定制uni-collapse(折叠面板)

需求 最近在做小程序&#xff0c;有一个类似折叠面板的ui控件&#xff0c;效果大概是这样 代码 因为项目使用的是uniapp&#xff0c;所以打算去找uniapp的扩展组件&#xff0c;果然给我找到了这个叫uni-collapse的组件&#xff08;链接&#xff1a;uni-collapse&#xff09…

Unity中Shader的Standard材质解析(一)

文章目录 前言一、在Unity中&#xff0c;按一下步骤准备1、在资源管理面板创建一个 Standard Surface Shader2、因为Standard Surface Shader有很多缺点&#xff0c;所以我们把他转化为顶点片元着色器3、整理只保留主平行光的Shader效果4、精简后的最终代码 前言 在Unity中&am…

Keil Vision5—新建工程project

注意&#xff1a;创建的工程目录必须是纯英文目录 目录 1.开始配置 2.为该路径下新建个文件夹 3.选择器件 4.工程配置 4.右击魔术棒&#xff0c;设置参数 ​编辑 &#xff08;1&#xff09;target配置 &#xff08;2&#xff09;output配置 &#xff08;3&#xff09;c…

Java实现王者荣耀小游戏

主要功能 键盘W,A,S,D键&#xff1a;控制玩家上下左右移动。按钮一&#xff1a;控制英雄发射一个矩形攻击红方小兵。按钮控制英雄发射魅惑技能&#xff0c;伤害小兵并让小兵停止移动。技能三&#xff1a;攻击多个敌人并让小兵停止移动。普攻&#xff1a;对小兵造成基础伤害。小…

软件工程——数据流图(20分把握在自己手里)【言简意赅】

数据流图【DFD -> Data Flow Diagram】 确定外部实体&#xff1a; 在一个对于某系统的描述中&#xff0c;我们需要分辨的是&#xff0c;该系统的使用人员(或外部设备)&#xff0c;以及系统所反馈的人员(或外部设备)是谁&#xff1f; 这就是外部实体&#xff01;与系统内部处…

基于STC12C5A60S2系列1T 8051单片读写掉电保存数据IIC总线器件24C02地址码并显示在液晶显示器LCD1602上应用

基于STC12C5A60S2系列1T 8051单片读写掉电保存数据IIC总线器件24C02地址码并显示在液晶显示器LCD1602上应用 STC12C5A60S2系列1T 8051单片机管脚图STC12C5A60S2系列1T 8051单片机I/O口各种不同工作模式及配置STC12C5A60S2系列1T 8051单片机I/O口各种不同工作模式介绍液晶显示器…

【标注数据】labelme的安装与使用

这里写目录标题 下载标数据 下载 标数据 打开自动保存 创建矩形

数据结构(超详细讲解!!)第二十四节 二叉树(下)

1.遍历二叉树 在二叉树的一些应用中&#xff0c;常常要求在树中查找具有某种特征的结点&#xff0c;或者对树中全部结点逐一进行某种处理。这就引入了遍历二叉树的问题&#xff0c;即如何按某条搜索路径访问树中的每一个结点&#xff0c;使得每一个结点仅且仅被访问一次。 …