Search in sources :

Example 6 with Display2D

use of edu.mit.d54.Display2D in project d54 by mitrisdev.

the class PongPlugin method loop.

@Override
protected void loop() {
    Display2D display = getDisplay();
    byte userInput = 0;
    if (!userInputList.isEmpty())
        userInput = userInputList.remove(0);
    switch(gameState) {
        case IDLE:
            // initialize 
            animTime = 0;
            animTimeLastStep = 0;
            vert = 0;
            p1Score = 0;
            p2Score = 0;
            gameState = State.IDLE_ANIM;
            break;
        case IDLE_ANIM:
            // show title until user presses button
            showTitle();
            switch(userInput) {
                case 'L':
                case 'R':
                case 'U':
                case 'D':
                    gameState = State.PLAY_START;
                    animTime = 0;
            }
            break;
        case PLAY_START:
            animTime += timestep;
            showNumber(p1Score, 2, 0, 255, 255, 255);
            // show player's scores
            showNumber(p2Score, 2, 9, 255, 255, 255);
            // reset the play area
            p1Pos = 4;
            p2Pos = 4;
            ballPosX = 4;
            ballPosY = 8;
            ballStepTime = 0.5;
            ballLastStep = 0.0;
            if (animTime > 3.0) {
                // wait 3 seconds and start play
                gameState = State.GAME;
            }
            break;
        case GAME:
            animTime += timestep;
            // draws the frog and all enabled cars in their corresponding colour
            drawScreen();
            // checks whether it's time to move the ball and does so if necessary 
            moveBall();
            // move paddles
            switch(userInput) {
                case // player 1 Move Left
                'L':
                    if (p1Pos > 0) {
                        p1Pos = p1Pos - 1;
                    }
                    break;
                case // player 2 Move Right
                'R':
                    if (p2Pos < 7) {
                        p2Pos = p2Pos + 1;
                    }
                    break;
                case // player 1 Move Right
                'U':
                    if (p1Pos < 7) {
                        p1Pos = p1Pos + 1;
                    }
                    break;
                case // player 2 Move Left
                'D':
                    if (p2Pos > 0) {
                        p2Pos = p2Pos - 1;
                    }
                    break;
                case //there was an error in the network socket or no client connected -- "pause" the game
                -1:
                    return;
            }
            break;
        case // win
        GAME_END_2:
            animTime += timestep;
            if (animTime < 5) {
                showP(255, 255, 255);
                showNumber(winner, 2, 9, 255, 255, 255);
            } else {
                if (animTime < 10) {
                    // write WIN on the screen
                    showWin();
                }
            }
            if (animTime > 10.0) {
                // wait a bit longer before going back to title screen
                gameState = State.IDLE;
            }
            break;
    }
}
Also used : Display2D(edu.mit.d54.Display2D)

Example 7 with Display2D

use of edu.mit.d54.Display2D in project d54 by mitrisdev.

the class LifePlugin method loop.

@Override
protected void loop() {
    Display2D d = getDisplay();
    incrementColor();
    m_nFrameRateTimer--;
    boolean bDoUpdate = m_nFrameRateTimer <= 0;
    if (bDoUpdate) {
        m_nFrameRateTimer = m_nFrameRatePeriod;
        // Determine next state from number of live neighbors
        for (int iCol = 0; iCol < m_Cells.length; ++iCol) {
            LifeCell[] rows = m_Cells[iCol];
            for (int iRow = 0; iRow < rows.length; ++iRow) {
                LifeCell cell = m_Cells[iCol][iRow];
                int nLiveNeighbors = getWorldWrapNeighborCount(iCol, iRow);
                if (cell.IsAlive()) {
                    boolean bLivesOn = nLiveNeighbors == 2 || nLiveNeighbors == 3;
                    cell.SetNextState(bLivesOn);
                } else {
                    boolean bLives = nLiveNeighbors == 3;
                    cell.SetNextState(bLives);
                }
            }
        }
    }
    // Update all cells
    boolean bHasChangedState = false;
    for (int iCol = 0; iCol < m_Cells.length; ++iCol) {
        LifeCell[] rows = m_Cells[iCol];
        for (int iRow = 0; iRow < rows.length; ++iRow) {
            LifeCell cell = m_Cells[iCol][iRow];
            bHasChangedState = (cell.UpdateLife() || bHasChangedState);
            if (cell.IsAlive()) {
                d.setPixelHSB(iCol, iRow, m_fColor, 1, 1);
            } else {
                d.setPixelHSB(iCol, iRow, 0, 0, 0);
            }
        }
    }
    if (bDoUpdate && !bHasChangedState) {
        m_nUnchangedStateCounter++;
    }
    if (m_nUnchangedStateCounter >= kNumUnchangedFramesBeforeReseting) {
        reset();
    }
}
Also used : Display2D(edu.mit.d54.Display2D)

