很久很久以前的了,分享给爱研究游戏的朋友,直接拿去运行即可。

代码如下:

package com.hui.dao.bird;

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 java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestBirdGame extends JPanel {
// 背景图片
private BufferedImage bgImage = null;
// 开始图片
private BufferedImage startImage = null;
// 结束图片
private BufferedImage endImage = null;

// 声明鸟类对象
Brid bird = null;
    // 地面对象
Ground ground = null;
//
Column column1= null;
//
Column column2 = null;

private int state;
// 游戏运行的三种状态
public final static int START = 0;
public final static int RUNNING = 1;
public final static int GAME_OVER = 2;

// 分数
private int score = 0;

public TestBirdGame() throws Exception {

state = START;

score = 0;

bgImage = ImageIO.read(getClass().getResource("bg.png"));
startImage = ImageIO.read(getClass().getResource("start.png"));
endImage = ImageIO.read(getClass().getResource("gameover.png"));

bird = new Brid();
  
ground = new Ground();

column1 = new Column(1);
   column2 = new Column(2);
}

// 鼠标监听事件和刷新页面方法
public void action() {

MouseListener listener = new MouseAdapter() {

@Override
public void mousePressed(MouseEvent arg0) {

switch (state) {
      case START://当鼠标点击开始状态变成START

state = RUNNING;//如果是START,就将状态置为运行RUNNING
break;

case RUNNING:
 
bird.flyUp();//如果是RUNNING,就调用flyUp()方法
break;
  
case GAME_OVER ://游戏结束状态

score = 0;
state = START;//游戏结束,将游戏界面置为开始状态
try {
bird = new Brid();//初始化鸟的属性
column1 = new Column(1);
column2 = new Column(2);
} catch (IOException e) {
e.printStackTrace();
}
break;
  }
}
};

this.addMouseListener(listener);
// 循环刷新
while (true) {

switch (state) {
case START:
// 开始
bird.fly();
ground.step();
break;

case RUNNING:
// 游戏运行状态
bird.fly();
ground.step();
column1.step();
column2.step();
bird.step();

if(bird.hit(column1)||bird.hit(column2)||bird.hit(ground)){
state =GAME_OVER;
}
if(bird.x == column1.x || bird.x ==column2.x){
score++;
}
break;
case GAME_OVER:
// 游戏结束状态
break;
}
repaint();//重绘

try {
Thread.sleep(1000/60);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

// 重写画笔方法
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
g.drawImage(bgImage, 0, 0, null);
        
Graphics2D graphics2d = (Graphics2D)g;
        graphics2d.rotate(-bird.rad,bird.x - bird.width / 2, bird.y - bird.height/ 2);
g.drawImage(bird.image, bird.x - bird.width / 2, bird.y - bird.height/ 2, null);
graphics2d.rotate(bird.rad,bird.x - bird.width / 2, bird.y - bird.height/ 2);
        System.out.println(column1.x+"::"+column2.x);
g.drawImage(column1.image, column1.x-column1.width/2, column1.y-column1.height/2, null);
g.drawImage(column2.image, column2.x-column2.width/2, column2.y-column2.height/2, null);

g.drawImage(ground.image, ground.x, ground.y, null);

Font font = new Font(Font.SANS_SERIF, Font.BOLD, 40);
g.setFont(font);
g.drawString(score + "", 40, 60);
g.setColor(Color.WHITE);
g.drawString(score + "", 40 - 3, 60 - 3);

switch (state) {
case START:

g.drawImage(startImage, 0, 0, null);
break;

case GAME_OVER:
g.drawImage(endImage, 0, 0, null);
break;
}
}

public class Brid {

private BufferedImage image = null;
private int x, y;
private int width;
private int height;
private int size;
private int index = 0;

private BufferedImage[] images = null;

//小鸟向上飞的属性

double g = 0; //重力加速度
double v0 = 0; //初速度
double time = 0; //时间
double s = 0; //距离
double speeh = 0;//当前速度

double rad = 0;

public Brid() throws IOException {
image = ImageIO.read(getClass().getResource("0.png"));//鸟头
width = image.getWidth();//获取鸟头的宽度和高度,及所在的坐标
height = image.getHeight();
x = 134;
y = 234;
size = 40;//试过了  这个取消也没什么影响

//向下的加速度,初速度,时间
g = 4;
v0 =20;
time = 0.25;
speeh = v0;

index = 0;//
images = new BufferedImage[8];//数组,8个单位,用于装图片
    
rad = 0;//弧度
//循环读取8张图片放到数组中
for (int i = 0; i < 8; i++) {

images[i] = ImageIO.read(getClass().getResource(i + ".png"));
}
}

public void fly() {
index++;
image = images[index/10 % 8];
}

public void step(){

double v  = speeh;
s = v*time - g*time*time/2;
y = y - (int)s;
speeh = v - g*time;
rad = Math.atan(s/8);
}

public void flyUp(){
speeh = v0;//游戏开始,速度置为0
}

public boolean hit(Ground ground){

if(ground.y<=y+height/2){

y = 500-height/2;
return true;
}
return false;
}

public boolean hit(Column column){

if(x>column.x-column.width/2-size/2&&x<column.x+column.width/2+size/2){

if(y>column.y-column.gap/2+size/2&&y<column.y+column.gap/2-size/2){

return false;
}
return true;
}
return false;
}
}
  
public class Ground{

private BufferedImage image ;
private int x ,y;

public Ground() throws IOException {
          image = ImageIO.read(getClass().getResource("ground.png"));
          x = 0;
          y=500;
  }

public void step(){
      x--;
      if(x==-109){
     x = 0;
      } 
       }
}

public class Column{

private BufferedImage image = null;
private int x,y;
private int distance = 0 ;
private int width = 0;
private int height = 0;
private int gap = 0;
Random random = new Random();
 
public Column(int n) throws IOException{

image = ImageIO.read(getClass().getResource("column.png"));//读取柱子的图片  
 width = image.getWidth();
 height = image.getHeight();
 gap = 144;//上下柱子之间的实际宽度
 distance = 245;//左右柱子之间的间隔
 y = random.nextInt(238)+132;//柱子坐标随机产生
 x = 550+(n-1)*distance;
 }
 
  public void step(){
  x--;
  if(x == -width/2){
  x = 2*distance-width/2;
  y = random.nextInt(238)+132;
  }
  } 
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
  
JFrame frame = new JFrame();

TestBirdGame birdGame = new TestBirdGame();

frame.setSize(450, 670);

frame.add(birdGame);
// 位于屏幕的中间
frame.setLocationRelativeTo(null);
// 点击关闭窗体
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

birdGame.action();
}
}

蹦蹦鸟游戏代码-JavaSE版本相关推荐

  1. 分享我的像素鸟游戏代码

    最近由于收到了像素鸟的刺激搞到我也跃跃欲试,想要尝试着做游戏,学了几天的unity,感觉还不错,现在分享一下我的像素鸟项目,希望能与大家做交流 https://gitcafe.com/GeorgeZe ...

  2. 搞笑python代码_教你用 Python 写一个搞笑版的 “笨鸟” 游戏:牛逼的黄瓜

    我想你应该玩过这个鸟游戏: 想当年,小帅b和身边的朋友在玩这个游戏的时候玩到手到快废了... 今天,小帅b心血来潮,要不咱们自己用 Python 撸一个这样的游戏吧,想想还是挺好玩的. 那么接下来就是 ...

  3. 【源码+图片素材+详细教程】Java游戏开发_Java开发经典游戏飞翔的小鸟_飞扬的小鸟_Java游戏项目Flappy Bird像素鸟游戏_Java课程设计项目

    课程目标: 1.通过本课程的学习巩固Java的相关基础知识,例如循环判断,数组和集合的使用,对象的继承,接口的实现,窗口的创建,事件监听,图形绘制. 2.完成小鸟的移动,管道自动生成.碰撞死亡,计分系 ...

  4. 零基础教你Unity制作像素鸟游戏 【文末源码】

    爆肝三天终于写完了,一文教你从零开启Unity制作像素鸟游戏 前言 一,新建目录 二,制作材质 三,场景搭建 四,创建地图 五,制作管道 六,创建主角 七,小鸟动起来 八,游戏状态控制 九,摄像机跟随 ...

  5. python 贪吃蛇小游戏代码_10分钟再用Python编写贪吃蛇小游戏

    Python编写贪吃蛇 前不久我们公众号发布了一篇C++编写贪吃蛇小游戏的推文,反响空前.看来大家对这类简单易上手小游戏还是很喜爱的. 恰逢2018年IEEE Spectrum编程语言排行榜新鲜出炉, ...

  6. c语言小游戏代码矿井逃生_如何选择编程语言和逃生教程炼狱

    c语言小游戏代码矿井逃生 A few weeks ago, I posted about my experience attempting to learn JavaScript, C#, Pytho ...

  7. lua游戏代码_在游戏中如何使用LUA脚本语言

    当你希望在你的游戏开始的时候读取一些信息,以配置你的游戏,这些信息通常都是放到一个文本文件中,在你的游戏启动的时候,你需要打开这个文件,然后解析字符串,找到所需要的信息. 或许你认为这样就足够了,为什 ...

  8. python像素鸟游戏

    目录 1.引言 2.系统结构 2.1 总体结构 2.2 局部结构 2.2.1 main模块结构 2.2.2 Bird模块结构 2.2.3 Tubing模块结构 3.代码实现 4.实验 5.总结和展望 ...

  9. 用原生js+html写一个像素鸟游戏

    前言: 用html+js+css写一个面向对象板的像素鸟游戏 看一下效果把: 游戏需引用的图片:(右键保存图片) bird.png land.png pipeDown.png pipeUp.png s ...

最新文章

  1. 火爆全网,却只有4页!ICLR爆款论文「你只需要Patch」到底香不香?
  2. 004-CSS3动画类
  3. 计算道路超高lisp_5G+AI超高清智能视频监控将迎来增长期
  4. JNative用法注意事项
  5. 简单的php,php简单语句
  6. Gmap.net 怎么导入离线地图
  7. 做一个完整的Java Web项目需要掌握的技能[转]
  8. 用计算机名共享打印机不能打印,共享打印机无法打印怎么办解决教程
  9. Cocos2d-x 整理 SDK,易接流程(写给自己看的
  10. 使用高德开放平台制作个性地图(一)
  11. Chrome translate plugins install 谷歌翻译插件安装
  12. Drupal项目实战-公司订餐系统
  13. android5.0 应用闪退,【Android】MultiDex;NoClassDefFoundError;5.0以下系统应用闪退
  14. 在服务器上创建文件夹,在服务器上创建文件夹
  15. RT throttling分析【转】
  16. 微信小程序---显示与隐藏hidden
  17. Yocto系列讲解[入门篇] 1 - 快速入门熟悉Yocto的构建
  18. Mysql 分库分表 Mycat
  19. LeetCode刷题(37)~无重复字符的最长子串
  20. Windows 录屏软件哪个好?据说这几款录制工具还不错!

热门文章

  1. c语言字符串去重用指针,用几条shell命令快速去重10G数据
  2. 苹果终止位置共享无法连接服务器,苹果发布临时解决方案指导用户处理macOS无法共享访问Windows的问题...
  3. stormzhang
  4. 基于python的个人博客_一款基于 Django 的极简主义个人博客系统
  5. leo生鲜配送管理系统2020
  6. ctf解密图片得到flag_CTF从入门到进(fang)阶(qi)之MISC
  7. html 人物行走动画,CSS3人行走动作图解和动画实现
  8. 调用MapReduce对文件各个单词出现的次数进行统计
  9. 通过对虚拟磁盘进行碎片整理来提高VMware VM性能
  10. statsby: 不用循环语句的循环