Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Const w = 10
- Public Const h = 20
- Public piece As Range
- Public board As Range
- Public lineCounter As Range
- Public nextPiece As Range
- Public piece2() As Boolean
- Public running As Boolean
- Public falling As Boolean
- Public canMove As Boolean
- Public isPaused As Boolean
- Public queue(1 To 2)
- Public RunWhen As Double
- Public Const cRunIntervalSeconds = 1 ' two minutes
- 'Public Const cRunWhat = "TheSub" ' the name of the procedure to run
- Public Const cRunWhat = "Update"
- Sub setup()
- Columns("A:J").ColumnWidth = 2
- Set board = Range(Cells(1, 1), Cells(h, w))
- Set lineCounter = Cells(1, w + 5)
- Cells.Interior.ColorIndex = -4142
- With board.Borders(xlEdgeBottom)
- .LineStyle = xlContinuous
- .ColorIndex = 0
- .TintAndShade = 0
- .Weight = xlThin
- End With
- With board.Borders(xlEdgeRight)
- .LineStyle = xlContinuous
- .ColorIndex = 0
- .TintAndShade = 0
- .Weight = xlThin
- End With
- lineCounter.Value = 0
- Application.OnKey "^t", "toggle"
- Application.OnKey "^q", "rotateLeft"
- Application.OnKey "^e", "rotateRight"
- Application.OnKey "^a", "moveLeft"
- Application.OnKey "^d", "moveRight"
- Application.OnKey "^s", "moveDown"
- Application.OnKey "^p", "pause"
- Application.OnKey "^r", "reset"
- Application.OnKey "^w", "HelloBusinessBrunch"
- Range("A1").Value = 0
- Range("N1").Value = "Lines:"
- Range("R1:S1").Merge
- Range("R1:S1").Value = "Controls"
- Range("R1:S1").Font.Underline = xlUnderlineStyleSingle
- Columns("R:R").ColumnWidth = 20.86
- Range("R2").Value = "Move Left"
- Range("S2").Value = "ctrl+a"
- Range("R3").Value = "Move Right"
- Range("S3").Value = "ctrl+d"
- Range("R4").Value = "Move Down"
- Range("S4").Value = "ctrl+s"
- Range("R5").Value = "Rotate Left"
- Range("S5").Value = "ctrl+q"
- Range("R6").Value = "Rotate Right"
- Range("S6").Value = "ctrl+e"
- Range("R7").Value = "Pause"
- Range("S7").Value = "ctrl+p"
- Range("R8").Value = "Reset"
- Range("S8").Value = "ctrl+r"
- Range("R9").Value = "Start"
- Range("S9").Value = "ctrl+t"
- End Sub
- Private Sub reset()
- MSG1 = MsgBox("Are you sure you want to restart?", vbYesNo, "Restart?")
- If MSG1 = vbNo Then
- Exit Sub
- End If
- running = False
- StopTimer
- Columns("A:S").Delete Shift:=xlToLeft
- Call setup
- Set nextPiece = Range(Cells(1, w + 1), Cells(2, w + 4))
- End Sub
- Private Sub toggle()
- If (running = False) Then
- begin
- Else
- running = False
- StopTimer
- End If
- End Sub
- Private Sub begin()
- Set nextPiece = Range(Cells(1, w + 1), Cells(2, w + 4))
- Set board = Range(Cells(1, 1), Cells(h, w))
- Set lineCounter = Cells(1, w + 5)
- Cells.Interior.ColorIndex = -4142
- With board.Borders(xlEdgeBottom)
- .LineStyle = xlContinuous
- .ColorIndex = 0
- .TintAndShade = 0
- .Weight = xlThin
- End With
- With board.Borders(xlEdgeRight)
- .LineStyle = xlContinuous
- .ColorIndex = 0
- .TintAndShade = 0
- .Weight = xlThin
- End With
- lineCounter.Value = 0
- queue(1) = WorksheetFunction.RandBetween(1, 7)
- queue(2) = WorksheetFunction.RandBetween(1, 7)
- running = True
- falling = True
- isPaused = False
- canMove = True
- getPiece
- StartTimer
- End Sub
- Private Sub StartTimer()
- RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds)
- Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, _
- Schedule:=True
- End Sub
- Private Sub StopTimer()
- On Error Resume Next
- Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, _
- Schedule:=False
- End Sub
- Private Sub Update()
- Cells(1, 1).Value = (Cells(1, 1).Value + 1) Mod 100
- r = piece.Resize(1, 1).Row + piece.Rows.Count - 1
- If (r >= h) Then
- checkForLines
- If (canMove = True) Then
- getPiece
- End If
- End If
- canMove = True
- nextTopLeftX = piece.Resize(1, 1).Column
- nextTopLeftY = piece.Resize(1, 1).Row + 1
- pieceW = piece.Columns.Count
- pieceH = piece.Rows.Count
- flag = False
- 'Cells(nextTopLeftY, nextTopLeftX).Resize(pieceH, pieceW).Select
- For i = 1 To piece.Columns.Count
- For j = 1 To piece.Rows.Count
- If (piece2(j, i) = True) Then
- piece.Cells(j, i).Interior.ColorIndex = -4142
- End If
- Next j
- Next i
- For i = 0 To pieceW - 1
- For j = 0 To pieceH - 1
- If (piece2(j + 1, i + 1) = True And Cells(nextTopLeftY, nextTopLeftX).Offset(j, i).Interior.ColorIndex = 1) Then
- flag = True
- End If
- Next j
- Next i
- If (flag = True) Then
- 'MsgBox ("!")
- For a = 1 To piece.Columns.Count
- For b = 1 To piece.Rows.Count
- If (piece2(b, a) = True) Then
- piece.Cells(b, a).Interior.ColorIndex = 1
- End If
- Next b
- Next a
- checkForLines
- getPiece
- End If
- 'If (falling = True) Then
- If (flag = False) Then
- If (piece.Resize(1, 1).Row + piece.Rows.Count <= h) Then
- 'Move piece down and fill in cells
- Set piece = piece.Offset(1, 0)
- For a = 1 To piece.Columns.Count
- For b = 1 To piece.Rows.Count
- If (piece2(b, a) = True) Then
- piece.Cells(b, a).Interior.ColorIndex = 1
- End If
- Next b
- Next a
- End If
- End If
- StartTimer
- End Sub
- Private Sub getPiece(Optional n = -1)
- If (n = -1) Then
- n = queue(1)
- queue(1) = queue(2)
- queue(2) = WorksheetFunction.RandBetween(1, 7)
- 'n = WorksheetFunction.RandBetween(1, 7)
- End If
- 'Line
- If (n = 1) Then
- Set piece = Range(Cells(1, 4), Cells(1, 7))
- ReDim piece2(1 To 1, 1 To 4)
- piece2(1, 1) = True
- piece2(1, 2) = True
- piece2(1, 3) = True
- piece2(1, 4) = True
- End If
- 'T
- If (n = 2) Then
- Set piece = Range(Cells(1, 4), Cells(2, 6))
- ReDim piece2(1 To 2, 1 To 3)
- piece2(1, 2) = True
- piece2(2, 1) = True
- piece2(2, 2) = True
- piece2(2, 3) = True
- End If
- 'Square
- If (n = 3) Then
- Set piece = Range(Cells(1, 5), Cells(2, 6))
- ReDim piece2(1 To 2, 1 To 2)
- piece2(1, 1) = True
- piece2(1, 2) = True
- piece2(2, 1) = True
- piece2(2, 2) = True
- End If
- 'Left zigzag
- If (n = 4) Then
- Set piece = Range(Cells(1, 4), Cells(2, 6))
- ReDim piece2(1 To 2, 1 To 3)
- piece2(1, 1) = True
- piece2(1, 2) = True
- piece2(2, 2) = True
- piece2(2, 3) = True
- End If
- 'Right zigzag
- If (n = 5) Then
- Set piece = Range(Cells(1, 4), Cells(2, 6))
- ReDim piece2(1 To 2, 1 To 3)
- piece2(1, 2) = True
- piece2(1, 3) = True
- piece2(2, 1) = True
- piece2(2, 2) = True
- End If
- 'Left L
- If (n = 6) Then
- Set piece = Range(Cells(1, 4), Cells(2, 6))
- ReDim piece2(1 To 2, 1 To 3)
- piece2(1, 1) = True
- piece2(2, 1) = True
- piece2(2, 2) = True
- piece2(2, 3) = True
- End If
- 'Right L
- If (n = 7) Then
- Set piece = Range(Cells(1, 4), Cells(2, 6))
- ReDim piece2(1 To 2, 1 To 3)
- piece2(1, 3) = True
- piece2(2, 1) = True
- piece2(2, 2) = True
- piece2(2, 3) = True
- End If
- flag = False
- For a = 1 To piece.Columns.Count
- For b = 1 To piece.Rows.Count
- If (piece2(b, a) = True And piece.Cells(b, a).Interior.ColorIndex = 1) Then
- flag = True
- End If
- Next b
- Next a
- If (flag = False) Then
- For a = 1 To piece.Columns.Count
- For b = 1 To piece.Rows.Count
- If (piece2(b, a) = True) Then
- piece.Cells(b, a).Interior.ColorIndex = 1
- End If
- Next b
- Next a
- Else
- StopTimer
- If (lineCounter.Offset(0, 1).Value < lineCounter.Value) Then
- lineCounter.Offset(0, 1).Value = lineCounter.Value
- End If
- MsgBox ("GAME OVER")
- End
- End If
- falling = True
- End Sub
- Function transpose(arr() As Boolean)
- y = UBound(arr, 1)
- x = UBound(arr, 2)
- ReDim tempArr(1 To x, 1 To y) As Boolean
- For a = 1 To x
- For b = 1 To y
- tempArr(a, b) = arr(b, a)
- Next b
- Next a
- transpose = tempArr
- End Function
- Private Sub rotateLeft()
- 'Transpose bottom, stick to left
- If (running = False) Then
- Exit Sub
- End If
- If (piece.Resize(1, 1).Column + piece.Rows.Count - 1 <= w And piece.Resize(1, 1).Row + piece.Columns.Count - 1 <= h) Then
- For i = 1 To piece.Columns.Count
- For j = 1 To piece.Rows.Count
- If (piece2(j, i) = True) Then
- piece.Cells(j, i).Interior.ColorIndex = -4142
- End If
- Next j
- Next i
- y = UBound(piece2, 1)
- x = UBound(piece2, 2)
- topLeftX = piece.Resize(1, 1).Column
- topLeftY = piece.Resize(1, 1).Row
- ReDim rotatedPiece(1 To x, 1 To y) As Boolean
- For bottom = y To 1 Step -1
- ReDim layer(1 To 1, 1 To x) As Boolean
- 'ReDim rotatedLayer(1 To x, 1 To 1) As Boolean
- For a = 1 To x
- layer(1, a) = piece2(bottom, a)
- 'rotatedLayer() = transpose(layer)
- rotatedPiece(a, bottom) = piece2(bottom, x - a + 1)
- Next a
- Next bottom
- flag = False
- For a = 1 To y
- For b = 1 To x
- If (board.Cells(topLeftY, topLeftX).Offset(b - 1, a - 1).Interior.ColorIndex = 1) Then
- If (rotatedPiece(b, a) = True) Then
- flag = True
- End If
- End If
- Next b
- Next a
- If (flag = False) Then
- piece2 = rotatedPiece
- Set piece = piece.Resize(x, y)
- For a = 1 To x
- For b = 1 To y
- If (piece2(a, b) = True) Then
- piece.Cells(a, b).Interior.ColorIndex = 1
- End If
- Next b
- Next a
- Else
- For i = 1 To piece.Columns.Count
- For j = 1 To piece.Rows.Count
- If (piece2(j, i) = True) Then
- piece.Cells(j, i).Interior.ColorIndex = 1
- End If
- Next j
- Next i
- End If
- End If
- End Sub
- Private Sub rotateRight()
- If (running = False) Then
- Exit Sub
- End If
- If (piece.Resize(1, 1).Column + piece.Rows.Count - 1 <= w And piece.Resize(1, 1).Row + piece.Columns.Count - 1 <= h) Then
- 'Transpose bottom, stick to left
- For i = 1 To piece.Columns.Count
- For j = 1 To piece.Rows.Count
- If (piece2(j, i) = True) Then
- piece.Cells(j, i).Interior.ColorIndex = -4142
- End If
- Next j
- Next i
- y = UBound(piece2, 1)
- x = UBound(piece2, 2)
- topLeftX = piece.Resize(1, 1).Column
- topLeftY = piece.Resize(1, 1).Row
- ReDim rotatedPiece(1 To x, 1 To y) As Boolean
- 'For bottom = y To 1 Step -1
- For bottom = 1 To y
- ReDim layer(1 To 1, 1 To x) As Boolean
- 'ReDim rotatedLayer(1 To x, 1 To 1) As Boolean
- For a = x To 1 Step -1
- layer(1, a) = piece2(bottom, a)
- 'rotatedLayer() = transpose(layer)
- rotatedPiece(a, y - bottom + 1) = piece2(bottom, a)
- Next a
- Next bottom
- flag = False
- For a = 1 To y
- For b = 1 To x
- If (board.Cells(topLeftY, topLeftX).Offset(b - 1, a - 1).Interior.ColorIndex = 1) Then
- If (rotatedPiece(b, a) = True) Then
- flag = True
- End If
- End If
- Next b
- Next a
- If (flag = False) Then
- piece2 = rotatedPiece
- Set piece = piece.Resize(x, y)
- For a = 1 To x
- For b = 1 To y
- If (piece2(a, b) = True) Then
- piece.Cells(a, b).Interior.ColorIndex = 1
- End If
- Next b
- Next a
- Else
- For i = 1 To piece.Columns.Count
- For j = 1 To piece.Rows.Count
- If (piece2(j, i) = True) Then
- piece.Cells(j, i).Interior.ColorIndex = 1
- End If
- Next j
- Next i
- End If
- End If
- End Sub
- Private Sub moveLeft()
- If (running = False) Then
- Exit Sub
- End If
- r = piece.Resize(1, 1).Row
- c = piece.Resize(1, 1).Column
- nextTopLeftX = piece.Resize(1, 1).Column - 1
- nextTopLeftY = piece.Resize(1, 1).Row
- pieceW = piece.Columns.Count
- pieceH = piece.Rows.Count
- flag = False
- For i = 1 To piece.Columns.Count
- For j = 1 To piece.Rows.Count
- If (piece2(j, i) = True) Then
- piece.Cells(j, i).Interior.ColorIndex = -4142
- End If
- Next j
- Next i
- If (nextTopLeftX = 0) Then
- flag = True
- Else
- For i = 0 To pieceW - 1
- For j = 0 To pieceH - 1
- If (piece2(j + 1, i + 1) = True And Cells(nextTopLeftY, nextTopLeftX).Offset(j, i).Interior.ColorIndex = 1) Then
- flag = True
- End If
- Next j
- Next i
- End If
- If (flag = True) Then
- For a = 1 To piece.Columns.Count
- For b = 1 To piece.Rows.Count
- If (piece2(b, a) = True) Then
- piece.Cells(b, a).Interior.ColorIndex = 1
- End If
- Next b
- Next a
- Else
- If (c > 1) Then
- 'piece.Interior.ColorIndex = -4142
- For i = 1 To piece.Columns.Count
- For j = 1 To piece.Rows.Count
- If (piece2(j, i) = True) Then
- piece.Cells(j, i).Interior.ColorIndex = -4142
- End If
- Next j
- Next i
- Set piece = piece.Offset(0, -1)
- y = UBound(piece2, 1)
- x = UBound(piece2, 2)
- For a = 1 To x
- For b = 1 To y
- If (piece2(b, a) = True) Then
- piece.Cells(b, a).Interior.ColorIndex = 1
- End If
- Next b
- Next a
- End If
- End If
- End Sub
- Private Sub moveRight()
- If (running = False) Then
- Exit Sub
- End If
- 'r = piece.Resize(1, 1).Row + piece.Rows.Count - 1
- nextTopLeftX = piece.Resize(1, 1).Column + 1
- nextTopLeftY = piece.Resize(1, 1).Row
- pieceW = piece.Columns.Count
- pieceH = piece.Rows.Count
- flag = False
- For i = 1 To piece.Columns.Count
- For j = 1 To piece.Rows.Count
- If (piece2(j, i) = True) Then
- piece.Cells(j, i).Interior.ColorIndex = -4142
- End If
- Next j
- Next i
- If ((nextTopLeftX + pieceW - 1) > w) Then
- flag = True
- Else
- For i = 0 To pieceW - 1
- For j = 0 To pieceH - 1
- If (piece2(j + 1, i + 1) = True And Cells(nextTopLeftY, nextTopLeftX).Offset(j, i).Interior.ColorIndex = 1) Then
- flag = True
- End If
- Next j
- Next i
- End If
- If (flag = True) Then
- For a = 1 To piece.Columns.Count
- For b = 1 To piece.Rows.Count
- If (piece2(b, a) = True) Then
- piece.Cells(b, a).Interior.ColorIndex = 1
- End If
- Next b
- Next a
- Else
- c = piece.Resize(1, 1).Column + piece.Columns.Count - 1
- 'MsgBox (c)
- If (c < w) Then
- 'piece.Interior.ColorIndex = -4142
- For i = 1 To piece.Columns.Count
- For j = 1 To piece.Rows.Count
- If (piece2(j, i) = True) Then
- piece.Cells(j, i).Interior.ColorIndex = -4142
- End If
- Next j
- Next i
- Set piece = piece.Offset(0, 1)
- y = UBound(piece2, 1)
- x = UBound(piece2, 2)
- For a = 1 To x
- For b = 1 To y
- If (piece2(b, a) = True) Then
- piece.Cells(b, a).Interior.ColorIndex = 1
- End If
- Next b
- Next a
- End If
- End If
- End Sub
- Private Sub moveDown()
- If (running = False) Then
- Exit Sub
- End If
- If (canMove = True) Then
- r = piece.Resize(1, 1).Row + piece.Rows.Count - 1
- nextTopLeftX = piece.Resize(1, 1).Column
- nextTopLeftY = piece.Resize(1, 1).Row + 1
- pieceW = piece.Columns.Count
- pieceH = piece.Rows.Count
- flag = False
- For i = 1 To piece.Columns.Count
- For j = 1 To piece.Rows.Count
- If (piece2(j, i) = True) Then
- piece.Cells(j, i).Interior.ColorIndex = -4142
- End If
- Next j
- Next i
- For i = 0 To pieceW - 1
- For j = 0 To pieceH - 1
- If (piece2(j + 1, i + 1) = True And Cells(nextTopLeftY, nextTopLeftX).Offset(j, i).Interior.ColorIndex = 1) Then
- flag = True
- End If
- Next j
- Next i
- If (flag = True) Then
- 'MsgBox ("!")
- For a = 1 To piece.Columns.Count
- For b = 1 To piece.Rows.Count
- If (piece2(b, a) = True) Then
- piece.Cells(b, a).Interior.ColorIndex = 1
- End If
- Next b
- Next a
- Else
- If (r >= h) Then
- For a = 1 To piece.Columns.Count
- For b = 1 To piece.Rows.Count
- If (piece2(b, a) = True) Then
- piece.Cells(b, a).Interior.ColorIndex = 1
- End If
- Next b
- Next a
- Else
- StopTimer
- StartTimer
- 'piece.Interior.ColorIndex = -4142
- Set piece = piece.Offset(1, 0)
- y = UBound(piece2, 1)
- x = UBound(piece2, 2)
- For a = 1 To x
- For b = 1 To y
- If (piece2(b, a) = True) Then
- piece.Cells(b, a).Interior.ColorIndex = 1
- End If
- Next b
- Next a
- End If
- End If
- 'StartTimer
- End If
- End Sub
- Private Sub pause()
- If (isPaused = False) Then
- StopTimer
- isPaused = True
- Else
- StartTimer
- isPaused = False
- End If
- End Sub
- Private Sub checkForLines()
- canMove = False
- For r = 1 To h
- If (Range(Cells(r, 1), Cells(r, w)).Interior.ColorIndex = 1) Then
- lineCounter.Value = lineCounter.Value + 1
- Range(Cells(r, 1), Cells(r, w)).Interior.ColorIndex = -4142
- For a = r To 2 Step -1
- For b = 1 To w
- Cells(a, b).Interior.ColorIndex = Cells(a - 1, b).Interior.ColorIndex
- Next b
- Next a
- End If
- Next r
- canMove = True
- End Sub
- Private Sub HelloBusinessBrunch()
- 'This sub is assigned the ctrl+w shortcut to prevent accidental closing of the amazing game
- 'Other than that, this sub does nothing
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement