⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tomadventurecanvas.java

📁 java 小程序 使用j2me 开发的一个小的游戏软件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
import javax.microedition.lcdui.*;
import java.io.*;

//import com.nokia.mid.ui.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: BBMF</p>
 * @author isac
 * @version 1.0
 */

public class TomAdventureCanvas
    extends Canvas
    implements Runnable, CommandListener
{
    private TomAdventureMIDlet midlet;
    private Display display;
    Thread thread;

    ////////////////////按键映射/////////////////////
    public static final byte KEY_UP = -1;
    public static final byte KEY_DOWN = -2;
    public static final byte KEY_LEFT = -3;
    public static final byte KEY_RIGHT = -4;
    public static final byte KEY_MIDDLE = -5;
    public static final byte KEY_LEFT_SOFTKEY = -6;
    public static final byte KEY_RIGHT_SOFTKEY = -7;
    public static final byte KEY_PAUSE = 55;
    public static final byte KEY_NUM0 = 48;
    public static final byte KEY_NUM1 = 49;
    public static final byte KEY_NUM2 = 50;
    public static final byte KEY_NUM3 = 51;
    public static final byte KEY_NUM4 = 52;
    public static final byte KEY_NUM5 = 53;
    public static final byte KEY_NUM6 = 54;
    public static final byte KEY_NUM7 = 55;
    public static final byte KEY_NUM8 = 56;
    public static final byte KEY_NUM9 = 57;

    ////////////////////////////////////////////////
    public static final int vieww = 128;
    public static final int viewh = 128;
    public static final int mapw = 240;
    public static final int maph = 185;
    public static int mapx = 0;
    public static int mapy = 0;
    public static int omapx = 0;
    public static int omapy = 0;

    private Logo logo;
    private BackGround background;
    public Map[] map;
    private Item[] item;
    public Tom tom;

    public static int timecount, loadcount; //时间、初始化计数器
    public static final byte BBMF_LOGO = 10;
    public static final byte GAME_LOGO = 11;
    public static final byte MAIN_MENU = 12;
    public static final byte SUB_MENU = 13;
    public static final byte RUNNING = 14;
    public static final byte LEFT_MOVE = 15;
    public static final byte RIGHT_MOVE = 16;
    public static final byte PAUSE = 17;
    public static final byte PAUSE_UP = 18;
    public static final byte PAUSE_DOWN = 19;
    public static final byte PAUSE_LEFT = 20;
    public static final byte PAUSE_RIGHT = 21;
    public static final byte PAUSE_NORMAL = 22;

    public static boolean STAGE_WIN = false;
    public static boolean STAGE_LOSE = false;

    public static int status = BBMF_LOGO;
    public static int pause = PAUSE_NORMAL;

    public static final String[] mainmenu =
        {
        "开始游戏", "读取进度", "帮助", "排行榜", "音效设置", "厂商资讯", "退出游戏"};
    public static final String[] submenu =
        {
        "继续游戏", "存储进度", "返回主菜单", "退出游戏"};
    public List listmainmenu, listsubmenu;
    private int mainmenuselected, submenuselected;

    public static int stage = 1;//当前关数

    //public static Image imgitems = null;
    public static Image[] imgitem = new Image[25];
    public static Image imgwin = null;
    public static Image imglose = null;

    public static byte[][] imgstage = {
        {0,2,3,4,5,6,7,8,9,10,11,12,13},
        {0,2,3,4,5,6,7,8,9,10,11,12}
    };

    public static byte[][][] initmapstage = {
        {
        {0, 8,16,11,15,15,34, 0, 0, 0, 0, 0, 0, 0, 0, 0},//地图起始左上角X,左上角Y,横向格数,纵向格数,每格宽,每格高,地图中非零格数
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0,18, 0, 0, 0, 0, 0, 0, 0},
        {2, 2, 2, 0, 0, 0, 0, 8, 9, 6, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,18, 0, 0, 0},
        {0, 0, 0, 0, 4, 4, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0},
        {1, 1,11, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 3, 0, 3, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 3, 0, 3, 0, 3, 0, 0, 0, 0, 0, 0},
        {0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0}
        },
        {
        {0, 8,16,11,15,15,38, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0},
        {4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4},
        {0, 0, 0, 0, 0, 0, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 7, 6, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 2, 2, 2, 2,11,10, 2, 2, 2, 2},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {9, 8, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0},
        {0, 1, 1, 1, 0,10, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0}
        }
    };

    public static byte[][][] mapstage = {
        {
        {0, 8,16,11,15,15,34, 0, 0, 0, 0, 0, 0, 0, 0, 0},//地图起始左上角X,左上角Y,横向格数,纵向格数,每格宽,每格高,地图中非零格数
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0,18, 0, 0, 0, 0, 0, 0, 0},
        {2, 2, 2, 0, 0, 0, 0, 8, 9, 6, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,18, 0, 0, 0},
        {0, 0, 0, 0, 4, 4, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0},
        {1, 1,11, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 3, 0, 3, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 3, 0, 3, 0, 3, 0, 0, 0, 0, 0, 0},
        {0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0}
        },
        {
        {0, 8,16,11,15,15,38, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0},
        {4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4},
        {0, 0, 0, 0, 0, 0, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 7, 6, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 2, 2, 2, 2,11,10, 2, 2, 2, 2},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {9, 8, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0},
        {0, 1, 1, 1, 0,10, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0}
        }
    };
    //{地图中坐标X,坐标Y,元素类型,状态}
    private int[][][] itemstage = {
        {
        {0,98,4,1},{30,128,2,1},{62,29,3,1},{62,89,3,1},
        {197,89,3,1},{31,140,1,0},{30,150,1,0},{50,138,1,0},{51,150,1,0}
        },
        {
        {179,128,2,1},{167,14,3,1},{92,44,3,1},{47,149,3,1},
        {182,140,1,0},{181,150,1,0},{201,138,1,0},{202,150,1,0}
        }

    };

    private int[][] tomstage = {
        {60,143},
        {210,23}
    };

    public TomAdventureCanvas()
    {
      //  display = d;
      //  midlet = m;
        timecount = 0;
        loadcount = 0;
        mainmenuselected = 0;
        submenuselected = 0;
        thread = new Thread(this);
        thread.start();
    }

    public void setMIDlet(TomAdventureMIDlet tm){
        midlet = tm;
        display = Display.getDisplay(tm);
    }

    ////////////////////游戏初始化////////////////////
    public void gameInit()
    {
        switch (loadcount)
        {
            case 0:
                logo = new Logo();
                break;
            case 1:
                /*if (imgitems == null)
                {
                    try
                    {
                        imgitems = Image.createImage("/images/items.png");
                    }
                    catch (IOException ex)
                    {
                    }
                }*/
                break;
            case 2:
                break;
            case 3:
                break;
        }
    }

    ////////////////////绘图函数/////////////////////
    public static void drawClipImage(Graphics g, int x, int y, Image img,
                                     int clipx,
                                     int clipy, int clipw, int cliph)
    {
        g.setClip(x, y, clipw, cliph);
        g.drawImage(img, x - clipx, y - clipy, Graphics.TOP | Graphics.LEFT);
        g.setClip(0, 0, TomAdventureCanvas.vieww, TomAdventureCanvas.viewh);
    }

    /////////////////判断物体所在格子//////////////////
    //x:物体左上X,y:物体左上Y,w:物体宽,h:物体高
    public static int[] whichGrid(int x,int y,int w,int h)
    {
        int[] grid = new int[2];
        grid[0] = (x+w/2-mapstage[stage-1][0][0])/mapstage[stage-1][0][4];
        grid[1] = (y-mapstage[stage-1][0][1])/mapstage[stage-1][0][5];
        return grid;
    }

    ///////////////判断下面的格子是否为空////////////////
    //gridx:横向格数,girdy:纵向格数
    public static boolean onGround(int gridx,int gridy)
    {
        if(gridx<mapstage[stage-1][0][2]&&gridy<mapstage[stage-1][0][3]-1)
        {
            if(mapstage[stage-1][gridy+2][gridx]!=0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    /////////////判断物体如要移动是否有阻挡///////////////
    //x:物体左上X,y:物体左上Y,w:物体宽,h:物体高,step:想要移动的像素(右正,左负)
    public static boolean isBlock(int x,int y,int w,int h,int step)
    {
        int[] grid = new int[2];
        grid = whichGrid(x,y,w,h);
        if(step>0)
        {
            if(grid[0] == mapstage[stage-1][0][2]-1)
            {
                if(x+w+step<=(grid[0]+1)*mapstage[stage-1][0][4])
                {
                    return false;
                }
                else
                {
                    if(mapstage[stage-1][grid[1]+1][0]!=0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            else
            {
                if(x+w+step<=(grid[0]+1)*mapstage[stage-1][0][4])
                {
                    return false;
                }
                else
                {
                    if(mapstage[stage-1][grid[1]+1][grid[0]+1]!=0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }
        else
        {
            if(grid[0] == 0)
            {
                if(x+step>=0)
                {
                    return false;
                }
                else
                {
                    if(mapstage[stage-1][grid[1]+1][mapstage[stage-1][0][2]-1]!=0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            else
            {
                if(x+step>=grid[0]*mapstage[stage-1][0][4])
                {
                    return false;
                }
                else
                {
                    if(mapstage[stage-1][grid[1]+1][grid[0]-1]!=0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }
    }

    /////////////////判断是否碰到物体/////////////////
    //x:TOM左上X,y:TOM左上Y,w:TOM宽,h:TOM高
    public int isItem(int x,int y,int w,int h)
    {
        int itemx,itemy,itemw,itemh;
        for(int i=0;i<itemstage[stage-1].length;i++)
        {
            if(Item.itemdetail[itemstage[stage-1][i][2]][2]<=15)
            {
                itemx = itemstage[stage-1][i][0];
                itemw = Item.itemdetail[itemstage[stage-1][i][2]][2];
            }
            else
            {
                itemx = itemstage[stage-1][i][0]+Item.itemdetail[itemstage[stage-1][i][2]][2]/2-8;
                itemw = 16;
            }
            if(Item.itemdetail[itemstage[stage-1][i][2]][3]<=15)
            {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -