【实例简介】
【实例截图】
【核心代码】
package com.plane.client;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import com.plane.core.Plane;
import com.plane.utils.FrameUtil;
import com.plane.utils.MusicUtil;
import com.plane.utils.MyCanvas;
public class PlaneClient extends FrameUtil {
MyCanvas canvas;
@Override
public void initFrame() {
canvas = new MyCanvas();
//把面板添加到窗口里
this.add(canvas);
//初始化窗口
super.initFrame();
//添加键盘监听
this.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {
canvas.plane.keyReleased(e);
canvas.keyReleased(e);
}
@Override
public void keyPressed(KeyEvent e) {
canvas.plane.keyPressed(e);
canvas.keyPressed(e);
}
});
//启动定时器去刷新面板
new Timer().start();
new Timer_fire().start();
//启动背景音乐播放
new MusicUtil("src/sound/backMusic.mp3").start();
}
class Timer extends Thread {
@Override
public void run() {
while(true) {
canvas.repaint();
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Timer_fire extends Thread{
public void run() {
while (canvas.plane.isFire) {
try {
canvas.plane.isFire = false;
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
new PlaneClient().initFrame();
}
}