论坛首页 Java企业应用论坛

山寨 植物大战僵尸 0.01beta版

浏览 32916 次
该帖已经被评为良好帖
作者 正文
   发表时间:2010-06-12  
很不错哦,我基本没做过游戏
0 请登录后投票
   发表时间:2010-06-21  
看了你的东西,比较感兴趣,回家利用几天 用flex做了一个山寨的。

你还接着做吗,有时间一起研究,我想吧这个添加点功能。玩家对战,一方僵尸,一方植物。
0 请登录后投票
   发表时间:2010-07-01  
看图就觉得很不错啊,要下过来仔细看看
0 请登录后投票
   发表时间:2010-07-07  
强烈抗议阉割版,强烈要求发原版,不能没有美女图啊
1 请登录后投票
   发表时间:2010-07-08   最后修改:2010-07-08
哇哈哈,坛子里都是些如饥似渴的郎啊
0 请登录后投票
   发表时间:2010-07-19  
唉,果然来晚了,没看到美女。
0 请登录后投票
   发表时间:2010-08-11  
好像俺来晚了!
0 请登录后投票
   发表时间:2010-08-15  
楼主的代码非常值得学习,收下了
0 请登录后投票
   发表时间:2010-08-15  
见过还有一个网页版的,纯js写的。
0 请登录后投票
   发表时间:2010-08-30   最后修改:2010-08-30
引用

哪位会用java2d实现那个图片,一部分亮,一部分暗啊。

我找了半天没有找到,只好用photoshop变暗了图片用两张实现的。

应该使用AffineTransform来实现吧,图形学不太会啊,

在这里抛砖引玉,给出一份 ColdDown 的例子
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Arc2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.EventListener;
import java.util.EventObject;

/**
 * Created by IntelliJ IDEA.
 * User: seth.yang
 * Date: 2010-8-30
 * Time: 15:56:26
 */
public class ColdDownSample {
    public static void main (String[] args) throws Exception {
        JFrame frame = new JFrame ("Test");
        ImageIcon icon = new ImageIcon ("20203.png");
        JPanel panel = new JPanel ();
        final ColdDownLabel cd1 = new ColdDownLabel (icon, .5f, 1000, ColdDownLabel.Type.Pie);
        final ColdDownLabel cd2 = new ColdDownLabel (icon, .5f, 5000, ColdDownLabel.Type.Line);
        final ColdDownLabel cd3 = new ColdDownLabel (icon, .8f, 10000, ColdDownLabel.Type.Line);

        cd1.setMaskColor (Color.ORANGE);
        cd2.setMaskColor (Color.MAGENTA);
        cd3.setMaskColor (Color.GREEN);

        panel.add (cd1);
        panel.add (cd2);
        panel.add (cd3);
        frame.getContentPane ().add (panel);

        panel = new JPanel ();
        final JButton button = new JButton ("Cold Down All");
        button.addActionListener (new ActionListener() {
            public void actionPerformed (ActionEvent e) {
                cd1.start ();
                cd2.start ();
                cd3.start ();
                button.setEnabled (false);
            }
        });
        panel.add (button);
        frame.getContentPane ().add (panel, BorderLayout.SOUTH);

        frame.pack ();
        frame.setVisible (true);
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

        ColdDownCompleteListener l = new ColdDownCompleteListener() {
            public void coldDown (ColdDownEvent e) {
                if (cd1.isColdDownComplete () && cd2.isColdDownComplete () && cd3.isColdDownComplete ())
                    button.setEnabled (true);
            }
        };
        cd1.addColdDownCompleteListener (l);
        cd2.addColdDownCompleteListener (l);
        cd3.addColdDownCompleteListener (l);
    }
}

class ColdDownEvent extends EventObject {
    public ColdDownEvent (Object source) {
        super (source);
    }
}

interface ColdDownCompleteListener extends EventListener {
    void coldDown (ColdDownEvent e);
}

