免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3505 | 回复: 0
打印 上一主题 下一主题

(java编程)一个打飞机的小游戏 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-05-21 13:38 |只看该作者 |倒序浏览
本帖最后由 mocow 于 2015-05-21 13:42 编辑
  1. package GameSubstance;

  2. import java.awt.Graphics;
  3. import java.awt.Image;
  4. import java.awt.Toolkit;
  5. import Tools.GameBox;
  6. public class EnemyBullet {
  7.     private int x,y;
  8.     private int bulletspeed=15;
  9.     private boolean live=true;

  10.     Image enemyplanebullet=GameBox.enemybullet;
  11.      
  12.     public EnemyBullet(int x,int y){
  13.         this.x=x;
  14.         this.y=y;
  15.     }
  16.     public void draw(Graphics g){
  17.         if(!live){
  18.             return;
  19.         }
  20.         y=y+bulletspeed;
  21.         g.drawImage(enemyplanebullet,x,y,GameBox.ENEMYBULLET_WIDTH,GameBox.ENEMYBULLET_HEIGHT,null);
  22.         if(y>GameBox.GAME_HEIGHT+GameBox.ENEMYBULLET_HEIGHT){
  23.             live=false;
  24.         }
  25.     }
  26.     public boolean isLive() {
  27.         return live;
  28.     }
  29.     public void setLive(boolean live) {
  30.         this.live = live;
  31.     }
  32.     public int getX() {
  33.         return x;
  34.     }
  35.     public int getY() {
  36.         return y;
  37.     }
  38.      
  39. }
复制代码
  1. package GameSubstance;
  2. import java.awt.Graphics;
  3. import java.awt.Image;
  4. import java.awt.Toolkit;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import java.util.Random;

  8. import Tools.GameBox;
  9. import View.StartFrame;

  10. public class EnemyPlane {
  11.     StartFrame sf;
  12.     private int xpeed=8;
  13.     private int yspeed=5;
  14.     private boolean live=true;
  15.     private boolean fired=true;
  16.     private int x,y=0;
  17.     private static int i=0;
  18.     private String dir;
  19.     Image enemy;
  20.     Random rand=new Random();
  21.     public EnemyPlane(StartFrame sf){
  22.         this.sf=sf;
  23.         if(i==0){
  24.             enemy=GameBox.enemy1;
  25.             i=1;
  26.         }else{
  27.             enemy=GameBox.enemy2;
  28.             i=0;
  29.         }
  30.         x=rand.nextInt(GameBox.GAME_WIDTH);
  31.         if(x%2==0){
  32.             dir="left";
  33.         }else{
  34.             dir="right";
  35.         }
  36.         if(x<0){
  37.             x=0;
  38.         }
  39.         if(x+GameBox.ENEMY_WIDTH>GameBox.GAME_WIDTH){
  40.             x=GameBox.GAME_WIDTH-GameBox.ENEMY_WIDTH;
  41.         }
  42.     }
  43.     public void draw(Graphics g){
  44.          
  45.         if(live){
  46.             g.drawImage(enemy,x,y,GameBox.ENEMY_WIDTH,GameBox.ENEMY_HEIGHT,null);
  47.             move();
  48.         }
  49.          
  50.          
  51.     }
  52.     private void move(){
  53.         y=y+xpeed;
  54.         int i=rand.nextInt(5)+2;
  55.         if(dir=="left"){
  56.             x=x-i;
  57.         }else{
  58.             x=x+i;
  59.         }
  60.         if(y>GameBox.GAME_HEIGHT+GameBox.ENEMY_HEIGHT){
  61.             live=false;
  62.         }
  63.         if(x<0){
  64.             x=0;
  65.             dir="right";
  66.         }
  67.         if(x+GameBox.ENEMY_WIDTH>GameBox.GAME_WIDTH){
  68.             x=GameBox.GAME_WIDTH-GameBox.ENEMY_WIDTH;
  69.             dir="left";
  70.         }
  71.          
  72.     }
  73.     public int getX() {
  74.         return x;
  75.     }
  76.     public int getY() {
  77.         return y;
  78.     }
  79.     public boolean isLive() {
  80.         return live;
  81.     }
  82.     public void setLive(boolean live) {
  83.         this.live = live;
  84.     }
  85.     public int getXpeed() {
  86.         return xpeed;
  87.     }
  88.     public void setXpeed(int xpeed) {
  89.         this.xpeed = xpeed;
  90.     }
  91.     public int getYspeed() {
  92.         return yspeed;
  93.     }
  94.     public void setYspeed(int yspeed) {
  95.         this.yspeed = yspeed;
  96.     }
  97.     public boolean isFired() {
  98.         return fired;
  99.     }
  100.     public void setFired(boolean fired) {
  101.         this.fired = fired;
  102.     }
  103. }