Example 8 with Display2D

use of edu.mit.d54.Display2D in project d54 by mitrisdev.

the class LifePlugin method reset.

private void reset() {
    m_nFrameRateTimer = 0;
    m_nUnchangedStateCounter = 0;
    Display2D d = getDisplay();
    m_Cells = new LifeCell[d.getWidth()][d.getHeight()];
    for (int iCol = 0; iCol < d.getWidth(); ++iCol) {
        for (int iRow = 0; iRow < d.getHeight(); ++iRow) {
            m_Cells[iCol][iRow] = new LifeCell(iCol, iRow);
            m_Cells[iCol][iRow].SetLife(false);
            if (java.lang.Math.random() < 0.25f) {
                m_Cells[iCol][iRow].SetLife(true);
            }
        }
    }
}
Also used : Display2D(edu.mit.d54.Display2D)

Example 9 with Display2D

use of edu.mit.d54.Display2D in project d54 by mitrisdev.

the class MarioPlugin method loop.

@Override
protected void loop() {
    if (gameState != State.IDLE) {
        if (!controller.isConnected())
            return;
    }
    Display2D display = getDisplay();
    Graphics2D g = display.getGraphics();
    switch(gameState) {
        case IDLE:
            int startScreenDisplayPosition = -1 * (startScreenScrollPosition / startScreenStepsPerFrame);
            if (startScreenDisplayPosition == (-1 * startScreenResetWidth)) {
                startScreenScrollPosition = 0;
            } else {
                startScreenScrollPosition++;
            }
            g.drawImage(startScreen, startScreenDisplayPosition, 0, null);
            break;
        case GAME:
            level.applyLevelColoring(display);
            switch(level.iterateActors(display)) {
                case KILLED:
                    lives--;
                    if (lives == 0) {
                        gameState = State.GAME_OVER;
                    } else {
                        gameState = State.LOAD_LEVEL;
                    }
                    break;
                case COMPLETE:
                    currentLevelIndex++;
                    // finished the levels regular and bonus
                    if (currentLevelIndex == levelMaps.size() && inBonusMode) {
                        // done! say good job and reset
                        gameState = State.COMPLETE;
                    } else if (currentLevelIndex == levelMaps.size() && !inBonusMode) {
                        currentLevelIndex = 0;
                        inBonusMode = true;
                        gameState = State.LOAD_LEVEL;
                    } else {
                        // load the next level and play!
                        gameState = State.LOAD_LEVEL;
                    }
                    break;
                case ACTIVE:
                default:
                    break;
            }
            break;
        case LOAD_LEVEL:
            try {
                level = new Level(levelMaps.get(currentLevelIndex), frameTime, inBonusMode);
                gameState = State.GAME;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            break;
        case COMPLETE:
            int completeScreenDisplayPosition = -1 * (completeScreenScrollPosition / completeScreenStepsPerFrame);
            if (completeScreenDisplayPosition == (-1 * completeScreenResetWidth)) {
                init();
                gameState = State.IDLE;
            } else {
                completeScreenScrollPosition++;
            }
            g.drawImage(completeScreen, completeScreenDisplayPosition, 0, null);
            break;
        case GAME_OVER:
            int gameOverScreenDisplayPosition = -1 * (gameOverScreenScrollPosition / gameOverScreenStepsPerFrame);
            if (gameOverScreenDisplayPosition == (-1 * gameOverScreenResetWidth)) {
                init();
                gameState = State.IDLE;
            } else {
                gameOverScreenScrollPosition++;
            }
            g.drawImage(gameOverScreen, gameOverScreenDisplayPosition, 0, null);
            break;
        default:
            break;
    }
}
Also used : Display2D(edu.mit.d54.Display2D) IOException(java.io.IOException) Graphics2D(java.awt.Graphics2D)

Example 10 with Display2D

use of edu.mit.d54.Display2D in project d54 by mitrisdev.

the class Plugin2048 method loop.

@Override
protected void loop() {
    Display2D display = getDisplay();
    Graphics2D gr = display.getGraphics();
    time += dt;
    switch(gameState) {
        case IDLE:
            break;
        default:
            if (!controller.isConnected())
                return;
            if (gameState == State.WIN || gameState == State.LOSE) {
                animTime += timestep;
                if (animTime - animTimeLastStep >= TEXT_ANIM_STEP) {
                    animTimeLastStep = animTime;
                    textPos++;
                    if (textPos > 50)
                        gameState = State.IDLE;
                }
                gr.setFont(PixelFont.getInstance());
                if (gameState == State.WIN)
                    gr.drawString("YOU WIN", 10 - textPos, 16);
                else
                    gr.drawString("YOU LOSE", 10 - textPos, 16);
            }
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    int r = 0;
                    int g = 0;
                    int b = 0;
                    boolean rotate_v = false;
                    boolean rotate_h = false;
                    switch(board[i][j]) {
                        //2
                        case 1:
                            r = 255;
                            g = 0;
                            b = 0;
                            break;
                        case 2:
                            r = 255;
                            g = 255;
                            b = 0;
                            break;
                        //8
                        case 3:
                            r = 0;
                            g = 255;
                            b = 0;
                            break;
                        //16
                        case 4:
                            r = 0;
                            g = 255;
                            b = 255;
                            break;
                        //32
                        case 5:
                            r = 0;
                            g = 0;
                            b = 255;
                            break;
                        //64
                        case 6:
                            r = 255;
                            g = 0;
                            b = 255;
                            break;
                        //128
                        case 7:
                            r = 255;
                            g = 255;
                            b = 255;
                            break;
                        //256
                        case 8:
                            r = 255;
                            g = 0;
                            b = 0;
                            rotate_v = true;
                            break;
                        //512
                        case 9:
                            r = 0;
                            g = 255;
                            b = 0;
                            rotate_v = true;
                            break;
                        //1024
                        case 10:
                            r = 0;
                            g = 0;
                            b = 255;
                            rotate_v = true;
                            break;
                        //2048
                        case 11:
                            r = 255;
                            g = 0;
                            b = 0;
                            rotate_h = true;
                            break;
                    }
                    float[][] hsb = new float[4][3];
                    for (int k = 0; k < 4; k++) {
                        Color.RGBtoHSB(r, g, b, hsb[k]);
                        if (rotate_v)
                            hsb[k][2] = (float) (0.5 * (Math.cos(2 * Math.PI * (time / ROTATE_PERIOD + k / 4f)) + 1));
                        if (rotate_h)
                            hsb[k][0] = (float) (time / ROTATE_PERIOD + (k / 4f)) % 1;
                    }
                    display.setPixelHSB(2 * j + BASE_X, 2 * i + BASE_Y, hsb[0][0], hsb[0][1], hsb[0][2]);
                    display.setPixelHSB(2 * j + BASE_X, 2 * i + 1 + BASE_Y, hsb[1][0], hsb[1][1], hsb[1][2]);
                    display.setPixelHSB(2 * j + 1 + BASE_X, 2 * i + 1 + BASE_Y, hsb[2][0], hsb[2][1], hsb[2][2]);
                    display.setPixelHSB(2 * j + 1 + BASE_X, 2 * i + BASE_Y, hsb[3][0], hsb[3][1], hsb[3][2]);
                }
            }
    }
}
Also used : Display2D(edu.mit.d54.Display2D) Graphics2D(java.awt.Graphics2D)

Aggregations

Display2D (edu.mit.d54.Display2D)13 Graphics2D (java.awt.Graphics2D)5 DisplayPanel (edu.mit.d54.DisplayPanel)1 GBDisplay (edu.mit.d54.GBDisplay)1 BasicStroke (java.awt.BasicStroke)1 Color (java.awt.Color)1 Ellipse2D (java.awt.geom.Ellipse2D)1 BufferedImage (java.awt.image.BufferedImage)1 IOException (java.io.IOException)1 JFrame (javax.swing.JFrame)1