永发信息网

VB三维立方体演示代码

答案:1  悬赏:20  手机版
解决时间 2021-07-21 12:18
  • 提问者网友:謫仙
  • 2021-07-20 18:29
详细的三维立方体演示代码
最佳答案
  • 五星知识达人网友:平生事
  • 2021-07-20 19:01

新建一个form1


添加一个timer1 间隔设置为1


添加一个picture1()如图



输入如下代码:


Private CenterX As Integer
Private CenterY As Integer
Private Const Size = 40
Private CurX As Integer
Private CurY As Integer
Private CurZ As Integer
Private MoveTo As Integer
Private Const MOVE_LEFT = 0
Private Const MOVE_RIGHT = 1
Private Const MOVE_UP = 2
Private Const MOVE_DOWN = 3
Private Const MOVE_FORWARD = 4
Private Const MOVE_BACKWARD = 5
Public Sub EraseBlock()
X = CurX: y = CurY: Z = CurZ
xs = (CenterX + X * Size) - Z * (Size / 2)
Ys = (CenterY - y * Size) + Z * (Size / 2)
Line (xs, Ys)-(xs + Size, Ys - Size), BackColor, BF
Line (xs - Size / 2, Ys + Size / 2)-(xs + Size / 2, Ys - Size / 2), BackColor, BF
For i = 0 To Size / 2
Line (xs - i, Ys + i)-(xs - i, Ys + i - Size - 1), BackColor
Line (xs - i + Size, Ys + i)-(xs - i + Size, Ys + i - Size), BackColor
Next
End Sub
Public Sub DrawBlock()
Line (CenterX, CenterY)-(CenterX + Size * 6, CenterY - Size * 6), vbBlue, B
Line (CenterX, CenterY)-(CenterX - Size * 6 / 2, CenterY + Size * 6 / 2), vbBlue
Line (CenterX, CenterY - Size * 6)-(CenterX - Size * 6 / 2, CenterY + Size * 6 / 2 - Size * 6), vbBlue
Line (CenterX + 1, CenterY - 1)-(CenterX + Size * 6 - 1, CenterY - Size * 6 + 1), RGB(0, 60, 0), BF
For i = 1 To Size * 6 / 2 - 1
Line (CenterX - i + 1, CenterY + i)-(CenterX - i + Size * 6, CenterY + i), RGB(0, 60 + i * 2, 0)
Next


For i = 0 To Size * 6 / 2 - 1
Line (CenterX - i - 1, CenterY + i)-(CenterX - i - 1, CenterY + i - Size * 6 + 1), RGB(0, 60 + i * 2, 0)
Next
Label1.Caption = "X : " & CurX & vbCrLf & "Y : " & CurY & vbCrLf & "Z : " & CurZ & vbCrLf
X = CurX: y = CurY: Z = CurZ
col = 10 + Z * 20
xs = (CenterX + X * Size) - Z * (Size / 2)
Ys = CenterY + Z * (Size / 2)
For i = 0 To Size / 2
Line (xs - i, Ys + i)-(xs - i + Size, Ys + i), vbBlack
Next
Ys = (CenterY - y * Size) + Z * (Size / 2)
Line (xs - Size / 2 + 1, Ys + Size / 2 - 1)-(xs + Size / 2 - 1, Ys - Size / 2 + 1), RGB(col + 120, 0, 0), BF
Line (xs + 1, Ys - 1)-(xs + Size - 1, Ys - Size + 1), RGB(col, 0, 0), BF
For i = 0 To Size / 2
Line (xs - i, Ys + i)-(xs - i + Size, Ys + i), RGB(col + i * 8, 0, 0)
Line (xs - i, Ys + i)-(xs - i, Ys + i - Size), RGB(col + i * 8, 0, 0)
Line (xs - i + Size, Ys + i)-(xs - i + Size, Ys + i - Size), RGB(col + i * 8, 0, 0)
Next
Line (CenterX - Size * 6 / 2, CenterY + Size * 6 / 2)-(CenterX + Size * 6 / 2, CenterY - Size * 6 / 2), vbBlue, B
Line (CenterX + Size * 6, CenterY)-(CenterX + Size * 6 - Size * 6 / 2, CenterY + Size * 6 / 2), vbBlue
Line (CenterX + Size * 6, CenterY - Size * 6)-(CenterX + Size * 6 - Size * 6 / 2, CenterY + Size * 6 / 2 - Size * 6), vbBlue
End Sub
Private Sub cmdDemo_Click()
Select Case cmdDemo.Caption
Case "开始运动"
Timer1.Enabled = True
cmdDemo.Caption = "结束运动"
Case Else
Timer1.Enabled = False
cmdDemo.Caption = "开始运动"
End Select
End Sub


Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyLeft
If CurX > 0 Then
EraseBlock
CurX = CurX - 1
DrawBlock
End If
Case vbKeyRight
If CurX < 5 Then
EraseBlock
CurX = CurX + 1
DrawBlock
End If
Case vbKeyUp
If Shift = 0 Then
If CurY < 5 Then
EraseBlock
CurY = CurY + 1
DrawBlock
End If
ElseIf Shift = 1 Then
If CurZ > 0 Then
EraseBlock
CurZ = CurZ - 1
DrawBlock
End If
End If
Case vbKeyDown
If Shift = 0 Then
If CurY > 0 Then
EraseBlock
CurY = CurY - 1
DrawBlock
End If
ElseIf Shift = 1 Then
If CurZ < 5 Then
EraseBlock
CurZ = CurZ + 1
DrawBlock
End If
End If
End Select
End Sub
Private Sub Form_Load()
Move 0, 0, Screen.Width, Screen.Height
Show
Label1.Move Picture1.ScaleWidth - Label2.Width, 0
Label2.Move Picture1.ScaleWidth - Label2.Width, Label1.Height * 4
Picture1.Move ScaleWidth - Picture1.Width, 0
CenterX = ScaleWidth / 4
CenterY = ScaleHeight / 1.5
DrawBlock
End Sub
Private Sub Timer1_Timer()
Randomize
d = Int(Rnd * 11)
If d > 5 And MoveTo < 4 Then MoveTo = RandomMove
EraseBlock
Select Case MoveTo
Case MOVE_DOWN
If CurY > 0 Then CurY = CurY - 1 Else: MoveTo = RandomMove
Case MOVE_UP
If CurY < 5 Then CurY = CurY + 1 Else: MoveTo = RandomMove
Case MOVE_RIGHT
If CurX < 5 Then CurX = CurX + 1 Else: MoveTo = RandomMove
Case MOVE_LEFT
If CurX > 0 Then CurX = CurX - 1 Else: MoveTo = RandomMove
Case MOVE_BACKWARD
If CurZ > 0 Then CurZ = CurZ - 1 Else MoveTo = RandomMove
Case MOVE_FORWARD
If CurZ < 5 Then CurZ = CurZ + 1 Else: MoveTo = RandomMove
End Select
DrawBlock
End Sub
Private Function RandomMove() As Integer
Randomize
Dim d As Integer
d = Int(Rnd * 6)
RandomMove = d
End Function

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