复制代码
  1. package GameSubstance;
  2. import java.awt.Graphics;
  3. import java.awt.Image;
  4. import java.awt.Toolkit;
  5. import Tools.GameBox;
  6. public class Explode {
  7.     private int x,y;
  8.     private boolean live=true;
  9.     private int count;
  10.     Image Explode1=GameBox.Explode1;
  11.     public Explode(int x,int y){
  12.         this.x=x;
  13.         this.y=y;
  14.     }
  15.     public void draw(Graphics g){
  16.         if(!live){
  17.             return;
  18.         }
  19.         g.drawImage(Explode1,x,y,GameBox.EXPLODE1_WIDTH,GameBox.EXPLODE1_HEIGHT,null);
  20.         if(count==8){
  21.             live=false;
  22.         }
  23.     }
  24.     public boolean isLive() {
  25.         return live;
  26.     }
  27.     public void setLive(boolean live) {
  28.         this.live = live;
  29.     }
  30.     public int getCount() {
  31.         return count;
  32.     }
  33.     public void setCount(int count) {
  34.         this.count = count;
  35.     }
  36. }
复制代码
  1. package GameSubstance;
  2. import Tools.GameBox;
  3. import View.StartFrame;
  4. import java.awt.Graphics;
  5. import java.awt.Image;
  6. import java.awt.Toolkit;
  7. import java.awt.event.KeyEvent;
  8. import javax.swing.ImageIcon;
  9. public class MyPlane {
  10.     private int x,y;
  11.     private static Image plane;
  12.     private Bullet  bullet;
  13.     private boolean dl=false,du=false,dr=false,dd=false;
  14.     public static final int MYPLANE_XSPEED=10;
  15.     public static final int MYPLANE_YSPEED=10;
  16.     private boolean live=true;
  17.     public MyPlane(int x,int y){
  18.         this.x=x;
  19.         this.y=y;
  20.     }
  21.     public void draw(Graphics g){
  22.         if(!live){
  23.             return;
  24.         }
  25.         g.drawImage(GameBox.plane,x,y,GameBox.PLANE_WIDTH,GameBox.PLANE_HEIGHT,null);
  26.         move();
  27.     }
  28.      
  29.     public void keyPressed(KeyEvent e) {
  30.         int key=e.getKeyCode();
  31.         switch(key){
  32.         case KeyEvent.VK_UP:
  33.             du=true;
  34.             break;
  35.         case KeyEvent.VK_DOWN:
  36.             dd=true;
  37.             break;
  38.         case KeyEvent.VK_LEFT:
  39.             dl=true;
  40.             break;
  41.         case KeyEvent.VK_RIGHT:
  42.             dr=true;
  43.             break;
  44.         }
  45.     }
  46.     public void keyReleased(KeyEvent e) {
  47.         int key=e.getKeyCode();
  48.         switch(key){
  49.         case KeyEvent.VK_UP:
  50.             du=false;
  51.             break;
  52.         case KeyEvent.VK_DOWN:
  53.             dd=false;
  54.             break;
  55.         case KeyEvent.VK_LEFT:
  56.             dl=false;
  57.             break;
  58.         case KeyEvent.VK_RIGHT:
  59.             dr=false;
  60.         }
  61.     }
  62.     private void move(){
  63.         if(du&&!dl&&!dd&&!dr){
  64.             y=y-MYPLANE_YSPEED;//上
  65.         }else if(dd&&!du&&!dl&&!dr){
  66.             y=y+MYPLANE_YSPEED;//下
  67.         }else if(!dd&&!du&&dl&&!dr){
  68.             x=x-MYPLANE_XSPEED;//左
  69.         }else if(!dd&&!du&&!dl&&dr){
  70.             x=x+MYPLANE_XSPEED;//右
  71.         }else if(!dd&&du&&dl&&!dr){
  72.             y=y-MYPLANE_YSPEED;//左上
  73.             x=x-MYPLANE_XSPEED;
  74.         }else if(du&&!dl&&!dd&&dr){
  75.             y=y-MYPLANE_YSPEED;//右上
  76.             x=x+MYPLANE_XSPEED;
  77.         }else if(dd&&!du&&dl&&!dr){
  78.             x=x-MYPLANE_XSPEED;//左下
  79.             y=y+MYPLANE_YSPEED;
  80.         }else if(dd&&!du&&!dl&&dr){
  81.             x=x+MYPLANE_XSPEED;//右下
  82.             y=y+MYPLANE_YSPEED;
  83.         }
  84.         if(x+GameBox.PLANE_WIDTH>GameBox.GAME_WIDTH){
  85.             x=GameBox.GAME_WIDTH-GameBox.PLANE_WIDTH;
  86.         }
  87.         if(x<0){
  88.             x=0;
  89.         }
  90.         if(y+GameBox.PLANE_HEIGHT*2>GameBox.GAME_HEIGHT){
  91.             y=GameBox.GAME_HEIGHT-GameBox.PLANE_HEIGHT*2;
  92.         }
  93.         if(y<30){
  94.             y=30;
  95.         }
  96.     }
  97.     public int getX() {
  98.         return x;
  99.     }
  100.     public int getY() {
  101.         return y;
  102.     }
  103.     public boolean isLive() {
  104.         return live;
  105.     }
  106.     public void setLive(boolean live) {
  107.         this.live = live;
  108.     }
  109. }
