/**
* 窗口类 飞机游戏
* @author 小帆敲代码
*
*/
public class MyGameFrame extends JFrame{
Plane plane=new Plane(GetImage.getImage("image/plane0.jpg"),Constant.PLANE_START_X,Constant.PLANE_START_Y,Constant.PLANE_SPEED);
ArrayList<Shell> shells=new ArrayList();
Exploer exploer;//爆炸
boolean flag=true;//控制画面停止
Timer timer=new Timer();//计时器
//初始化窗口
public void init() {
this.setTitle("小帆敲代码--飞机游戏");
this.setVisible(true);
this.setSize(Constant.FRAME_WIDTH, Constant.FRAME_HEITH);
this.setLocation(300,100);
timer.startTimer();//开始计时
//内部类实现重画和动画
class FrameThread extends Thread{
public void run() {
while(flag) {
repaint();
try {
Thread.sleep(40);//形成动画
} catch (InterruptedException e) {
}
}
}
}
new FrameThread().start();
//添加炮弹
for(int i=0;i<Constant.SHELL_NUM;i++) {
shells.add(new Shell());
}
class KeyMinitor extends KeyAdapter{//键盘监听 内部实现类
//重写按下
public void keyPressed(java.awt.event.KeyEvent e) {
plane.addMove(e);
};
//松开
public void keyReleased(java.awt.event.KeyEvent e) {
plane.miniMove(e);
};
}
this.addWindowListener(new WindowAdapter(){//设置窗口监听
public void windowClosing(WindowEvent e) {
System.exit(0);//退出
}
});
this.addKeyListener(new KeyMinitor());//键盘监听
}
public void paint(Graphics g) {
Color c=g.getColor();
g.drawImage(GetImage.getImage("image/bg.jpg"), 0,0, null);//bg
plane.drawSelf(g);//飞机
for(int i=0;i<shells.size();i++) {
if(plane.live)
{
shells.get(i).drawSelfMoving(g);
//检测碰撞
boolean boom=shells.get(i).getRect().intersects(plane.getRect());
if(boom) {
plane.live=false;//飞机结束
exploer=new Exploer(plane.x,plane.y);
timer.running=false;//计时停止
}
}
else {
//游戏结束场景 炮弹静止 飞机爆炸
shells.get(i).drawSelfStop(g);
exploer.draw(g);
}
}
//显示时间
g.setColor(Color.WHITE);
Font f=new Font("宋体",Font.BOLD,50);
g.setFont(f);
g.drawString(timer.time+"秒",30,100);
g.setColor(c);
}
public static void main(String[] args) {
MyGameFrame f=new MyGameFrame();
f.init();
}
* 窗口类 飞机游戏
* @author 小帆敲代码
*
*/
public class MyGameFrame extends JFrame{
Plane plane=new Plane(GetImage.getImage("image/plane0.jpg"),Constant.PLANE_START_X,Constant.PLANE_START_Y,Constant.PLANE_SPEED);
ArrayList<Shell> shells=new ArrayList();
Exploer exploer;//爆炸
boolean flag=true;//控制画面停止
Timer timer=new Timer();//计时器
//初始化窗口
public void init() {
this.setTitle("小帆敲代码--飞机游戏");
this.setVisible(true);
this.setSize(Constant.FRAME_WIDTH, Constant.FRAME_HEITH);
this.setLocation(300,100);
timer.startTimer();//开始计时
//内部类实现重画和动画
class FrameThread extends Thread{
public void run() {
while(flag) {
repaint();
try {
Thread.sleep(40);//形成动画
} catch (InterruptedException e) {
}
}
}
}
new FrameThread().start();
//添加炮弹
for(int i=0;i<Constant.SHELL_NUM;i++) {
shells.add(new Shell());
}
class KeyMinitor extends KeyAdapter{//键盘监听 内部实现类
//重写按下
public void keyPressed(java.awt.event.KeyEvent e) {
plane.addMove(e);
};
//松开
public void keyReleased(java.awt.event.KeyEvent e) {
plane.miniMove(e);
};
}
this.addWindowListener(new WindowAdapter(){//设置窗口监听
public void windowClosing(WindowEvent e) {
System.exit(0);//退出
}
});
this.addKeyListener(new KeyMinitor());//键盘监听
}
public void paint(Graphics g) {
Color c=g.getColor();
g.drawImage(GetImage.getImage("image/bg.jpg"), 0,0, null);//bg
plane.drawSelf(g);//飞机
for(int i=0;i<shells.size();i++) {
if(plane.live)
{
shells.get(i).drawSelfMoving(g);
//检测碰撞
boolean boom=shells.get(i).getRect().intersects(plane.getRect());
if(boom) {
plane.live=false;//飞机结束
exploer=new Exploer(plane.x,plane.y);
timer.running=false;//计时停止
}
}
else {
//游戏结束场景 炮弹静止 飞机爆炸
shells.get(i).drawSelfStop(g);
exploer.draw(g);
}
}
//显示时间
g.setColor(Color.WHITE);
Font f=new Font("宋体",Font.BOLD,50);
g.setFont(f);
g.drawString(timer.time+"秒",30,100);
g.setColor(c);
}
public static void main(String[] args) {
MyGameFrame f=new MyGameFrame();
f.init();
}
}
/**
* 计时器
* @author 小帆敲代码
*
*/
public class Timer {
* 计时器
* @author 小帆敲代码
*
*/
public class Timer {
int time;
boolean running=true;
public void startTimer() {
new Thread(()-> {
while (running) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
time++;
}
}).start();
}
}
boolean running=true;
public void startTimer() {
new Thread(()-> {
while (running) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
time++;
}
}).start();
}
}
/**
* 炮弹
* @author 小帆敲代码
*
*/
public class Shell extends GameObject{
double degree;//角度
public Shell() {
//初始位置
x=Math.random()*Constant.FRAME_WIDTH;
y=Math.random()*(Constant.FRAME_HEITH-200);
//宽高速度
width=Constant.SHELL_WIDTH;
height=Constant.SHELL_HEIGTH;
speed=Constant.PLANE_SPEED;
//随机角度
degree=Math.random()*Math.PI*2;
}
//炮弹移动
public void drawSelfMoving(Graphics g) {
Color c=g.getColor();
g.setColor(Color.YELLOW);
g.fillOval((int)x, (int)y, width, height);
//移动的坐标变化
x+=speed*Math.cos(degree);
y+=speed*Math.sin(degree);
//碰到墙壁的角度变化
if(x<0||x>Constant.FRAME_WIDTH-width) {
degree=Math.PI-degree;
}
if(y<30||y>Constant.FRAME_HEITH-height) {
degree=-degree;
}
g.setColor(c);
}
//炮弹静止
public void drawSelfStop(Graphics g) {
Color c=g.getColor();
g.setColor(Color.YELLOW);
g.fillOval((int)x, (int)y, width, height);
g.setColor(c);
}
}
* 炮弹
* @author 小帆敲代码
*
*/
public class Shell extends GameObject{
double degree;//角度
public Shell() {
//初始位置
x=Math.random()*Constant.FRAME_WIDTH;
y=Math.random()*(Constant.FRAME_HEITH-200);
//宽高速度
width=Constant.SHELL_WIDTH;
height=Constant.SHELL_HEIGTH;
speed=Constant.PLANE_SPEED;
//随机角度
degree=Math.random()*Math.PI*2;
}
//炮弹移动
public void drawSelfMoving(Graphics g) {
Color c=g.getColor();
g.setColor(Color.YELLOW);
g.fillOval((int)x, (int)y, width, height);
//移动的坐标变化
x+=speed*Math.cos(degree);
y+=speed*Math.sin(degree);
//碰到墙壁的角度变化
if(x<0||x>Constant.FRAME_WIDTH-width) {
degree=Math.PI-degree;
}
if(y<30||y>Constant.FRAME_HEITH-height) {
degree=-degree;
}
g.setColor(c);
}
//炮弹静止
public void drawSelfStop(Graphics g) {
Color c=g.getColor();
g.setColor(Color.YELLOW);
g.fillOval((int)x, (int)y, width, height);
g.setColor(c);
}
}
/**
* 飞机类
* @author 小帆敲代码
*
*/
public class Plane extends GameObject{
boolean left,right,up,down;//使用可以同时向两个方向行驶
boolean live=true;//游戏状态
//飞机显示
public void drawSelf(Graphics g) {
if(live) {
if(up) {
y-=speed;
}
if(down) {
y+=speed;
}
if(right) {
x+=speed;
}
if(left) {
x-=speed;
}
g.drawImage(img,(int)x,(int)y,null);
}
}
//WASD建控制移动
//如果直接对位置进行修改 没办法沿两个方向同时进行
public void addMove(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_W:
up=true;break;
case KeyEvent.VK_S:
down=true;break;
case KeyEvent.VK_D:
right=true;break;
case KeyEvent.VK_A:
left=true;break;
}
}
public void miniMove(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_W:
up=false;break;
case KeyEvent.VK_S:
down=false;break;
case KeyEvent.VK_D:
right=false;break;
case KeyEvent.VK_A:
left=false;break;
}
}
public Plane(Image img,double x,double y,int speed) {
super(x,y,speed,img);
height=img.getHeight(null);
width=img.getWidth(null);
}
public Plane() {
}
* 飞机类
* @author 小帆敲代码
*
*/
public class Plane extends GameObject{
boolean left,right,up,down;//使用可以同时向两个方向行驶
boolean live=true;//游戏状态
//飞机显示
public void drawSelf(Graphics g) {
if(live) {
if(up) {
y-=speed;
}
if(down) {
y+=speed;
}
if(right) {
x+=speed;
}
if(left) {
x-=speed;
}
g.drawImage(img,(int)x,(int)y,null);
}
}
//WASD建控制移动
//如果直接对位置进行修改 没办法沿两个方向同时进行
public void addMove(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_W:
up=true;break;
case KeyEvent.VK_S:
down=true;break;
case KeyEvent.VK_D:
right=true;break;
case KeyEvent.VK_A:
left=true;break;
}
}
public void miniMove(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_W:
up=false;break;
case KeyEvent.VK_S:
down=false;break;
case KeyEvent.VK_D:
right=false;break;
case KeyEvent.VK_A:
left=false;break;
}
}
public Plane(Image img,double x,double y,int speed) {
super(x,y,speed,img);
height=img.getHeight(null);
width=img.getWidth(null);
}
public Plane() {
}
}
/**
* 工具类 用来获得图片
* @author 小帆敲代码
*
*/
public class GetImage {
private GetImage() {//构造函数私有化
}
public static Image getImage(String path) {
BufferedImage bi=null;
try {
URL u=GetImage.class.getClassLoader().getResource(path);
bi=ImageIO.read(u);
} catch (IOException e) {
e.printStackTrace();
}
return bi;
}
}
* 工具类 用来获得图片
* @author 小帆敲代码
*
*/
public class GetImage {
private GetImage() {//构造函数私有化
}
public static Image getImage(String path) {
BufferedImage bi=null;
try {
URL u=GetImage.class.getClassLoader().getResource(path);
bi=ImageIO.read(u);
} catch (IOException e) {
e.printStackTrace();
}
return bi;
}
}
/**
* 物体的根类
* @author 小帆敲代码
*
*/
* 物体的根类
* @author 小帆敲代码
*
*/
public class GameObject {
//坐标
double x,y;
//长宽
int width,height;
//运动速度
int speed;
//图片
Image img;
//显示自己
public void drawSelf(Graphics g) {
g.drawImage(img,(int)x,(int)y,null);
}
//得到矩形用于碰撞检测
public Rectangle getRect() {
return new Rectangle((int)x,(int)y,width,height);
}
public GameObject(double x, double y, int speed, Image img) {
super();
this.x = x;
this.y = y;
this.speed = speed;
this.img = img;
}
public GameObject() {
}
}
//坐标
double x,y;
//长宽
int width,height;
//运动速度
int speed;
//图片
Image img;
//显示自己
public void drawSelf(Graphics g) {
g.drawImage(img,(int)x,(int)y,null);
}
//得到矩形用于碰撞检测
public Rectangle getRect() {
return new Rectangle((int)x,(int)y,width,height);
}
public GameObject(double x, double y, int speed, Image img) {
super();
this.x = x;
this.y = y;
this.speed = speed;
this.img = img;
}
public GameObject() {
}
}
/**
* 爆炸类
* @author 小帆敲代码
*
*/
public class Exploer {
* 爆炸类
* @author 小帆敲代码
*
*/
public class Exploer {
double x,y;
//动画爆炸 静态加载
static Image[] images=new Image[14];
static {
for(int i=0;i<14;i++) {
images[i]=GetImage.getImage("image/exploer/boom"+(i+1)+".gif");
images[i].getWidth(null);
}
}
int count;
public void draw(Graphics g) {//爆炸场景
new Thread(()-> {
for(int i=0;i<13;i++)
{
if(count<=13) {
g.drawImage(images[count], (int)x,(int) y, null);
count++;
}
try {
Thread.sleep(180);
} catch (InterruptedException e) {
}
}
}).start();
}
//
// int begingsize=50;
// public void draw(Graphics g) {
// Color c=g.getColor();
// g.setColor(Color.RED);
// g.fillOval((int)x,(int) y, begingsize, begingsize);
// if(begingsize>0)
// {
// begingsize--;
// }
//
// g.setColor(c);
// }
public Exploer(double x, double y) {
this.x = x;
this.y = y;
}
}
//动画爆炸 静态加载
static Image[] images=new Image[14];
static {
for(int i=0;i<14;i++) {
images[i]=GetImage.getImage("image/exploer/boom"+(i+1)+".gif");
images[i].getWidth(null);
}
}
int count;
public void draw(Graphics g) {//爆炸场景
new Thread(()-> {
for(int i=0;i<13;i++)
{
if(count<=13) {
g.drawImage(images[count], (int)x,(int) y, null);
count++;
}
try {
Thread.sleep(180);
} catch (InterruptedException e) {
}
}
}).start();
}
//
// int begingsize=50;
// public void draw(Graphics g) {
// Color c=g.getColor();
// g.setColor(Color.RED);
// g.fillOval((int)x,(int) y, begingsize, begingsize);
// if(begingsize>0)
// {
// begingsize--;
// }
//
// g.setColor(c);
// }
public Exploer(double x, double y) {
this.x = x;
this.y = y;
}
}
/**
* 常量
* @author 小帆敲代码
*
*/
public class Constant {
* 常量
* @author 小帆敲代码
*
*/
public class Constant {
public static final int FRAME_WIDTH=500;
public static final int FRAME_HEITH=500;
public static final int SHELL_NUM=25;
public static final int SHELL_WIDTH=15;
public static final int SHELL_HEIGTH=15;
public static final int SHELL_SPLEED=4;
public static final int PLANE_START_X=150;
public static final int PLANE_START_Y=350;
public static final int PLANE_SPEED=4;
}
public static final int FRAME_HEITH=500;
public static final int SHELL_NUM=25;
public static final int SHELL_WIDTH=15;
public static final int SHELL_HEIGTH=15;
public static final int SHELL_SPLEED=4;
public static final int PLANE_START_X=150;
public static final int PLANE_START_Y=350;
public static final int PLANE_SPEED=4;
}