JAVA编写 飞翔的小鸟

  • 窗口类:
  • 画板类:
  • 地面类:
  • 柱子类:
  • 鸟类:


编写时使用的图片:
----小鸟图片:0.png 1.png 2.png 3.png 4.png 5.png 6.pnh 7.png ----柱子图片:column.png
----背景图片:bg.png
----地面图片:ground.png
----开始图片:start.png
----结束图片:over.png

开始界面:

飞扬的小鸟java文件 飞翔的小鸟java代码_游戏


运行界面:

飞扬的小鸟java文件 飞翔的小鸟java代码_初始化_02

窗口类:

import java.io.IOException;

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

public class GameFrame extends JFrame{
		public GameFrame() {
			setTitle("飞翔的小鸟");//标题
			setSize(432,644);//大小
			setLocationRelativeTo(null);//居中
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗体时终止程序
			//Logo
			try {
				setIconImage(ImageIO.read(this.getClass().getResource("../p/0.png")));
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
	public static void main (String[]args) {
		//创建一个窗体对象
		GameFrame fly=new GameFrame();
		//创建一个画板对象
		GamePanel panel=new GamePanel();
		//向窗体内添加体块画板
		fly.add(panel);
		//显示窗体
		fly.setVisible(true);
		
	}
}

画板类:

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class GamePanel extends JPanel{
	//存取背景图片的变量
	BufferedImage bg;
	//开始图片变量
	BufferedImage startImg;
	//结束图片变量
	BufferedImage overImg;
	//声明一个存放地面图片的变量
	Ground ground;
	//声明两个存放柱子的变量
	Column column1;
	Column column2;
	//声明一个存放鸟了变量
	Bird bird;
	boolean start;
	boolean gaveOver;
	int score;//分数
	//构造器初始化变量
	public GamePanel() {
		score=0;
		//初始化游戏开始界面图片
		try {
			startImg=ImageIO.read(this.getClass().getResource("../p/start.png"));
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		//初始化游戏结束界面图片
		try {
			overImg=ImageIO.read(this.getClass().getResource("../p/over.png"));
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		//初始化背景图片
		try {
			bg=ImageIO.read(this.getClass().getResource("../p/bg.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}

		ground=new Ground();
		column1=new Column(1);
		column2=new Column(2);
		bird=new Bird();
		start=false;
		gaveOver=false;
		//鼠标监听
		MouseAdapter adapter =new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				super.mouseClicked(e);
				if(start==false) {
					start=true;
					start();
				}else if(gaveOver){
					start=false;
					gaveOver=false;
					ground=new Ground();
					column1=new Column(1);
					column2=new Column(2);
					bird=new Bird();
					repaint();

				}else {
					bird.moveUp();//
				}

			}
		};
		this.addMouseListener(adapter);

	}

	public void start() {
		Mythread mt=new Mythread();
		Thread t=new Thread(mt);
		t.start();
	}

	//向画板绘制内容 arg0相当于画笔
	@Override
	public void paint(Graphics arg0) {
		super.paint(arg0);
		//绘制背景图片的位置
		arg0.drawImage(bg,0,0,null);
		//绘制第一个柱子图片的大小位置
		arg0.drawImage(column1.img,column1.x,column1.y,column1.w,column1.h,null);
		//绘制第二个柱子图片的大小位置
		arg0.drawImage(column2.img,column2.x,column2.y,column2.w,column2.h,null);
		//绘制地面图片的位置
		arg0.drawImage(ground.img,ground.x,ground.y,null);
		//绘制小鸟图片的位置
		arg0.drawImage(bird.img,bird.x,bird.y,bird.w,bird.h,null);
		//游戏开始时显示的图片
		if(start==false) {
			arg0.drawImage(startImg,0,0,null);
		}
		//游戏结束时显示的图片
		if(gaveOver) {
			arg0.drawImage(overImg,0,0,null);
		}
		//分数显示的大小位置字体及颜色
		Font f=new Font("宋体",Font.BOLD,30);
		arg0.setFont(f);
		arg0.setColor(Color.red);
		arg0.drawString("分数:"+score,10,30);


	}
	
	//线程
	class Mythread implements Runnable{
		@Override
		public void run() {
			while(true) {
				ground.move();
				column1.move();
				column2.move();
				bird.move();
				bird.fly();
				boolean boo1=bird.hit();
				boolean boo2=bird.hit(column1);
				boolean boo3=bird.hit(column2);
				//游戏结束条件 
				if(boo1||boo2||boo3) {
					gaveOver=true;
					break;
				}
				//每通过一个柱子分数+1
				if(bird.x==column1.x+column1.w||bird.x==column2.x+column2.w){
					score++;
				}
				try {
					//暂停 每0.03秒输出一次
					Thread.sleep(30);
					//刷新
					repaint();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

			}
		}
	}
}

地面类:

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;


public class Ground {
	BufferedImage img;
	int x,y,w,h;
	
	public Ground() {
		//初始化地面图片
		try {
			img=ImageIO.read(this.getClass().getResource("../p/ground.png"));
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		//获取地面图片宽度
		w=img.getWidth();
		//获取地面图片高度
		h=img.getHeight();
		//图片放置位置
		x=0;
		y=644-h;
		
	}
	public void move() {
		//循环地面图片的移动
		if(x<-(w-432)) {
			x=0;
		}
		x--;
	}
}

柱子类:

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;

public class Column {
	Random  ran=new Random();
	BufferedImage img;//存放柱子的变量
	int x,y,w,h;

	int distance;//两个柱子间的距离
	int gap;//
	
	public Column(int i) {
		//初始化柱子图片
		try {
			img=ImageIO.read(this.getClass().getResource("../p/column.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		distance=245;//两个柱子间的距离
		
		w=img.getWidth()/2;
		h=img.getHeight()/2;

		x=300+245*(i-1);
		y=-ran.nextInt(100);
		gap=75;
	}
	//柱子移动
	public void move() {
		if(x<=-w) {
			x=300+distance;
		}
		x--;
	}
}

鸟类:

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

public class Bird {
	BufferedImage img;
	//存放图片组的变量
	List<BufferedImage> list;
	int x,y,w,h;
	//初速度
	double v0=3;
	double t=0.2;
	double s;
	//下落速度
	int g=5;

	public Bird() {
		//向图片组里添加7张图片
		list =new ArrayList<BufferedImage>();
		for(int i=0;i<8;i++) {
			try {
				list.add(ImageIO.read(this.getClass().getResource("../p/"+i+".png")));
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		
		try {
			img=ImageIO.read(this.getClass().getResource("../p/0.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		w=img.getWidth()/2;
		h=img.getHeight()/2;
		x=50;
		y=300;

	}
	int in=0;
	public void fly() {
		img=list.get(in%list.size());
		in++;
	}
	public void move() {
		s=v0*t;
		y=y-(int)s;
		double v2=v0-g*t;
		v0=v2;
	}
	public void moveUp() {
		v0=20;
	}
	public boolean hit(){
		if(y<=0||y>=644-146-h) {
			return true;
		}
		return false;

	}
	public boolean hit(Column column) {
		if(x>=column.x-w&&x<=column.x+column.w) {
			if(y>column.h/2+column.y-column.gap/2&&y<column.h/2+column.y+column.gap/2) {
				return false;
			}
			return true;
		}
		return false;
	}

}