VB--自编魔方游戏(2D)
众所周知,魔方原本是一个三维的游戏,是一款锻炼IQ的好游戏哈,所以难度就有点大,因此为了降低难度,我们就编一个二维平面的魔方玩玩(虽然是二维的,但其难度还是大哟,如本程序给的5*5的魔方,就够你玩的哈)
玩魔方游戏胜的条件:1,运行程序后将随便生成五种颜色.
2,将每种颜色排列,可以通过横向或纵向移动改变游戏颜色.
3,如果横向或纵向的颜色一致,则胜哈.
4,不能斜线移动.
//---现在开始编写程序-----//
首先创建一个FORM,添加一个PICTUREBOX控件并把autoredraw设置为true,在其中定义两个通用型全局变量:
Dim x1 As Integer
Dim y1 As Integer //鼠标定位
//---定位picturebox控件
Private Sub Form_Load()
Me.Picture1.Scale (0, 5)-(5, 0)
End Sub
//--设置填充颜色
Sub fill_color()
Dim color As Integer
Dim color_col As New Collection '定义一个集合变量
'在集合中装载5种颜色
For i = 0 To 24
color_col.Add Int(i / 5), Str(i)
Next i
'在方格中填充颜色
j = 0
For n = 0 To 4
For m = 1 To 5
Randomize
X = Int((25 - j) * Rnd() + 1)
color = color_col.Item(X)
Picture1.Line (n, m)-(n + 1, m - 1), QBColor(color), BF
color_col.Remove (X)
j = j + 1
Next m
Next n
End Sub
//---在picturebox控件中画等长等宽的距形
Sub draw()
For n = 0 To 5
Picture1.Line (0, n)-(5, n), QBColor(15)
Picture1.Line (n, 0)-(n, 5), QBColor(15)
Next n
End Sub
//---颜色和方块的生成
Private Sub Form_Activate()
fill_color
draw
End Sub
//----鼠标按下事件,将选择指定颜色块
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
x1 = Int(X)
y1 = Int(Y)
Picture1.MousePointer = 14
End Sub
//----移动方块,但只能横向和纵向移动哈.
Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim color1(4) As Long
Dim x2 As Integer
Dim y2 As Integer
x2 = Int(X)
y2 = Int(Y)
If y1 = y2 Then
For n = 0 To 4
color1(n) = Picture1.Point(n + 0.5, y2 + 0.5)
Next n
a = x2 - x1
For n = 0 To 4
If n - a <= 4 And n - a >= 0 Then
z = n - a
Else
z = Abs(5 - Abs(n - a))
End If
Picture1.Line (n, y2 + 1)-(n + 1, y2), color1(z), BF
Next n
Else
If x1 = x2 Then
For n = 0 To 4
color1(n) = Picture1.Point(x2 + 0.5, n + 0.5)
Next n
a = y2 - y1
For n = 0 To 4
If n - a <= 4 And n - a >= 0 Then
z = n - a
Else
z = Abs(5 - Abs(n - a))
End If
Picture1.Line (x2 + 1, n)-(x2, n + 1), color1(z), BF
Next n
Else
MsgBox "你的移动无效", 16, "警告"
End If
End If
draw
Picture1.MousePointer = vbDefault
End Sub
//--哈哈,一款魔方游戏就被VB征服了哈...业余时间编写.....
//--里面的乐趣就留给朋友们去玩,去体会哈....
//-------------James--------2006-05-21-------//
阅读(2268) | 评论(1) | 转发(0) |