复制代码
  1. package Tools;
  2. import java.awt.Font;
  3. import java.awt.Image;
  4. import java.awt.Toolkit;
  5. import java.util.ArrayList;
  6. import java.util.List;

  7. import GameSubstance.EnemyBullet;
  8. import GameSubstance.Explode;
  9. public class GameBox {
  10.     public static int GAME_WIDTH=800;
  11.     public static int GAME_HEIGHT=600;
  12.     public static Font f1=new Font("oúìå",3,20);
  13.     public static Toolkit tk = Toolkit.getDefaultToolkit();
  14.     public static Image backgroundimage=tk.createImage("images\\±3¾°.jpg");
  15.     public static Image plane=tk.createImage("images\\Îò·½·é»ú.gif");
  16.     public static int PLANE_WIDTH=30;
  17.     public static int PLANE_HEIGHT=30;
  18.     public static Image Explode1=tk.createImage("images/±¬Õ¨.gif");
  19.     public static int EXPLODE1_WIDTH=30;
  20.     public static int EXPLODE1_HEIGHT=30;
  21.     public static Image bullet=tk.createImage("images\\×óμˉ.gif");
  22.     public static int BULLET_WIDTH=18;
  23.     public static int BULLET_HEIGHT=35;
  24.     public static Image enemy1=tk.createImage("images\\μD»ú1.gif");
  25.     public static Image enemy2=tk.createImage("images\\μD»ú2.gif");
  26.     public static int ENEMY_WIDTH=30;
  27.     public static int ENEMY_HEIGHT=30;
  28.     public static Image enemybullet=tk.createImage("images\\·ßÅ-μÄ×óμˉ.gif");
  29.     public static int ENEMYBULLET_WIDTH=15;
  30.     public static int ENEMYBULLET_HEIGHT=30;
  31. }
