Download presentation
Presentation is loading. Please wait.
1
計算機程式及實習 課堂作業之口頭報告ppt製作 題目:俄羅斯方塊
期末報告 機械工程系 自控三乙 學號:4A012901 張凱翔
2
俄羅斯方塊遊戲說明 按開始即為遊戲開始 組合物 當連成一線可消除並且獲得分數 Level越高速度越快 當某一行跌到最高層級為遊戲結束
3
程式碼撰寫 Option Strict On Imports MyGames.Tetris
Imports MyGames.Tetris.TetrisBlock Partial Class TetrisGame Private GameBoard As TetrisBoard Private FallingBlock As TetrisBlock Private PreviewBoard As TetrisBoard Private PreviewBlock As TetrisBlock Private Score As Double Private Level As Integer Private Speed As Integer Private Lines As Integer Private RandomNumbers As New Random Private Status As GameStatus = GameStatus.Stopped
4
程式碼撰寫 Private Enum GameStatus Running Paused Stopped End Enum
#Region "Event Handlers" Private Sub TetrisGame_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load PreviewBoard = New TetrisBoard(PreviewBox) With PreviewBoard .Rows = 4 .Columns = 4 .CellSize = New Size(20, 20) .Style = BorderStyle.FixedSingle .SetupBoard() End With PreviewBlock = New TetrisBlock(PreviewBoard) PreviewBlock.CenterCell = PreviewBoard.Cells(2, 2) PreviewBlock.Shape = GetRandomShape()
5
程式碼撰寫 GameBoard = New TetrisBoard(GameBox) With GameBoard .Rows = 20
.Columns = 10 .CellSize = New Size(20, 20) .Style = BorderStyle.FixedSingle .SetupBoard() End With FallingBlock = New TetrisBlock(GameBoard) HelpLabel.Text = HelpLabel.Text.Replace("|", vbCrLf) StylesLabel.Text = StylesLabel.Text.Replace("|", vbCrLf) ShowMessage(String.Format("點I這o裡MI開始l", vbCrLf)) End Sub
6
程式碼撰寫 Private Sub TetrisGame_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown Select Case e.KeyCode Case Keys.Left, Keys.Right, Keys.Down, Keys.Up If Status = GameStatus.Running Then With FallingBlock Case Keys.Left If .CanMove(MoveDirection.Left) Then .Move(MoveDirection.Left) Case Keys.Right If .CanMove(MoveDirection.Right) Then .Move(MoveDirection.Right) Case Keys.Down If .CanMove(MoveDirection.Down) Then .Move(MoveDirection.Down) Case Keys.Up
7
程式碼撰寫 If .CanRotate Then .Rotate() End Select End With End If
Case Keys.P If Status <> GameStatus.Stopped Then TogglePauseGame() Case Keys.N If Status = GameStatus.Stopped Then StartNewGame() ElseIf DialogResult.Yes = MessageBox.Show("Abort current game?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) Then
8
程式碼撰寫 End If Case Keys.B GameBoard.Style = BorderStyle.FixedSingle
PreviewBoard.Style = BorderStyle.FixedSingle GameBoard.Color = Color.Empty PreviewBoard.Color = Color.Empty Case Keys.M GameBoard.Style = BorderStyle.None PreviewBoard.Style = BorderStyle.None Case Keys.F GameBoard.Color = Color.Orange PreviewBoard.Color = Color.Orange
9
程式碼撰寫 Case Keys.S GameBoard.Style = BorderStyle.None
PreviewBoard.Style = BorderStyle.None GameBoard.Color = Color.SkyBlue PreviewBoard.Color = Color.SkyBlue Case Keys.W GameBoard.Color = Color.Blue PreviewBoard.Color = Color.Blue Case Keys.C GameBoard.Color = Color.Chocolate PreviewBoard.Color = Color.Chocolate End Select End Sub
10
程式碼撰寫 Private Sub MessageLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MessageLabel.Click Select Case Status Case GameStatus.Stopped StartNewGame() Case GameStatus.Paused TogglePauseGame() End Select End Sub
11
程式碼撰寫 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick If FallingBlock.CanMove(MoveDirection.Down) Then FallingBlock.Move(MoveDirection.Down) Else '' Fix block to base For Each cell As TetrisCell In FallingBlock.Cells cell.IsEmpty = False Next '' Remove completed rows Dim checkRows = From cell In FallingBlock.Cells _ Order By cell.Row _ Select cell.Row Distinct Dim rowsRemoved As Integer = 0 For Each row In checkRows If GameBoard.IsRowComplete(row) Then GameBoard.RemoveRow(row) rowsRemoved += 1 End If
12
程式碼撰寫 Next '' Update Statistics
Score += Math.Pow(rowsRemoved, 2) * 100 Lines += rowsRemoved Speed = 1 + Lines \ 10 If Speed Mod 10 = 0 Then Level += 1 : Speed = 1 Timer1.Interval = (10 - Speed) * 100 UpdateStatistics() '' Make the next block fall DropNextFallingBlock() '' Check if game has ended If Not FallingBlock.CanMove(FallingBlock.CenterCell) Then EndGame() End If End Sub
13
程式碼撰寫 Region "Private Methods"
Private Function GetRandomShape() As TetrisBlock.Shapes Dim number As Integer = RandomNumbers.Next(Shapes.I1, Shapes.Z4 + 1) 'first to last Return CType(number, TetrisBlock.Shapes) End Function Private Sub DropNextFallingBlock() FallingBlock.CenterCell = GameBoard.Cells(2, GameBoard.Columns \ 2) FallingBlock.Shape = PreviewBlock.Shape PreviewBlock.Shape = GetRandomShape() PreviewBlock.RefreshBackGround() PreviewBlock.Refresh() End Sub
14
程式碼撰寫 Private Sub StartNewGame() Score = 0 Lines = 0 Speed = 1
Level = 1 Timer1.Interval = 1000 UpdateStatistics() GameBoard.SetupBoard() 'PreviewBlock.Shape = GetRandomShape() DropNextFallingBlock() MessageLabel.Visible = False Timer1.Enabled = True Status = GameStatus.Running End Sub
15
程式碼撰寫 Private Sub EndGame() Timer1.Enabled = False
Status = GameStatus.Stopped ShowMessage(String.Format("{0}{0}GAME OVER{0}{0}{0}{0}Click here to start new game", vbCrLf)) End Sub Private Sub UpdateStatistics() ScoreLabel.Text = Score.ToString("000") LinesLabel.Text = Lines.ToString LevelLabel.Text = Level.ToString SpeedLabel.Text = Speed.ToString
16
程式碼撰寫 Private Sub TogglePauseGame() If Status = GameStatus.Paused Then
Status = GameStatus.Running MessageLabel.Visible = False Timer1.Enabled = True Else Status = GameStatus.Paused ShowMessage(String.Format("{0}{0}GAME PAUSED{0}{0}{0}{0}Click here to resume.", vbCrLf)) End If End Sub Private Sub ShowMessage(ByVal message As String) MessageLabel.Text = message MessageLabel.Visible = True Timer1.Enabled = False #End Region
17
程式碼撰寫 Private Sub HelpLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HelpLabel.Click End Sub End Class
18
心得 一開始覺得VB 跟其他程式比起來不算太難,可能是謝老師教的很詳細,VB本身介面也有許多中文解釋,還有程式只要一有錯誤,程式碼就會顯示錯誤提醒我們。 程式語言其實不單單只有大學學校所教的,程式語言在很多領域都會用到,當然以後要當一個工程師程式要有一定的水準與基礎是必要的,期末這個作業我也花很多很多的時間在思考,曾經想放棄期末作業,但後來想說謝老師這麼花心思在教導我們,總不能這麼容易就放棄,我也上網找了很多資料,也請教了很多熟悉vb的人,最後才把這個程式做出來。 謝謝老師這學期的程式教導讓我受益良多,讓我對程式感到興趣。
Similar presentations