王思聪吃热狗 - 飞机大战小游戏




这个小游戏是用java写的,java swing, 源码比较简单,一般都能看懂 ,素材找不到,看着比较low,如果你找到热狗飞机图片,改一下就好




运行结果图:
王思聪吃热狗 - 飞机大战小游戏

**

图片素材:

**
https://blog.csdn.net/qq_40456064/article/details/84787388

分为8个class:

1. Bullet.java
2. GetImage.java
3. MyGame.java
4. Plane.java
5. SiCongPlaneFight_Main.java
6. Stone.java
7. Table.java
8. Window.java

程序入口在

SiCongPlaneFight_Main.java

  1. Bullet.java:
/**
  *  子弹类  - 热狗子弹
 * @ClassName: bullet
 * @Description: TODO
 * @author GatesMa
 * @date 2018年12月3日
 * @Email: [email protected]
 */
import java.awt.*;

public class Bullet {
	int x, y;//子弹的坐标
	Image i;
	boolean destory = false;
	Stone stone[];
	Bullet(Image img, int x,int y, Stone stone[]){
		this.x = x;
		this.y = y;
		this.i = img;
		this.stone = stone;
	}
	
	void shot() {
		y -= Table.table*2;
	}
	
	void draw(Graphics g) {
		g.drawImage(i, x, y, Table.size/2, Table.size, null);
		ifdestroy();
		shot();
	}
	
	void ifdestroy() {
		if(this.y < 0) {
			destory = true;
		}
		for(int i =0;i < stone.length; i++) {
			if(stone[i] != null) {
				if(stone[i].x-Table.size/2<this.x&&this.x<(stone[i].x+Table.size/2)&&this.y>stone[i].y-Table.size/2&&this.y<stone[i].y+Table.size/2) 
				{
					stone[i].live--;
					destory = true;
				}
			}
		}
	}
}

  1. GetImage.java
/**
  *  图片导入工具类
 * @ClassName: GetImage
 * @Description: TODO
 * @author GatesMa
 * @date 2018年12月3日
 * @Email: [email protected]
 */
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

public  class GetImage {//此类封装了方法获取图片 
	private GetImage() {}//工具类封装构造
	public static BufferedImage get(String path) 
	{
		URL u= GetImage.class.getClassLoader().getResource(path);//获取到图片的资源
		BufferedImage b = null;
		try {
			b=ImageIO.read(u);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return b;
	}
}

  1. MyGame.java
/**
  *  图片导入工具类
 * @ClassName: GetImage
 * @Description: TODO
 * @author GatesMa
 * @date 2018年12月3日
 * @Email: [email protected]
 */
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

public  class GetImage {//此类封装了方法获取图片 
	private GetImage() {}//工具类封装构造
	public static BufferedImage get(String path) 
	{
		URL u= GetImage.class.getClassLoader().getResource(path);//获取到图片的资源
		BufferedImage b = null;
		try {
			b=ImageIO.read(u);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return b;
	}
}

  1. Plane.java
/**
  *  飞机类 - 热狗炮弹
 * @ClassName: Plane
 * @Description: TODO
 * @author GatesMa
 * @date 2018年12月3日
 * @Email: [email protected]
 */
import java.awt.*;

import javax.swing.ImageIcon;




public class Plane {
	int x, y;
	Image i;
	boolean destory = false;
	Stone stone[];
	double step = Table.table;
	
	private boolean left, right, up, down;
	
	public boolean isLeft() {
		return left;
	}
	public void setLeft(boolean left) {
		this.left = left;
	}
	public boolean isRight() {
		return right;
	}
	public void setRight(boolean right) {
		this.right = right;
	}
	public boolean isUp() {
		return up;
	}
	public void setUp(boolean up) {
		this.up = up;
	}
	public boolean isDown() {
		return down;
	}
	public void setDown(boolean down) {
		this.down = down;
	}
	
