皮皮安实战项目(飞机大战)一

写在前面

开始根据AWT和Swing写一个微信飞机大战的小项目。

今日所学

Test

public class Test {
    public static void main(String[] args) {
        MyGameFrame f = new MyGameFrame();
        f.launchFrame();
    }
}

MyGameFrame类

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class MyGameFrame extends Frame {
    //飞机的坐标的初始位置,初始值(200,200)
    int planeX = 200;
    int planeY = 200;
    static int count = 0;

    public void launchFrame(){
        setTitle("pipian");
        //窗口是否显示
        setVisible(true);
        //窗口大小
        setSize(500,500);
        //窗口位置
        setLocation(300,300);
        //关闭事件
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        new PaintThread().start();
    }

    //将背景图片与飞机图片定义为成员变量
    Image bgImg = GameUtil.getImage("image\\20190310203441206.png");
    Image planeImg = GameUtil.getImage("image\\20190310203706565.png");
   // Plane plane = new Plane(planeImg,300,300);

    //画出整个窗口和内部内容,为系统所自动调用
    public void paint(Graphics g)
    {
      /*  //画一个位置(100,50)到(400,400)的直线
        g.drawLine(100,50,400,400);
        //画一个位置(100,50)宽高为300矩形
        g.drawRect(100,50,300,300);
        //以上面的矩形为基础画椭圆
        g.drawOval(100,50,300,300);
       */
        g.drawImage(bgImg,0,0,null);
       // System.out.println("调用print,重画窗口,次数:"+(count++));
       // plane.drawMySelf(g);
        //移动位置
       // g.drawImage(planeImg,200,200,null);
        g.drawImage(planeImg,planeX,planeY,null);
        planeX+=3;
    }
    class PaintThread extends Thread{
        public void run(){
            while (true)
            {
                repaint();
                try{
                    Thread.sleep(40);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        }
    }

    private Image offScreenImage = null;
    public void update(Graphics g){
        if (offScreenImage == null)
            offScreenImage = this.createImage(500,500);
        Graphics gOff = offScreenImage.getGraphics();
        paint(gOff);
        g.drawImage(offScreenImage,0,0,null);
    }
}

GameUtil类

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

public class GameUtil {
    private 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;
    }
}

GameObject类

import java.awt.*;

public class GameObject {
    Image img;//该物体对应的图片对象
    double x,y;//该物体的坐标
    int spead;//该物体的运行速度
    int width,height;//该物体所有在矩形区域的宽度和高度


/*
绘制对象
 */

    public void drawMySelf(Graphics g){
        g.drawImage(img,(int)x,(int)y,null);
    }

    public GameObject() {

    }

    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(Image img, double x, double y, int spead, int width, int height) {
        this.img = img;
        this.x = x;
        this.y = y;
        this.spead = spead;
        this.width = width;
        this.height = height;
    }

    //返回物体对应的矩形区域,便于后续在碰撞检测中使用
    public Rectangle getRect(){
        return new Rectangle((int)x,(int)y,width,height);
    }
}


Plane类

import java.awt.*;

public class Plane extends GameObject{
    public void drawMySelf(Graphics g){
        super.drawMySelf(g);
        this.x+=3;
    }
    public Plane(Image img,double x,double y){
        super(img,x,y);
    }
}

效果图
在这里插入图片描述
总结:由于能力有限,时间有限,今天只能做到让飞机平行移动起来,主要运用了以下操作:

	1.使用AWT画出游戏主窗口。
	2.图形和文本绘制。
	3.ImageIO实现图片加载技术。
	4.多线程和内部类实现动画效果。(多线程还不会)
	5.双缓冲技术解决闪烁问题。
	6.GameObject类设计
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值