class ColdDownLabel extends JLabel implements Runnable {
    private long interval = 0;
    private int top = 0, angle = 360;
    private Type type = Type.Line;
    private static int lineDelta = 1, angleDelta = 1;
    private double x, y; // 饼状 CD 时候的左上角坐标
    private double r;    // 饼状 CD 时的半径
    private boolean running;
    private float alpha = 1f;
    private boolean coldDownComplete;

    private final Object lock = new Object ();

    private Color maskColor;
    private BufferedImage buff;
    private Icon icon;
    private final java.util.List<ColdDownCompleteListener> listeners = new ArrayList<ColdDownCompleteListener> ();

    private Runnable repainter = new Runnable() {
        public void run () {
            repaint ();
        }
    };

    public static enum Type { Line, Pie }

    public ColdDownLabel (Icon icon, float alpha, long delay, Type type) {
        this.icon = icon;
        setSize (icon.getIconWidth (), icon.getIconWidth ());
        buff = new BufferedImage (icon.getIconWidth (), icon.getIconHeight (), BufferedImage.TYPE_INT_RGB);        

        setAlpha (alpha);
        setDelay (delay);
        setType (type);
        paintShawdow ();
    }

    public void setType (Type type) {
        this.type = type;
        if (type == Type.Pie) {
            int x0 = icon.getIconWidth () / 2, y0 = icon.getIconHeight () / 2; // center point
            r = Math.sqrt (x0 * x0 + y0 * y0);
            x = x0 - r;
            y = y0 - r;
        }
    }

    public void setDelay (long delay) {
        if (type == Type.Line) {
            interval = delay * lineDelta / icon.getIconHeight ();
        } else {
            interval = delay * angleDelta / 360;
        }
    }

    public void setAlpha (float alpha) {
        this.alpha = alpha;
        setMaskColor (Color.black);
    }

    public void start () {
        synchronized (lock) {
            if (running) return;
        }

        top = 0;
        angle = 360;
        running = true;
        coldDownComplete = false;
        new Thread (this).start ();
    }

    public boolean isColdDownComplete () {
        return coldDownComplete;
    }

    @Override
    public Dimension getPreferredSize () {
        if (icon == null)
            return super.getPreferredSize ();
        return new Dimension (icon.getIconWidth (), icon.getIconHeight ());
    }

    @Override
    public void paint (Graphics g) {
        g.drawImage (buff, 0, 0, this);
    }

    public void setMaskColor (Color c) {
        maskColor = new Color (c.getRed (), c.getGreen (), c.getBlue (), (int)(255 * alpha)); // 不透明度为 aplha
    }

    public void run () {
        while (running) {
            if (type == Type.Line) top += lineDelta;
            else angle -= angleDelta;
            paintShawdow ();
            SwingUtilities.invokeLater (repainter);
            try {
                Thread.sleep (interval);
            } catch (InterruptedException e) {
                e.printStackTrace ();
            }
            running = type == Type.Line ? top < icon.getIconHeight () : angle > 0;
        }
        coldDownComplete = true;
        if (listeners.size () > 0) {
            ColdDownEvent e = new ColdDownEvent (this);
            for (ColdDownCompleteListener l : listeners) l.coldDown (e);
        }
    }

    public void addColdDownCompleteListener (ColdDownCompleteListener l) {
        synchronized (listeners) { listeners.add (l); }
    }

    private void paintShawdow () {
        Graphics2D g2 = buff.createGraphics ();
        g2.setColor (Color.white);
        g2.fillRect (0, 0, getWidth (), getHeight ());
        icon.paintIcon (this, g2, 0, 0);
        g2.setPaint (maskColor);
        if (type == Type.Line) g2.fill (new Rectangle (0, top, getWidth (), getHeight () - top));
        else g2.fill (new Arc2D.Double (x, y, 2 * r, 2 * r, 90, angle, Arc2D.PIE));
    }
}
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics