package com.bjsxt.plane;
/**
* 存放相关的常量
*/
public class Constant {
//游戏窗口的宽度
public static final int GAME_WIDTH = 500;
//游戏窗口的高度
public static final int GAME_HEIGHT = 500;
}
package com.bjsxt.plane;
import java.awt.*;
/**
* 游戏物体的分类
*/
public class GameObject {
Image img; //图片
double x, y; //物体的坐标
int planeX = 100; //物体移动的速度
int width, height; //物体的宽度和高度
public GameObject(Image img, double x, double y, int planeX, int width, int height) {
this.img = img;
this.x = x;
this.y = y;
this.planeX = planeX;
this.width = width;
this.height = height;
}
public void drawMyself(Graphics g) {
g.drawImage(img, (int) x, (int) y, width, height, null);
}
//所有的物体都是矩形,当你获得对应的矩形的时候,我们就可以做一些相关的判断操作!
public Rectangle getRec() {
return new Rectangle((int) x, (int) y, width, height);
}
}
package com.bjsxt.plane;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
//import java.imageio.ImageIO;
/**
* 游戏工具类
*/
public class GameUtil {
//构造器私有,防止别人创建本类的对象
private GameUtil() {
}
public static Image getImage(String path) { //images/plane.png
BufferedImage img = null;
URL u = GameUtil.class.getClassLoader().getResource(path);
try {
img = ImageIO.read(u);
} catch (IOException e) {
e.printStackTrace();
}
return img;
}
public static void main(String[] args) {
Image img = GameUtil.getImage("images/plane.png");
System.out.println(img);
}
}
package com.bjsxt.plane;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* 游戏主窗口
*/
public class MyGameFrame extends Frame {
Image planeImg = GameUtil.getImage("images/plane.png");
Image bg = GameUtil.getImage("images/bg.jpg");
static int count = 0;
GameObject plane2 = new GameObject(planeImg, 100, 100, 3, 22, 33);
GameObject plane3 = new GameObject(planeImg, 200, 100, 3, 22, 33);
@Override
public void paint(Graphics g) { //g当做是一支画笔
System.out.println("绘制窗口次数:" + count);
count++;
g.drawImage(bg, 0, 0, 500, 500, null);
plane2.drawMyself(g);
plane3.drawMyself(g);
}
//初始化窗口
public void launchFrame() {
this.setTitle("飞机大战-尚学堂");
setVisible(true); //窗口是否可见
setSize(Constant.GAME_WIDTH, Constant.GAME_HEIGHT); //窗口大小
setLocation(400, 400); //窗口打开的位置
//增加关闭窗口的动作
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0); //正常退出程序
}
});
new PaintThread().start(); //启动重画窗口的线程
}
/**
* 定义了一个重画窗口的线程类
* 定义成内部类是为了方便直接使用窗口类的相关方法
*/
class PaintThread extends Thread {
@Override
public void run() {
while (true) {
repaint(); //内部类可以直接使用外部类的成员!
try {
Thread.sleep(50); //1s=1000ms 1s画20次(20*50=1000)
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private Image offScreenImage = null;
public void update(Graphics g) {
if (offScreenImage == null)
offScreenImage = this.createImage(Constant.GAME_WIDTH, Constant.GAME_HEIGHT);//这是游戏窗口的宽度和高度
Graphics gOff = offScreenImage.getGraphics();
paint(gOff);
g.drawImage(offScreenImage, 0, 0, null);
}
public static void main(String[] args) {
MyGameFrame gameFrame = new MyGameFrame();
gameFrame.launchFrame();
}
}
![1605104266273216.png X}}][SRIF6YFL{`GA4[]TGV.png](/ueditor/php/upload/image/20201111/1605104266273216.png)