首页 > 代码库 > FlyingGame

FlyingGame

最近在学习java,觉得很有意思,以前也对软件方面有兴趣,刚好练练手,后期会继续修改这个游戏。


  1 package MyShoot;
  2 
  3 import java.awt.Graphics;
  4 import java.awt.event.MouseAdapter;
  5 import java.awt.event.MouseEvent;
  6 import java.awt.image.BufferedImage;
  7 import java.util.Arrays;
  8 import java.util.Random;
  9 import java.util.Timer;
 10 import java.util.TimerTask;
 11 
 12 import javax.imageio.ImageIO;
 13 import javax.swing.JFrame;
 14 import javax.swing.JPanel;
 15 
 16 public class ShootGame extends JPanel{
 17     public static final int WIDTH=490;
 18     public static final int HEIGHT=680;
 19     public static BufferedImage background;
 20     public static BufferedImage start;
 21     public static BufferedImage pause;
 22     public static BufferedImage gameover;
 23     public static BufferedImage hero0;
 24     public static BufferedImage hero1;
 25     public static BufferedImage airplane;
 26     public static BufferedImage bee;
 27     public static BufferedImage bullet;
 28     
 29     static {
 30         try{
 31             background=ImageIO.read(ShootGame.class.getResource("background.png"));
 32             start=ImageIO.read(ShootGame.class.getResource("start.png"));
 33             pause=ImageIO.read(ShootGame.class.getResource("pause.png"));
 34             gameover=ImageIO.read(ShootGame.class.getResource("gameover.png"));
 35             airplane=ImageIO.read(ShootGame.class.getResource("airplane.png"));
 36             bee=ImageIO.read(ShootGame.class.getResource("bee.png"));
 37             hero0=ImageIO.read(ShootGame.class.getResource("hero0.png"));
 38             hero1=ImageIO.read(ShootGame.class.getResource("hero1.png"));
 39             bullet=ImageIO.read(ShootGame.class.getResource("bullet.png"));
 40         }catch(Exception e){
 41             e.printStackTrace();
 42         }
 43     }
 44     
 45     int interval=10;
 46     public void launch(){
 47         MouseAdapter l=new MouseAdapter(){
 48             /** 重写mouseMoved()鼠标移动时间 */
 49             public void mouseMoved(MouseEvent e){
 50                 int x=e.getX();
 51                 int y=e.getY();
 52                 hero.moveTo(x, y);
 53             }
 54         };    
 55             this.addMouseListener(l); //处理鼠标操作事件
 56             this.addMouseMotionListener(l); //处理鼠标滑动事件
 57         Timer time=new Timer();
 58         time.schedule(new TimerTask(){
 59             public void run(){    //每10ms走一次
 60                 enteredAction();   //敌人入场
 61                 shootAction();
 62                 stepAction();
 63                 outOfBoundsAction();
 64                 repaint();
 65             }
 66         },interval,interval);
 67     }
 68     
 69     
 70     public void outOfBoundsAction(){
 71         int index=0;
 72         FlyingObject[] flyingsLives=new FlyingObject[flyings.length];
 73         for(int i=0;i<flyings.length;i++){
 74             FlyingObject f=flyings[i];
 75             if(!f.outOfBounds()){
 76                 flyingsLives[index]=f;
 77                 index++;
 78             }
 79         }
 80         flyings=Arrays.copyOf(flyingsLives,index);
 81         
 82         index=0;
 83         Bullet[] bulletsLives=new Bullet[bullets.length];
 84         for(int i=0;i<bullets.length;i++){
 85             Bullet b=bullets[i];
 86             if(!b.outOfBounds()){
 87                 bulletsLives[index]=b;
 88                 index++;
 89             }
 90         }
 91         bullets=Arrays.copyOf(bulletsLives,index);
 92     }
 93     
 94     public void stepAction(){
 95         hero.step();
 96         for(int i=0;i<flyings.length;i++){
 97             FlyingObject f=flyings[i];
 98             f.step();
 99         }
100         for(int i=0;i<bullets.length;i++){
101             Bullet b=bullets[i];
102             b.step();
103         }
104     }
105     
106     int shootIndex=0;
107     public void shootAction(){
108         shootIndex++;
109         if(shootIndex%10==0){
110             Bullet[] b=hero.shoot();
111             bullets=Arrays.copyOf(bullets,bullets.length+b.length);
112             System.arraycopy(b,0,bullets,bullets.length-b.length,b.length);
113         }
114     }
115     
116     int enteredIndex=0;
117     public void enteredAction(){
118         enteredIndex++;
119         if(enteredIndex%40==0){
120             FlyingObject f=nextOne();  //随机出现一个敌人对象
121             flyings=Arrays.copyOf(flyings,flyings.length+1); //扩容
122             flyings[flyings.length-1]=f;  //将敌人放入数组的最后一位
123         }
124     }
125     
126     public FlyingObject nextOne(){   //随机出现一个敌人对象,
127         Random rand=new Random();
128         int type=rand.nextInt(20);
129         if(type<5){
130             return new Bee();
131         }else{
132             return new Airplane();
133         }
134     }
135     
136     public void paint(Graphics g){
137         g.drawImage(background,0,0,null);
138         paintHero(g);
139         paintFlyingObject(g);
140         paintBullet(g);
141     }
142     
143     public void paintHero(Graphics g){
144         g.drawImage(hero.image,hero.x,hero.y,null);
145     }
146     
147     public void paintFlyingObject(Graphics g){
148         for(int i=0;i<flyings.length;i++){
149             FlyingObject f=flyings[i];
150             g.drawImage(f.image,f.x,f.y,null);
151         }
152     }
153     
154     public void paintBullet(Graphics g){
155         for(int i=0;i<bullets.length;i++){
156             Bullet b=bullets[i];
157             g.drawImage(b.image,b.x,b.y,null);
158         }
159     }
160     
161     
162     Hero hero=new Hero();
163     FlyingObject[] flyings={};
164     Bullet[] bullets={};
165     
166     public static void main(String[] args) {
167         JFrame frame=new JFrame("Flyings");
168         ShootGame game=new ShootGame();
169         frame.add(game);
170         frame.setSize(WIDTH, HEIGHT);
171         frame.setAlwaysOnTop(true);
172         frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
173         frame.setLocationRelativeTo(null);
174         frame.setVisible(true);
175         game.launch();
176     }
177 
178 }
179 
180 
181 import java.awt.image.BufferedImage;
182 
183 public abstract class FlyingObject {
184     
185     protected BufferedImage image;
186     protected int width;
187     protected int height;
188     protected int x;
189     protected int y;
190     
191     public abstract void step();
192     
193     public abstract boolean outOfBounds();
194 
195 }
196 
197 
198 public interface Award {
199     public int getType();
200 }
201 
202 public interface Enemy {
203     public int getScore();
204 }
205 
206 import java.util.Random;
207 
208 public class Airplane extends FlyingObject implements Enemy{
209     private int speed=3;
210     
211     Airplane(){
212         image=ShootGame.airplane;
213         width=image.getWidth();
214         height=image.getHeight();
215         Random rand=new Random();
216         x=rand.nextInt(ShootGame.WIDTH-this.width);
217         y=-height;
218     }
219     
220     public int getScore(){
221         return 5;
222     }
223     
224     public void step(){
225         this.y +=speed;
226     }
227     
228     public boolean outOfBounds(){
229         return this.y>=ShootGame.WIDTH;
230     }
231 }
232 
233 import java.util.Random;
234 
235 public class Bee extends FlyingObject implements Award{
236     
237     private int xSpeed=3;
238     private int ySpeed=3;
239     private int type;
240     
241     Bee(){
242         image=ShootGame.bee;
243         width=image.getWidth();
244         height=image.getHeight();
245         Random rand=new Random();
246         x=rand.nextInt(ShootGame.WIDTH-this.width);
247         y=-height;
248     }
249     
250     public int getType(){
251         return type;
252     }
253     
254     public void step(){
255         x +=xSpeed;
256         y +=ySpeed;
257         if(this.x>=ShootGame.WIDTH-this.width){
258             xSpeed=-3;
259         }
260         if(this.x<=0){
261             xSpeed=3;
262         }
263     }
264     
265     public boolean outOfBounds(){
266         return this.y>=ShootGame.WIDTH;
267     }
268 }
269 
270 public class Bullet extends FlyingObject{
271     
272     private int speed=3;
273     
274     Bullet(int x,int y){
275         image=ShootGame.bullet;
276         width=image.getWidth();
277         height=image.getHeight();
278         this.x=x;
279         this.y=y;
280     }
281     
282     public void step(){
283         this.y -=speed;
284     }
285     
286     public boolean outOfBounds(){
287         return this.y<=-height;
288     }
289 }
290 
291 import java.awt.image.BufferedImage;
292 
293 public class Hero extends FlyingObject{
294     private int life;
295     private int doubleFire;
296     private BufferedImage[] images;
297     private int index;
298     
299     Hero(){
300         image=ShootGame.hero0;
301         width=image.getWidth();
302         height=image.getHeight();
303         x=190;
304         y=500;
305         life=3;
306         doubleFire=0;
307         images=new BufferedImage[]{ShootGame.hero0,ShootGame.hero1};
308         index=0;
309     }
310     
311     public void step(){
312         image=images[index++/10%images.length];
313     }
314     
315     public Bullet[] shoot(){
316         int xStep=this.width/4;
317         int yStep=10;
318         if(doubleFire>0){
319             Bullet[] b=new Bullet[2];
320             b[0]=new Bullet(this.x+1*xStep,this.y-yStep);
321             b[1]=new Bullet(this.x+3*xStep,this.y-yStep);
322             return b;
323         }else{
324             Bullet[] b=new Bullet[1];
325             b[0]=new Bullet(this.x+2*xStep,this.y-yStep);
326             return b;
327         }
328     }
329     
330     public void moveTo(int x,int y){
331         this.x=x-width/2;
332         this.y=y-height/2;
333     }
334     
335     public boolean outOfBounds(){
336         return false;
337     }
338 }

 


 

FlyingGame