カテゴリー
Visual Basic

全てのブロックを消した時のゲームの続行

全てのブロックを消したら、ゲームを続行できるようにします。

現状では「クリアしました!」とメッセージを表示しているだけで、ゲームを続行できませんので、これを改善します。

メッセージを「クリアしました!続けますか?」に変更してYes/Noを問い合わせるようにします。
Noだったらアプリを終了します。

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        HitWall()
        HitRacket()
        HitBricks()
        If CountBricks() = 0 Then
            Dim r As Long
            Timer1.Enabled = False
            r = MsgBox("クリアしました!続けますか?", vbYesNo)
            If r = vbNo Then
                Me.Close()
                Exit Sub
            End If
        End If
        If y > Racket.Top + Racket.Height Then
            Timer1.Enabled = False
            MsgBox("ゲームオーバー")
            If score > highscore Then
                MsgBox("ハイスコアを更新しました!!!!!!!")
                highscore = score
                Save()
            End If
        End If
        x = x + dx
        y = y + dy
        Ball.Left = x
        Ball.Top = y
        ScoreLabel.Text = "スコア: " & score
    End Sub

Yesなら画面を初期状態に戻してゲームを再開します。

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        HitWall()
        HitRacket()
        HitBricks()
        If CountBricks() = 0 Then
            Dim r As Long
            Timer1.Enabled = False
            r = MsgBox("クリアしました!続けますか?", vbYesNo)
            If r = vbNo Then
                Me.Close()
                Exit Sub
            End If
            PrepareGame()
        End If
        If y > Racket.Top + Racket.Height Then
            Timer1.Enabled = False
            MsgBox("ゲームオーバー")
            If score > highscore Then
                MsgBox("ハイスコアを更新しました!!!!!!!")
                highscore = score
                Save()
            End If
        End If
        x = x + dx
        y = y + dy
        Ball.Left = x
        Ball.Top = y
        ScoreLabel.Text = "スコア: " & score
    End Sub

    Public Sub PrepareGame()
        '全ブロックを再表示する
        ShowAllBricks()
        'ボールの位置を適当な場所に移動する
        x = 230
        y = 180
        'ボールの方向を落ちる方にする
        dy = Math.Abs(dy)
        Timer1.Enabled = True
    End Sub

    Public Sub ShowAllBricks()
        Dim i, j As Integer
        For i = 0 To 9
            For j = 0 To 5
                Select Case j
                    Case 0, 1
                        brick(i, j).BackColor = Color.Red
                    Case 2, 3
                        brick(i, j).BackColor = Color.Yellow
                    Case 4, 5
                        brick(i, j).BackColor = Color.Green
                End Select
                brick(i, j).Visible = True
            Next
        Next
    End Sub