复制代码
  1. package View;
  2. import GameSubstance.*;
  3. import Tools.*;
  4. import java.awt.Color;
  5. import java.awt.Font;
  6. import java.awt.Graphics;
  7. import java.awt.Image;
  8. import java.awt.Toolkit;
  9. import java.awt.event.KeyAdapter;
  10. import java.awt.event.KeyEvent;
  11. import java.awt.image.ImageObserver;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import java.util.Random;

  15. import javax.swing.ImageIcon;
  16. import javax.swing.JFrame;
  17. import javax.swing.JPanel;
  18. public class StartFrame extends JFrame implements Runnable{
  19.     //游戏组件
  20.     Image bk=GameBox.backgroundimage;
  21.     MyPlane myplane;
  22.     List<EnemyPlane> enemyplanelist=new ArrayList<EnemyPlane>();
  23.     private int count=0;
  24.     private float sourse=0;
  25.     List<Bullet> bulletlist=new ArrayList<Bullet>();
  26.     List<Explode> explodelist=new ArrayList<Explode>();
  27.     List<EnemyBullet> enemybulletlist=new ArrayList<EnemyBullet>();
  28.     JPanel jp;
  29.     public static void main(String[] args) {
  30.         new Thread(new StartFrame()).start();

  31.     }
  32.     public StartFrame(){
  33.         this.setSize(GameBox.GAME_WIDTH,GameBox.GAME_HEIGHT);
  34.         //窗口居中
  35.         int width=Toolkit.getDefaultToolkit().getScreenSize().width;
  36.         int height=Toolkit.getDefaultToolkit().getScreenSize().height;
  37.         setLocation(width/2-GameBox.GAME_WIDTH/2, height/2-GameBox.GAME_HEIGHT/2);
  38.         setResizable(false);
  39.         myplane=new MyPlane(GameBox.GAME_WIDTH/2,GameBox.GAME_HEIGHT-GameBox.PLANE_HEIGHT*2);
  40.         new Thread(new AddEnemy()).start();
  41.         this.addKeyListener(new KeyMonitor());
  42.         this.add(new GameJPanel());
  43.         setTitle("打飞机小游戏");
  44.         this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
  45.         setVisible(true);
  46.     }

  47.     @Override
  48.     public void run() {
  49.         while(true){
  50.             repaint();
  51.             Random rand=new Random();
  52.             try {
  53.                 Thread.sleep(50);
  54.             } catch (InterruptedException e) {
  55.                 e.printStackTrace();
  56.             }
  57.         }
  58.     }

  59.     private class KeyMonitor extends KeyAdapter {

  60.         public void keyReleased(KeyEvent e) {
  61.             if(e.getKeyCode()==KeyEvent.VK_F2){
  62.                 myplane.setLive(true);
  63.             }
  64.             myplane.keyReleased(e);
  65.         }

  66.         public void keyPressed(KeyEvent e) {
  67.             myplane.keyPressed(e);
  68.         }

  69.     }

  70.     private class AddEnemy implements Runnable{
  71.         @Override
  72.         public void run() {
  73.             while(true){
  74.                 try {
  75.                     Thread.sleep(500);
  76.                     addEnemyPlane();
  77.                 } catch (InterruptedException e) {
  78.                     e.printStackTrace();
  79.                 }
  80.             }
  81.         }

  82.         private void addEnemyPlane(){
  83.             EnemyPlane enemyplane=new EnemyPlane(StartFrame.this);
  84.             enemybulletlist.add(new EnemyBullet(enemyplane.getX(),enemyplane.getY()));
  85.             enemyplanelist.add(enemyplane);
  86.         }
  87.     }


  88.     private class GameJPanel extends JPanel{

  89.         public void paint(Graphics g){
  90.             super.paint(g);
  91.             g.drawImage(bk,0,0,GameBox.GAME_WIDTH,GameBox.GAME_HEIGHT,null);
  92.             myplane.draw(g);
  93.             if (count==0){
  94.                 if(myplane.isLive()){
  95.                     createBullet();
  96.                 }else{
  97.                     for(int i=0;i<bulletlist.size();i++){
  98.                         bulletlist.remove(i);
  99.                     }
  100.                 }
  101.             }
  102.             count++;
  103.             if(count==2){
  104.                 count=0;
  105.             }
  106.             for(int i=0;i<bulletlist.size();i++){
  107.                  
  108.                 Bullet bullet=bulletlist.get(i);
  109.                 bullet.draw(g);
  110.                 clearBullet();
  111.             }
  112.             for(int i=0;i<enemyplanelist.size();i++){
  113.                 EnemyPlane enemyplane=enemyplanelist.get(i);
  114.                 enemyplane.draw(g);
  115.                 clearEnemyplane();
  116.                 Random rand=new Random();
  117.                 if(rand.nextInt(100)>93&&enemyplane.isLive()){
  118.                     enemybulletlist.add(new EnemyBullet(enemyplane.getX(),enemyplane.getY()));
  119.                 }
  120.                 //如果敌机面对我方飞机时发射一枚子弹
  121.                 if(enemyplane.getX()>=myplane.getX()&&enemyplane.getX()<=myplane.getX()+GameBox.PLANE_WIDTH&&enemyplane.isFired()&&enemyplane.isLive()){
  122.                     enemybulletlist.add(new EnemyBullet(enemyplane.getX(),enemyplane.getY()));
  123.                     enemyplane.setFired(false);
  124.                 }
  125.                  
  126.             }
  127.             for(int i=0;i<enemybulletlist.size();i++){
  128.                 EnemyBullet eb=enemybulletlist.get(i);
  129.                 eb.draw(g);
  130.                 clearEnemyBullet();
  131.             }
  132.             
  133.             hitPlane();//攻击敌机
  134.             impactMyplane();//敌机与我方飞机发生碰撞
  135.             if(enemybulletlist.size()>=1)hitMyplane();//
  136.             for(int i=0;i<explodelist.size();i++){
  137.                 Explode exp=explodelist.get(i);
  138.                 exp.draw(g);
  139.                 exp.setCount(exp.getCount()+1);
  140.                 clearExplode();
  141.             }
  142.             g.setColor(Color.RED);
  143.         //  g.setFont(GameBox.f1);//设置字体之后加载窗口会卡2秒钟
  144.             if(sourse!=0){
  145.                 g.drawString("您的得分:"+(int)sourse+"0", 50, 50);
  146.             }else{
  147.                 g.drawString("您的得分:0", 50, 50);
  148.             }

  149.         }
  150.          
  151.         private void clearEnemyBullet() {
  152.             for(int i=0;i<enemybulletlist.size();i++){
  153.                 if(!(enemybulletlist.get(i).isLive())){
  154.                     enemybulletlist.remove(i);
  155.                 }
  156.             }
  157.         }
  158.         private void createBullet(){
  159.             Bullet bullet=new Bullet((myplane.getX()+5),(myplane.getY()-30));
  160.             bulletlist.add(bullet);
  161.         }
  162.         private void clearBullet(){
  163.             for(int i=0;i<bulletlist.size();i++){
  164.                 if(!(bulletlist.get(i).isLive())){
  165.                     bulletlist.remove(i);
  166.                 }
  167.             }
  168.         }
  169.         private void clearEnemyplane() {
  170.             for(int i=0;i<enemyplanelist.size();i++){
  171.                 if(!(enemyplanelist.get(i).isLive())){
  172.                     enemyplanelist.remove(i);
  173.                 }
  174.             }
  175.         }
  176.         private void clearExplode() {
  177.             for(int i=0;i<explodelist.size();i++){
  178.                 if(!(explodelist.get(i).isLive())){
  179.                     explodelist.remove(i);
  180.                 }
  181.             }
  182.         }
  183.         private void hitPlane(){
  184.             int bx,ex,by,ey;
  185.             for(int i=0;i<bulletlist.size();i++){
  186.                 Bullet bullet=bulletlist.get(i);
  187.                 bx=bullet.getX();
  188.                 by=bullet.getY();
  189.                 for(int j=0;j<enemyplanelist.size();j++){
  190.                     EnemyPlane enemyplane=enemyplanelist.get(j);
  191.                     ex=enemyplane.getX();
  192.                     ey=enemyplane.getY();
  193.                     if(bx>=ex&&bx<=ex+GameBox.ENEMY_WIDTH&&by>=ey&&by<=ey+GameBox.ENEMY_HEIGHT&&enemyplane.isLive()&&bullet.isLive()){
  194.                         bullet.setLive(false);
  195.                         enemyplane.setLive(false);
  196.                         explodelist.add(new Explode(bx,by));
  197.                         sourse=sourse+1.168320194f;//防止CE恶意修改分数(我的QQ号码)
  198.                     }
  199.                 }
  200.             }
  201.         }
  202.         private void impactMyplane(){
  203.             int mx,my,ex,ey;
  204.             mx=myplane.getX();
  205.             my=myplane.getY();
  206.             for(int i=0;i<enemyplanelist.size();i++){
  207.                 EnemyPlane enemyplane=enemyplanelist.get(i);
  208.                 ex=enemyplane.getX();
  209.                 ey=enemyplane.getY();
  210.                 if(mx>=ex&&mx<=ex+GameBox.ENEMY_WIDTH&&my>=ey&&my<=ey+GameBox.ENEMY_HEIGHT&&myplane.isLive()&&enemyplane.isLive()){
  211.                     myplane.setLive(false);
  212.                     enemyplane.setLive(false);
  213.                     explodelist.add(new Explode(mx,my));
  214.                     explodelist.add(new Explode(ex,ey));
  215.                 }
  216.             }
  217.         }
  218.         private void hitMyplane() {
  219.             int mx,my,ex,ey;
  220.             mx=myplane.getX();
  221.             my=myplane.getY();
  222.             for(int i=0;i<enemyplanelist.size();i++){
  223.                 EnemyBullet eb=enemybulletlist.get(i);
  224.                 ex=eb.getX();
  225.                 ey=eb.getY();
  226.                 if(ex+GameBox.ENEMYBULLET_WIDTH>=mx&&ex+GameBox.ENEMYBULLET_WIDTH<=mx+GameBox.PLANE_WIDTH&&ey+GameBox.ENEMYBULLET_HEIGHT>=my&&ey+GameBox.ENEMYBULLET_HEIGHT<=my+GameBox.PLANE_HEIGHT&&myplane.isLive()&&eb.isLive()){
  227.                     myplane.setLive(false);
  228.                     eb.setLive(false);
  229.                     explodelist.add(new Explode(mx,my));
  230.                 }
  231.             }
  232.         }
  233.     }
  234. }
复制代码
爆炸.gif

背景.jpg

敌机1.gif

敌机2.gif

愤怒的子弹.gif

子弹.gif


您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP