伊莉討論區

標題: VB2008打磚塊小遊戲問題 [打印本頁]

作者: 夢境未來    時間: 2013-7-31 07:40 PM     標題: VB2008打磚塊小遊戲問題

[attach]93314670[/attach]
這是我自己上網學的打磚塊小遊戲程式!不過我希望它可以進行接關跟暫停!(例如下一關可以加快球速還有磚塊數量等等!)
希望有大大可以教我!
  1. Public Class Form1

  2.     '表單載入
  3.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
  4.         For i As Integer = 0 To 4 '五個橫列
  5.             For j As Integer = 0 To 4 '五個直行
  6.                 Brick(100 * i, 50 * j) '製作磚塊(100x50點大小)
  7.             Next
  8.         Next
  9.         Me.Width = (Me.Width - Me.ClientSize.Width) + 100 * 5 '調整表單寬度
  10.         Me.Height = (Me.Height - Me.ClientSize.Height) + 50 * 5 + 200 '調整表單高度
  11.         B.Top = 50 * 5 + 30 '調整球的高度,五排磚塊下30點
  12.         B.Left = (Me.ClientSize.Width - B.Width) / 2 '球水平置中
  13.         P.Top = Me.ClientSize.Height - 30 '調整球拍的高度
  14.         P.Left = (Me.ClientSize.Width - P.Width) / 2 '球拍水平置中
  15.         Timer1.Start() '啟動球的運動
  16.     End Sub
  17.     '製作指定位置磚塊的副程序
  18.     Sub Brick(ByVal X As Integer, ByVal Y As Integer)
  19.         Dim Q As New Label '建置新的磚塊物件
  20.         With Q
  21.             .Width = 100 '寬
  22.             .Height = 50 '高
  23.             .BackColor = Color.DarkRed '磚紅色
  24.             .BorderStyle = BorderStyle.FixedSingle '邊框
  25.             .Left = X '座標X
  26.             .Top = Y '座標Y
  27.         End With
  28.         Me.Controls.Add(Q) '磚塊加入表單
  29.     End Sub
  30.     Dim Vx As Single = 5, Vy As Single = 5 '速度值  '球的運動控制
  31.     Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
  32.         B.Left += Vx 'X方向移動
  33.         B.Top += Vy 'Y方向移動
  34.         If B.Left < 0 Then Vx = Math.Abs(Vx) '碰左牆
  35.         If B.Right > Me.ClientSize.Width Then Vx = -Math.Abs(Vx) '碰右牆
  36.         If B.Top < 0 Then Vy = Math.Abs(Vy) '碰屋頂
  37.         Dim C As Single = (B.Left + B.Right) / 2 '球的中心點X座標
  38.         If B.Bottom > P.Top And C > P.Left And C < P.Right Then '撞到球拍
  39.             My.Computer.Audio.Play(My.Resources.Bang, AudioPlayMode.Background) '擊球音效
  40.             Vy = -Math.Abs(Vy) '向上彈
  41.             Dim F As Single = (C - P.Left) / P.Width '計算擊球點
  42.             If Vx < 0 Then F = 1 - F '方向調整
  43.             Vx = Vx * (F + 0.5) 'X速度修正
  44.         End If
  45.         If B.Top > Me.ClientSize.Height Then '漏接了,球掉出畫面
  46.             Timer1.Stop()
  47.             MsgBox("Game over!")
  48.         End If
  49.         '檢查磚塊碰撞情況
  50.         For Each q In Me.Controls '每一個控制項
  51.             If TypeOf (q) Is Label Then '如果是Label(磚塊)
  52.                 If chkHit(q) Then '檢查是否擊中磚塊
  53.                     My.Computer.Audio.Play(My.Resources.Bee, AudioPlayMode.Background) '破磚音效
  54.                 End If
  55.             End If
  56.         Next
  57.     End Sub
  58.     '檢查球與磚塊或牆壁碰撞的程式
  59.     Function chkHit(ByVal Q As Label) As Boolean
  60.         If B.Right < Q.Left Then Return False '偏左未碰到
  61.         If B.Left > Q.Right Then Return False '偏右未碰到
  62.         If B.Top > Q.Bottom Then Return False '偏下未碰到
  63.         If B.Bottom < Q.Top Then Return False '偏上未碰到
  64.         '碰撞目標左側(剛剛越過左邊界)往左彈
  65.         If B.Right >= Q.Left And (B.Right - Q.Left) <= Math.Abs(Vx) Then Vx = -Math.Abs(Vx)
  66.         '碰撞目標右側(剛剛越過右邊界)往右彈
  67.         If B.Left <= Q.Right And (Q.Right - B.Left) <= Math.Abs(Vx) Then Vx = Math.Abs(Vx)
  68.         '碰撞目標底部(剛剛越過底邊界)往下彈
  69.         If B.Top <= Q.Bottom And (Q.Bottom - B.Top) <= Math.Abs(Vy) Then Vy = Math.Abs(Vy)
  70.         '碰撞目標頂部(剛剛越過頂邊界)往上彈
  71.         If B.Bottom >= Q.Top And (B.Bottom - Q.Top) <= Math.Abs(Vy) Then Vy = -Math.Abs(Vy)
  72.         Q.Dispose() '刪除磚塊物件
  73.         Return True '回傳有碰撞
  74.     End Function
  75.     '拖曳球拍的程式
  76.     Dim mdx As Integer '拖曳起點
  77.     Private Sub P_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles P.MouseDown
  78.         mdx = e.X '拖曳起點
  79.     End Sub
  80.     '拖曳中
  81.     Private Sub P_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles P.MouseMove
  82.         If e.Button = Windows.Forms.MouseButtons.Left Then
  83.             Dim X As Integer = P.Left + (e.X - mdx) '試算拖曳位置
  84.             If X < 0 Then X = 0 '左移極限控制
  85.             If X > Me.ClientSize.Width - P.Width Then
  86.                 X = Me.ClientSize.Width - P.Width '右移極限控制
  87.             End If
  88.             P.Left = X '球拍位置(不超出邊界)
  89.         End If
  90.     End Sub
  91. End Class
複製代碼











作者: yuehtim    時間: 2013-8-8 09:44 PM

暫停部分建議用timer比較好
阿增加關卡可以寫副程式加If或Select Case 作判斷
我會寫在模組吧,因為可以節省空間
記得要用Public Sub,若用Private Sub主程式會抓不到模組的副程式喔




歡迎光臨 伊莉討論區 (http://wwwaaa.eyny.com/) Powered by Discuz!