用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字

大大胡    -  云代码空间

——

飞扬的小鸟,自己写的BirdGame

2016-12-25|954阅||

摘要:飞扬的小鸟,打包

package bird.com;

import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Arrays;
import java.util.Random;


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












public class Game extends JPanel{
	public static final int WIDTH=576;
	public static final int HEIGHT=512;
	
	public static final int START=1;
	public static final int RUNNING=2;
	public static final int PAUSE=3;
	public static final int GOMEOVER=4;
	
	public static BufferedImage dayback;
	public static BufferedImage nightback;
	public static BufferedImage down1;
	public static BufferedImage up1;
	public static BufferedImage down2;
	public static BufferedImage up2;
	public static BufferedImage land;
	public static BufferedImage bird1_0;
	public static BufferedImage bird1_1;
	public static BufferedImage bird1_2;
	public static BufferedImage gameover;
	public static BufferedImage start;
	public static BufferedImage pause;
	public static BufferedImage flappybird;
	
	
	protected Bird bird=new Bird();
	protected Column[] columns={};
	protected Ground[] ground={new Ground(),new Ground(WIDTH-(Game.land).getWidth())};
	public static int state=START;
	
	private Timer timer;
	private int indexTime=0;//小鸟上升定时
	private int clickTimes=0;
	
	static{
		try {
			Random ran=new Random();
			int d=ran.nextInt(101);
			if(d>50){
				dayback=ImageIO.read(Game.class.getResource("dayback.png"));				
			}
			else{
				dayback=ImageIO.read(Game.class.getResource("nightback.png"));
			}
			nightback=ImageIO.read(Game.class.getResource("nightback.png"));
			down1=ImageIO.read(Game.class.getResource("down1.png"));
			up1=ImageIO.read(Game.class.getResource("up1.png"));
			down2=ImageIO.read(Game.class.getResource("down2.png"));
			up2=ImageIO.read(Game.class.getResource("up2.png"));
			land=ImageIO.read(Game.class.getResource("land.png"));
			bird1_0=ImageIO.read(Game.class.getResource("bird1_0.png"));
			bird1_1=ImageIO.read(Game.class.getResource("bird1_1.png"));
			bird1_2=ImageIO.read(Game.class.getResource("bird1_2.png"));
			gameover=ImageIO.read(Game.class.getResource("gameover.png"));
			pause = ImageIO.read(Game.class.getResource("pause.png"));
			start = ImageIO.read(Game.class.getResource("start.png"));
			flappybird = ImageIO.read(Game.class.getResource("flappybird.png"));
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	
		
	}
	public void paint(Graphics g){
		indexTime++;
		if(indexTime%80==0){
			bird.setupy(2);  //下降
		}
		g.drawImage(dayback, 0, 0, null);
		g.drawImage(dayback, 288, 0, null);
		paintColumn(g);
		paintBird(g);
		paintGround(g);
		paintState(g);
	}
	public void paintBird(Graphics g) {
		g.drawImage(bird.getImage(),bird.x ,bird.y,null);
	
	}
	public void paintColumn(Graphics g) {
		for(int i=0;i<columns.length;i++){
		g.drawImage(columns[i].getImage(),columns[i].x ,columns[i].y,null);
		}
	}
	
	public void paintGround(Graphics g) {
		for(int i=0;i<ground.length;i++){
		g.drawImage(ground[i].getImage(),ground[i].x ,ground[i].y,null);
		}
	}
	/** 画游戏状态 */
	public void paintState(Graphics g) {
		switch (state) {
		case START:
			g.drawImage(start, 195, 195, null);
			g.drawImage(flappybird, 170, 100, null);
			break;
		case PAUSE:
			g.drawImage(pause, 180, 120, null);
			break;
		case GOMEOVER:
			g.drawImage(gameover, 180, 120, null);
			break;
		}
	}
	

	public static void main(String[] args) {
		
		JFrame frame = new JFrame("飞扬的小鸟");
		Game game = new Game(); // 面板对象
		frame.add(game); // 将面板添加到JFrame中
		frame.setSize(WIDTH, HEIGHT); // 大小
		frame.setAlwaysOnTop(true); // 其总在最上
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 默认关闭操作
		frame.setIconImage(new ImageIcon("images/icon.jpg").getImage()); // 设置窗体的图标
		frame.setLocationRelativeTo(null); // 设置窗体初始位置
		frame.setVisible(true); // 尽快调用paint

		game.action(); // 启动执行

	}
	
	public void action() { // 启动执行代码
		// 鼠标监听事件
				MouseAdapter l = new MouseAdapter() {
					
					@Override
					public void mouseClicked(MouseEvent e) { // 鼠标点击
						switch (state) {
						case START:
						{state = RUNNING;}
						break;
						case RUNNING:
						{ 
							clickTimes++;
						bird.setupy(-1);  //上升
							
						}
//						case GOMEOVER: // 游戏结束,清理现场
//						{columns=new Column[0];
//						ground = new Ground[0];
//						bird=new Bird();
//						//score = 0;
//						state = START;
//						break;
//						}
						}
					}
					@Override
					public void mouseEntered(MouseEvent e) { // 鼠标进入
						if (state == PAUSE) { // 暂停时运行
							state = RUNNING;
						}
					}

					@Override
					public void mouseExited(MouseEvent e) { // 鼠标退出
						if (state == RUNNING) {
							state = PAUSE; // 游戏未结束,则设置其为暂停
						}
					}

				};
				this.addMouseListener(l); // 处理鼠标点击操作
				this.addMouseMotionListener(l); // 处理鼠标滑动操作
		
		timer = new Timer(); // 主流程控制
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				if(state==RUNNING){
					erterColumnsAction();//柱子入场
					
					Gameover();  //游戏结束
					outOfBoundsAction();//删除越界地面和柱子
				}
				enterGroundAction();//产生后面的地面图片
				stepAction();//地面和柱子的移动
					
				repaint(); // 重绘,调用paint()方法
			}


		},10, 10);
	}
	
	public void enterGroundAction(){ //产生地面图片数组
		int groundwidth=(Game.land).getWidth();//地面图片宽
		if(ground[ground.length-1].x==-(groundwidth-WIDTH)){
			Ground gro=new Ground(WIDTH);
			ground=Arrays.copyOf(ground, ground.length+1);
			ground[ground.length-1]=gro;
		}
	}
	
	public Column[] produceColumnsAction(){
		Random ran=new Random();
		int UP_DOWN=80+ran.nextInt(80);//柱子之间的通行距离
		int CUpy=140+ran.nextInt(240);//随机产生UP柱子的Y坐标
		Column[] grods={new ColumnUp1(CUpy)};  //创建向上的柱子
		grods=Arrays.copyOf(grods, grods.length+1);  //局部柱子数组扩容
		Column down1=new ColumnDown1(CUpy-UP_DOWN-(Game.down1).getHeight());  //创建向下的柱子
		grods[grods.length-1]=down1;    //把向下柱子添加进入数组
		
		return grods;
	}
	
	int columnIndex=0;
	public void erterColumnsAction(){ // 多少毫秒产生一对柱子
		columnIndex++;
		if(columnIndex%180==0){
			Column[] grods1=produceColumnsAction();
			columns=Arrays.copyOf(columns, columns.length+grods1.length);
			
			System.arraycopy(grods1, 0, columns, columns.length-grods1.length, grods1.length);
		
		}
		
	}
	
	/** 删除出界柱子和地面*/
	public void outOfBoundsAction() {
		int index = 0;
		Column[] columns11 = new Column[columns.length]; // 画面中的柱子
		for (int i = 0; i < columns.length; i++) {
			Column f = columns[i];
			if (!f.outOfBounds()) {
				columns11[index++] = f; // 不越界的留着
			}
		}
		columns = Arrays.copyOf(columns11, index); // 将没有出界的柱子留着
		

		index = 0; // 重置为0
		Ground[] ground11 = new Ground[ground.length];
		for (int i = 0; i < ground.length; i++) {
			Ground b = ground[i];
			if (!b.outOfBounds()) {
				ground11[index++] = b;
			}
		}
		ground = Arrays.copyOf(ground11, index); // 将不越界的地面图片留着
	}
	
	/**
	 * 检测是否碰撞
	 */
	public void Gameover(){
		if((0<bird.y)&&(bird.y<HEIGHT-(Game.land).getHeight()-Game.bird1_0.getHeight()+10)){
			for(int i = 0; i < columns.length; i++){
				if(bird.hitBy(columns[i])){
					state=GOMEOVER;
					return;
				}
				
			}			
		}
		else {state=GOMEOVER;}
	}
	
	
	 public void stepAction() {  //地面,小鸟。柱子的移动
			 if(state==RUNNING){
				 bird.move();
			 for(int i=0;i<ground.length;i++){
				 ground[i].move();
			 }
			 for(int i=0;i<columns.length;i++){
				 columns[i].move();
			 }
			 
		 }
			 if(state==START){
				 bird.move();

				 for(int i=0;i<ground.length;i++){
					 ground[i].move();
				 }
		 }
			 if(state==GOMEOVER){
				 bird.move();
			 }
	 }

}

顶 1踩 0收藏
文章评论
    发表评论

    个人资料

    • 昵称: 大大胡
    • 等级: 初级程序员
    • 积分: 0
    • 代码: 0 个
    • 文章: 5 篇
    • 随想: 0 条
    • 访问: 0 次
    • 关注

    人气代码

      最新提问

        站长推荐