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

📄 gamecanvas.java

📁 一个基于J2ME的手机纸牌游戏~~b/s系统~很简单~大家可以交流一下
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                    myCard[i - 1] = myCard[i];
                }
                myCard[cardCount - 1] = null;
                cardCount--;
            }
            return c;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    private void alert()// 警告信息
    {
        keyPress = false;
        backImage = Image.createImage(panel);
        messageBuffer.drawImage(messageImage, 0, 0, Graphics.LEFT
                | Graphics.TOP);

        bufferg.drawImage(messageBak, WIDTH / 2, HEIGHT / 2, Graphics.HCENTER
                | Graphics.VCENTER);
        repaint();
        new Thread()
        {
            public void run()
            {
                try
                {
                    sleep(1000);
                    bufferg.drawImage(backImage, WIDTH / 2, HEIGHT / 2,
                            Graphics.HCENTER | Graphics.VCENTER);
                    keyPress = true;
                    repaint();
                    backImage = null;
                }
                catch (Exception e)
                {
                }
            }
        }.start();
    }

    public void deal()// 处理FIRE按键和出牌等。。
    {
        if (selectIndex < 0)
        {
            return;
        }
        Card c = show(selectIndex, cardsRound);
        if (c == null)
        {
            return;
        }
        else
        {
            put(c);
            showButton(false);
            drawOtherCard(seatPos, c.getValue());
            client.sendMessage("show:" + seatPos + "," + c.getValue());
            myTurn = false;
            selectIndex = -1;
            return;
        }
    }

    private void put(Card c)// 把出牌放入每轮临时牌里
    {
        roundNum++;
        if (cardsRound.size() == 0)
            roundKey = c.getSuitIndex();
        else if (cardsRound.size() == 4)
        {
            roundKey = c.getSuitIndex();
            cardsRound.removeAllElements();
            clearDesk();
        }
        cardsRound.addElement(c);
    }

    private void clearDesk()// 清除玩家桌面上的牌,以便重新出牌
    {
        int x = WIDTH / 2 - cardWidth - cardWidth / 2;
        int y = HEIGHT / 2 - cardHeight - cardHeight / 2;
        int w = cardWidth * 3;
        int h = cardHeight * 2;
        int oldx = bufferg.getClipX();
        int oldy = bufferg.getClipY();
        int oldw = bufferg.getClipWidth();
        int oldh = bufferg.getClipHeight();

        bufferg.setClip(x, y, w, h);
        cls();
        repaint(x, y, w, h);
        bufferg.setClip(oldx, oldy, oldw, oldh);
    }

    public void receiveMessage(String message)// 接受信息
    {
        if (message.startsWith("turn"))
        {
            showButton(true);
            myTurn = true;
            return;
        }
        else if (message.startsWith("deliver"))
        {// 发牌的信息
            dinfo = false;
            gameOver = false;
            receiveCards(message);
            drawUBackCard();
            drawRBackCard();
            drawLBackCard();
            drawCards();
            return;
        }
        else if (message.startsWith("banker"))
        {// 庄的信息
            receiveBanker(message);
            return;
        }
        else if (message.startsWith("show"))
        {// //出牌信息
            receiveShow(message);
            return;
        }
        else if (message.startsWith("GameOver"))
        {// 结束的信息
            receiveScores(message);
            gameOver = true;
            gameOver();
            return;
        }
        else if (message.startsWith("exitgame"))
        {// 退出信息
            game.initialize();
            MyMidlet.display.setCurrent(game.getPlayerList());
            return;
        }
    }

    public void receiveShow(String message)// 出牌信息
    {
        int index0 = message.indexOf(":");
        int index1 = message.indexOf(",", index0 + 1);
        int seat = Integer.parseInt(message.substring(index0 + 1, index1));
        int card = Integer.parseInt(message.substring(index1 + 1));
        Card temp = new Card(card);
        put(temp);
        drawOtherCard(seat, card);
    }

    private void drawOtherCard(int seat, int card)// 从绘玩家出的牌
    {
        int x = -1, y = -1;
        if (seat == seatPos)
        {
            x = WIDTH / 2 - cardWidth / 2;
            y = HEIGHT / 2 - cardHeight / 2;
        }
        else if (seat == (seatPos + 1) % NUM)
        {
            RCount--;
            drawRBackCard();
            x = WIDTH / 2 + cardWidth / 2;
            y = HEIGHT / 2 - cardHeight;
        }
        else if (seat == (seatPos + 2) % NUM)
        {
            UCount--;
            drawUBackCard();
            x = WIDTH / 2 - cardWidth / 2;
            y = HEIGHT / 2 - cardHeight - cardHeight / 2;
        }
        else if (seat == (seatPos + 3) % NUM)
        {
            LCount--;
            drawLBackCard();
            x = WIDTH / 2 - cardWidth - cardWidth / 2;
            y = HEIGHT / 2 - cardHeight;
        }
        else
            return;

        try
        {
            bufferg.drawImage(new Card(card).getImage(), x, y, Graphics.LEFT
                    | Graphics.TOP);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        repaint();
    }

    private void drawCards()// 绘制自己的牌
    {
        int px = WIDTH / 2 - 13 * 10 / 2;
        int x = WIDTH / 2 - cardCount * 10 / 2;
        int y = HEIGHT - cardHeight - 10;

        int oldx = bufferg.getClipX();
        int oldy = bufferg.getClipY();
        int oldw = bufferg.getClipWidth();
        int oldh = bufferg.getClipHeight();
        bufferg.setClip(px, y - 8, 10 * 13 + 5, HEIGHT - 10);
        cls();
        try
        {
            int n = 0;
            for (int i = 0; i < cardCount; i++)
            {
                if (myCard[i] != null)
                {
                    int y2 = y;

                    if (n == getSelectIndex())
                    {
                        y2 -= 8;
                    }
                    bufferg.drawImage(myCard[i].getImage(), x, y2,
                            Graphics.LEFT | Graphics.TOP);
                    x += 10;
                    n++;
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        repaint();
        bufferg.setClip(oldx, oldy, oldw, oldh);
    }

    private void sortCard()
    {
        Card temp = null;
        for (int i = myCard.length - 1; i > 0; i--)
        {
            for (int j = 0; j < i; j++)
            {
                if (myCard[j].getSuitIndex() > myCard[i].getSuitIndex())
                {
                    temp = myCard[i];
                    myCard[i] = myCard[j];
                    myCard[j] = temp;
                }
                else if (myCard[i].getSuitIndex() == myCard[j].getSuitIndex()
                        && myCard[j].getCardIndex() < myCard[i].getCardIndex())
                {
                    temp = myCard[i];
                    myCard[i] = myCard[j];
                    myCard[j] = temp;
                }
            }
        }

    }

    private void receiveCards(String str)
    {
        String card = null;
        int count = 0, index1;
        int index0 = str.indexOf(",");
        while (count < myCard.length)
        {
            index1 = str.indexOf(",", index0 + 1);
            if (index1 > 0)
                card = str.substring(index0 + 1, index1);
            else
                card = str.substring(index0 + 1);
            myCard[count] = new Card(Integer.parseInt(card));
            index0 = index1;
            count++;
        }
        LCount = 13;
        RCount = 13;
        UCount = 13;
        sortCard();
    }

    private void receiveBanker(String str)
    {
        int index0 = str.indexOf(":");
        if (Integer.parseInt(str.substring(index0 + 1)) == seatPos)
        {
            banker = true;
        }
    }

    private void receiveScores(String str)
    {
        String card = null;
        int count = 0, index0 = 0, index1 = 0, indexs;
        index0 = str.indexOf(",");
        indexs = str.indexOf(":");
        for (int i = 0; i < NUM; i++)
        {
            count = 0;
            while (count < scores[i].length)
            {
                index1 = str.indexOf(",", index0 + 1);
                if (index1 > 0)
                    card = str.substring(index0 + 1, index1);
                else
                    card = str.substring(index0 + 1, indexs);
                if (!card.equals("-1"))
                    scores[i][count] = new Card(Integer.parseInt(card));
                else
                    scores[i][count] = null;
                index0 = index1;
                count++;
            }
        }
        index0 = indexs;
        String pscore = null;
        for (int i = 0; i < NUM; i++)
        {
            pscore = "";
            index1 = str.indexOf(":", index0 + 1);
            if (index1 > 0)
                pscore = str.substring(index0 + 1, index1);
            else
                pscore = str.substring(index0 + 1);
            scoreAmount.put(playerName[i], pscore);
            index0 = index1;
        }
    }

    public int getSelectIndex()
    {
        return selectIndex;
    }

    public void setSelectIndex(int selectIndex)
    {
        this.selectIndex = selectIndex;
    }

    private void drawTitle(Graphics g)
    {
        String t = "得分:本局得分/总分";
        String n = null;
        if (n == null)
        {
            g.setColor(0x000000);
            g.drawString(t, 40, 30, 20);
            return;
        }
        else
        {
            g.setColor(0x000000);
            t = String.valueOf(String.valueOf(n)).concat("失败,游戏结束!");
            g.drawString(t, 40, 30, 20);
            return;
        }
    }

    private void drawScore(Graphics buffer, int seat, Card[] cards,
            String name, int x1, int y1)
    {
        buffer.setColor(0x000000);
        String str = name + ":";
        buffer.drawString(str, x1, y1, 20);
        Font font = Font.getDefaultFont();
        int x = x1 + font.charsWidth(str.toCharArray(), 0, str.length());
        int y = y1;
        for (int i = 0; i < cards.length; i++)
        {
            if (cards[i] != null)
            {
                buffer.drawImage(cards[i].getImage(), x, y, 20);
                x += 8;
            }
        }
        buffer.drawString(String.valueOf(String.valueOf((new StringBuffer(""))
                .append(getScore(seat)).append("/").append(
                        scoreAmount.get(name)))), x + 7, y, 20);
    }

    public void gameOver()
    {
        int[] SCORE1 =
        { 10, 50 };
        int[] SCORE2 =
        { 10, 78 };
        int[] SCORE3 =
        { 10, 106 };
        int[] SCORE4 =
        { 10, 134 };
        backImage = Image.createImage(panel);
        drawTitle(scoreBuffer);
        drawScore(scoreBuffer, 0, scores[0], playerName[0], SCORE1[0],
                SCORE1[1]);
        drawScore(scoreBuffer, 1, scores[1], playerName[1], SCORE2[0],
                SCORE2[1]);
        drawScore(scoreBuffer, 2, scores[2], playerName[2], SCORE3[0],
                SCORE3[1]);
        drawScore(scoreBuffer, 3, scores[3], playerName[3], SCORE4[0],
                SCORE4[1]);

        bufferg.drawImage(scoreImage, WIDTH / 2, HEIGHT / 2, Graphics.HCENTER
                | Graphics.VCENTER);
        gameOver = true;

        repaint();
        addCommand(start);
        addCommand(exitCmd);
        return;
    }

    public int getScore(int seat)
    {
        int rtn = 0;

        for (int i = 0; i < scores[seat].length; i++)
        {
            if (scores[seat][i] != null)
            {
                rtn = (i >= 13) ? (rtn + 13) : rtn + 1;
            }
        }
        return rtn;
    }

    private void drawInfo(Graphics g)
    {
        int w = infoImage.getWidth();
        int h = infoImage.getHeight();
        int x = (WIDTH - w) / 2;
        int y = (HEIGHT - h) / 2;

        g.drawImage(infoImage, WIDTH / 2, HEIGHT / 2, Graphics.HCENTER
                | Graphics.VCENTER);
        g.drawImage(playerImg[seatPos % 2], x + 5, y + 5, Graphics.LEFT
                | Graphics.TOP);
        Font f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_ITALIC,
                Font.SIZE_MEDIUM);
        g.setFont(f);
        g.drawString("昵  称:" + "player" + seatPos, x + 65, y + 30,
                Graphics.LEFT | Graphics.TOP);
        g.drawString("端  口:" + client.getPort(), x + 65, y + 60, Graphics.LEFT
                | Graphics.TOP);
        g.drawString("桌  号:" + desknum, x + 65, y + 90, Graphics.LEFT
                | Graphics.TOP);
        g.drawString("座  号:" + seatPos, x + 65, y + 120, Graphics.LEFT
                | Graphics.TOP);
    }
}

⌨️ 快捷键说明

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