Search in sources :

Example 1 with Display2D

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

the class MITrisPlugin method loop.

@Override
protected void loop() {
    Display2D display = getDisplay();
    Graphics2D g = display.getGraphics();
    switch(gameState) {
        case IDLE:
            animTime = 0;
            animTimeLastStep = 0;
            logoPos = 0;
            gameState = State.IDLE_ANIM;
            break;
        case IDLE_ANIM:
            animTime += timestep;
            if (animTime - animTimeLastStep >= LOGO_ANIM_STEP) {
                animTimeLastStep = animTime;
                logoPos++;
                if (logoPos > 100)
                    gameState = State.IDLE;
            }
            g.drawImage(mitrisLogo, 10 - logoPos, 6, null);
            break;
        case GAME_START:
            mitrisGame = new MITrisGame(width, height, timestep);
            gameState = State.GAME;
            break;
        case GAME:
            if (!controller.isConnected())
                return;
            //move piece down if it's time
            mitrisGame.clockTick();
            gameDisplayTime = mitrisGame.getTime();
            sendBoardToDisplay(mitrisGame.getDisplayBoard(), display);
            if (mitrisGame.isGameOver()) {
                System.out.println(String.format("Game over! Lines cleared: %d  Time: %3.1f", mitrisGame.getDisplayBoard().getNumCleared(), mitrisGame.getTime()));
                gameState = State.GAME_END_1;
                gameOverBoard = mitrisGame.getDisplayBoard();
                animTime = 0;
                String extra = "";
                if (gameOverBoard.getLevel() >= 5)
                    extra = " Great job!";
                if (gameOverBoard.getLevel() >= 8)
                    extra = " Amazing!!";
                TwitterClient.tweet(String.format("Someone just played Tetris on the MIT Green Building! " + "They cleared %d lines and lasted %1.1f seconds!%s #mittetris", gameOverBoard.getNumCleared(), mitrisGame.getTime(), extra));
            }
            break;
        case GAME_END_1:
            animTime += timestep;
            if (animTime > GAME_END_WAIT) {
                gameState = State.GAME_END_2;
                animTime = 0;
                animTimeLastStep = 0;
                if (gameOverBoard.getLevel() >= 8) {
                    gameState = State.GAME_END_ALT_2;
                    circTime = new double[] { -100, -100, -100, -100, -100 };
                    circX = new double[5];
                    circY = new double[5];
                    circHue = new double[5];
                    circPos = 0;
                }
            }
            sendBoardToDisplay(gameOverBoard, display);
            break;
        case GAME_END_2:
            animTime += timestep;
            if (animTime - animTimeLastStep >= ANIM_TIME_STEP) {
                animTimeLastStep = animTime;
                if (gameOverBoard.isBoardEmpty()) {
                    gameState = State.IDLE;
                }
                gameOverBoard = gameOverBoard.shiftBoardDown();
            }
            sendBoardToDisplay(gameOverBoard, display);
            break;
        case GAME_END_ALT_2:
            animTime += timestep;
            if (animTime >= CIRC_ANIM_FADE_WAIT_TIME) {
                if (animTime <= CIRC_ANIM_FADE_WAIT_TIME + CIRC_ANIM_FADE_TIME) {
                    double brightness = 1 - (animTime - CIRC_ANIM_FADE_WAIT_TIME) / CIRC_ANIM_FADE_TIME;
                    sendBoardToDisplay(gameOverBoard, display, brightness);
                }
            } else
                sendBoardToDisplay(gameOverBoard, display);
            if (animTime - animTimeLastStep >= CIRC_ADD_TIME && animTime <= CIRC_ANIM_STOPADD_TIME) {
                animTimeLastStep = animTime;
                circX[circPos] = Math.random() * width;
                circY[circPos] = Math.random() * height;
                circTime[circPos] = animTime - 0.3;
                circHue[circPos] = Math.random();
                circPos = (circPos + 1) % circX.length;
            }
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
            for (int k = 0; k < circX.length; k++) {
                g.setColor(new Color(Color.HSBtoRGB((float) circHue[k], 1, 1)));
                g.setStroke(new BasicStroke(1.5f));
                double circH = (animTime - circTime[k]) * 5;
                double circW = circH / display.getPixelAspect();
                g.draw(new Ellipse2D.Double(circX[k] - circW / 2, circY[k] - circH / 2, circW, circH));
            }
            if (animTime >= CIRC_ANIM_TOTAL_TIME)
                gameState = State.IDLE;
    }
}
Also used : Display2D(edu.mit.d54.Display2D) BasicStroke(java.awt.BasicStroke) Color(java.awt.Color) Ellipse2D(java.awt.geom.Ellipse2D) Graphics2D(java.awt.Graphics2D)

