1 做出来的结果
2 Game 主类
package yxx;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Date;
import javax.swing.JFrame;
public class Game extends Frame{
Image PlanImg =GameUtil.getImage("Images/su.png");
Image b1=GameUtil.getImage("Images/s1.png");
Plan plan1=new Plan(PlanImg,100,300);
Shell shell=new Shell();
Shell[] shells=new Shell[10];
Explode bao;
Date startTime=new Date();
Date endTime;
int period;
//双缓冲技术
private Image offScreenImage = null;
//private int i;
public void update(Graphics g) {
if(offScreenImage == null)
offScreenImage = this.createImage(Constant.GAME_WIDH,Constant.GAME_HEIGHT);
Graphics gOff = offScreenImage.getGraphics();
paint(gOff);
g.drawImage(offScreenImage, 0, 0, null);
}
public void launchFrame(){
this.setTitle("晓晓");
setSize(Constant.GAME_WIDH,Constant.GAME_HEIGHT); //画板的大小
setLocation(100,100);//画板放置的位置
setVisible(true);//窗口的可见
new PaintThread().start();// 启动重画线程
addKeyListener(new KeyMonitor());//给窗口增加键盘的监听
//初始化50个炮弹
for(int i=0;i<shells.length;i++){
shells[i]=new Shell();
}
//窗口关闭,程序结束
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);}
});
}
//自动调用
public void paint(Graphics g) {//g 相当于画笔
Color c=g.getColor();
g.drawImage(b1, 0, 0, null);
plan1.drawSelf(g);
shell.draw(g);
for(int i=0;i<shells.length;i++){
shells[i].draw(g);
boolean peng=shells[i].getRect().intersects(plan1.getRect());
if(peng){
plan1.live=false;
if(bao==null){
bao=new Explode(plan1.x,plan1.y);
endTime =new Date();
period=(int) ((endTime.getTime()-startTime.getTime())/1000);
}
bao.draw(g);
}
if(!plan1.live){
g.setColor(Color.red);
Font f=new Font("宋体",Font.BOLD,50);
g.setFont(f);
g.drawString(+period+"秒",(int)plan1.x,(int)plan1.y);
}
}
g.setColor(c);
}
class PaintThread extends Thread {
public void run() {
while (true) {
System.out.println();
repaint();
try {
Thread.sleep(40);// 1s=1000ms
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Game f=new Game();
f.launchFrame();
}
}
//定义为内部类,可以方便的使用外部类的普通属性
class KeyMonitor extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
System.out.println(+e.getKeyCode());
Plan.addDirection(e);
}
@Override
public void keyReleased(KeyEvent e) {
System.out.println(+e.getKeyCode());
Plan.minusDirection(e);
}
}
3 游戏的父类
package yxx;
/**
* genlei//youxi物体的父类
*/
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
public class GameObject {
Image img;
double x,y;
int speed = 3;
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);
}
}
4 加载图片
package yxx;
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) {
URL u = GameUtil.class.getClassLoader().getResource(path);
BufferedImage b = null;
try {
b = ImageIO.read(u);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
}
6飞机类
package yxx;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import yxx.GameUtil;
public class Plan extends GameObject {
static boolean left;
static boolean up;
static boolean right;
static boolean down;
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;
}
}
else{
}
}
public Plan(Image img,double x,double y){
this.img=img;
this.x=x;
this.y=y;
this.speed=3;
this.width=img.getWidth(null);
this.height=img.getHeight(null);
}
//按下哪个键增加方向
public static 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;
default:
break;
}
}
public static 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;
default:
break;
}
}
}
7 shell
package yxx;
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(y>Constant.GAME_HEIGHT -height-9||y<30){//边框和标题栏 250 窗口高度 40边框 80边框+标题栏 30 球的直径
degree=-degree;//和x轴对称
}
//x=x+1; he 加十不一样
if(x<9||x>Constant.GAME_WIDH-width-9){//边框和标题栏 250 窗口高度 40
degree=Math.PI-degree;
}
g.setColor(c);
}
}
8 爆炸类
package yxx;
import java.awt.Graphics;
import java.awt.Image;
import yxx.GameUtil;
/*
* 爆炸类
*/
public class Explode {
double x,y;//爆炸的位置
static Image[] imgs = new Image[16];//不希望反复加载
//静态初始化块,可以挨个进行加载
static {
for(int i=0;i<8;i++){
imgs[i] = GameUtil.getImage("Images/baozha/10"+(i+1)+".png");
imgs[i].getWidth(null);
}
}
int count;
public void draw(Graphics g){
if(count<=8){
g.drawImage(imgs[count], (int)x, (int)y, null);
count++;
}
}
public Explode(double x,double y){
this.x = x;
this.y = y;
}
}
9 常量类
package yxx;
public class Constant {
public static final int GAME_WIDH=450;
public static final int GAME_HEIGHT=600;
}
10 所需要的图片