package com.bjsxt.plane09;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* <p>Title: 游戏主窗口(飞机大战1.0版)</p>
* <p>Description: 尚学堂</p>
* @author xiaoding
* @date Apr 16, 2020
* @version 1.0
*/
public class MyGameFrame extends Frame{
//调用GameUtil类的静态方法,把图片的地址传过去
Image feiJi = GameUtil.getImage("images/plane.png");
Image bj = GameUtil.getImage("images/bg.jpg");
//创建飞机类
Plane plane = new Plane(feiJi,100,100,9);
//创建炮弹数组
Shell[] shell = new Shell[50];
//爆炸类
Explode explode;
//画出窗口
@Override
public void paint(Graphics g) { //g当做是一支画笔
g.drawImage(bj,0,0,500,500,null);
plane.drawMyself(g);
for (int i = 0;i<shell.length;i++) {
shell[i].drawMySelf(g);
//炮弹碰撞飞机检测
boolean peng = shell[i].getRect().intersects(plane.getRect());
if (peng) {
// System.err.println("飞机挂了");
plane.live = false;
if (explode == null) {
explode = new Explode(plane.x,plane.y);
}
explode.drawMySelf(g);
}
}
}
//初始化窗口
public void launchFrame() {
//游戏名称
this.setTitle("飞机大战-尚学堂");
setVisible(true); //窗口是否可见
setSize(Constant.GAME_WIHTH,Constant.GAME_HEIGHT); //窗口大小
setLocation(400,400); //窗口打开位置
//增加关闭窗口的动作
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0); //正常退出程序
}
});
//启动重画窗口内部类方法
new PaintThread().start();
//启动键盘监听
this.addKeyListener(new KeyMonitor());
//循环赋值炮弹数组
for (int i = 0;i<shell.length;i++) {
shell[i] = new Shell();
}
}
/*
* 定义一个重画窗口的线程类
* 定义内部类是类为方便直接使用窗口类的相关方法
*/
class PaintThread extends Thread {
public void run() {
while(true) {
repaint(); //内部类可以直接使用外部类的成员!
try {
Thread.sleep(50); //1s=1000ms (1000 / 50) = 20
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/*
* 创建一个键盘控制内部类
* 实现键盘的监听处理
*/
class KeyMonitor extends KeyAdapter {
//当键盘按下时
@Override
public void keyPressed(KeyEvent e) {
plane.addDirection(e);
}
//当键盘抬起时
@Override
public void keyReleased(KeyEvent e) {
plane.minusDirection(e);
}
}
//解决屏幕闪烁缓冲技术
private Image offScreenImage = null;
public void update(Graphics g) {
if(offScreenImage == null)
//这是游戏窗口的宽度和高度
offScreenImage = this.createImage(Constant.GAME_WIHTH,Constant.GAME_HEIGHT);
Graphics gOff = offScreenImage.getGraphics();
paint(gOff);
g.drawImage(offScreenImage, 0, 0, null);
}
//main主方法,程序执行入口
public static void main(String[] args) {
MyGameFrame gameFrame = new MyGameFrame();
gameFrame.launchFrame();
}
}
/**
* <p>Title: Explode.java</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2018</p>
* <p>Company: www.baidudu.com</p>
* @author xiaoding
* @date Apr 18, 2020
* @version 1.0
*/
package com.bjsxt.plane09;
import java.awt.Graphics;
import java.awt.Image;
import com.bjsxt.plane03.GameUtil;
/**
* <p>Title: 爆炸类</p>
* <p>Description: 尚学堂</p>
* @author xiaoding
* @date Apr 18, 2020
* @version 1.0
*/
public class Explode {
//图片的路径
static Image[] img = new Image[16];
double x,y; //坐标
int count; //计数器
public Explode() {}
public Explode(double x,double y) {
this.x = x;
this.y = y;
}
static {
for (int i = 0;i<img.length;i++) {
img[i] = GameUtil.getImage("images/explode/e" +(i+1)+".gif");
img[i].getWidth(null);
}
}
public void drawMySelf(Graphics g) {
if (count<15) {
g.drawImage(img[count],(int)x,(int)y,null);
count++;
}
}
}
报错提示:
