飞翔的小鸟

APP包(运行)

GameApp类
package APP;
import mian.GameFrame;
 
public class GameApp {
	public static void main(String[] args) {
		new GameFrame();
		
	}
}

mian包(主内容)

Barrier 类(障碍物)
package mian;
 
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
 
import util.Constant;
import util.GameUtil;
 
/*
 * 障碍物类
 */
public class Barrier {
	
	
	
	
	private Rectangle rect;//小鸟矩形对象
	
	//
	private boolean mob=true;
	
	private int speed=3;//障碍物移动速度
	
	private static BufferedImage[] imgs;//障碍物需要的三个图片
	
	
	//障碍物的状态
	private boolean visible;
	
	static {
		final int COUNT=3;
		//类加载的时候将三个图片化
		imgs=new BufferedImage[COUNT];
		for(int i=0;i=250) {
    		 mob=false;
    	 }
     }else if(!mob) {
    	 y-=5;
    	 if(y<=100) {
    		 mob=true;
    	 }
     }
    
	}	
	
	
	/*
	 * 绘制障碍物矩形
	 */
	public void rect(Graphics g) {
		int x1=this.x;
		int y1=this.y;
		int w1=imgs[0].getWidth();
//		g.drawRect(x1, y1, w1,height);
		setRectyangle(x1,y1,w1,height);
		
	}
	
	/*
	 * 障碍物矩形参数
	 */
	public void setRectyangle(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;
	}
		
	}
Bird(鸟)
package mian;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import static util.Constant.*;
import util.GameUtil;
 
/*
 * 小鸟类
 */
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;i10) {
				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;
		
	}
 
}
Cloud(云彩)
package mian;
import java.awt.Graphics;
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;
	}
	
	
}
GameBackground(游戏背景)
package mian;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
 
import util.Constant;
import util.GameUtil;
 
/*
 * 游戏背景类
 */
public class GameBackground {
	
	private BufferedImage bkimg;//背景需要的资源图片
	
	//构造器初始化资源
	public GameBackground() {
		 bkimg=GameUtil.loadBufferedImage(Constant.BK_CATH_OATH);
	}
 
	//绘制图片
	public  void draw(Graphics g) {
		
		//填充背景颜色
		g.setColor(Constant.BK_COLOR);
		g.fillRect(0, 0, Constant.FRAM_WIDTH,Constant.FRAM_HEIGHT);
		g.setColor(Color.black);
		
		//得到图片的高宽和宽高
		int height=bkimg.getHeight();
		int width=bkimg.getWidth();
		//循环的次数
		int count=Constant.FRAM_WIDTH/width+1;
		for(int i=0;i
GameBarrierLayer(游戏中障碍物层)
package mian;
 
 
 
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
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 Listbarriers;
	
	public GameBarrierLayer() {
		barriers=new ArrayList<>();
		gameTime=new GameTime();
	}
	
	//绘制障碍物
	public void draw(Graphics g,Bird bird) throws NumberFormatException, IOException {
		for(int i=0;i450){
					insert(600,125,200,6);
				}else {
					insert(600,0,numberTop,0);
					insert(600,500-numberDown,numberDown,2);
				}
				
			}
		}
	}
	
