baobao
2/21/2018 - 5:10 PM

シューティングゲーム制作中

シューティングゲーム制作中

/**
 * 避けるシューティングゲームを作ってみる
 */
let state = 0
const STATE_TITLE = 0;
const STATE_INGAME = 1;
const STATE_RESULT = 2;

// キャラが動けるx座標範囲
const MIN_X = 0;
const MAX_X = 4;
const PLAYER_Y = 4;
let currentPlayerX = 2;

// PlayerSpriteを生成して予め消しておく
let _playerSprite = game.createSprite(currentPlayerX, PLAYER_Y)
_playerSprite.off()

basic.forever(() => {
    SetState(STATE_TITLE)
    while (state == STATE_TITLE) {
        // タイトル表示
        ShowStartTitle()
    }
    // TODO wait animation

    // InGame
    SetState(STATE_INGAME)
    _playerSprite.on()
    while (state == STATE_INGAME) {
        UpdateInGameLoop()
    }

    // Result
    while (state == STATE_RESULT) {
        basic.showString("GAME OVER!!")
    }
})

function SetState(changeState: number) {
    state = changeState;
}

function UpdateInGameLoop() {
    // _playerSprite.isTouching(other)
    _playerSprite.goTo(currentPlayerX, PLAYER_Y)
}

input.onButtonPressed(Button.A, () => {
    if (state == STATE_TITLE) {
        SetState(STATE_INGAME);
    }
    else if (state == STATE_RESULT) {
        SetState(STATE_TITLE);
    }
    else if (state == STATE_INGAME) {
        // 左へ
        MovePlayer(-1)
    }
})

input.onButtonPressed(Button.B, () => {
    if (state == STATE_INGAME) {
        // 右へ
        MovePlayer(1)
    }
})

// Playerの移動
function MovePlayer(pos: number) {
    currentPlayerX += pos
    if (currentPlayerX < MIN_X) {
        currentPlayerX = MIN_X
    }
    else if (currentPlayerX > MAX_X) {
        currentPlayerX = MAX_X
    }
}

// ステージ
// let stagePattern0 = 



// タイトル表示
function ShowStartTitle() {
    let txtScrollSpeed = 300 //state == STATE_TITLE ? 200 : 0
    let speed = 1//state == STATE_TITLE ? 1 : 1000
    for (let i = 0; i < list.length; i++) {
        list[i].scrollImage(speed, txtScrollSpeed)
        space.scrollImage(speed, txtScrollSpeed)
    }
}
let space = images.createImage(`
.
.
.
.
.
`)

let list = [
    images.createImage(`
# # # # #
. . . # .
. . # . .
. # . # .
# . . . #
`),
    images.createImage(`
. . # # #
. # . . #
. . # . #
. . . # .
. # # . .
`),
    images.createImage(`
. . . . .
. . . . .
# # # # #
. . . . .
. . . . .
`),
    images.createImage(`
. # . . .
. # . . .
. # # # .
. # . . .
. # . . .
`),
    images.createImage(`
# . # . .
# . # . .
# . # . .
. . . . .
# . # . .
`)
];