主窗口类:
package Game;
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.sql.Date;
import javax.swing.JFrame;
public class MyGameFram extends JFrame{
Image planeImg = GameUtil.getImage("images/plan.jpg");
Image bg = GameUtil.getImage("images/bg.jpg");
Image ex = GameUtil.getImage("images/explode.jpg");
Plane plane = new Plane(planeImg, 250, 250);
Shell[] shells = new Shell[50];
Date starTime = new Date(System.currentTimeMillis());
Date endTime;
long period = 0;
public void paint(Graphics g){
g.drawImage(bg, 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;
g.drawImage(ex, (int)plane.x, (int)plane.y, null);
endTime = new Date(System.currentTimeMillis());
period = (endTime.getTime() - starTime.getTime())/1000;
}
if(!plane.live){
g.setColor(Color.red);
Font f = new Font("宋体",Font.BOLD,30);
g.setFont(f);
g.drawString("存活时间:" + period + "秒", (int)plane.x, (int)plane.y);
for(int i1=0; i1<2000000000; i1++){
}
System.exit(0);
}
}
}
class PaintTread extends Thread{
@Override
public void run() {
while(true){
repaint();
try{
Thread.sleep(40);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
class KeyMonitor extends KeyAdapter{
public void keyPressed(KeyEvent e) {
plane.addDirection(e);
}
public void keyReleased(KeyEvent e) {
plane.minDirection(e);
}
}
public void launchFrame(){
this.setTitle("会程作品");
this.setVisible(true);
this.setSize(Constant.GAME_HEIGHT,Constant.GAME_WIDTH);
this.setLocation(300,100);
this.addWindowListener(new WindowAdapter(){
public void windowClosed(WindowEvent e){
System.exit(0);
}
});
new PaintTread().start();
addKeyListener(new KeyMonitor());
for(int i=0; i<shells.length; i++){
shells[i] = new Shell();
}
}
public static void main(String[] args){
MyGameFram f = new MyGameFram();
f.launchFrame();
}
private Image offScreenImage = null;
public void update(Graphics g){
if(offScreenImage==null){
offScreenImage = this.createImage(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);
}
Graphics gOff = offScreenImage.getGraphics();
paint(gOff);
g.drawImage(offScreenImage, 0, 0, null);
}
}
常量类:
package Game;
public class Constant {
public static final int GAME_WIDTH = 500;
public static final int GAME_HEIGHT = 500;
}
所有物体的父类:
package 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) {
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 Game;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.IIOException;
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 Game;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
public class Plane extends GameObject{
boolean live = true;
boolean left, up, right, down;
public void drawSelf(Graphics g){
if(!live){
System.out.println("飞机与炮弹发生碰撞了");
}
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, int x, int y){
this.img = img;
this.x = x;
this.y = y;
this.speed = 6;
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;
}
}
public void minDirection(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;
}
}
}
炮弹类:
package 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.setColor(c);
}
}
运行界面:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/503d7f5e3711c4a3d6a21e3c41c53cb2.png)
死亡界面:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4340bfcb1cc12c94f88ef0382adcc0c7.png)