1 / 14

題目 : 打磚塊遊戲

題目 : 打磚塊遊戲. 計算機程式及實習 期末報告 ppt 製作 學校 : 南台科技大學 系 所 : 機械工程系 班級 : 自控一甲 姓名 : 李浩銘 學號 : 4A212114 老師 : 謝慶存. 題目說明 : 打磚塊設計. 試 設計小遊戲,按下「 START 」鈕之後 ,即 會開始遊戲。又上小有「 Time( 暫停 ) 」及「 Play( 繼續 ) 」,可在遊戲中按下。鈕按「 End 」,關閉程式。. 程式開始 畫面. 按下「 Play 」,則繼續遊戲。. 按下「 Time 」,則暫停遊戲。. 漏接球的畫面.

bunme
Download Presentation

題目 : 打磚塊遊戲

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 題目:打磚塊遊戲 計算機程式及實習 期末報告ppt製作 學校:南台科技大學 系所:機械工程系 班級:自控一甲 姓名:李浩銘 學號:4A212114 老師:謝慶存

  2. 題目說明:打磚塊設計 試設計小遊戲,按下「START」鈕之後,即會開始遊戲。又上小有「Time(暫停)」及「 Play(繼續) 」,可在遊戲中按下。鈕按「End」,關閉程式。

  3. 程式開始畫面 按下「Play」,則繼續遊戲。 按下「Time」,則暫停遊戲。

  4. 漏接球的畫面 漏接球後將出現「 Game Over! 」 按下確認後即出現「START 」, 按下可重新遊戲。

  5. 版面配置及屬性設定 Time(暫停) Play(繼續) End(結束) START(開始)

  6. Msgbox 「Game Over!」 會有「漏接音效」 球 撞到磚塊時會有「破磚音效」 球拍 球彈起時會有「擊球音效」

  7. 程式碼設計 Public Class 打磚塊 Dim Vx As Single = 10, Vy As Single = 10'速度值  '球的運動控制  '製作指定位置磚塊的副程序 Sub Brick(ByVal X As Integer, ByVal Y As Integer) Dim Q As New Label'建置新的磚塊物件  Q.Image = Image.FromFile("磚塊.jpg") With Q .Width = 100'寬 .Height = 50'高 .BorderStyle = BorderStyle.FixedSingle .Left = X'座標X .Top = Y'座標Y End With Me.Controls.Add(Q)'磚塊加入表單  End Sub

  8. Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick B.Left += Vx'X方向移動 B.Top += Vy'Y方向移動 If B.Left < 0 Then Vx = Math.Abs(Vx)'碰左牆 If B.Right > Me.ClientSize.Width Then Vx = -Math.Abs(Vx)'碰右牆 If B.Top < 0 Then Vy = Math.Abs(Vy)'碰屋頂  Dim C As Single = (B.Left + B.Right) / 2'球的中心點X座標 If B.Bottom > P.Top And C > P.Left And C < P.RightThen'撞到球拍  My.Computer.Audio.Play(My.Resources.stomp, AudioPlayMode.Background) '擊球音效 Vy = -Math.Abs(Vy)'向上彈 Dim F As Single = (C - P.Left) / P.Width'計算擊球點 If Vx < 0 Then F = 1 - F'方向調整 Vx = Vx * (F + 0.5)'X速度修正  End If If B.Top > Me.ClientSize.HeightThen'漏接了,球掉出畫面 Timer1.Stop() My.Computer.Audio.Play(My.Resources.mariodie, AudioPlayMode.Background) '漏接音效 MsgBox(“Game over!”)'顯示Game Over! START.Visible = True End If

  9. '檢查磚塊碰撞情況 For Each q In Me.Controls'每一個控制項 If TypeOf (q) Is Label Then'如果是Label(磚塊)  If chkHit(q) Then'檢查是否擊中磚塊 My.Computer.Audio.Play(My.Resources.kick, AudioPlayMode.Background) '破磚音效

  10. '檢查球與磚塊或牆壁碰撞的程式 Function chkHit(ByVal Q As Label) As Boolean If B.Right < Q.Left Then Return False '偏左未碰到 If B.Left > Q.Right Then Return False '偏右未碰到 If B.Top > Q.Bottom Then Return False '偏下未碰到 If B.Bottom < Q.Top Then Return False '偏上未碰到 '碰撞目標左側(剛剛越過左邊界)往左彈 If B.Right >= Q.Left And (B.Right - Q.Left) <= Math.Abs(Vx) Then Vx = -Math.Abs(Vx) '碰撞目標右側(剛剛越過右邊界)往右彈 If B.Left <= Q.Right And (Q.Right - B.Left) <= Math.Abs(Vx) Then Vx = Math.Abs(Vx) '碰撞目標底部(剛剛越過底邊界)往下彈 If B.Top <= Q.Bottom And (Q.Bottom - B.Top) <= Math.Abs(Vy) Then Vy = Math.Abs(Vy) '碰撞目標頂部(剛剛越過頂邊界)往上彈 If B.Bottom >= Q.Top And (B.Bottom - Q.Top) <= Math.Abs(Vy) Then Vy = -Math.Abs(Vy) Q.Dispose()'刪除磚塊物件 Return True'回傳有碰撞  End Function

  11. '拖曳球拍的程式 Dim mdx As Integer'拖曳起點 Private Sub P_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles P.MouseDown mdx = e.X'拖曳起點 End Sub '拖曳中 Private Sub P_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles P.MouseMove If e.Button = Windows.Forms.MouseButtons.Left Then Dim X As Integer = P.Left + (e.X - mdx)'試算拖曳位置 If X < 0 Then X = 0'左移極限控制  If X > Me.ClientSize.Width - P.Width Then X = Me.ClientSize.Width - P.Width'右移極限控制 End If P.Left = X'球拍位置(不超出邊界)  End If End Sub

  12. Private Sub START_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles START.Click START.Visible = False B.Visible = True P.Visible = True For i As Integer = 0 To 9'十個橫列 For j As Integer = 0 To 9'十個直行  Brick(100 * i, 50 * j)'製作磚塊(100x50點大小) Next Next Me.Width = (Me.Width - Me.ClientSize.Width) + 200 * 5'調整表單寬度 Me.Height = (Me.Height - Me.ClientSize.Height) + 100 * 5 + 150'調整表單高度  B.Top = 100 * 5 + 100'調整球的高度,五排磚塊下100點 B.Left = (Me.ClientSize.Width - B.Width) / 2'球水平置中  P.Top = Me.ClientSize.Height - 25'調整球拍的高度  P.Left = (Me.ClientSize.Width - P.Width) / 2'球拍水平置中  Timer1.Start()'啟動球的運動  Time.Top = 10 Time.Left = Me.ClientSize.Width - B.Width '調整按鈕位置 Play.Top = 10 Play.Left = Me.ClientSize.Width - B.Width'調整按鈕位置 End1.Top = 50 End1.Left = Me.ClientSize.Width - B.Width'調整按鈕位置 End Sub

  13. Private Sub Time_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Time.Click Timer1.Stop() '遊戲暫停 Play.Visible = True End Sub Private Sub Play_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Play.Click Timer1.Start() '遊戲開始 Play.Visible = False End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Play.Visible = False End Sub Private Sub End1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles End1.Click End '遊戲結束 End Sub End Class

  14. 心得 上網找了一些範例來參考,然後自己試著做做看,在翻閱課本找些 更詳細的步驟操作。 花了不少時間在程式碼上,要了解程式的動作有沒有衝突到或合不 合邏輯,這些都挺花時間的。還要做程式說明也費了一番功夫。 撰寫程式時會有很多瓶頸,但這也是想讓我突破的地方。做完後 會有種成就感。往後會多加練習Visual Basic ,熟悉及了解裡面的物件、 屬性、及程式的運用。

More Related