JAVA飞机大战/躲避炮弹 1.1
- MyGameFrame类
- GameObject类
- GameUtil类
- Plane类
- Shell类
- Time类
- Constant类
游戏效果
增加了加速减速,免伤功能(1.1);
MyGameFrame类
```java
package com.wenchi666.plane;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MyGameFrame extends Frame{
Image planeImg = GameUtil.getImage("images/plane.png");
Image bgImg = GameUtil.getImage("images/bg.jpg");
//static int count = 0;
//初始化飞机
Plane p1 = new Plane(planeImg,(int)(Math.random()*Constant.GAME_WIDTH),(int)(Math.random()*Constant.GAME_HEIGHT),5);
Time t =new Time();Time unH=new Time();
Shell []shells = new Shell[50];
Explode explode;//explode=null;
public void paint(Graphics g) {
//绘制背景
g.drawImage(bgImg, 0, 0,Constant.GAME_WIDTH,Constant.GAME_HEIGHT, null);
// g.drawImage(planeImg,c,100,22,33,null);
// System.out.println("调用paint,重画窗口,次数:"+(count++));
//绘制时间
t.paint(p1,g);
//绘制飞机
p1.drawMyself(g);
//绘制炮弹
for(int i=0;i=5)
p1.unhurt=false;
System.out.println(unH.getS());
}
}
public static void main(String[] args) {
MyGameFrame gameFrame=new MyGameFrame();
gameFrame.launchFrame();
}
}
GameObject类
package com.wenchi666.plane;
import java.awt.*;
public class GameObject {
Image img;
double x,y;
int speed;
int width ,height;
public GameObject(){
}
public void drawMyself(Graphics g){
g.drawImage(img,(int)x,(int)y,null);
}
public GameObject(Image img,double x,double y){
this.img=img;
this.x=x;
this.y=y;
if(img==null){
this.width = img.getWidth(null);
this.height=img.getHeight(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,int speed){
this.img=img;
this.x=x;
this.y=y;
this.speed=speed;
this.height=img.getHeight(null);
this.width=img.getWidth(null);
}
public Rectangle getRect(){
return new Rectangle((int)x,(int)y,width,height);
}
}
GameUtil类
package com.wenchi666.plane;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.nio.Buffer;
public class GameUtil {
private GameUtil(){
}
public static Image getImage(String path){
BufferedImage img = null;
URL u = GameUtil.class.getClassLoader().getResource(path);
try {
img = ImageIO.read((u));
} catch (IOException e) {
e.printStackTrace();
}
return img;
}
public static void main(String[] args) {
Image img = GameUtil.getImage("images/plane.png");
System.out.println(img);
}
}
Plane类
package com.wenchi666.plane;
import java.awt.*;
import java.awt.event.KeyEvent;
public class Plane extends GameObject {
boolean left,right,up,down;
boolean live=true,unhurt=false;
Time t;
private Graphics gr;
public void addDirection(KeyEvent e) {
System.out.println("anxia"+e.getKeyCode());
switch (e.getKeyCode()){
case KeyEvent.VK_LEFT:left=true;break;
case KeyEvent.VK_RIGHT:right=true;break;
case KeyEvent.VK_UP:up=true;break;
case KeyEvent.VK_DOWN:down=true;break;
}
}
public void minusDirection(KeyEvent e){
System.out.println("taiqi"+e.getKeyCode()+speed+" "+this.unhurt);
switch (e.getKeyCode()){
case KeyEvent.VK_LEFT:left=false;break;
case KeyEvent.VK_RIGHT:right=false;break;
case KeyEvent.VK_UP:up=false;break;
case KeyEvent.VK_DOWN:down=false;break;
}
}
public void changeSpeed(KeyEvent e){
if(e.getKeyCode()==81) speed++;
if(e.getKeyCode()==87) speed--;
}
public Time unHurt(KeyEvent e){
if(unhurt==false)
t=new Time();
if(e.getKeyCode()== 32)
unhurt=true;
return t;
}
@Override
public void drawMyself(Graphics g){
if(live==true) {
super.drawMyself(g);
if (left) {
x -= speed;
}
if (right) {
x += speed;
}
if (up) {
y -= speed;
}
if (down) {
y += speed;
}
}
}
public Plane(Image img,double x,double y,int speed){
super(img,x,y,speed);
Time t=new Time();
}
public Plane(Image img,double x,double y,int speed,int width,int height){
super(img,x,y,speed,width,height);
Time t=new Time();
}
}
Shell类
```java
package com.wenchi666.plane;
import java.awt.*;
public class Shell extends GameObject{
double degree;
int basicSpeed=4;
public Shell(){
x = 333;
y = 333;
degree = Math.random()*Math.PI*2;
width=8;
height=8;
speed = basicSpeed;
}
public void drawMyself(Graphics g,int time){
Color c = Color.yellow;
speed = 3 + time/6;
if(time>=30) {
c=new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255));
speed = 5 + time/10;
}
Color t=g.getColor();
g.setColor(c);
g.fillOval((int)x,(int)y,width,height);
x += speed*Math.cos(degree);
y += speed*Math.sin(degree);
int bian=10;
if(y>=Constant.GAME_HEIGHT-height-bian||y<=40)
degree = -degree;
if(x<=0+bian||x>=Constant.GAME_WIDTH-width-bian)
degree = Math.PI-degree;
g.setColor(t);
}
}
# Explod类
```java
package com.wenchi666.plane;
import java.awt.*;
public class Explode {
int 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 c=0;
public void drawMyself(Graphics g){
if(c<16){
g.drawImage(imgs[c],x,y,null);
c++;
//炸的慢一点
// try {
// Thread.sleep(50);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
}
}
public Explode(int x, int y) {
this.x = x;
this.y = y;
}
}
Time类
package com.wenchi666.plane;
import java.awt.*;
import java.util.Date;
public class Time {
Date start = new Date();
Date end;
long period = 0;
public int getS(){
return (int)(System.currentTimeMillis() - start.getTime())/1000;
}
public void paint (Plane p,Graphics g){
Color c = g.getColor();
g.setColor(Color.GREEN);
if(p.live){
period = (System.currentTimeMillis() - start.getTime())/1000;
g.drawString("坚持 "+period,55,55);
}
else{
if(end==null){
end = new Date();
period = (end.getTime() - start.getTime());
}
g.setColor(Color.RED);
g.setFont(new Font("微软雅黑",Font.BOLD,30));
if(period/1000<=10)
g.drawString("共坚持了 "+period/1000+" 秒 \"实在是菜(评级1)\"",Constant.GAME_HEIGHT/3,Constant.GAME_WIDTH/3);
else if(period/1000>=10&&period/1000<=20)
g.drawString("共坚持了 "+period/1000+" 秒 \"一般般吧(评级2)\"",Constant.GAME_HEIGHT/3,Constant.GAME_WIDTH/3);
else if(period/1000>=20&&period/1000<=25)
g.drawString("共坚持了 "+period/1000+" 秒 \"还行?(评级3)\"",Constant.GAME_HEIGHT/3,Constant.GAME_WIDTH/3);
else if(period/1000>=25&&period/1000<=30)
g.drawString("共坚持了 "+period/1000+" 秒\"哎呦不错呦(评级4)\"",Constant.GAME_HEIGHT/3,Constant.GAME_WIDTH/3);
else if(period/1000>=30&&period/1000<=35)
g.drawString("共坚持了 "+period/1000+" 秒\"哇塞,这么强?(评级5)\"",Constant.GAME_HEIGHT/3,Constant.GAME_WIDTH/3);
else if(period/1000>=35&&period/1000<=60)
g.drawString("共坚持了 "+period/1000+" 秒\"?????你是神仙吧!!简直不要太强!(评级6)\"",Constant.GAME_HEIGHT/3,Constant.GAME_WIDTH/3);
else if(period/1000>=60)
g.drawString("共坚持了 "+period/1000+" 秒\"已达到最高评级!!!!!!(评级7)\"",Constant.GAME_HEIGHT/3,Constant.GAME_WIDTH/3);
g.setColor(c);
}
}
public Time(){
}
}
Constant类
package com.wenchi666.plane;
public class Constant {
public static final int GAME_WIDTH = 1200;
public static final int GAME_HEIGHT = 800;
}
关于角度的解释
免伤状态下的飞机,此版本还未添加此功能
这是十六张爆炸的图片
图片文件关系如下
整体的关系如下