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

📄 galaxywarcanvas.java

📁 经典的宇宙大战游戏代码(坦克大战)
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                isStartWait = false;
            }

        }
        //作输入处理
        switch (key) {
        case KEY_NUM2:
            if (thisChar == 0) {
                thisChar = 'A';
                typedInfo = typedInfo + (char)thisChar;
                isStartWait = true;
                charWaitCounter = 0;
            } else {
                if (thisChar >= 'A' && thisChar <= 'C' && isStartWait
                    || typedInfo.length() == SCORELEN) {
                    charWaitCounter = 0;
                    thisChar++;
                    if (thisChar > 'C')
                        thisChar = 'A';
                    typedInfo = typedInfo.substring(0, typedInfo.length() - 1)
                    + (char)thisChar;
                }else {
                    thisChar = 'A';
                    typedInfo = typedInfo + (char)thisChar;
                    isStartWait = true;
                    charWaitCounter = 0;
                }
            }
            break;
        case KEY_NUM3:
            if (thisChar == 0) {
                thisChar = 'D';
                typedInfo = typedInfo + (char)thisChar;
                isStartWait = true;
                charWaitCounter = 0;
            } else {
                if (thisChar >= 'D' && thisChar <= 'F' && isStartWait
                    || typedInfo.length() == SCORELEN) {
                    charWaitCounter = 0;
                    thisChar++;
                    if (thisChar > 'F')
                        thisChar = 'D';
                    typedInfo = typedInfo.substring(0, typedInfo.length() - 1)
                        + (char)thisChar;
                }else {
                    thisChar = 'D';
                    typedInfo = typedInfo + (char)thisChar;
                    isStartWait = true;
                    charWaitCounter = 0;
                }
            }
            break;
        case KEY_NUM4:
            if (thisChar == 0) {
                thisChar = 'G';
                typedInfo = typedInfo + (char)thisChar;
                isStartWait = true;
                charWaitCounter = 0;
            } else {
                if (thisChar >= 'G' && thisChar <= 'I' && isStartWait
                    || typedInfo.length() == SCORELEN) {
                    charWaitCounter = 0;
                    thisChar++;
                    if (thisChar > 'I')
                        thisChar = 'G';
                    typedInfo = typedInfo.substring(0, typedInfo.length() - 1)
                        + (char)thisChar;
                }else {
                    thisChar = 'G';
                    typedInfo = typedInfo + (char)thisChar;
                    isStartWait = true;
                    charWaitCounter = 0;
                }
            }
            break;
        case KEY_NUM5:
            if (thisChar == 0) {
                thisChar = 'J';
                typedInfo = typedInfo + (char)thisChar;
                isStartWait = true;
                charWaitCounter = 0;
            } else {
                if (thisChar >= 'J' && thisChar <= 'L' && isStartWait
                    || typedInfo.length() == SCORELEN) {
                    charWaitCounter = 0;
                    thisChar++;
                    if (thisChar > 'L')
                        thisChar = 'J';
                    typedInfo = typedInfo.substring(0, typedInfo.length() - 1)
                        + (char)thisChar;
                }else {
                    thisChar = 'J';
                    typedInfo = typedInfo + (char)thisChar;
                    isStartWait = true;
                    charWaitCounter = 0;
                }
            }
            break;
        case KEY_NUM6:
            if (thisChar == 0) {
                thisChar = 'M';
                typedInfo = typedInfo + (char)thisChar;
                isStartWait = true;
                charWaitCounter = 0;
            } else {
                if (thisChar >= 'M' && thisChar <= 'O' && isStartWait
                    || typedInfo.length() == SCORELEN) {
                    thisChar++;
                    charWaitCounter = 0;
                    if (thisChar > 'O')
                        thisChar = 'M';
                    typedInfo = typedInfo.substring(0, typedInfo.length() - 1)
                        + (char)thisChar;
                }else {
                    thisChar = 'M';
                    typedInfo = typedInfo + (char)thisChar;
                    isStartWait = true;
                    charWaitCounter = 0;
                }
            }
            break;
        case KEY_NUM7:
            if (thisChar == 0) {
                thisChar = 'P';
                typedInfo = typedInfo + (char)thisChar;
                isStartWait = true;
                charWaitCounter = 0;
            } else {
                if (thisChar >= 'P' && thisChar <= 'S' && isStartWait
                    || typedInfo.length() == SCORELEN) {
                    charWaitCounter = 0;
                    thisChar++;
                    if (thisChar > 'S')
                        thisChar = 'P';
                    typedInfo = typedInfo.substring(0, typedInfo.length() - 1)
                        + (char)thisChar;
                }else {
                    thisChar = 'P';
                    typedInfo = typedInfo + (char)thisChar;
                    isStartWait = true;
                    charWaitCounter = 0;
                }
            }
            break;
        case KEY_NUM8:
            if (thisChar == 0) {
                thisChar = 'T';
                typedInfo = typedInfo + (char)thisChar;
                isStartWait = true;
                charWaitCounter = 0;
            } else {
                if (thisChar >= 'T' && thisChar <= 'V' && isStartWait
                    || typedInfo.length() == SCORELEN) {
                    thisChar++;
                    charWaitCounter = 0;
                    if (thisChar > 'V')
                        thisChar = 'T';
                    typedInfo = typedInfo.substring(0, typedInfo.length() - 1)
                        + (char)thisChar;
                }else {
                    thisChar = 'T';
                    typedInfo = typedInfo + (char)thisChar;
                    isStartWait = true;
                    charWaitCounter = 0;
                }
            }
            break;
        case KEY_NUM9:
            if (thisChar == 0) {
                thisChar = 'W';
                typedInfo = typedInfo + (char)thisChar;
                isStartWait = true;
                charWaitCounter = 0;
            } else {
                if (thisChar >= 'W' && thisChar <= 'Z' && isStartWait
                    || typedInfo.length() == SCORELEN) {
                    thisChar++;
                    charWaitCounter = 0;
                    if (thisChar > 'Z')
                        thisChar = 'W';
                    typedInfo = typedInfo.substring(0, typedInfo.length() - 1)
                        + (char)thisChar;
                }else {
                    thisChar = 'W';
                    typedInfo = typedInfo + (char)thisChar;
                    isStartWait = true;
                    charWaitCounter = 0;
                }
            }
            break;
        }
    }

    private void flowReport() {
    }

    private void drawInit(Graphics g) {
        int guageLen;
        int guageWidth = width >> 1, guageHeight = 8;
        int startX = (width - guageWidth) >> 1;
        int startY = height - (height >> 2);
        int infoWidth;
        String drawInfo = "Version 1.00";
        if (loadedCounter >= 0 && loadedCounter <= totalLoad) {
            guageLen = (loadedCounter * (guageWidth - 4)) / totalLoad;
            g.drawImage(img, 0, 0, g.LEFT | g.TOP);
            g.setColor(0x00FFFFFF);
            g.drawRect(startX, startY, guageWidth, guageHeight);
            g.setColor(0x00505058);
            g.fillRect(startX + 1, startY + 1, guageLen, guageHeight - 2);
            infoWidth = getStringWidth(drawInfo);
            startX = (width - infoWidth) >> 1;
            startY = startY + guageHeight + 4;
            g.setColor(0x00FFFFFF);
            g.drawString(drawInfo,startX,startY,g.LEFT | g.TOP);
            g.setColor(0x00000000);
            g.drawString(drawInfo,startX + 1,startY + 1,g.LEFT | g.TOP);
        }
    }


    private void drawLoading(Graphics g) {
        int guageLen;
        int guageWidth = width >> 1, guageHeight = 8;
        int startX = (width - guageWidth) >> 1;
        int startY = (height - guageHeight - font.getHeight() - 20) >> 1;
        String info = "Loading......";
        if (loadedCounter > 0 && loadedCounter < totalLoad) {
            guageLen = (loadedCounter * (guageWidth - 4)) / totalLoad;
            g.drawImage(panel_up, 0, 0, g.LEFT | g.TOP);
            g.setColor(0x00FFFFFF);
            g.drawString(info
                ,(width - getStringWidth(info)) >> 1
                ,startY,g.LEFT | g.TOP);
            g.setColor(0x00FFFFFF);
            g.drawRect(startX, startY + font.getHeight() + 20
                , guageWidth, guageHeight);
            g.setColor(0x00505058);
            g.fillRect(startX + 1, startY + font.getHeight() + 20 + 1
                , guageLen, guageHeight - 2);
        }
    }


    //还要根据读出的关卡数增加继续功能
    private void drawMenu(Graphics g) {
        int menuID;
        String info = "";
        int infoWidth;
        int startX,startY;
        //panel的尺寸必须于屏幕实际尺寸相符
        g.drawImage(panel_up, 0, 0, g.TOP | g.LEFT);
        for (int i=0;i<showedNum;i++) {
            menuID = menu[i];
            if (menuID == currentMenu)
                g.setColor(0x00FF0000);
            else
                g.setColor(0x00FFFF00);
            switch(menuID) {
            case 0:
                info = "Continue";
                break;
            case 1:
                info = "New Game";
                break;
            case 2:
                info = "High Score";
                break;
            case 3:
                info = "Internet Ranking";
                break;
            case 4:
                info = "Options";
                break;
            case 5:
                info = "Help";
                break;
            case 6:
                info = "About";
                break;
            case 7:
                info = "Exit Game";
                break;
            }
            if (menuID >= MENU_NUM) continue;
            infoWidth = getStringWidth(info);
            startX = (width - infoWidth) >> 1;
            startY = panel_up.getHeight() + SPACING2
                + (font.getHeight() + SPACING) * i;
            g.drawString(info,startX,startY,g.LEFT | g.TOP);
        }
        drawHintArrow(g,MENU_NUM);
    }

    private void drawPauseMenu(Graphics g) {
        int menuID;
        String info = "";
        int infoWidth;
        int startX,startY;
        //panel的尺寸必须于屏幕实际尺寸相符
        g.drawImage(panel_up, 0, 0, g.TOP | g.LEFT);
        for (int i=0;i<showedNum;i++) {
            menuID = menu[i];
            if (menuID == currentMenu)
                g.setColor(0x00FF0000);
            else
                g.setColor(0x00FFFF00);
            switch(menuID) {
            case 0:
                info = "Restart";
                break;
            case 1:
                info = "Options";
                break;
            case 2:
                info = "Help";
                break;
            case 3:
                info = "Menu";
                break;
            }
            if (menuID > PAUSE_MENU_NUM) continue;
            infoWidth = getStringWidth(info);
            startX = (width - infoWidth) >> 1;
            startY = panel_up.getHeight() + SPACING2
                + (font.getHeight() + SPACING) * i;
            g.drawString(info,startX,startY,g.LEFT | g.TOP);
        }
        drawHintArrow(g,PAUSE_MENU_NUM);
    }

    //地图元素坐标 = 元素绝对坐标 - 玩家当前绝对坐标 + 屏幕偏移量
    private void drawMap(Graphics g) {
        Tank p = player[0];
        int pX,pY,drawX,drawY;
        byte mapIndex;
        pX = p.x;
        pY = p.y;
        //draw map
        for (int y = startMapGy; y < endMapGy; y++) {
            for (int x = startMapGx; x < endMapGx; x++) {
                if (osX == 0)
                    drawX = x * IMAGESIZE;
                else if (osX != offsetX)
                    drawX = x * IMAGESIZE - osX;
                else
                    drawX = x * IMAGESIZE - pX + osX;
                if (osY == 0)
                    drawY = y * IMAGESIZE;
                else if (osY != offsetY)
                    drawY = y * IMAGESIZE - osY;
                else
                    drawY = y * IMAGESIZE - pY + osY;
                mapIndex = map[y][x];
                g.setClip(drawX, drawY, IMAGESIZE,IMAGESIZE);
                g.drawImage(mapImg, drawX - mapIndex * IMAGESIZE, drawY,
                    Graphics.TOP | Graphics.LEFT);
            }
        }
        g.setClip(0, 0, width,height);
    }

    //留意Boss图片的坐标
    private void drawPlayer(Graphics g) {
        //"distance" is the distance between two hex
        Tank p,currentP = player[0];
        int pX,pY;
        for (int i = maxPlayer -1; i >= 0; i--) {
            p = player[i];
            if (p.isDead) continue;
            if (p.type == BOSS)
                imageSize = BOSSSIZE;
            else
                imageSize = IMAGESIZE;
            if (osX == 0)
                pX = p.x;
            else if (osX != offsetX)
                pX = p.x - osX;
            else
                pX = p.x - currentP.x + osX;
            if (osY == 0)
                pY = p.y;

⌨️ 快捷键说明

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