	File file=new File("D:\\飞翔的小鸟.txt");
	
	
	/*
	 * 用于得到文件中的数据
	 */
	public int getTxt() throws NumberFormatException, IOException {
		 BufferedReader in=new BufferedReader(new FileReader(file));
		 int read=Integer.parseInt(in.readLine());
		 in.close();
		 return read;
	}
	
	
	/*
	 * 用于储存数据
	 */
	public void setTxt(String str) throws IOException {
		FileWriter fileWriter=new FileWriter(file);
		fileWriter.write(str);
		fileWriter.close();
	}
	
	
	/*
	 * 用来从池中获取对象,并把参数封装成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
GameFrame  
package mian;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
 
import static util.Constant.*;
import util.Constant;
 
public class GameFrame extends Frame{
 
	//实例化GrameBackground类
	private GameBackground gameBackground;
	
	//实例化Bird类
	private Bird bird;
	
	//实例化GameBarrierLayer
	private GameBarrierLayer gameBarrierLayer;
	
	//实例化GameFrontground类
	private GameFrontground gameFrontground;
	
	//存放图片的图片
	private BufferedImage buffimg=new BufferedImage(FRAM_WIDTH,FRAM_HEIGHT,BufferedImage.TYPE_4BYTE_ABGR);
 
	//构造方法中初始化一些参数
	public GameFrame() {
		setVisible(true);//窗口是否可见
		setSize(FRAM_WIDTH,FRAM_HEIGHT);//窗体的大小
		setTitle(FRAM_Ttile);//窗体的标题
		setLocation(FRAM_x,FRAM_y);//窗体的初始位置
		setResizable(false);//窗体的大小不可改变
		
		//窗口的关闭事件
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);//结束程序
			}
		});
		
		//初始化游戏对象
		initGame();
		
		new run().start();
		
		//添加案件监听器
		addKeyListener(new KeyAdapter (){
			
			public void keyPressed(KeyEvent e) {
				//super.keyPressed(e);
				add(e);
			}
			
			public void keyReleased(KeyEvent e) {
				//super.keyReleased(e);
				minu(e);
			}
			
		});
		
		
	}
	
	//对游戏中的对象初始化
	public void initGame() {
		gameBackground=new GameBackground();
		bird=new Bird();
		gameFrontground=new GameFrontground();
		gameBarrierLayer=new GameBarrierLayer();
		
	}
	
	
	class run extends Thread{
		public void run() {
			while(true) {
			repaint();
			try {
				Thread.sleep(33);	
			}catch(InterruptedException e) {
				e.printStackTrace();
			}
			}
		}
	}
	
	
	
	/*
	 * 所有的我们需要绘制的内容都在此方法中进行调用绘制
	 */
	@Override
	public void update(Graphics g) {
		if(bird.life) {
			//得到图片的画笔
			Graphics graphics=buffimg.getGraphics();
			
			gameBackground.draw(graphics);
			bird.draw(graphics);
			gameFrontground.draw(graphics);
			try {
				gameBarrierLayer.draw(graphics,bird);
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			//一次性的将图片绘制到屏幕中
			g.drawImage(buffimg,0,0,null);
		}else {
			String over="游戏结束";
			g.setColor(Color.red);
			g.setFont(new Font("微软雅黑",1,60));
			g.drawString(over,300,250);
			
			String reset="Spce 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();
		
	}
	
	
}
GameTime(计时)
package mian;
/*
 * 计时类
 */
public class GameTime {
 
	private long begingTime;//开始
	private long endTime;//结束
	private long differ;//时间差
	
	public GameTime() {
		
	}
	
	public void begin() {
		begingTime=System.currentTimeMillis();
		}
	public long differ() {
		endTime=System.currentTimeMillis();
		return differ=(endTime-begingTime)/1000;
	}
}

util(窗体)

Constant 
package util;
 
 
 
import java.awt.Color;
 
public class Constant {
	//窗口的大小
	public static final int FRAM_WIDTH=600;
	public static final int FRAM_HEIGHT=500;
	
	//窗口标题
	public static final String FRAM_Ttile="飞翔的小鸟";
	
	//窗口的初始化位置
	public static final int FRAM_x=200;
	public static final int FRAM_y=200;
	
	//图片路径
	public static final String BK_CATH_OATH="img/bird_bk.png";
	
	//背景颜色
	public static final Color BK_COLOR=new Color(0x4B4CF);
	
	//小鸟的图片资源
	public static final String [] BIRD_IMG=
		{"img/bird_normal.png","img/bird_up.png","img/bird_down.png"};
	
	//障碍物图片资源
	public static final String[] BARRIER_IMG_PATH= {
		"img/barrier.png","img/barrier_up.png","img/barrier_down.png",
	};
	
}
GameUtil  
package util;
 
 
 
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
 
import javax.imageio.ImageIO;
 
public class GameUtil {
	public static BufferedImage loadBufferedImage(String imgpath) {
		try {
			return ImageIO.read(new FileInputStream(imgpath));
		}catch(IOException e){
			e.printStackTrace();
		}
		return null;
	}
 
}

你可能感兴趣的:(python,开发语言)