Java用awt编写飞机躲避子弹小游戏(含资源)

效果图:
游戏
结束

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

 
public class GameUtil {//调用图片

    public static Image getImage(String path) {
        BufferedImage bi = null;
        try {
            URL u = GameUtil.class.getClassLoader().getResource(path);
            bi = ImageIO.read(u);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bi;
    }
}

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;

public class GameObject {
	Image img; //该物体对应的图片对象
    double x,y;    //该物体的坐标
    int speed; //该物体的运行速度
    int width,height;  //该物体所在矩形区域的宽度和高度
     
    public void drawMySelf(Graphics  g){//绘制对象
        g.drawImage(img, (int)x, (int)y, null);
    }
     
    public GameObject(Image img, double x, double y) {
        this.img = img;
        this.x = x;
        this.y = y;
        if(img!=null){//存入飞机图片的宽高
            this.width = img.getWidth(null);
            this.height = img.getHeight(null);
        }
    }
     
    public GameObject() {
    }
    public Rectangle getRect(){//返回物体对应矩形区域,便于后续在碰撞检测中使用
        return  new Rectangle((int)x,(int) y, width, height);
    }  
}


import java.awt.Graphics;
import java.awt.Image;

public class Plane extends GameObject {
	boolean left, up, right, down;
    boolean live = true;
    
    public Plane(Image img, double x, double y, int speed) {
        super(img, x, y);
        this.speed = speed;
    }
 

    public void drawMySelf(Graphics g) {
    	if(live) {
    		super.drawMySelf(g);
    		if (left&&x>=0) {
    			x -= speed;
    		}
    		if (right&&x<=480-width) {
    			x += speed;
    		}
    		if (up&&y>=0) {
    			y -= speed;
    		}
    		if (down&&y<=700-height) {
    			y += speed;
    		}
    	}// 根据按键方向,算出飞机新的坐标
    }
 
}
import java.awt.Color;
import java.awt.Graphics;

public class Bullet extends GameObject{
	double degree;
    
    public Bullet(){
        degree = Math.random()*Math.PI*2;
        x = 200;
        y = 200;
        width = 10;
        height = 10;
        speed = 3;
    }
     
    public void draw(Graphics g){
        Color c = g.getColor();//对象g的状态保存好
        g.setColor(Color.yellow);
        g.fillOval((int)x, (int)y, width, height);//画出子弹     
        x += speed*Math.cos(degree);
        y += speed*Math.sin(degree); //炮弹沿着任意角度飞行
        if(y>700-height||y<30){
            degree = -degree;
        }
        if(x<0||x>480-width){
            degree = Math.PI-degree;
        }//如果碰到边界,子弹反弹回来
        g.setColor(c);//变回以前的颜色
    }
}

import java.awt.Graphics;
import java.awt.Image;

public class Explode {//画出爆炸效果
	double x,y;
    static Image[] imgs= new Image[4];
    static {
    	for(int i=0;i<4;i++){
            imgs[i] = GameUtil.getImage("picture/b"+(i+1)+".png");
    }
    }
     
    public void draw(Graphics g){
        for(int i=0;i<4;i++){
            g.drawImage(imgs[i], (int)x, (int)y, null);
        }
    }
     
    public Explode(double x,double y){
        this.x = x;
        this.y = y;
    }
}

import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Date;

public class GameFrame extends Frame {
	Image bgImg = GameUtil.getImage("picture/bg.png");
	Image planeImg = GameUtil.getImage("picture/plane.png");//将背景图片与飞机图片定义为成员变量
	Plane plane=new Plane(planeImg,200,550,3);//创建飞机对象
	 ArrayList<Bullet>  bulletList = new ArrayList<Bullet>();//创建子弹对象
	 Explode explode;//创建爆炸对象
	 Date startTime = new Date();    //游戏起始时刻
	 Date endTime;  //游戏结束时刻
	
    public GameFrame(String title){
		super(title);
		init();
	}

    public void paint(Graphics g) {  //画出整个窗口及内部内容。被系统自动调用
    	Color c = g.getColor();
        g.drawImage(bgImg, 0, 0, null);
        plane.drawMySelf(g);
        for(int i=0;i<bulletList.size();i++){
        	Bullet b =  bulletList.get(i);
            b.draw(g);//画出容器中所有的子弹
            if(b.getRect().intersects(plane.getRect())){//碰撞检测
                plane.live = false;   //飞机爆炸,停止显示
                explode = new Explode(plane.x,plane.y);
                explode.draw(g);
            }
        }
        if(!plane.live){
            endTime = new Date();
            int time = (int)((endTime.getTime()-startTime.getTime())/1000);
            g.setColor(Color.red);
            Font f = new Font("宋体",Font.BOLD,50);
            g.setFont(f);
            g.drawString("时间:"+time+"秒",120,260);//显示存活时间
            g.setColor(c);
        }
    }
    public void init() {
    	this.setBounds(300,50,480,730);
    	new Restart(this);//增加重新开始按钮
    	this.setVisible(true);
    	new PlaneThread(this);//启动线程
        for(int i=0;i<50;i++){//生成50个子弹
        	Bullet b = new Bullet();
        	bulletList.add(b);
        }
        this.requestFocus();//改变焦点
    	addKeyListener(new KeyListener(plane));//键盘监听
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});//关闭游戏
    }
    private Image offScreenImage = null;//双缓冲解决闪烁
    
    public void update(Graphics g) {
        if(offScreenImage == null)
            offScreenImage = this.createImage(480,700);
         
        Graphics gOff = offScreenImage.getGraphics();
        paint(gOff);
        g.drawImage(offScreenImage, 0, 0, null);
}
}
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class KeyListener extends KeyAdapter {//监听上下左右键
	Plane plane;
	 public KeyListener(Plane plane) {
		this.plane = plane;
	}

	public void keyPressed(KeyEvent e) {
		    switch (e.getKeyCode()) {
	        case KeyEvent.VK_LEFT:
	            plane.left = true;
	            break;
	        case KeyEvent.VK_UP:
	            plane.up = true;
	            break;
	        case KeyEvent.VK_RIGHT:
	            plane.right = true;
	            break;
	        case KeyEvent.VK_DOWN:
	            plane.down = true;
	            break;
	        default:
	            break;
	        }
     }

     public void keyReleased(KeyEvent e) {
    	 switch (e.getKeyCode()) {
         case KeyEvent.VK_LEFT:
             plane.left = false;
             break;
         case KeyEvent.VK_UP:
             plane.up = false;
             break;
         case KeyEvent.VK_RIGHT:
             plane.right = false;
             break;
         case KeyEvent.VK_DOWN:
             plane.down = false;
             break;
         default:
             break;
         }
     }
}
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Restart {
	GameFrame gf;

	public Restart(GameFrame gf) {
		this.gf = gf;
		Button restart=new Button("重新开始");
		gf.add(restart, BorderLayout.SOUTH);//加入按钮
		restart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gf.dispose();
				new GameFrame("躲避子弹") ;//重新开始游戏
			}	
		});
	}
}

public class PlaneThread extends Thread {
	GameFrame gf;

	public PlaneThread(GameFrame gf) {
		this.gf = gf;
		start();
	}//引入对象并启动线程

	public void run() {
		while(gf.plane.live) {
			gf.repaint();//重画
			try {
				this.sleep(40);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
}

public class Game {

	public static void main(String[] args) {
		new GameFrame("躲避子弹") ;//启动游戏
	}

}

图片:
在这里插入图片描述
b1
b2
b3
b4
plane
bg

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wslxc00

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值