package cn.sxt.game;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import javax.swing.JFrame;
import javax.xml.crypto.Data;
public class MyGameFrame extends JFrame {
//导入图片
Image planeImg = GameUtil.getImage("images/plane.png");
Image bj = GameUtil.getImage("images/bg.jpg");
Plane plane = new Plane(planeImg,250,250);
//生成炮弹
Shell[] shells = new Shell[20];
//爆炸对象
Explode bao;
//起始时间对象
Date startTime = new Date();
Date endTime;
//计算时间
int period;
public void paint(Graphics g) {
//保存当前颜色
Color c = g.getColor();
g.drawImage(bj, 0, 0, null);
//飞机移动
plane.drawSelf(g);
//数组循环生成炮弹
for(int i=0;i<shells.length;i++) {
shells[i].draw(g);
boolean peng = shells[i].getRect().intersects(plane.getRect());
//判断是否发生碰撞
if(peng) {
//飞机状态变为爆炸
plane.live=false;
if(bao==null) {
//生成爆炸对象
bao = new Explode(plane.x, plane.y);
//记录结束时间
endTime = new Date();
//计算时长
period=(int)((endTime.getTime()-startTime.getTime())/1000);
}
//爆炸画面
bao.draw(g);
}
//判断飞机是否爆炸
if(!plane.live) {
g.setColor(Color.yellow);
//改变字体大小
Font f=new Font("宋体", Font.BOLD, 30);
g.setFont(f);
g.drawString("游戏时间:"+period+"秒", 200,300);
}
}
g.setColor(c);
}
//窗口重画
class PaintThread extends Thread{
@Override
public void run() {
while(true) {
repaint();
try {
Thread.sleep(40);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//键盘监听
class KeyMonitor extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
plane.addDirection(e);
}
@Override
public void keyReleased(KeyEvent e) {
plane.minusDirection(e);
}
}
public void launchFrame() {
this.setTitle("飞机大战");
//窗口设置为可见
this.setVisible(true);
this.setSize(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);
this.setLocation(300,300);
this.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent arg0) {
}
@Override
public void windowIconified(WindowEvent arg0) {
}
@Override
public void windowDeiconified(WindowEvent arg0) {
}
@Override
public void windowDeactivated(WindowEvent arg0) {
}
@Override
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
@Override
public void windowClosed(WindowEvent arg0) {
}
@Override
public void windowActivated(WindowEvent arg0) {
}
});
new PaintThread().start();
addKeyListener(new KeyMonitor());
for(int i=0;i<shells.length;i++) {
shells[i]=new Shell();
}
}
public static void main(String[] args) {
MyGameFrame f = new MyGameFrame();
f.launchFrame();
}
//双缓冲稳定画面闪烁
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);
}
}
package cn.sxt.game;
public class Constant {
public static final int GAME_WIDTH=570;
public static final int GAME_HEIGHT=600;
}
package cn.sxt.game;
import java.awt.Graphics;
import java.awt.Image;
public class Explode {
double x,y;
static Image[]imgs=new Image[16];
static {
for(int i=0;i<16;i++) {
imgs[i]= GameUtil.getImage("images/explode/e"+(i+1)+".gif");
imgs[i].getWidth(null);
}
}
int count;
public void draw(Graphics g) {
if(count<=15) {
g.drawImage(imgs[count], (int)x,(int) y, null);
count++;
}
}
public Explode(double x,double y) {
this.x=x;
this.y=y;
}
}
package cn.sxt.game;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
public class GameObject {
Image img;
double x,y;
int speed;
int width,height;
public void drawSelf(Graphics g) {
g.drawImage(img, (int)x, (int)y, null);
}
public GameObject(Image img, double x, double y, int speed, int width, int height) {
super();
this.img = img;
this.x = x;
this.y = y;
this.speed = speed;
this.width = width;
this.height = height;
}
public GameObject(Image img, double x, double y) {
super();
this.img = img;
this.x = x;
this.y = y;
}
public GameObject() {
}
public Rectangle getRect() {
return new Rectangle((int)x,(int)y,width,height);
}
}
package cn.sxt.game;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
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;
}
}
package cn.sxt.game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
public class Plane extends GameObject{
boolean left,up,right,down,fire;
boolean live = true;
public void drawSelf(Graphics g) {
if(live) {
g.drawImage(img, (int)x, (int)y, null);
if(left) {
x-=speed;
}
if(right) {
x+=speed;
}
if(up) {
y-=speed;
}
if(down) {
y+=speed;
}
}
}
public Plane(Image img,double x,double y) {
this.img=img;
this.x=x;
this.y=y;
this.speed=10;
this.width=img.getWidth(null);
this.height=img.getHeight(null);
}
public void addDirection(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_LEFT:
left = true;
break;
case KeyEvent.VK_UP:
up = true;
break;
case KeyEvent.VK_RIGHT:
right = true;
break;
case KeyEvent.VK_DOWN:
down = true;
break;
case KeyEvent.VK_W:
fire = true;
break;
}
}
public void minusDirection(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_LEFT:
left = false;
break;
case KeyEvent.VK_UP:
up = false;
break;
case KeyEvent.VK_RIGHT:
right = false;
break;
case KeyEvent.VK_DOWN:
down = false;
break;
}
}
private void drawfire(Graphics g) {
if(fire) {
Color c = g.getColor();
g.setColor(Color.red);
for(int i=(int)y;i<height;i+=10) {
g.fillOval((int)x, (int)y, width, height);
}
g.getColor();
}
}
}
package cn.sxt.game;
import java.awt.Color;
import java.awt.Graphics;
//炮弹类
public class Shell extends GameObject{
double degree;
public Shell() {
x=200;
y=200;
width=10;
height=10;
speed=3;
degree = Math.random()*Math.PI*2;
}
public void draw(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.GAME_WIDTH-width) {
degree=Math.PI-degree;
}
if(y<30||y>Constant.GAME_HEIGHT-height) {
degree=-degree;
}
g.getColor();
}
}