java飞扬的小鸟

这是达内培训班java培训很经典的一个案例,分享一下吧。

Game主类

package huat;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.*;

public class Game extends JPanel { // 继承面板
	BufferedImage bg;
	Bird bird;
	Arrow arrow;
	Ground ground;
	Column column1, column2;
	Heart heart;
	int score;
	boolean gamestart;
	BufferedImage gameoverImg;
	BufferedImage gamestartImg;
	boolean gameover;
	boolean showheart;

	public Game() throws Exception { // 构造方法
		try { // 抛出异常或捕获异常
			bg = ImageIO.read(getClass().getResource("bg.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		ground = new Ground();
		column1 = new Column(1);
		column2 = new Column(2);
		bird = new Bird();
		heart = new Heart();
		arrow = new Arrow();

		gamestartImg = ImageIO.read(getClass().getResource("start.png"));
		gameoverImg = ImageIO.read(getClass().getResource("gameover.png"));
		score = 0;
		gamestart = false;
		showheart = true;

	}

	public static void main(String[] args) throws Exception {

		JFrame frame = new JFrame("昌哥出品,闲人勿仿!");
		Game game = new Game();// game引用,new Game()对象
		frame.add(game); // 基本数据类型,引用数据类型
		frame.setSize(432, 644);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.setAlwaysOnTop(true);
		frame.setVisible(true);// 调用paint方法绘制对话框
		game.action();
	}

	public void paint(Graphics g) {// 重写paint方法
		g.drawImage(bg, 0, 0, null);
		g.drawImage(column1.image, column1.x - column1.width / 2, column1.y - column1.height / 2, null);
		g.drawImage(column2.image, column2.x, column2.y - column2.height / 2, null);
		g.drawImage(ground.image, ground.x, ground.y, null);
		Graphics2D g2 = (Graphics2D) g;
		g2.rotate(bird.alpha, bird.x, bird.y); // 旋转角度由鸟决定
		g.drawImage(bird.image, bird.x - bird.width / 2, bird.y - bird.height / 2, null);
		g2.rotate(-bird.alpha, bird.x, bird.y);
		Font f = new Font(Font.SANS_SERIF, Font.BOLD, 40);
		g.setFont(f);
		g.drawString(score + "", 40, 60);
		g.setColor(new Color(255, 255, 255));
		g.drawString(score + "", 42, 63);
		g.drawString("life:" + bird.life + "", 300, 63);
		g.drawImage(arrow.image, arrow.x, arrow.y, null);
		if (score >= 8 && score < 9) {
			g.setColor(new Color(25, 165, 105));
			g.drawString("がんばって", 50, 120);
		}
		if ((score % 5 == 1 || score % 5 == 2) && showheart == true) {
			g.drawImage(heart.image, heart.x, heart.y, null);
		}
		if (!gamestart) {
			g.drawImage(gamestartImg, 0, 0, null);
		}
		if (gameover) {
			g.drawImage(gameoverImg, 0, 0, null);
			g.setColor(Color.RED);
			g.drawString("your grade is:" + score + "", 60, 100);
		}
	}

	public void action() throws InterruptedException {
		MouseListener L = new MouseAdapter() {// 鼠标监听器,适配器
			@Override // 标签
			public void mouseClicked(MouseEvent e) {
				if (!gamestart && !gameover) {
					gamestart = true;
				} else if (gameover) {
					gameover = false;
					score = 0;
				} else if (heart.x >= bird.x - bird.width && heart.x <= bird.x + bird.width
						&& heart.y >= bird.y - bird.height && heart.y <= bird.y + bird.height) {
					bird.life++;
					bird.flyup();
					showheart = false;
					heart.x = 1000;
				}
				bird.flyup();
			}
		};
		this.addMouseListener(L);

		while (true) {
			if (!gameover && gamestart) {
				ground.step();
				column1.step();
				column2.step();
				arrow.step();
				bird.step();
				bird.fly();
				if (score % 5 == 1 || score % 5 == 2) {
					heart.step();
				}
				if (column1.x == bird.x || column2.x == bird.x) {
					score++;
				}
				if (bird.hit(ground) || bird.hit(column1) || bird.hit(column2) || bird.hit(arrow)) {
					bird.life--;
					Thread.sleep(100);
				}
				if (bird.life <= 0) {
					gameover = true;
					bird.life = 1;
				}
				if (heart.x > bird.x)
					showheart = true;
			}
			repaint();
			if (score < 8) {
				Thread.sleep(1000 / 60);
			} else if (score >= 8 && score < 14) {
				bird.g = 2;
				Random ran = new Random();
				arrow.y = ran.nextInt(156) + 172;
				column1.y = ran.nextInt(30) + 192;
				column2.y = ran.nextInt(30) + 192;
				ground.y = ran.nextInt(30) + 450;
				Thread.sleep(1000 / 90);
			} else if (score >= 14 && score < 50) {
				bird.g = 2.5;
				Thread.sleep(1000 / 110);
			}
		}
	}
}

Bird类

package huat;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class Bird {
	BufferedImage image;
	int x, y;
	int width, height;
	int size;
	BufferedImage[] images;
	int index;
	double s;
	double t;
	double v;
	double alpha;
	double g;
	int life;

	public Bird() throws Exception {
		image = ImageIO.read(getClass().getResource("0.png"));
		width = image.getWidth();
		height = image.getHeight();
		x = 100;
		y = 100;
		s = 0;
		v = 0;
		t = 0.25;
		g = 1.25;
		index = 0;
		life = 1;
		images = new BufferedImage[8];
		for (int i = 0; i < images.length; i++) {
			images[i] = ImageIO.read(getClass().getResource(i + ".png"));
		}
	}

	public void flyup() {
		v = -10;
	}

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

	public void step() {
		v = v + 1.25 * t;
		s = v * t + g * t * t;
		y += s;
		alpha = Math.atan2(s, 6);
	}

	public boolean hit(Ground g) {
		if (y > g.y) {
			alpha = Math.PI / 2;
			x = 100;
			y = 200;
			return true;
		} else
			return false;
	}

	public boolean hit(Column c) {
		if (x > c.x - c.width / 2 && x < c.x + c.width / 2 && (y > c.y + c.gap / 2 || y < c.y - c.gap / 2)) {
			x += 100;
			y = 200;
			return true;
		}
		return false;
	}

	public boolean hit(Arrow a) {
		if (a.y > y - height && a.y <= y && a.x <= x + width / 2 && a.x + 200 > x) {
			a.x = -200;
			return true;
		}
		return false;
	}
}

Column柱子类

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

public class Column {
	BufferedImage image;
	int width,height;
	int x,y;//图片中性点
	int gap;//柱子中间间隙
	int distance;
	Random ran=new Random();
	public Column(int n) throws IOException{
		image = ImageIO.read(getClass().getResource("column.png"));
		width=image.getWidth();
		height=image.getHeight();
		gap=141;
		distance=260;		
		y=ran.nextInt(156)+172;//[0,100] //172 , 328
		x=50+(n-1)*distance;
	}
	public void step(){
		x--;
		if(x==-100)
		{
			x=480;
			y=ran.nextInt(156)+172;
		}		
	}
}

Ground地面类

package huat;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Ground {
	BufferedImage image;
	int width,hight;
	int x,y;//左上角点
	public Ground() throws IOException{
		image = ImageIO.read(getClass().getResource("ground.png"));
		width=image.getWidth();
		hight=image.getHeight();
		x=0;
		y=500;	
	}
	public void step(){
		x--;
		if(x==-109)
		{
			x=0;
		}		
	}
}

Heart类用于增加生命值

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

public class Heart {
	BufferedImage image;
	
	int x,y;//图片中性点
	int width;
	int height;
	Random ran=new Random();
	public Heart() throws IOException{
		image = ImageIO.read(getClass().getResource("heart.png"));
		y=ran.nextInt(156)+172;//[0,100] //172 , 328
		x=200;
		width=image.getWidth();
		height=image.getHeight();
	}
	public void step(){
		x--;
		if(x==-100)
		{
			x=ran.nextInt(156)+172;
			y=ran.nextInt(156)+172;
		}		
	}
}

Arrow类用于增加难度

package huat;

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

public class Arrow {
	BufferedImage image;
	int x,y;
	int v;
	double t;
	double g;
	double s;
	double alpha;
	Random ran = new Random();
	public Arrow() throws IOException{
		image = ImageIO.read(getClass().getResource("arrow.png"));
		t=0.25;
		g=0.25;
		x=600;
		v=-40;
		y=ran.nextInt(156)+172;
	}
	public void step(){
		x+=v*t;
		y+=g*t*t;
		alpha=Math.atan2(-x, 6);
		if(x<=-200){
			x=800;
			y=ran.nextInt(300)+50;
		}	
	}
}

 

转载于:https://my.oschina.net/u/3254224/blog/862530

你可能感兴趣的:(java飞扬的小鸟)