Java仿太阁立志传5

没有标题写的那么大……真要用Java重写太阁五不说别的,光整理原图就够累死我的……

游戏截图:



 只是一个太阁五中加农业水平小游戏的再现……用本人loonframework-game包进行图形处理,算是一个对jar包的测试程序,完整程序下载地址,仅将使用到的类和图形打包:

[url]http://looframework.sourceforge.net/download/Tg.jar[/url]

源码如下:

TgIrrigationScript.java
package org.loon.framework.test.tg;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;

import javax.swing.JOptionPane;


import org.loon.framework.game.collection.SimpleArray;
import org.loon.framework.game.image.Bitmap;

/** *//**
 * <p>
 * Title: LoonFramework
 * </p>
 * <p>
 * Description:[太阁立志传小游戏Java模拟之水渠贯通]loonframework-game包由于标准类及接口未完成暂不开源(我懒……目前只完成大约45%而已|||三月没动……)
 * </p>
 * <p>
 * Copyright: Copyright (c) 2007
 * </p>
 * <p>
 * Company: LoonFramework
 * </p>
 *
 * @author chenpeng
 * @email: [email protected]
 * @version 0.1
 */
public class TgIrrigationScript extends Panel implements KeyListener ...{

    /** *//**
     *
     */
    private static final long serialVersionUID = 1L;

    // 生成缓存用背景Image
    Image _screen = new Bitmap(640, 480, 1).getImage();

    //获得画布
    Graphics _graphics = _screen.getGraphics();

    final private int _rs = 6;

    private int[][] _chess = null;

    private int[] _obj_block = null;
    // Bitmap为loonframework提供类,内部混合封装了Image和BufferedImage,提供了通用的处理方式
    // 背景图
    Bitmap _back_bitmap = null;

    // 出水图1
    Bitmap _water_start = null;

    // 出水图2
    Bitmap _water_start2 = null;

    // 左边框图
    Bitmap _left_box = null;

    // 左侧辅助框
    Bitmap[] _leftnew_sprites = null;

    // 砖块地图
    Bitmap[][] _character_sprites = null;

    // 光标
    Bitmap _cursor = null;

   
    Bitmap bitmap = null;

    /** *//**
     * 构造函数
     *
     */
    public TgIrrigationScript() ...{
        initialize();
    }

    /** *//**
     * 初始化
     *
     */
    public void initialize() ...{
       
        setBackground(Color.BLACK);
        setSize(640, 480);

        _leftnew_sprites = new Bitmap[4];

        _character_sprites = new Bitmap[_rs][_rs];

         _back_bitmap = new Bitmap("./image/pano.png");

         _water_start = new Bitmap("./image/water_start.png");

         _water_start2 = new Bitmap("./image/spr16.png");

         _left_box = new Bitmap("./image/left_box.png");

         _cursor = new Bitmap("./image/choose_box.png");

         bitmap = new Bitmap("./image/mud.png");
        // 出水图绘制坐标
        _water_start.setDrawXY(272, 2);
        // 左边框绘制坐标
        _left_box.setDrawXY(25, 20);
        _water_start2.setDrawXY(308, 45);
        //游标绘制坐标
        _cursor.setDrawXY(308, 91);
        for (int i = 0; i < _leftnew_sprites.length; i++) ...{
            _leftnew_sprites[i] = new Bitmap("./image/mud.png");
            _leftnew_sprites[i].setDrawXY(42, 204 - i * 56);
        }
        _leftnew_sprites[0].setDrawY(212);
        for (int i = 0; i <_rs; i++) ...{
            for (int j = 0; j < _rs; j++) ...{
                _character_sprites[i][j] = new Bitmap("./image/mud.png");
                _character_sprites[i][j].setDrawXY(i * 48 + 164, j * 48 + 91);
            }
        }

         _chess = new int[_rs][_rs];

         _obj_block = new int[4];
            // 左侧辅助栏读取图形
            for (int i = 0; i < 4; i++) ...{
                down();
            }
        // 设定监听
        setFocusable(true);
        addKeyListener(this);
    }

    public void update(Graphics g) ...{
        paint(g);
    }

