| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
跟大牛学C++学算法数据结构
共有 1376 人关注过本帖
标题:飞机大战
只看楼主 加入收藏
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:114
帖 子:1819
专家分:7935
注 册:2015-3-19
  得分:0 
回复 20楼 摇滚萝卜
那你下次把图片发我,我帮你加上吧,明天还要上课,我先下了,拜拜

知命以下无敌
2019-01-06 20:11
摇滚萝卜
Rank: 2
等 级:论坛游民
帖 子:16
专家分:20
注 册:2019-1-4
  得分:0 
回复 21楼 林月儿
然后在爆炸那里帮我标注下   谢谢大佬!

[此贴子已经被作者于2019-1-6 22:50编辑过]

附件: 您没有浏览附件的权限,请 登录注册
2019-01-06 20:23
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:114
帖 子:1819
专家分:7935
注 册:2015-3-19
  得分:0 
程序代码:
package game;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import java.util.Timer;
import javax.swing.*;

public class Test extends JFrame implements MouseListener, MouseMotionListener {
    /**
     *
     
*/
    private static final long serialVersionUID = 1L;
    private static final int START = 0,
            RUNNING = 1,
            PAUSE = 2,
            OVER = 3,
            DEFAULT_WIDTH = 610,
            DEFAULT_HEIGHT = 506;
    private int state = START;
    private MoveObj self = new MoveObj(0, 0, 40, 40);
    private List<MoveObj> enemys = new ArrayList<MoveObj>();
    private List<MoveObj> bullets = new ArrayList<MoveObj>();
    private List<MoveObj> bombs = new ArrayList<MoveObj>();
    public Test() {
        setVisible(true);
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        addMouseListener(this);
        addMouseMotionListener(this);
        setResizable(false);
        final JPanel myPanel = new MyPanel();
        add(myPanel);
        new Timer().schedule(new TimerTask() {
            
            @Override
            public void run() {
                if(state == RUNNING) {
                    addEnemyAction();
                    shootAction();
                    bangAction();
                    hideBomb();
                    moveAction();
                    outOfBorderAction();
                    checkGameOver();
                    moveIndex++;
                }
                myPanel.repaint();
            }
        }, 10, 10);
    }
    protected void hideBomb() {
        for(MoveObj e:bombs) {
            if(moveIndex % 4 == 0) {
                if(e.life>0)
                    e.life--;
            }
            e.width+=1;
            e.height+=1;
        }
        for(MoveObj e:new ArrayList<MoveObj>(bombs)) {
            if(e.life<=0)
                bombs.remove(e);
        }
        
    }
    protected void checkGameOver() {
        for(MoveObj obj : enemys) {
            if(self.x + self.width > obj.x && self.x < obj.x + obj.width &&
                    self.y + self.height > obj.y && self.y < obj.y + obj.height) {
                state = OVER;
                enemys.clear();
                bullets.clear();
                return;
            }
        }
    }
    long moveIndex = 0;
    protected void outOfBorderAction() {
//        enemys = enemys.stream().filter(MoveObj::isNotOutOfBorder)
//                .collect(Collectors.toList());
//        bullets = bullets.stream().filter(MoveObj::isNotOutOfBorder)
//                .collect(Collectors.toList());
        for(MoveObj e:new ArrayList<MoveObj>(enemys)) {
            if(!e.isNotOutOfBorder()) {
                enemys.remove(e);
            }
        }
        for(MoveObj e:new ArrayList<MoveObj>(bullets)) {
            if(!e.isNotOutOfBorder()) {
                bullets.remove(e);
            }
        }
    }
    protected void moveAction() {
        if(moveIndex % 2 == 0) {
//            enemys.forEach(ele->ele.y+=1);
            for(MoveObj e:enemys) e.y+=1;
//            bullets.forEach(ele->ele.y-=2);
            for(MoveObj e:bullets) e.y-=2;
        }
    }
    protected void bangAction() {
        for(MoveObj b:new ArrayList<MoveObj>(bullets)) {
            for(MoveObj e:new ArrayList<MoveObj>(enemys)) {
                if(b.bang(e)) {
                    bullets.remove(b);
                    enemys.remove(e);
                    bombs.add(new MoveObj(e.x, e.y, 12, 12));
                    break;
                }
            }
        }
    }
    protected void shootAction() {
        // 0.2s
        if(moveIndex % 20 == 0) {
            bullets.add(new MoveObj(self.x+self.width/2, self.y, 8, 12));
        }
    }
    protected void addEnemyAction() {
        // 2s
        if(moveIndex % 200 == 0) {
            int x = new Random().nextInt(DEFAULT_WIDTH - 40);
            enemys.add(new MoveObj(x, 0, 40, 40));
        }
    }
    // my plan
    Image myplanImage = new ImageIcon("image/MyPlane.png").getImage();
    // area map
    Image gameMap = new ImageIcon("image/map/gamemap.jpg").getImage();
    // enemy
    Image enemyImage = new ImageIcon("image/monster/PlaneOne.png").getImage();
    // bullet
    Image bulletImage = new ImageIcon("image/bullet.png").getImage();
    // bomb
    Image bombImage = new ImageIcon("image/bomb.png").getImage();

    class MyPanel extends JPanel {
        /**
         *
         
*/
        private static final long serialVersionUID = 1L;
        @Override
        public void paint(Graphics g) {
           g.drawImage(gameMap, 0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT, null);
           g.drawImage(myplanImage, self.x, self.y, self.width, self.height, null);
//           enemys.forEach(e->g.drawImage(enemyImage, e.x, e.y, e.width, e.height, null));
           for(MoveObj e:enemys)g.drawImage(enemyImage, e.x, e.y, e.width, e.height, null);
//           bullets.forEach(e->g.drawImage(bulletImage, e.x, e.y, e.width, e.height, null));
           for(MoveObj e:bullets)g.drawImage(bulletImage, e.x, e.y, e.width, e.height, null);
           for(MoveObj e:bombs) {
               if(e.life > 0)
               g.drawImage(bombImage, e.x, e.y, e.width, e.height, null);
           }
           Font font = new Font("gbk", Font.BOLD, 32);
           g.setFont(font);
           g.setColor(Color.RED);
           switch(state) {
           case START:
               g.drawString("开始", DEFAULT_WIDTH/3, DEFAULT_HEIGHT/2);
               break;
           case PAUSE:
               g.drawString("暂停", DEFAULT_WIDTH/3, DEFAULT_HEIGHT/2);
               break;
           case OVER:
               g.drawString("结束", DEFAULT_WIDTH/3, DEFAULT_HEIGHT/2);
               break;
           }
        }
    }
    public static void main(String[] args) {
        new Test();
    }
    @Override
    public void mouseClicked(MouseEvent e) {
        if(state == START)
            state = RUNNING;
        else if(state == OVER)
            state = START;
    }
    @Override
    public void mousePressed(MouseEvent e) {}
    @Override
    public void mouseReleased(MouseEvent e) {}
    @Override
    public void mouseEntered(MouseEvent e) {
        if(state == PAUSE)
            state = RUNNING;
    }
    @Override
    public void mouseExited(MouseEvent e) {
        if(state == RUNNING)
            state = PAUSE;
    }
    @Override
    public void mouseDragged(MouseEvent e) {}
    @Override
    public void mouseMoved(MouseEvent e) {
        if(state == RUNNING) {
            self.x = e.getX() - 20;
            self.y = e.getY() - 40;
        }
    }
    class MoveObj {
        int x;
        int y;
        int width;
        int height;
        int life = 10;
        public MoveObj(int x, int y, int width, int height) {
            super();
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
        }
        public boolean bang(MoveObj e) {
            return x+width>e.x&&x<e.x+e.width&&y+height>e.y&&y<e.y+e.height;
        }
        public boolean isNotOutOfBorder() {
            return !(x < 0 || y < 0 || x + width > DEFAULT_WIDTH || y + height > DEFAULT_HEIGHT);
        }
    }
}

知命以下无敌
2019-01-07 21:18
摇滚萝卜
Rank: 2
等 级:论坛游民
帖 子:16
专家分:20
注 册:2019-1-4
  得分:0 
2019-01-08 11:02
幻紫灵心
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:山咔咔里面
等 级:贵宾
威 望:34
帖 子:322
专家分:2218
注 册:2018-3-30
  得分:0 
以下是引用林月儿在2019-1-6 20:11:10的发言:

那你下次把图片发我,我帮你加上吧,明天还要上课,我先下了,拜拜

月儿还是学生?

saber,别哭.
2019-01-09 17:45
豆豆的滴
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:14
帖 子:325
专家分:943
注 册:2018-5-7
  得分:0 
嗯嗯玩物丧志
2019-01-10 20:58
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:114
帖 子:1819
专家分:7935
注 册:2015-3-19
  得分:0 
回复 26楼 豆豆的滴
老脸一红

知命以下无敌
2019-01-10 21:47
xzc912
Rank: 1
等 级:新手上路
帖 子:15
专家分:7
注 册:2019-1-7
  得分:0 
回复 27楼 林月儿
老夫猜可能是大学生吧
2019-01-12 16:50
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:114
帖 子:1819
专家分:7935
注 册:2015-3-19
  得分:0 
回复 28楼 xzc912
新员工培训

知命以下无敌
2019-01-13 05:35
xzc912
Rank: 1
等 级:新手上路
帖 子:15
专家分:7
注 册:2019-1-7
  得分:0 
回复 29楼 林月儿
好吧
2019-01-13 11:22







关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.062837 second(s), 8 queries.
Copyright©2004-2019, BCCN.NET, All Rights Reserved