如何用visual basic 寫一個2048?
具體功能實現的語句是什麼?
以下是實現的的具體代碼。
Option ExplicitDim BoxValue(3, 3) As Integer 格子的數值
Dim Score As Long 得分Dim fWidth As Single
Dim mLeft As Integer, mTop As IntegerDim mSize As Integer按鍵
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) Select Case KeyCode Case vbKeyLeft Call MoveBox(1) Case vbKeyRight Call MoveBox(2)Case vbKeyUp
Call MoveBox(3) Case vbKeyDown Call MoveBox(4) Case vbKeySpace Call NewGame End SelectEnd SubPrivate Sub Form_Load()Me.Width = 8000
Me.Height = 9000 Me.Caption = "2048" Me.KeyPreview = True Me.AutoRedraw = True Me.ScaleMode = 3 Me.FontSize = 32 fWidth = TextWidth("0")mSize = 450
mLeft = (Me.ScaleWidth - mSize) / 2mTop = (Me.ScaleHeight - mSize - mLeft)
Call NewGame
End Sub開始遊戲
Private Sub NewGame() Dim R As Integer, C As IntegerLine (mLeft, mTop)-(mLeft + 450, mTop + 450), RGB(128, 128, 128), BF
Line (mLeft + 1, mTop + 1)-(Me.ScaleWidth - mLeft, Me.ScaleHeight - mLeft - 1), RGB(40, 40, 40), BFor R = 0 To 3
For C = 0 To 3DrawBox 0, R, C
Next Next Score = 0Call PrintScore
Call NewBox Call NewBoxEnd Sub畫格子
Private Sub DrawBox(ByVal N As Integer, ByVal R As Integer, ByVal C As Integer)Dim L As Integer, T As Integer
Dim tmpStr As StringL = C * 110 + 10 + mLeft
T = R * 110 + 10 + mTopIf N = 0 Then
Line (L + 1, T + 1)-(L + 102, T + 102), RGB(100, 100, 100), BF Line (L, T)-(L + 100, T + 100), RGB(200, 200, 200), BF Else Line (L, T)-(L + 100, T + 100), BoxColor(N), BF Line (L + 2, T + 2)-(L + 99, T + 99), RGB(100, 100, 100), BLine (L + 1, T + 1)-(L + 98, T + 98), RGB(255, 255, 255), B
tmpStr = Trim(Str(N))
CurrentX = L + (100 - TextWidth(tmpStr)) / 2 - fWidth CurrentY = T + (100 - TextHeight(tmpStr)) / 2Print N
End IfBoxValue(R, C) = N
End Sub移動格子
Private Sub MoveBox(ByVal Fx As Integer)Dim B As Integer, N As Integer, S As Integer
Dim R As Integer, C As Integer, K As Integer Dim bMove As BooleanIf Fx &< 3 Then 左右移動
If Fx = 1 Then B = 1: N = 3: S = 1 Else B = 2: N = 0: S = -1 End IfFor R = 0 To 3
K = IIf(Fx = 1, 0, 3)
For C = B To N Step S If BoxValue(R, C) &> 0 Then If (BoxValue(R, C) = BoxValue(R, K)) Then DrawBox BoxValue(R, C) * 2, R, K DrawBox 0, R, C Score = Score + BoxValue(R, K) If BoxValue(R, K) = 2048 Then MsgBox "哇塞!太厲害了!佩服佩服~", vbInformation End If bMove = True Else If BoxValue(R, K) &> 0 Then K = K + S If K &<&> C Then DrawBox BoxValue(R, C), R, K DrawBox 0, R, C bMove = True End If Else DrawBox BoxValue(R, C), R, K DrawBox 0, R, C bMove = True End If End If End If Next C Next R Else 上下移動 If Fx = 3 Then B = 1: N = 3: S = 1 Else B = 2: N = 0: S = -1 End IfFor C = 0 To 3
K = IIf(Fx = 3, 0, 3) For R = B To N Step S If BoxValue(R, C) &> 0 Then If BoxValue(R, C) = BoxValue(K, C) Then DrawBox BoxValue(R, C) * 2, K, C DrawBox 0, R, C Score = Score + BoxValue(K, C) If BoxValue(R, K) = 2048 Then MsgBox "哇塞!太厲害了!佩服佩服~", vbInformation End If bMove = True Else If BoxValue(K, C) &> 0 Then K = K + S If K &<&> R Then DrawBox BoxValue(R, C), K, C DrawBox 0, R, C bMove = True End If Else DrawBox BoxValue(R, C), K, C DrawBox 0, R, C bMove = True End If End If End If Next R Next C End IfIf bMove Then
Call PrintScore Call NewBox檢查死局
For R = 0 To 3 For C = 0 To 3 If BoxValue(R, C) = 0 Then Exit Sub If R &< 3 Then If BoxValue(R, C) = BoxValue(R + 1, C) Then Exit Sub If C &< 3 Then If BoxValue(R, C) = BoxValue(R, C + 1) Then Exit Sub Next NextMsgBox "無路可走了~~~下次好運!", vbInformation
Call NewGame
End IfEnd Sub產生新方格
Private Sub NewBox() Dim R As Integer, C As IntegerRandomize
R = Int(Rnd * 4) C = Int(Rnd * 4)Do While BoxValue(R, C) &> 0
R = Int(Rnd * 4) C = Int(Rnd * 4) LoopBoxValue(R, C) = 2
DrawBox 2, R, CEnd Sub方格顏色
Private Function BoxColor(ByVal N As Integer) As Long Select Case N Case 2 BoxColor = HC0E0FF Case 4 BoxColor = H80C0FF Case 8 BoxColor = H80FFFF Case 16 BoxColor = HC0FFC0 Case 32 BoxColor = HFFFF80 Case 64 BoxColor = HFFC0C0 Case 128 BoxColor = HFF8080 Case 256 BoxColor = HFFC0FF Case 512 BoxColor = HFF80FF Case 1024 BoxColor = HC0C0FF Case 2048 BoxColor = H8080FF End SelectEnd Function
列印得分
Private Sub PrintScore() Me.FontSize = 24 Line (mLeft, mLeft)-(Me.ScaleWidth, mTop), BackColor, BF CurrentX = mLeft CurrentY = mLeft Print "得分:" Score Me.FontSize = 32End Subexcelhome裡面有代碼
推薦閱讀:
※我現在基本掌握C語言,因為女朋友計算機二級學的VB,所以想大概了解VB,請問學習困難么?
※有什麼是 Visual Basic 6 可以做,但是別的大多數語言不能做的?
※25 萬條數據的 excel 文件,把每個數字都除以 10 ,怎麼做?
TAG:編程 | VisualBasic |