LoginSignup
0
0

More than 5 years have passed since last update.

[GroovyとProcessing]画面のステータスを遷移させる

Posted at

画面のステータスをキーボードからの入力で遷移させていく
エスケープキーを押されるとどのステータスでもアプリケーションが終了してしまうので小細工が必要

動画リスト:LWJGLのチュートリアル
動画はココ

DisplayTest.groovy
package episode006

import groovy.swing.SwingBuilder
import processing.core.PApplet

import javax.swing.*

class DisplayTest extends PApplet {

    static enum State {
        INTRO, MAIN_MENU, GAME
    }

    def state = State.INTRO

    def void setup() {
        frameRate(60)
    }

    def void draw() {
        background(0, 0, 0)

        switch(state) {
            case State.INTRO:
                noStroke()
                fill(255, 0, 0) // 赤
                rect(0, 0, 640, 480)
                break
            case State.GAME:
                noStroke()
                fill(0, 255, 0) // 緑
                rect(0, 0, 640, 480)
                break
            case State.MAIN_MENU:
                noStroke()
                fill(0, 0, 255) // 青
                rect(0, 0, 640, 480)
                break
        }
    }

    def void keyPressed() {
        def isEscapeKeyPressed = false
        if (keyCode == ESC || key == ESC) {
            isEscapeKeyPressed = true
            key = 0
            keyCode = 0
        }

        switch(state) {
            case State.INTRO:
                if (key == 's') {
                    state = State.MAIN_MENU
                }
                if (isEscapeKeyPressed) {    // エスケープ
                    System.exit(0)
                }
                break
            case State.GAME:
                if (keyCode == BACKSPACE) {    // バックスペース
                    state = State.MAIN_MENU
                }
                break
            case State.MAIN_MENU:
                if (keyCode == ENTER) {  // エンター
                    state = State.GAME
                }
                if (key == ' ') {  // スペース
                    state = State.INTRO
                }
                break
        }
    }

    def void keyReleased() {
        if (keyCode == ESC || key == ESC) {
            key = 0
            keyCode = 0
        }
    }

    def void keyTyped() {
        if (keyCode == ESC || key == ESC) {
            key = 0
            keyCode = 0
        }
    }

    def static void main(args) {
        def display = new DisplayTest()
        new SwingBuilder().frame(
                title: 'Episode 6',
                defaultCloseOperation: JFrame.EXIT_ON_CLOSE,

               size: [640, 480], show: true) {
            widget(display)
        }
        display.init()
    }
}

grocessingGL006.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0