实现机理:
Public Type TCycle
Y As Byte
X As Byte
Cid As Byte
FD As Byte
Dead As Boolean
End Type
Dim Player(1 To 4) As TCycle
Dim DoublePlayer As Boolean
Dim Blocks(1 To 160, 1 To 120) As Byte
Dim CycleColors(1 To 4)
Dim DX(1 To 4) As Integer
Dim DY(1 To 4) As Integer
Dim P1Input As String, P2Input As String '按键缓存
Private Sub Form_Load()
cycelcolors(1) = vbBlue
cycelcolors(2) = vbRed
cycelcolors(3) = vbYellow
cycelcolors(4) = vbGreen
DX(1) = 0
DX(2) = 1
DX(3) = 0
DX(4) = -1
DY(1) = -1
DY(2) = 0
DY(3) = 1
DY(4) = 0
End Sub
Public Sub FenPeiWeiZhi()
Dim Dice(1 To 4) As Byte
Dim WZ(1 To 4) As Byte
Dim i As Byte, j As Byte, k As Byte
For i = 1 To 4
WZ(i) = i
Dice(i) = Int(Rnd * 255)
Next
For i = 1 To 3
For j = i + 1 To 4
If Dice(WZ(i)) < Dice(WZ(j)) Then
k = WZ(i)
WZ(i) = WZ(j)
WZ(j) = k
End If
Next
Next
For i = 1 To 4
Player(i).Cid = WZ(i)
Next
End Sub
Public Sub InitGame(DP As Boolean)
Dim i As Byte
DoublePlayer = DP
Call FenPeiWeiZhi
For i = 1 To 4
Player(i).X = startx(Player(i).Cid) '分配起始位置
Player(i).Y = starty(Player(i).Cid)
Player(i).FD = startd(Player(i).Cid)
Player(i).Dead = False
Blocks(Player(i).X, Player(i).Y) = Player(i).Cid
Next
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 27 Then
Unload Me
End If
If KeyCode = 81 Then P1Input = P1Input & "1"
If KeyCode = 87 Then P1Input = P1Input & "2"
If (KeyCode = 37) And DoublePlayer Then P2Input = P2Input & "1"
If (KeyCode = 39) And DoublePlayer Then P2Input = P2Input & "2"
End Sub
Private Sub Timer1_Timer()
If Left(P1Input, 1) = "1" Then
Player(1).FD = Player(1).FD - 1
If Player(1).FD = 0 Then Player(1).FD = 4
P1Input = Mid(P1Input, 2, Len(P1Input) - 1)
End
If Left(P1Input, 1) = "2" Then
Player(1).FD = Player(1).FD + 1
If Player(1).FD > 4 Then Player(1).FD = 1
P1Input = Mid(P1Input, 2, Len(P1Input) - 1)
End
If (Left(P2Input, 1) = "1") And (DoublePlayer) Then
Player(2).FD = Player(2).FD - 1
If Player(2).FD = 0 Then Player(2).FD = 4
P2Input = Mid(P2Input, 2, Len(P2Input) - 1)
End
If (Left(P2Input, 1) = "2") And (DoublePlayer) Then
Player(2).FD = Player(2).FD + 1
If Player(2).FD > 4 Then Player(2).FD = 1
P2Input = Mid(P2Input, 2, Len(P2Input) - 1)
End
Call RunAI '处理AI
For i = 1 To 4
If Not Player(i).Dead Then
Player(i).X = Player(i).X + DX(Player(i).FD)
Player(i).Y = Player(i).Y + DY(Player(i).FD)
If (Player(i).X * Player(i).Y = 0) Or (Player(i).X > 160) Or (Player(i).Y > 120) Or (Blocks(Player(i).X, Player(i).Y) > 0) Then
Player(i).Dead = True
Else
Blocks(Player(i).X, Player(i).Y) = Player(i).Cid
End If
End If
Next
Call DrawBG '画BG
Call DrawBlocks '画格子
k = 0
For i = 1 To 4
If Player(i).Dead Then
k = k + 1
End If
Next
If k >= 3 Then
Call 游戏结束
End If
End Sub
============================================================
画图就各位自己画吧~
