找传奇、传世资源到传世资源站!

java 飞翔的小鸟 小游戏源码

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

小鸟躲避障碍物,以及计分。游戏规则:只要鼠标 不断点击,小鸟就向上飞,不要小鸟碰到障碍物 为赢java 飞翔的小鸟 小游戏源码 Java-第1张
package day06;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.image.BufferedImage;import javax.imageio.ImageIO;import javax.swing.JFrame;import javax.swing.JPanel;//继承 public class zlfday03 extends JPanel {zlf b;bird f;zhuzi a;zhuzi c;BufferedImage background;// 创建一个背景图片boolean gameOver;// 标示游戏是否结束BufferedImage gameOverImage;// 游戏结束的图片int score;// 计算游戏分数boolean started;BufferedImage startedImage;// main 方法,主方法,主函数public static void main(String[] args) throws Exception {JFrame frame = new JFrame();// 创建一个窗体frame.setTitle("飞翔的小鸟!");// 设置窗体的标志frame.setSize(432 10, 644 30);// 设置窗体的尺寸zlfday03 panel = new zlfday03();// JPanel panel =new JPanel();//创建面板frame.add(panel);// 把面板添加到窗体中frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置当关闭窗体时,程序也退出frame.setLocationRelativeTo(null);// 设置窗体居中frame.setVisible(true);// 让窗体可见while (true) {panel.action();// 让游戏动起来}}// 创建一个类的构造器 构造器是用于初始化属性的public zlfday03() throws Exception {background = ImageIO.read(getClass().getResource("bg.png"));// 反射// 初始化背景图片b = new zlf();f = new bird();a = new zhuzi(1);c = new zhuzi(2);gameOver = false;gameOverImage = ImageIO.read(getClass().getResource("gameover.png"));score =0;started = false;startedImage = ImageIO.read(getClass().getResource("start.png"));}// 绘图public void paint(Graphics g) {g.drawImage(background, 0, 0, null);// 绘制背景图片g.drawImage(a.image, a.x - a.width / 2, a.y - a.height / 2, null);g.drawImage(c.image, c.x - c.width / 2, c.y - c.height / 2, null);g.drawImage(b.image, b.x, b.y, null);Font m = new Font(Font.SANS_SERIF, Font.BOLD, 40);g.setFont(m);g.drawString(score "", 40, 60);g.setColor(Color.WHITE);g.drawString(score "", 40 - 3, 60 - 3);// 创建一个可旋转坐标系Graphics2D g2 = (Graphics2D) g;// 把坐标系旋转alpha角度g2.rotate(-f.alpha, f.x, f.y);g.drawImage(f.image, f.x - f.width / 2, f.y - f.height / 2, null);// 把坐标系旋转回来,以防影响后面的制图g2.rotate(f.alpha, f.x, f.y);if (gameOver) {g.drawImage(gameOverImage, 0, 0, null);}if (!started) {g.drawImage(startedImage, 0, 0, null);}}// 添加方法,用于控制整个游戏所有组件的运动方式public void action() throws Exception {// 创建鼠标监听事件MouseListener L = new MouseAdapter() {public void mousePressed(MouseEvent e) {try {if (gameOver) {a = new zhuzi(1);c = new zhuzi(2);f = new bird();score =0;gameOver = false;started = false;} else {f.flappy();started = true;}} catch (Exception e2) {e2.printStackTrace();// 打印堆栈信息}}};// 将监听器挂起(添加)addMouseListener(L);while (true) {// 添加死循环,让游戏一直运动if (!gameOver) {if (started) {a.step();c.step();f.step();}b.step();// 让地面动起来f.fly();if (f.hit(b) || f.hit(a) || f.hit(c)) {gameOver = true;}if (f.x == a.x || f.x == c.x) {score ;}}repaint();// 调用重绘方法,重复画面Thread.sleep(1000 / 60);// 线程的休眠方法}}}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复