用java实现一个飞机小游戏 飞机躲避子弹 显示挑战时间
gameobject
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package cn.sxt.game;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.ImageObserver;
public class GameObject {
Image img;
double x;
double y;
int speed;
int width;
int height;
public void drawSelf(Graphics g) {
g.drawImage(this.img, (int)this.x, (int)this.y, (ImageObserver)null);
}
public GameObject(Image img, double x, double y, int speed, int width, int height) {
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) {
this.img = img;
this.x = x;
this.y = y;
}
public GameObject() {
}
public Rectangle getRect() {
return new Rectangle((int)this.x, (int)this.y, this.width, this.height);
}
}
mygamefrom
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package cn.sxt.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.awt.image.ImageObserver;
import java.util.Date;
public class MyGameFrame extends Frame {
Image planeImg = GameUtil.getImage("images/plane.png");
Image bg = GameUtil.getImage("images/bg.jpg");
Plane plane;
Shell[] shells;
Explode bao;
Date startTime;
Date endTime;
int period;
private Image offScreenImage;
public MyGameFrame() {
this.plane = new Plane(this.planeImg, 250.0D, 250.0D);
this.shells = new Shell[50];
this.startTime = new Date();
this.offScreenImage = null;
}
public void paint(Graphics g) {
Color c = g.getColor();
g.drawImage(this.bg, 0, 0, (ImageObserver)null);
this.plane.drawSelf(g);
for(int i = 0; i < this.shells.length; ++i) {
this.shells[i].draw(g);
boolean peng = this.shells[i].getRect().intersects(this.plane.getRect());
if (peng) {
this.plane.live = false;
if (this.bao == null) {
this.bao = new Explode(this.plane.x, this.plane.y);
this.endTime = new Date();
this.period = (int)((this.endTime.getTime() - this.startTime.getTime()) / 1000L);
}
this.bao.draw(g);
}
if (!this.plane.live) {
g.setColor(Color.red);
Font f = new Font("宋体", 1, 50);
g.setFont(f);
g.drawString("时间:" + this.period + "秒", (int)this.plane.x, (int)this.plane.y);
}
}
g.setColor(c);
}
public void launchFrame() {
this.setTitle("北明有鱼");
this.setVisible(true);
this.setSize(500, 500);
this.setLocation(300, 300);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
(new MyGameFrame.PaintThread()).start();
this.addKeyListener(new MyGameFrame.KeyMonitor());
for(int i = 0; i < this.shells.length; ++i) {
this.shells[i] = new Shell();
}
}
public static void main(String[] args) {
MyGameFrame f = new MyGameFrame();
f.launchFrame();
}
public void update(Graphics g) {
if (this.offScreenImage == null) {
this.offScreenImage = this.createImage(500, 500);
}
Graphics gOff = this.offScreenImage.getGraphics();
this.paint(gOff);
g.drawImage(this.offScreenImage, 0, 0, (ImageObserver)null);
}
class KeyMonitor extends KeyAdapter {
KeyMonitor() {
}
public void keyPressed(KeyEvent e) {
MyGameFrame.this.plane.addDirection(e);
}
public void keyReleased(KeyEvent e) {
MyGameFrame.this.plane.minusDirection(e);
}
}
class PaintThread extends Thread {
PaintThread() {
}
public void run() {
while(true) {
MyGameFrame.this.repaint();
try {
Thread.sleep(40L);
} catch (InterruptedException var2) {
var2.printStackTrace();
}
}
}
}
}
plane
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package cn.sxt.game;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.image.ImageObserver;
public class Plane extends GameObject {
boolean left;
boolean up;
boolean right;
boolean down;
boolean live = true;
public void drawSelf(Graphics g) {
if (this.live) {
g.drawImage(this.img, (int)this.x, (int)this.y, (ImageObserver)null);
if (this.left) {
this.x -= (double)this.speed;
}
if (this.right) {
this.x += (double)this.speed;
}
if (this.up) {
this.y -= (double)this.speed;
}
if (this.down) {
this.y += (double)this.speed;
}
}
}
public Plane(Image img, double x, double y) {
this.img = img;
this.x = x;
this.y = y;
this.speed = 3;
this.width = img.getWidth((ImageObserver)null);
this.height = img.getHeight((ImageObserver)null);
}
public void addDirection(KeyEvent e) {
switch(e.getKeyCode()) {
case 37:
this.left = true;
break;
case 38:
this.up = true;
break;
case 39:
this.right = true;
break;
case 40:
this.down = true;
}
}
public void minusDirection(KeyEvent e) {
switch(e.getKeyCode()) {
case 37:
this.left = false;
break;
case 38:
this.up = false;
break;
case 39:
this.right = false;
break;
case 40:
this.down = false;
}
}
}
shell
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package cn.sxt.game;
import java.awt.Color;
import java.awt.Graphics;
public class Shell extends GameObject {
double degree;
public Shell() {
this.x = 200.0D;
this.y = 200.0D;
this.width = 10;
this.height = 10;
this.speed = 3;
this.degree = Math.random() * 3.141592653589793D * 2.0D;
}
public void draw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.YELLOW);
g.fillOval((int)this.x, (int)this.y, this.width, this.height);
this.x += (double)this.speed * Math.cos(this.degree);
this.y += (double)this.speed * Math.sin(this.degree);
if (this.x < 0.0D || this.x > (double)(500 - this.width)) {
this.degree = 3.141592653589793D - this.degree;
}
if (this.y < 30.0D || this.y > (double)(500 - this.height)) {
this.degree = -this.degree;
}
g.setColor(c);
}
}
实现效果
子弹和飞机可以用自己的图片进行替换 还有爆炸效果 代码非常简单