Example 2 with Display2D

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

the class TestRunner method main.

public static void main(String[] args) {
    Display2D display = new GBDisplay();
    JFrame frame = new JFrame("test");
    DisplayPanel dPanel = new DisplayPanel(display);
    frame.add(dPanel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
    TestPlugin plugin = new TestPlugin(display, 15);
    plugin.start();
}
Also used : Display2D(edu.mit.d54.Display2D) GBDisplay(edu.mit.d54.GBDisplay) DisplayPanel(edu.mit.d54.DisplayPanel) JFrame(javax.swing.JFrame)

Example 3 with Display2D

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

the class GNU30Plugin method loop.

@Override
protected void loop() {
    synchronized (this) {
        Display2D display = getDisplay();
        BufferedImage frame = this.frames.get(this.currentFrame);
        int width = frame.getWidth() > this.widthMax ? this.widthMax : frame.getWidth();
        int height = frame.getHeight() > this.heightMax ? this.heightMax : frame.getHeight();
        for (int ix = 0; ix < width; ++ix) {
            for (int iy = 0; iy < height; ++iy) {
                int pixel = frame.getRGB(ix, iy);
                int red = (pixel >> 16) & 0xFF;
                int green = (pixel >> 8) & 0xFF;
                int blue = pixel & 0xFF;
                display.setPixelRGB(ix, iy, red, green, blue);
            }
        }
        if (this.lastUpdateTime + (this.secondDelay * 1000) < System.currentTimeMillis()) {
            if (this.currentFrame == (this.frameCount - 1)) {
                if (this.currentGif == (this.images.size() - 1)) {
                    /* Loop! */
                    this.loadGif(0);
                } else {
                    this.loadGif(this.currentGif + 1);
                }
            }
            this.currentFrame = (this.currentFrame + 1) % this.frameCount;
            this.lastUpdateTime = System.currentTimeMillis();
        }
    }
}
Also used : Display2D(edu.mit.d54.Display2D) BufferedImage(java.awt.image.BufferedImage)

Example 4 with Display2D

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

the class HacManPlugin method loop.

@Override
protected void loop() {
    switch(state) {
        case Attract:
            long currentTime = System.nanoTime();
            float dt = (float) (currentTime - timeSinceLastUpdate) / 1000000000.0f;
            timeSinceLastUpdate = currentTime;
            Display2D disp = getDisplay();
            Graphics2D g = disp.getGraphics();
            logoScrollTimer -= dt;
            if (logoScrollTimer < 0) {
                logoScrollTimer = logoScrollPeriod;
                logoScrollCount++;
            }
            float percentage = logoScrollPeriod == 0 ? 1.0f : 1.0f - logoScrollTimer / logoScrollPeriod;
            // auto-start the game after 3 cycles
            if (logoScrollCount >= 3) {
                startNewGame();
                break;
            }
            int logoPos = (int) (percentage * (logo.getWidth() + disp.getWidth() + 1));
            g.drawImage(logo, disp.getWidth() + 1 - logoPos, (isMini ? 4 : 0), null);
            break;
        case Play:
            // TODO: go to the main menu on a loss
            if (scenePlay.state == ScenePlay.State.Lost) {
                continueCurrentGame();
            }
            // on a win, the map gets incremented
            if (scenePlay.state == ScenePlay.State.Won) {
                advanceToNextLevel();
            }
            // update the game
            scenePlay.update();
            // display lives as hud
            Display2D d = getDisplay();
            for (int life = 0; life < numLivesCurrent; ++life) {
                int col = life;
                // right now, there's no way to get more lives, but... why not, right?
                if (life > d.getWidth()) {
                    continue;
                }
                int row = d.getHeight() - 1 - (isMini ? 2 : 0);
                d.setPixelHSB(col, row, 0.15f, 1, 1);
            }
            break;
    }
}
Also used : Display2D(edu.mit.d54.Display2D) Graphics2D(java.awt.Graphics2D)

Example 5 with Display2D

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

the class FrogPlugin 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;
            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.LEVEL_START;
                    animTime = 0;
            }
            break;
        case LEVEL_START:
            animTime += timestep;
            // show which level
            showL();
            showLevelNumber(levelNumber);
            // load the level data
            loadLevel(levelNumber);
            // put frog on the left
            frogPosX = 0;
            frogPosY = 8;
            if (animTime > 3.0) {
                // wait 3 seconds and start level
                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 each lane and does so if necessary 
            moveCars();
            // move frog
            switch(userInput) {
                case 'L':
                    if (frogPosX > 0) {
                        frogPosX = frogPosX - 1;
                    }
                    break;
                case 'R':
                    if (frogPosX < 8) {
                        frogPosX = frogPosX + 1;
                    }
                    if (frogPosX == 8) {
                        // if the frog has reached the right hand side
                        // go up a level
                        levelNumber = levelNumber + 1;
                        if (levelNumber == 10) {
                            // if you've finished all 9 levels
                            clearScreen();
                            gameState = State.GAME_END_2;
                            animTime = 0;
                        } else {
                            // or if it's just the next stage
                            clearScreen();
                            gameState = State.LEVEL_START;
                            animTime = 0;
                        }
                    }
                    break;
                case 'U':
                    if (frogPosY > 0) {
                        frogPosY = frogPosY - 1;
                    }
                    break;
                case 'D':
                    if (frogPosY < 16) {
                        frogPosY = frogPosY + 1;
                    }
                    break;
                case //there was an error in the network socket or no client connected -- "pause" the game
                -1:
                    return;
            }
            // check whether frog and any of the enabled cars occupy the same area and if so, die
            hitCheck();
            break;
        case // lose
        GAME_END_1:
            animTime += timestep;
            for (int i = 0; i < vert; i++) {
                // death animation
                for (int horiz = 0; horiz < 9; horiz++) {
                    // (red)
                    display.setPixelRGB(horiz, i, 255 - (i * 15), 0, 0);
                }
            }
            if (animTime - animTimeLastStep > 0.1) {
                // time to write a new line
                if (vert < 17) {
                    vert = vert + 1;
                }
                animTimeLastStep = animTime;
            }
            if (animTime > 2.5) {
                // after 2.5 secs go back to the title screen
                gameState = State.IDLE;
            }
            break;
        case // win
        GAME_END_2:
            animTime += timestep;
            // write WIN on the screen
            showWin();
            for (int i = 0; i < vert; i++) {
                // animated wipe away of "WIN" 
                for (int horiz = 0; horiz < 9; horiz++) {
                    // (BLACK)
                    display.setPixelRGB(horiz, i, 0, 0, 0);
                }
            }
            if (animTime > 5.0) {
                if (animTime - animTimeLastStep > 0.1) {
                    // time to write a new line
                    if (vert < 17) {
                        vert = vert + 1;
                    }
                    animTimeLastStep = animTime;
                }
            }
            if (animTime > 8.0) {
                // wait a bit longer before going back to title screen
                gameState = State.IDLE;
            }
            break;
    }
}
Also used : Display2D(edu.mit.d54.Display2D)

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