飞翔的鸟代码

 

package 飞翔的鸟;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 / 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; // 末速度 = 当前速度 + 重力加速度 * 时间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 飞翔的鸟;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读取到程序中的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 的构造方法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 画笔*  */@Overridepublic 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); // 画柱子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); // 结束图片}// 画分数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() {@Overridepublic 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() {@Overridepublic 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);// 设置不允许玩家拖动界面// 调用actiongame.action();}}
package 飞翔的鸟;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 飞翔的鸟;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.hqwc.cn/news/268451.html

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

相关文章

SELinux介绍

本章主要介绍在RHEL8中如何使用 SELinux。 了解什么是 SELinux了解 SELinux 的上下文配置端口上下文了解SELinux的布尔值了解SELinux的模式 在 Windows系统中安装了一些安全软件后&#xff0c;当执行某个命令时&#xff0c;如果安全软件认为这个命令对系统是一种危害&#…

为什么有些程序员敲代码这么慢?

首先&#xff0c;每个程序员都是会利用工具的人&#xff0c;也有自己囊里私藏的好物。独乐乐不如众乐乐&#xff0c;今天笔者整理了 3 个辅助我们写代码的黑科技&#xff0c;仅供参考。如果你有更好的工具&#xff0c;欢迎评论区分享。 1、Google/Stackoverflow——搜索解决方…

卷积神经网络(CNN)中感受野的计算问题

感受野 在卷积神经网络中&#xff0c;感受野&#xff08;Receptive Field&#xff09;的定义是卷积神经网络每一层输出的特征图&#xff08;feature map&#xff09;上每个像素点在原始图像上映射的区域大小&#xff0c;这里的原始图像是指网络的输入图像&#xff0c;是经过预处…

12.11_黑马数据结构与算法笔记Java

目录 070 栈 链表实现 概念理清&#xff1a;什么时候是指针的指向&#xff0c;什么时候是元素本身&#xff1f; 071 栈 数组实现 072 栈 e01 有效的括号 072 栈 e02 后缀表达式求值 072 栈 e03 中缀表达式转后缀1 072 栈 e03 中缀表达式转后缀2 072 栈 e03 中缀表达式转…

C:算术移位和逻辑移位傻傻分不清楚

1. 算术移位与逻辑移位概念 算术移位指令对带符号数进行移位。 逻辑移位指令对无符号数进行移位。 算术左移、右移&#xff0c;逻辑左移、右移 如图所示 &#xff1a; 这里有一个进位位C&#xff0c;它就是标志寄存器 &#xff08;即状态寄存器&#xff0c;亦称程序状态字寄…

【pytest】单元测试文件的写法

前言 可怜的宾馆&#xff0c;可怜得像被12月的冷雨淋湿的一条三只腿的黑狗。——《舞舞舞》 \;\\\;\\\; 目录 前言test_1或s_test格式非测试文件pytest.fixture()装饰器pytestselenium test_1或s_test格式 要么 test_前缀 在前&#xff0c;要么 _test后缀 在后&#xff01; …

【技巧】导出和导入Typecho的文章数据

转载请注明出处&#xff1a;小锋学长生活大爆炸[xfxuezhang.cn] 背景介绍 1、要换服务器了&#xff0c;虽然Typecho和Theme都可以重装&#xff0c;但文章数据由于是存在数据库里的&#xff0c;所以需要额外一些操作才行。 2、在进行下面的数据导入导出之前&#xff0c;新服务器…

使用函数计算,数禾如何实现高效的数据处理?

作者&#xff5c;邱鑫鑫&#xff0c;王彬&#xff0c;牟柏旭 公司背景和业务 数禾科技以大数据和技术为驱动&#xff0c;为金融机构提供高效的智能零售金融解决方案&#xff0c;服务银行、信托、消费金融公司、保险、小贷公司等持牌金融机构&#xff0c;业务涵盖消费信贷、小…

遥感深度学习:如何读取TIFF文件和切片成256*256?

01 前言 最近打算认真从头开始学习深度学习和遥感结合的相关内容&#xff0c;主要通过Python进行处理。此前用深度学习进行遥感相关的学习一直都是用 tensorflow3.0 框架&#xff0c;但是考虑很多因素我后面打算换用 Pytorch 进行学习。好在tensorflow我只是浅尝辄止&#xff…

学习深度强化学习---第1部分----RL介绍、基本模型、Gym介绍

文章目录 1.1节 强化学习简介1.2节 强化学习的模型1.3节 Gym介绍 视频所在地址&#xff1a;深度强化学习的理论与实践 经典的强化学习有三种&#xff1a;1、基于动态规划的强化学习、2、基于蒙特卡洛算法的强化学习、3、基于时序差分的强化学习&#xff0c;以上3种方法分为第2、…

错误记录 apt --fixed-broken install

1.报错 E: Unmet dependencies. Try apt --fix-broken install with no packages (or specify a solution). 无法直接运行apt --fix-broken install解决 直接报错 没有输入y那个步骤 无法直接使用 sudo apt-get remove 无法使用 sudo dpkg -r 删除 查了很多解决方式无法…

10.仿简道云公式函数实战-逻辑函数-XOR

1. XOR函数 XOR 函数可返回所有参数的异或值。异或的含义是&#xff1a;两个逻辑值相同&#xff0c;返回 false&#xff0c;两个逻辑值不同&#xff0c;则返回 true。 2. 函数用法 XOR(logical1,logical2, …) 3. 函数示例 如&#xff0c;判断两个答案值是否一致时&#x…