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

📄 gamescreen.java

📁 利用javame开发的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            g.drawImage(images[wheelValues[bottomImageNumber]],
                        wheelLeft,
                        bottomImageTopOffset,
                        Graphics.TOP | Graphics.LEFT);
        }

        // write the credit in top left and bet in top right
        g.setClip(0, 0, width, height);
        g.setColor(255, 255, 255);
        Font font = Font.getFont(Font.FACE_PROPORTIONAL,
                                 Font.STYLE_PLAIN,
                                 Font.SIZE_SMALL);
        g.drawString(Integer.toString(account.getCredit()),
                     0, 0,
                     Graphics.TOP | Graphics.LEFT);
        g.drawString(Integer.toString(account.getCurrentBet()),
                     width-1, 0,
                     Graphics.TOP | Graphics.RIGHT);
    }


    // called by the UI thread to notify us of a menu action
    public void commandAction(Command c, Displayable d)
    {
        // can quit any time
        if (c == quitCommand)
        {
            quitAction();
        }
        else if (readyForInput)
        {
            // these commands can't be done while the wheels are spinning
            if (c == spinCommand)
            {
                spinAction();
            }
            else if (c == changeBetCommand)
            {
                changeBetAction();
            }
            else if (c == buyCreditCommand)
            {
                buyCreditAction();
            }
        }
    }


    // called by the UI thread to notify us of a key press which isn't
    // a menu action; here we just use it for Hold keys
    public void keyPressed(int keyCode)
    {
        if (!readyForInput)
        {
            return;
        }

        if (getGameAction(keyCode) == FIRE)
        {
            spinAction();
        }

        switch (keyCode)
        {
        case KEY_NUM1:
        case KEY_NUM4:
        case KEY_NUM7:
            toggleHoldAction(0);
            break;
        case KEY_NUM2:
        case KEY_NUM5:
        case KEY_NUM8:
            toggleHoldAction(1);
            break;
        case KEY_NUM3:
        case KEY_NUM6:
        case KEY_NUM9:
            toggleHoldAction(2);
            break;
        default:
            break;
        }
    }


    private void quitAction()
    {
        if (quitting)
        {
            // they asked a second time; don't wait for logout request
            // to return a response
            midlet.gameScreenQuit();
        }
        else
        {
            try
            {
                sendLogoutRequest();
                readyForInput = false;
                quitting = true;
            }
            catch (IOException e)
            {
                ErrorScreen.showError("Communications error");
            }
        }
    }


    private void spinAction()
    {
        try
        {
            sendSpinRequest();
            readyForInput = false;
            spinning = true;
            spinningFreely = true;
            synchronized (this)
            {
                notifyAll();     // wake up animation thread
            }
        }
        catch (IOException e)
        {
            ErrorScreen.showError("Communications error");
        }
    }


    private void changeBetAction()
    {
        midlet.gameScreenChangeBet(isHolding(), previousBet);
    }


    private void buyCreditAction()
    {
        midlet.gameScreenBuyCredit();
    }


    private void toggleHoldAction(int i)
    {
        if (hold[i])
        {
            // can always remove a hold
            hold[i] = false;
            repaint(0, 0, getWidth(), getHeight());
        }
        else
        {
            if (newSpin)
            {
                ErrorScreen.showError("Can't hold before first spin");
            }
            else if (account.getCurrentBet() > previousBet)
            {
                ErrorScreen.showError("Can't increase bet AND hold");
            }
            else
            {
                hold[i] = true;
                repaint(0, 0, getWidth(), getHeight());
            }
        }
    }


    private void sendSpinRequest()
        throws IOException
    {
        ValueGenerator generator = new ValueGenerator();
        generator.addValue("spin");
        generator.addValue(Integer.toString(account.getCurrentBet()));
        generator.addValue(hold[0] ? "true" : "false");
        generator.addValue(hold[1] ? "true" : "false");
        generator.addValue(hold[2] ? "true" : "false");
        String dataStr = generator.getString();
        httpPoster.sendRequest(dataStr, this);
    }


    private void sendLogoutRequest()
        throws IOException
    {
        ValueGenerator generator = new ValueGenerator();
        generator.addValue("logout");
        String dataStr = generator.getString();
        httpPoster.sendRequest(dataStr, this);
    }


    private void setInvalidEndPositions()
    {
        synchronized (wheelPosition)
        {
            wheelPosition[0] = imageHeight / 3;
            wheelPosition[1] = imageHeight / 2;
            wheelPosition[2] = 2 * imageHeight / 3;
        }
    }


    private void clearHolds()
    {
        hold[0] = false;
        hold[1] = false;
        hold[2] = false;
    }


    private boolean isHolding()
    {
        return hold[0] || hold[1] || hold[2];
    }


    private void setEndPositions(int wheel0, int wheel1, int wheel2)
    {
        synchronized (wheelPosition)

        {
            calculateSpinTicks(0, wheel0 * imageHeight);
            calculateSpinTicks(1, wheel1 * imageHeight);
            calculateSpinTicks(2, wheel2 * imageHeight);
        }
        spinningFreely = false;   // start calculated spindown
    }


    // Calculate for given wheel how to make it stop at the given place.
    // These calculations are a bit tricky because the wheel is circular,
    // so there's some 'modulo' adjustments going on.
    private void calculateSpinTicks(int i, int endPosition)
    {
        // first calculate how long it takes to spin down to stopped from
        // spinSpeed[i] - 1, spending SPINDOWN_STEP ticks at each speed
        int spindownDistance = 0;
        int speed = spinSpeed[i] - 1;
        if (speed > 0)
        {
            // sum (n=1..N) n = 0.5 * N * (N+1)
            spindownDistance = SPINDOWN_STEP * speed * (speed + 1) / 2;
        }
        spindownTicks[i] = (spinSpeed[i] - 1) * SPINDOWN_STEP;

        // now calculate how we adjust before the spin down, by spinning some
        // ticks at spinSpeed[i] then some ticks at spinSpeed[i]-1
        for (;; endPosition += wheelHeight)
        {
            // calculate how far we need to spin in 'adjustment mode'
            int adjustment = endPosition - wheelPosition[i] - spindownDistance;
            if (adjustment >= 0)
            {
                // We must achieve this with a certain number of ticks at
                // spinSpeed[i], then a certain number at spinSpeed[i]-1.
                int adjustmentTicks = (adjustment + spinSpeed[i] - 1) /
                                                              spinSpeed[i];
                slowerTicks[i] = adjustmentTicks * spinSpeed[i] -
                                                              adjustment;
                fasterTicks[i] = adjustmentTicks - slowerTicks[i];
                if (fasterTicks[i] >= 0)
                {
                    break;
                }
            }

            // if we got here, it wasn't possible to spin down without
            // spinning the wheel at least once more; so do that and
            // try again
        }
    }


    public void receiveHttpResponse(String responseStr)
    {
        ValueParser parser = new ValueParser(responseStr);
        String response = parser.getNextValue();
        if ("spin-win".equals(response))    // no harm if response is null
        {
            try
            {
                wasWin = true;
                winAmount = parser.getNextIntValue();
                updatedCredit = parser.getNextIntValue();
                int wheel0 = parser.getNextIntValue();
                int wheel1 = parser.getNextIntValue();
                int wheel2 = parser.getNextIntValue();
                setEndPositions(wheel0, wheel1, wheel2);
            }
            catch (NumberFormatException e)
            {
                ErrorScreen.showError("Server error");
                setInvalidEndPositions();
                spinning = false;
                readyForInput = true;
            }
        }
        else if ("spin-lose".equals(response))
        {
            try
            {
                wasWin = false;
                updatedCredit = parser.getNextIntValue();
                int wheel0 = parser.getNextIntValue();
                int wheel1 = parser.getNextIntValue();
                int wheel2 = parser.getNextIntValue();
                setEndPositions(wheel0, wheel1, wheel2);
            }
            catch (NumberFormatException e)
            {
                ErrorScreen.showError("Server error");
                setInvalidEndPositions();
                spinning = false;
                readyForInput = true;
            }
        }
        else if ("spin-error".equals(response))
        {
            String errorMessage = parser.getNextValue();
            if (errorMessage != null)
            {
                ErrorScreen.showError(errorMessage);
            }
            else
            {
                ErrorScreen.showError("Server error");
            }
            setInvalidEndPositions();
            spinning = false;
            readyForInput = true;
        }
        else if ("logout-OK".equals(response))
        {
            midlet.gameScreenQuit();
            readyForInput = true;
        }
        else if ("logout-error".equals(response))
        {
            String errorMessage = parser.getNextValue();
            if (errorMessage != null)
            {
                ErrorScreen.showError(errorMessage);
            }
            else
            {
                ErrorScreen.showError("Server error");
            }
            midlet.gameScreenQuit();
            readyForInput = true;
        }
        else
        {
            ErrorScreen.showError("Server error");
            setInvalidEndPositions();
            spinning = false;
            readyForInput = true;
        }
    }


    public void handleHttpError(String errorStr)
    {
        ErrorScreen.showError("Error communicating with server");
        setInvalidEndPositions();
        spinning = false;
        readyForInput = true;
    }
}

⌨️ 快捷键说明

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