    public void paint(Graphics g) ...{
   

        // 背景绘制
        _back_bitmap.draw(_graphics);
        // 出水图绘制
        _water_start.draw(_graphics);
        //
        _water_start2.draw(_graphics);
        // 左边框图绘制
        _left_box.draw(_graphics);
        // 左侧辅助框绘制
        for (int i = 0; i < _leftnew_sprites.length; i++) ...{
            _leftnew_sprites[i].draw(_graphics);
        }
        // 绘制砖块
        for (int i = 0; i < _character_sprites.length; i++) ...{
            for (int j = 0; j < _character_sprites[i].length; j++) ...{
                _character_sprites[i][j].draw(_graphics);
            }
        }
        // 绘制选中光标
        _cursor.draw(_graphics);
        g.drawImage(_screen, 0, 0, this);
    }

   

    public void down() ...{
        int steps = 0;
        _obj_block[0] = _obj_block[1];
        _leftnew_sprites[0].setBufferedImage(_leftnew_sprites[1]
                .getBufferedImage());
        _obj_block[1] = _obj_block[2];
        _leftnew_sprites[1].setBufferedImage(_leftnew_sprites[2]
                .getBufferedImage());
        _obj_block[2] = _obj_block[3];
        _leftnew_sprites[2].setBufferedImage(_leftnew_sprites[3]
                .getBufferedImage());
        if (steps < (_rs * _rs)) ...{
            // 位图
            int[] tmparr = new int[] ...{3,5,6,7,9,10,11,12,13,14,15};
            // 随即选取
            int tmpint = tmparr[new Random().nextInt(11)];

            _obj_block[3] = tmpint;

            _leftnew_sprites[3].setBufferedImage(new Bitmap("./image/spr"
                    + tmpint + ".png").getBufferedImage());
        } else ...{
            _obj_block[3] = 0;
            _leftnew_sprites[3].setBufferedImage(bitmap.getBufferedImage());
        }
        steps += 1;
    }

    /** *//**
     * 放下事件处理
     * @param x
     * @param y
     */
    public void putCase(int x, int y) ...{
        if (this._chess[x][y] == 0) ...{
            _character_sprites[x][y].setBufferedImage(_leftnew_sprites[0]
                    .getBufferedImage());
            _chess[x][y] = _obj_block[0];
            down();
            //_obj_block[0]为最后的渠道方块,所以为0时代表数据读完
            if (_obj_block[0] == 0) ...{
                endCase();
            }
        }
    }


    /** *//**
     * 判断选中图形能否贯通
     * @param x
     * @param y
     * @param dire
     * @return
     */
    public boolean crossInclude(int x, int y, int dire) ...{
        boolean flag = false;

        switch (dire) ...{
        case 1:
            flag = SimpleArray.include("3,5,7,9,11,13,15", _chess[x][y]);
            break;
        case 2:
            flag = SimpleArray.include("3,6,7,10,11,14,15", _chess[x][y]);
            break;
        case 3:
            flag = SimpleArray.include("5,6,7,12,13,14,15", _chess[x][y]);
            break;
        case 4:
            flag = SimpleArray.include("9,10,11,12,13,14,15", _chess[x][y]);
            break;
        }

        return flag;
    }

    public int tableToObject(int x, int y) ...{
        return (y * _rs) + x;
    }

    public int[] objectToTable(int pos) ...{
        int iy = pos / _rs;
        int ix = pos - (iy * _rs);
        return new int[] ...{ ix, iy };
    }



    public void keyPressed(KeyEvent e) ...{
        int keyCode = e.getKeyCode();

        switch (keyCode) ...{

        case KeyEvent.VK_LEFT:

            if (this._cursor.getDrawX() > 164) ...{
                _cursor.setDrawX(_cursor.getDrawX() - 48);
            }

            break;

        case KeyEvent.VK_RIGHT:

            if (this._cursor.getDrawX() < (_rs * 48 + 164 - 48)) ...{
                _cursor.setDrawX(_cursor.getDrawX() + 48);
            }
            break;

        case KeyEvent.VK_UP:

            if (this._cursor.getDrawY() > 91) ...{
                _cursor.setDrawY(_cursor.getDrawY() - 48);
            }

            break;

        case KeyEvent.VK_DOWN:
            if (this._cursor.getDrawY() < (_rs * 48 + 91 - 48)) ...{
                _cursor.setDrawY(_cursor.getDrawY() + 48);
            }

            break;

        case KeyEvent.VK_ENTER:
           
            putCase((_cursor.getDrawX() - 164) / 48,
                    (_cursor.getDrawY() - 91) / 48);
           
            break;
        case KeyEvent.VK_ESCAPE:
            endCase();
            break;

        }

        repaint();

    }

    public void keyReleased(KeyEvent e) ...{

    }

    public void keyTyped(KeyEvent e) ...{


    }