	Plane(Image bi1,int x,int y,Stone stone[]){
		this.i = bi1;
		this.x = x;
		this.y = y;
		this.stone = stone;
	}
	
	public void move() {
		if(MyGame.count >= 2) {
			step = Table.table / 1.414;
		}else {
			step = Table.table;
		}
		
		if(left && x > 0) {
			this.x -= step;
		}
		if(right && x < Table.width - Table.size) {
			this.x += step;
		}
		if(up && y > 0){
			this.y -= step;
		}
		if(down && y < Table.height - Table.size) {
			this.y += step;
		}
	}
	
	public void draw(Graphics g) {
		move();
		ifdestory();
		g.drawImage(i, x, y, Table.size, Table.size, null);
	}
	
	private void ifdestory() {
		for(int i =0;i < stone.length;i++) {
			if(stone[i] != null) {
				if(stone[i].x - Table.size/2 < this.x && this.x < (stone[i].x+Table.size/2)&&this.y>stone[i].y-Table.size/2&&this.y<stone[i].y+Table.size/2) {
					destory=true;
				}
			}
		}
	}
}

  1. SiCongPlaneFight_Main.java
/**
  *  主程序入口
 * @ClassName: SiCongPlaneFight_Main
 * @Description: TODO
 * @author GatesMa
 * @date 2018年12月3日
 * @Email: [email protected]
 */
public class SiCongPlaneFight_Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new MyGame();
	}

}

  1. Stone.java
/**
  * 障碍物类 - 思聪头像
 * @ClassName: Stone
 * @Description: TODO
 * @author GatesMa
 * @date 2018年12月3日
 * @Email: [email protected]
 */
import java.awt.*;
import java.util.Random;
public class Stone {
	int x, y;
	Image i;
	Plane p;
	int live;
	boolean destory = false;
	
	public Stone(Image i, Plane p,int live) {
		this.x = getRandom();
		this.y = 0;
		this.i = i;
		this.p = p;
		this.live = live;
	}
	
	int getRandom() {
		Random r = new Random();
		return r.nextInt(550) + 20;
	}
	
	void draw(Graphics g) {
		g.drawImage(i, x, y, 50, 50, null);
		ifdestory();
		movegetclose();
	}
	
	private void movegetclose() {
		y += 2;
	}
	
	void ifdestory() {
		if(live <= 0) {
			destory = true;
			MyGame.score++;
		}
		if(this.y > Table.height) {
			destory = true;
		}
	}
}

  1. Table.java

/**
  *  窗口大小存储 
 * @ClassName: Table
 * @Description: TODO
 * @author GatesMa
 * @date 2018年12月3日
 * @Email: [email protected]
 */
public class Table {
	public static double table=10; 
	public static int size=50;//飞机和炮弹大小
	public static int width=600;//窗口的大小
	public static int height=700;
}

  1. Window.java
/**
  *  失败窗口 - 重开游戏或者退出
 * @ClassName: Window
 * @Description: TODO
 * @author GatesMa
 * @date 2018年12月3日
 * @Email: [email protected]
 */
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
public class Window extends JFrame{
	private JTextField t1;
	private JButton btn1;
	private JButton btn2;
	boolean ificansee = true;
	
	Window(){
		super("这么快就结束了???");
		setBounds((400 + Table.width), (Table.height / 4), 500, 100);
		setVisible(ificansee);
		t1 = new JTextField("是否重新开始?");
		t1.setEnabled(false);
		
		btn1 = new JButton("重新开始");
		btn2 = new JButton("退出");
		btn1.setBounds(31, 13, 13, 13);
		btn2.setBounds(31, 13, 13, 13);
		
		Container c1 = getContentPane();
		c1.setLayout(new FlowLayout());
		//c1.add(t1);
		c1.add(btn1);
		c1.add(btn2);
		addListener();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	private void addListener() {
		btn1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				MyGame.replay = true;
				dispose();
			}
		});
		
		btn2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
		});
	}
}