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

📄 tomadventurecanvas.java

📁 java 小程序 使用j2me 开发的一个小的游戏软件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                itemy = itemstage[stage-1][i][1];
                itemh = Item.itemdetail[itemstage[stage-1][i][2]][3];
            }
            else
            {
                itemy = itemstage[stage-1][i][1]+Item.itemdetail[itemstage[stage-1][i][2]][3]-15;
                itemh = 15;
            }
            if(x+w/2>=itemx
               && x+w/2<=itemx+itemw
               && y+h/2>=itemy
               && y+h/2<=itemy+itemh)
            {
                return i;
            }
        }
        return -1;
    }

    /////////////////对碰到的物体处理函数/////////////////
    //itemnum:遇到item的本关序号
    public void itemProcess(int itemnum)
    {
        if(itemnum>=0)
        {
            switch(itemstage[stage-1][itemnum][2])
            {
                case 2: //78通关门
                    if(tom.baby == 3)
                    {
                        win();
                    }
                    break;
                case 3: //35过关宝宝
                    if(item[itemnum].status == 1)
                    {
                        item[itemnum].status = 0;
                        tom.baby = tom.baby + 1;
                        if(tom.baby == 3)
                        {
                            for(int i=0;i<item.length;i++)
                            {
                                if(item[i].type == 1)
                                {
                                    item[i].status = 2;
                                }
                            }
                        }
                    }
                    break;
                case 4: //73潜水服
                    if(item[itemnum].status == 1)
                    {
                        item[itemnum].status = 0;
                        if (tom.time <= 80)
                        {
                            tom.time = tom.time + 20;
                        }
                        else
                        {
                            tom.time = 100;
                        }
                    }
                    break;
            }
        }
    }

    ///////////////////////////////////////////////
    //gridx:横向格数,girdy:纵向格数,status:状态字
    public void changeItemStatus(int gridx,int gridy,int itemstatus)
    {
        if(gridx>=0 && gridx<mapstage[stage-1][0][2]
           && gridy>=0 && gridy<mapstage[stage-1][0][3])
        {
            if(itemstatus == 1)
            {
                if (map[gridy * mapstage[stage - 1][0][2] + gridx].type == 3 ||
                    map[gridy * mapstage[stage - 1][0][2] + gridx].type == 4 ||
                    map[gridy * mapstage[stage - 1][0][2] + gridx].type == 5 ||
                    map[gridy * mapstage[stage - 1][0][2] + gridx].type == 6 ||
                    map[gridy * mapstage[stage - 1][0][2] + gridx].type == 7 ||
                    map[gridy * mapstage[stage - 1][0][2] + gridx].type == 8 ||
                    map[gridy * mapstage[stage - 1][0][2] + gridx].type == 9)
                    map[gridy * mapstage[stage - 1][0][2] + gridx].status = 2;
                else if (map[gridy * mapstage[stage - 1][0][2] + gridx].type == 18 ||
                    map[gridy * mapstage[stage - 1][0][2] + gridx].type == 19)
                    map[gridy * mapstage[stage - 1][0][2] + gridx].status = 6;
            }
            else if(itemstatus == 2)
            {
                if (map[gridy * mapstage[stage - 1][0][2] + gridx].type == 3 ||
                    map[gridy * mapstage[stage - 1][0][2] + gridx].type == 4 ||
                    map[gridy * mapstage[stage - 1][0][2] + gridx].type == 5 ||
                    map[gridy * mapstage[stage - 1][0][2] + gridx].type == 6 ||
                    map[gridy * mapstage[stage - 1][0][2] + gridx].type == 7 ||
                    map[gridy * mapstage[stage - 1][0][2] + gridx].type == 8 ||
                    map[gridy * mapstage[stage - 1][0][2] + gridx].type == 9)
                    map[gridy * mapstage[stage - 1][0][2] + gridx].status = 2;
                else if (map[gridy * mapstage[stage - 1][0][2] + gridx].type == 18 ||
                    map[gridy * mapstage[stage - 1][0][2] + gridx].type == 19)
                    map[gridy * mapstage[stage - 1][0][2] + gridx].status = 7;
            }

        }
    }

    public void initStage()
    {
        for(int i=0;i<25;i++)
        {
            imgitem[i] = null;
        }
        for(int i=0;i<imgstage[stage-1].length;i++)
        {
            //System.out.println(imgstage[stage-1][i]);
            if (imgitem[imgstage[stage-1][i]] == null)
            {
                try
                {
                    imgitem[imgstage[stage-1][i]] = Image.createImage("/images/item"+imgstage[stage-1][i]+".png");
                }
                catch (IOException ex)
                {
                }
            }
        }

        background = new BackGround();
        for(int i=0;i<initmapstage[stage-1].length;i++)
        {
            for(int j=0;j<initmapstage[stage-1][i].length;j++)
            {
                mapstage[stage-1][i][j] = initmapstage[stage-1][i][j];
            }
        }
        map = new Map[mapstage[stage-1][0][2]*mapstage[stage-1][0][3]];
        for(int i=1;i<mapstage[stage-1].length;i++)
        {
            for(int j=0;j<mapstage[stage-1][i].length;j++)
            {
                     map[(i-1)*mapstage[stage-1][i].length+j] = new Map(this,mapstage[stage-1][0][0]+j*15,mapstage[stage-1][0][1]+(i-1)*15,mapstage[stage-1][i][j]);
            }
        }
        item = new Item[itemstage[stage-1].length];
        for(int i=0;i<itemstage[stage-1].length;i++)
        {
            item[i] = new Item(itemstage[stage-1][i][0],itemstage[stage-1][i][1],itemstage[stage-1][i][2],itemstage[stage-1][i][3]);
        }
        tom = new Tom(this,tomstage[stage-1][0], tomstage[stage-1][1]);
        mapx = tomstage[stage-1][0] - TomAdventureCanvas.vieww/2;
        if(mapx<0)
        {
            mapx = 0;
        }
        else if(mapx>mapw-vieww)
        {
            mapx = mapw - vieww;
        }
        mapy = tomstage[stage-1][1] - TomAdventureCanvas.viewh/2;
        if(mapy<0)
        {
            mapy = 0;
        }
        else if(mapy>maph-viewh)
        {
            mapy = maph - viewh;
        }
    }

    public void lose()
    {
        try
        {
            imglose = Image.createImage("/images/lose.png");
        }
        catch (IOException ex)
        {
        }
        STAGE_LOSE = true;
        initStage();
        if(Tom.life>0)
        {
            Tom.life = Tom.life - 1;
        }
        else
        {
            Tom.life = 3;
        }
        System.gc();
    }

    public void win()
    {
        try
        {
            imgwin = Image.createImage("/images/win.png");
        }
        catch (IOException ex)
        {
        }
        STAGE_WIN = true;
        if(stage<2)
        {
            stage = stage + 1;
        }
        initStage();
        Tom.life = Tom.life + 1;
        System.gc();
    }

    ////////////////////////////////绘图函数////////////////////////////////////
    protected void paint(Graphics g)
    {
        switch (status)
        {
            case BBMF_LOGO: //公司LOGO
                if (loadcount < 4)
                {
                    gameInit();
                    loadcount++;
                }
                else
                {
                    status = GAME_LOGO;
                    loadcount = 0;
                }
                logo.paint(g);
            case GAME_LOGO: //游戏LOGO
                timecount++;
                if (timecount > 30)
                {
                    System.out.println("ddd");
                    logo.imgbbmflogo = null;
                    logo.imggamelogo = null;
                    status = MAIN_MENU;
                    timecount = 0;
                }
                logo.paint(g);
                break;
            case MAIN_MENU: //主菜单
                listmainmenu = new List("主菜单", List.IMPLICIT, mainmenu, null);
                listmainmenu.setSelectedIndex(mainmenuselected, true);
                listmainmenu.setCommandListener(this);
                display.setCurrent(listmainmenu);
                break;
            case SUB_MENU: //游戏中的菜单
                listsubmenu = new List("游戏菜单", List.IMPLICIT, submenu, null);
                listsubmenu.setSelectedIndex(submenuselected, true);
                listsubmenu.setCommandListener(this);
                display.setCurrent(listsubmenu);
                break;
            case RUNNING:
                if(STAGE_LOSE == true)
                {
                    timecount++;
                    if(timecount>30)
                    {
                        timecount = 0;
                        STAGE_LOSE = false;
                        imglose = null;
                        System.gc();
                    }
                    else
                    {
                        drawClipImage(g,0,0,imglose,0,0,this.vieww,this.viewh);
                    }
                }
                else if(STAGE_WIN == true)
                {
                    timecount++;
                    if(timecount>30)
                    {
                        timecount = 0;
                        STAGE_WIN = false;
                        imgwin = null;
                        System.gc();
                    }
                    else
                    {
                        drawClipImage(g,0,0,imgwin,0,0,this.vieww,this.viewh);
                    }
                }
                else
                {
                    background.paint(g);
                    for (int i = 0; i < itemstage[stage - 1].length; i++)
                    {
                        item[i].paint(g);
                    }
                    for (int i = 0;
                         i <
                         mapstage[stage - 1][0][2] * mapstage[stage - 1][0][3];
                         i++)
                    {
                        map[i].paint(g);
                    }
                    tom.paint(g);
                    itemProcess(isItem(tom.x,tom.y,11,15));
                }
                break;
            case PAUSE:
                switch(pause)
                {
                    case PAUSE_UP:
                        if (mapy - 5 >= 0)
                        {
                            mapy = mapy - 5;
                        }
                        break;
                    case PAUSE_DOWN:
                        if (mapy + 5 <= maph - viewh)
                        {
                            mapy = mapy + 5;
                        }
                        break;
                    case PAUSE_LEFT:
                        if (mapx - 5 >= 0)
                        {
                            mapx = mapx - 5;
                        }
                        break;
                    case PAUSE_RIGHT:
                        if (mapx + 5 <= mapw - vieww)
                        {
                            mapx = mapx + 5;
                        }
                        break;
                }
                background.paint(g);
                for(int i=0;i<mapstage[stage-1][0][2]*mapstage[stage-1][0][3];i++)
                {
                    map[i].paint(g);
                }
                for(int i=0;i<itemstage[stage-1].length;i++)
                {
                    item[i].paint(g);
                }
                tom.paint(g);
                drawClipImage(g, vieww / 2 - 7, viewh / 2 - 7, imgitem[10], 0,
                              0, 15, 15);
                break;
            case LEFT_MOVE:
                if(mapx<=mapw-vieww-4)
                {
                    mapx = mapx + 4;
                }
                else
                {
                    status = RUNNING;
                }
                background.paint(g);
                for(int i=0;i<mapstage[stage-1][0][2]*mapstage[stage-1][0][3];i++)
                {
                    map[i].paint(g);
                }
                for(int i=0;i<itemstage[stage-1].length;i++)
                {
                    item[i].paint(g);
                }
                tom.paint(g);
                break;
            case RIGHT_MOVE:
                if(mapx>=4)
                {
                    mapx = mapx - 4;
                }
                else
                {

⌨️ 快捷键说明

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