package com.test.shoot; |
import java.util.Random; |
public class Airplane extends Flyer { |
private int speed= 2 ; |
private int score= 5 ; |
public int getScore() { |
return score; |
} |
|
public Airplane() { |
image=ShootGame.airplane; |
width=image.getWidth(); |
height=image.getHeight(); |
y=-height; |
Random r= new Random(); |
x=r.nextInt(ShootGame.WIDTH-width); |
} |
@Override |
public void step() { |
// TODO Auto-generated method stub |
y+=speed; |
} |
@Override |
public boolean outOfBounds() { |
// TODO Auto-generated method stub |
return y>ShootGame.HEIGHT; |
} |
} |
package com.test.shoot; |
import java.util.Random; |
public class Bee extends Flyer{ |
public static final int DOUBLE_FIRE= 0 ; |
public static final int LIFE= 1 ; |
private int xspeed= 1 ; |
private int yspeed= 2 ; |
private int awardType; |
public int getAwardType() { |
return awardType; |
} |
|
public Bee() { |
image=ShootGame.bee; |
width=image.getWidth(); |
height=image.getHeight(); |
y=-height; |
Random r= new Random(); |
x=r.nextInt(ShootGame.WIDTH-width); |
awardType=r.nextInt( 2 ); |
} |
|
@Override |
public void step() { |
x+=xspeed; |
y+=yspeed; |
if (x< 0 ||x>(ShootGame.WIDTH-width)) { |
xspeed*=- 1 ; |
} |
} |
@Override |
public boolean outOfBounds() { |
return y>ShootGame.HEIGHT; |
} |
} |
package com.test.shoot; |
public class Bullet extends Flyer { |
private int speed= 5 ; |
|
public Bullet( int x, int y) { |
image=ShootGame.bullet; |
width=image.getWidth(); |
height=image.getHeight(); |
this .x=x; |
this .y=y; |
} |
@Override |
public void step() { |
y-=speed; |
} |
@Override |
public boolean outOfBounds() { |
return y+height< 0 ; |
} |
} |
package com.test.shoot; |
import java.awt.image.BufferedImage; |
public abstract class Flyer { |
protected int x; |
protected int y; |
protected int width; |
protected int height; |
protected BufferedImage image; |
|
public abstract void step(); |
|
public abstract boolean outOfBounds(); |
|
public static boolean bang(Flyer f1,Flyer f2) { |
//求中心点 |
int f1x=f1.x+f1.width/ 2 ; |
int f1y=f1.y+f1.height/ 2 ; |
int f2x=f2.x+f2.width/ 2 ; |
int f2y=f2.y+f2.height/ 2 ; |
//检测碰撞 |
boolean H=Math.abs(f1x-f2x)<((f1.width+f2.width)/ 2 ); |
boolean V=Math.abs(f1y-f2y)<((f1.height+f2.height)/ 2 ); |
return H&V; |
} |
} |
package com.test.shoot; |
import java.util.Random; |
public class Hero extends Flyer { |
private int doubleFire; |
private int life; |
private int score; |
public int getLift() { |
return life; |
} |
public int getScore() { |
return score; |
} |
|
public Hero() { |
image=ShootGame.hero0; |
width=image.getWidth(); |
height=image.getHeight(); |
x= 150 ; |
y= 450 ; |
doubleFire= 0 ; |
life= 3 ; |
score= 0 ; |
} |
|
@Override |
public void step() { |
Random r= new Random(); |
if (r.nextInt( 2 )== 0 ) { |
image=ShootGame.hero0; |
} else { |
image=ShootGame.hero1; |
} |
} |
@Override |
public boolean outOfBounds() { |
return false ; |
} |
|
public void move( int x, int y) { |
this .x=x-width/ 2 ; |
this .y=y-height/ 2 ; |
} |
|
public void getScore_Award(Flyer f) { |
if (f instanceof Airplane) { |
score+=((Airplane)f).getScore(); |
} else { |
if (((Bee)f).getAwardType()==Bee.DOUBLE_FIRE) { |
doubleFire+= 100 ; |
} else { |
life+= 1 ; |
} |
} |
} |
|
public Bullet[] shoot() { |
Bullet[] bullets= null ; |
if (doubleFire!= 0 ) { |
bullets= new Bullet[ 2 ]; |
Bullet b1= new Bullet(x+width/ 4 ,y-ShootGame.bullet.getHeight()); |
Bullet b2= new Bullet(x+width* 3 / 4 ,y-ShootGame.bullet.getHeight()); |
bullets[ 0 ]=b1; |
bullets[ 1 ]=b2; |
doubleFire-= 2 ; |
} else { //单倍火力 |
bullets= new Bullet[ 1 ]; |
bullets[ 0 ]= new Bullet(x+width/ 2 ,y-ShootGame.bullet.getHeight()); |
} |
return bullets; |
} |
|
public boolean hit(Flyer f) { |
boolean r=Flyer.bang( this , f); |
if (r) { |
life--; |
doubleFire= 0 ; |
} |
return r; |
} |
} |
package com.test.shoot; |
import java.awt.Font; |
import java.awt.Graphics; |
import java.awt.event.MouseAdapter; |
import java.awt.event.MouseEvent; |
import java.awt.image.BufferedImage; |
import java.io.IOException; |
import java.util.Arrays; |
import java.util.Random; |
import java.util.Timer; |
import java.util.TimerTask; |
import javax.imageio.ImageIO; |
import javax.swing.JFrame; |
import javax.swing.JPanel; |
public class ShootGame extends JPanel { |
public static final int WIDTH= 400 ; |
public static final int HEIGHT= 700 ; |
|
public static BufferedImage background; |
public static BufferedImage start; |
public static BufferedImage airplane; |
public static BufferedImage bee; |
public static BufferedImage bullet; |
public static BufferedImage hero0; |
public static BufferedImage hero1; |
public static BufferedImage pause; |
public static BufferedImage gameover; |
|
public Hero hero= new Hero(); |
public Flyer[] flyers= {}; |
public Bullet[] bullets= {}; |
|
private int state=START; |
public static final int START= 0 ; |
public static final int RUNNING= 1 ; |
public static final int PAUSE= 2 ; |
public static final int GAME_OVER= 3 ; |
|
static { |
try { |
background=ImageIO.read(ShootGame. class .getResource( "background.png" )); |
airplane=ImageIO.read(ShootGame. class .getResource( "airplane.png" )); |
bee=ImageIO.read(ShootGame. class .getResource( "bee.png" )); |
bullet=ImageIO.read(ShootGame. class .getResource( "bullet.png" )); |
hero0=ImageIO.read(ShootGame. class .getResource( "hero0.png" )); |
hero1=ImageIO.read(ShootGame. class .getResource( "hero1.png" )); |
pause=ImageIO.read(ShootGame. class .getResource( "pause.png" )); |
gameover=ImageIO.read(ShootGame. class .getResource( "gameover.png" )); |
start=ImageIO.read(ShootGame. class .getResource( "start.png" )); |
|
} catch (IOException e) { |
e.printStackTrace(); |
} |
} |
|
public static void main(String[] args) { |
JFrame frame= new JFrame( "Fly" ); |
frame.setSize(WIDTH, HEIGHT); |
frame.setAlwaysOnTop( true ); |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
frame.setLocationRelativeTo( null ); |
ShootGame game= new ShootGame(); |
frame.add(game); |
frame.setVisible( true ); |
game.action(); |
} |
public void action() { |
/*游戏开始时,鼠标事件的监听*/ |
MouseAdapter l= new MouseAdapter() { |
@Override |
public void mouseMoved(MouseEvent e) { |
if (state==RUNNING) { |
int x=e.getX(); |
int y=e.getY(); |
hero.move(x, y); |
} |
} |
@Override |
public void mouseClicked(MouseEvent e) { |
if (state==START) { |
state=RUNNING; |
} else if (state==GAME_OVER) { |
state=START; |
flyers= new Flyer[ 0 ]; |
bullets= new Bullet[ 0 ]; |
hero= new Hero(); |
} |
} |
@Override |
public void mouseExited(MouseEvent e) { |
if (state==RUNNING) { |
state=PAUSE; |
} |
} |
@Override |
public void mouseEntered(MouseEvent e) { |
if (state==PAUSE) { |
state=RUNNING; |
} |
} |
|
|
}; |
this .addMouseMotionListener(l); |
this .addMouseListener(l); |
Timer timer= new Timer(); |
timer.schedule( new TimerTask() { |
private int runTimes= 0 ; |
@Override |
public void run() { |
if (state==RUNNING) { |
runTimes++; |
if (runTimes% 40 == 0 ) { |
nextOne(); |
} |
for ( int i= 0 ;i<flyers.length;i++) { |
flyers[i].step(); |
} |
if (runTimes% 15 == 0 ) { |
shoot(); |
} |
for ( int i= 0 ;i<bullets.length;i++) { |
bullets[i].step(); |
} |
hero.step(); |
bang(); |
hit(); |
outOfBounds(); |
} |
repaint(); |
} |
|
}, 5 , 5 ); |
} |
@Override |
public void paint(Graphics g) { |
|
g.drawImage(background, 0 , 0 , null ); //super.paint(arg0); |
|
paintHero(g); |
|
paintFlyers(g); |
|
paintBullets(g); |
|
paintScore_Life(g); |
|
if (state==START) { |
g.drawImage(start, 0 , 0 , null ); |
} else if (state==PAUSE){ |
g.drawImage(pause, 0 , 0 , null ); |
} else if (state==GAME_OVER) { |
g.drawImage(gameover, 0 , 0 , null ); |
} |
} |
public void paintHero(Graphics g) { |
g.drawImage(hero.image,hero.x,hero.y, null ); |
} |
public void paintFlyers(Graphics g) { |
for ( int i= 0 ;i<flyers.length;i++) { |
g.drawImage(flyers[i].image,flyers[i].x,flyers[i].y, null ); |
} |
} |
public void paintBullets(Graphics g) { |
for ( int i= 0 ;i<bullets.length;i++) { |
g.drawImage(bullets[i].image, bullets[i].x, bullets[i].y, null ); |
} |
} |
|
/** |
* 随机生成1个敌人 |
* 每生成一个新敌人,flyers数组都要扩容1 |
* 然后将新敌人放入数组最后一个元素 |
*/ |
public void nextOne() { |
Random r= new Random(); |
Flyer f= null ; |
if (r.nextInt( 20 )== 0 ) { //只有随机数取0时,才创建蜜蜂 |
f= new Bee(); |
} else { //其余都是敌机 |
f= new Airplane(); |
} |
flyers=Arrays.copyOf(flyers, flyers.length+ 1 ); |
flyers[flyers.length- 1 ]=f; |
} |
|
public void shoot() { |
Bullet[]newBullets=hero.shoot(); |
bullets=Arrays.copyOf(bullets, bullets.length+newBullets.length); |
System.arraycopy(newBullets, 0 , bullets, bullets.length-newBullets.length, newBullets.length); |
} |
|
public void bang() { |
for ( int i= 0 ;i<bullets.length;i++) { |
for ( int j= 0 ;j<flyers.length;j++) { |
if (Flyer.bang(bullets[i], flyers[j])) { |
hero.getScore_Award(flyers[j]); |
flyers[j]=flyers[flyers.length- 1 ]; |
flyers=Arrays.copyOf(flyers, flyers.length- 1 ); |
bullets[i]=bullets[bullets.length- 1 ]; |
bullets=Arrays.copyOf(bullets, bullets.length- 1 ); |
i--; |
break ; |
} |
} |
} |
} |
public void paintScore_Life(Graphics g) { |
int x= 10 ; |
int y= 15 ; |
Font font= new Font(Font.SANS_SERIF,Font.BOLD, 14 ); |
g.setFont(font); |
g.drawString( "分数:" +hero.getScore(), x, y); |
y+= 20 ; |
g.drawString( "生命:" +hero.getLift(), x, y); |
} |
|
public void outOfBounds() { |
Flyer[] Flives= new Flyer[flyers.length]; |
int index= 0 ; |
for ( int i= 0 ;i<flyers.length;i++) { |
if (!flyers[i].outOfBounds()) { |
Flives[index]=flyers[i]; |
index++; |
} |
} |
flyers=Arrays.copyOf(Flives, index); |
|
Bullet[] Blives= new Bullet[bullets.length]; |
index= 0 ; |
for ( int i= 0 ;i<bullets.length;i++) { |
if (!bullets[i].outOfBounds()) { |
Blives[index]=bullets[i]; |
index++; |
} |
} |
bullets=Arrays.copyOf(Blives, index); |
} |
public void hit() { |
Flyer[] lives= new Flyer[flyers.length]; |
int index= 0 ; |
for ( int i= 0 ;i<flyers.length;i++) { |
if (!hero.hit(flyers[i])) { |
lives[index]=flyers[i]; |
index++; |
} |
} |
if (hero.getLift()<= 0 ) { |
state=GAME_OVER; |
} |
flyers=Arrays.copyOf(lives, index); |
} |
} |
初级程序员
by: 一半忧伤 发表于:2018-03-06 11:55:48 顶(0) | 踩(0) 回复
请问相关图片呢 在哪里 能发出来吗
网友回复
回复Java小小煊 : 你百度一下“imageio.read的路径问题” 其实就是你 和你的mian 方法 在一个目录下就 ok 我试了一下 debug 不抱错了 但是 这几个图片都是代表什么啊
顶(0) 踩(0) 2018-03-26 11:51:18
回复评论