    public void endCase() ...{

        int allscore = 0;
        int allsum = _rs * _rs;
        //SimpleArray为loonframework-game提供集合类,以字符串方式操作字符数组
        SimpleArray sia1=new SimpleArray(0,35);
        SimpleArray sia2=new SimpleArray();
        _water_start2.setBufferedImage(new Bitmap("./image/wspr16.png")
                .getBufferedImage());
        if (crossInclude(3, 0, 1)) ...{
            sia2.pushAll(sia1.delete(tableToObject(3, 0)));
        }
        while ((sia2.all()!=null)) ...{
            int tmp = Integer.parseInt(sia2.pop());
            int[] result = objectToTable(tmp);
            int tx = result[0];
            int ty = result[1];
              if(crossInclude(tx,ty,1) && ty > 0)...{
                  if(crossInclude(tx,ty-1,3) && (sia1.include(tableToObject(tx,ty-1))))...{
                      sia2.pushAll(sia1.delete(tableToObject(tx,ty-1)));
                  }
              }
              if(crossInclude(tx,ty,2) && tx < _rs-1)...{
                  if(crossInclude(tx+1,ty,4) && (sia1.include(tableToObject(tx+1,ty))))...{
                      sia2.pushAll(sia1.delete(tableToObject(tx+1,ty)));
                  }
               
              }
              if(crossInclude(tx,ty,3) && ty < _rs-1)...{
                  if(crossInclude(tx,ty+1,1) && sia1.include(tableToObject(tx,ty+1)))...{
                      sia2.pushAll(sia1.delete(tableToObject(tx,ty+1)));
                  }
              }
              if(crossInclude(tx,ty,4) && tx > 0 )...{
                  if(crossInclude(tx-1,ty,2) && sia1.include(tableToObject(tx-1,ty)))...{
                      sia2.pushAll(sia1.delete(tableToObject(tx-1,ty)));
                  }
              }
       
            int tmpint = _chess[tx][ty];
           
            _character_sprites[tx][ty].setBufferedImage(new Bitmap(
                    "./image/wspr" + tmpint + ".png").getBufferedImage());
            allscore = allscore+1;

        }

        // 刷新
        repaint();
        int pscore = allscore * 100 / allsum;
        int rscore = pscore < 70 ? 1 : (pscore < 82 ? 2 : (pscore < 92 ? 3
                : (pscore < 100 ? 4 : 5)));
        String result = "";
        switch (rscore) ...{
        case 1:
            result = "完成率:"
                    + pscore
                    + "%  级别:[小白] 你在挑战小白的极致吗?!!!!";
            break;
        case 2:
            result = "完成率:" + pscore
                    + "%  级别:[庸才] 嗯,我想你对这个游戏的规则已经基本了解,剩下的可能只是积累经验了。";
            break;
        case 3:
            result = "完成率:"
                    + pscore
                    + "%  级别:[中智] 作为一个普通人,我觉得你在这个游戏来说做的已经不错了,但是你相信你或许是个天才么?记得不要只看着即将落下的那块,也要多考虑以后的情况哟^_^";
            break;
        case 4:
            result = "完成率:"
                    + pscore
                    + "%  级别:[上品] 你离开尽善尽美只有一步之遥,除了运气以外,你可能还需要掌握一个诀窍:为最后几块留下一个万能的空格。";
            break;
        case 5:
            result = "完成率:" + pscore + "%  级别:[天之骄子] 想当年我玩太阁,也就这样了……(^^)b";
            break;

        }
   
        //虽然做了Message类,但是为了演示打包方便所以不用……(其实是关联太杂不好单用|||)
        JOptionPane.showMessageDialog(this,result, "完成", JOptionPane.INFORMATION_MESSAGE);
    }
   
    public static void main(String[] args) ...{
        java.awt.EventQueue.invokeLater(new Runnable() ...{
            public void run() ...{
                Frame frm = new Frame("Java模拟太阁立志传灌溉游戏");
                frm.add(new TgIrrigationScript());
                frm.setResizable(false);
                frm.setSize(640, 480);
                frm.addWindowListener(new WindowAdapter()...{
                    public void windowClosing(WindowEvent e)...{
                        System.exit(0);
                    }
                });
                frm.setLocationRelativeTo(null);
                frm.setVisible(true);
            }
        });
    }

}

PS:自从9月发布loonframework-game包的演示例子以后,超过3个月一直没有连续的时间写,所以一直没动手完成,目前完成率只有大约45%……争取过年写,明年年底前一定发布整套源码……残念……||||

你可能感兴趣的:(java,职场,休闲,仿制,太阁立志传5)