Java swing 图形化基础学习之模拟flybird小程序

Bird类:

package FlyBird.demo;

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

public class Bird{
	int x;
	int y;
	int index;
	BufferedImage img[] = new BufferedImage[3];
	int width;
	int height;
	double time;
	double g;
	double y_speed;
	double y_s;
	double angle;
	public Bird(){
		for(int i = 0;i<3;i++){
			try {
				this.img[i] = ImageIO.read(getClass().getResource(i + ".png"));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.index = 0;
		this.width = img[index].getWidth();
		this.height = img[index].getHeight();
		this.x = (288-width)/2;
		this.y = (400-height)/2;
		this.time = 0.25;
		this.g = 9.8;
		this.y_speed = 10;
		this.y_s = 0;
		this.angle = 0;
	}
	
	public void move(){
		y_s = y_speed*time - time*time*g/2;
		y_speed = y_speed - g * time;
		y-=y_s;
		if(y<-10){
			y=-10;
		}
		index++;
		angle = Math.atan(y_s/3);
		if(index == 3){
			index = 0;
		}
		//鼠标点击事件触发改变y轴。。。。判断
	}
	public void up(){
		y_speed = 30;
	}
}

Floor类:
 

package FlyBird.demo;

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

import javax.imageio.ImageIO;

public class Floor {
    int x;
    int y;
    BufferedImage img;
    int width;
    int speed;
    public Floor() {
        // TODO Auto-generated constructor stub
        this.x = 0;
        this.y = 400;
        try {
            this.img = ImageIO.read(getClass().getResource("ground1.png"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        this.width = img.getWidth();
        this.speed = 1;
    }
    
    public void move(){
        x-=speed;
        if(x==288-336){
            x = 0;
        }
    }
    public boolean hit(Bird bird){
        if(y + 20 <= bird.y + bird.height){
            return true;
        }
        return false;
    }
}

Pillar类:

package FlyBird.demo;

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

import javax.imageio.ImageIO;

public class Pillar {
	int x;
	int y;
	BufferedImage img_up;
	BufferedImage img_down;
	int width;
	int height;
	int gap_min;
	int gap_max;
	int gap;
	int y_speed;
	int speed;
	int temp;

	public Pillar(int num) {
		// TODO Auto-generated constructor stub
		x = 288 + num * 170;
		speed = 1;
		try {
			img_up = ImageIO.read(getClass().getResource("zzshang.png"));
			img_down = ImageIO.read(getClass().getResource("zzxia.png"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		width = img_up.getWidth();
		height = img_down.getHeight();
		gap_min = 120;
		gap = gap_min;
		gap_max = 180;
		y_speed = 2;
		temp = 1;
		y=(int)(Math.random()*250)-height;//上面柱子的X坐标范围
	}
	
	public void move(){
		gap+=y_speed;
		if(gap>=gap_max||gap<=gap_min){
			y_speed = -y_speed;
		}
		x=x-speed;
		if(x < -width ){	
			x = 288;
			temp = 1;
			y=(int)(Math.random()*27) * 10 -height + 5; //改变y的参数
		}
	}
	
	public boolean hit(Bird bird){
		if(bird.x>=x-bird.width+10&&bird.x<=x+width-10){
			if(bird.y>y+height-15&&bird.y<y+height+gap-bird.height+15){
				return false;
			}
			return true;
		}
		return false;
	}
}

Panel画板上基本方法实现主要代码如下:

public void paint(Graphics g) {
		// TODO Auto-generated method stub
		super.paint(g);
		g.drawImage(bg, 0, 0,null);
		g.drawImage(pillar1.img_up, pillar1.x, pillar1.y,null);
		g.drawImage(pillar1.img_down, pillar1.x, pillar1.y + pillar1.height + pillar1.gap,null);
		g.drawImage(pillar2.img_up, pillar2.x, pillar2.y,null);
		g.drawImage(pillar2.img_down, pillar2.x, pillar2.y + pillar2.height + pillar2.gap,null);
		g.drawImage(floor.img, floor.x, floor.y,null);
		if(status==0){
			g.drawImage(start, (288-start.getWidth())/2, (512-start.getHeight())/2,null);
		}
		if(status==2){
			g.drawImage(gameover, (288-gameover.getWidth())/2, (512-gameover.getHeight())/2,null);
		}
		g.drawString("得分:"+score, 10, 20);
		g.drawString("最高分:"+top, 210, 20);
		
		Graphics2D g2 = (Graphics2D)g;
		g2.rotate(-bird.angle,bird.x+bird.width/2,bird.y+bird.height/2);
		g.drawImage(bird.img[bird.index], bird.x, bird.y,null);
	}
	
	public void action(){
		while(true){
			if(status==1){
				floor.move();
				pillar1.move();
				pillar2.move();
				bird.move();
				if(floor.hit(bird)||pillar1.hit(bird)||pillar2.hit(bird)){
					if(top < score)
						top = score;
					status = 2;
				}else if(!(pillar1.hit(bird)||pillar2.hit(bird))&&bird.x>=pillar1.x+pillar1.width-5){
					if(pillar1.temp==1){
						pillar1.temp=0;
						score++;
					}
				}else if(!(pillar1.hit(bird)||pillar2.hit(bird))&&bird.x>=pillar2.x+pillar2.width-5){
					if(pillar2.temp==1){
						pillar2.temp=0;
						score++;
					}
				}
			}
			repaint();
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		if(status==0){
			if(e.getX()>=(288-start.getWidth())/2&&e.getX()<=(288-start.getWidth())/2+start.getWidth()
					&&e.getY()>= (512-start.getHeight())/2&&e.getY()<= (512-start.getHeight())/2 + start.getHeight()){
				status = 1;
			}
		}else if(status == 2){
			if(e.getX()>=(288-gameover.getWidth())/2&&e.getX()<=(288-gameover.getWidth())/2+gameover.getWidth()
					&&e.getY()>= (512-gameover.getHeight())/2&&e.getY()<= (512-gameover.getHeight())/2 + gameover.getHeight()){
				status = 0;
				//游戏重新初始化
				bird = new Bird();
				pillar1 = new Pillar(0);
				pillar2 = new Pillar(1);
				score = 0;
				
			}
		}else if(status==3){	//暂停状态,需在加一个图标进行暂停
			
		}else{
			bird.up();
		}
	}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值