用VB编写贪吃蛇的问题
一个问题:编写VB贪吃蛇的思路:shape2为果实,shape1为蛇身,shape1的index属性为0,通过创建控件数组来增加蛇身.label1显示当前分数,label...
一个问题:编写VB贪吃蛇的思路:
shape2为果实,shape1为蛇身,shape1的index属性为0,通过创建控件数组来增加蛇身.label1显示当前分数,label2显示剩下时间,在窗体上添加6个时钟控件,用于控制蛇的速度与发布果实与减去时间.
代码如下
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 37 And Timer3.Enabled = False Then
Timer1.Enabled = True
Timer2.Enabled = False
Timer3.Enabled = False
Timer4.Enabled = False
End If
If KeyCode = 38 And Timer4.Enabled = False Then
Timer2.Enabled = True
Timer1.Enabled = False
Timer3.Enabled = False
Timer4.Enabled = False
End If
If KeyCode = 39 And Timer1.Enabled = False Then
Timer3.Enabled = True
Timer1.Enabled = False
Timer2.Enabled = False
Timer4.Enabled = False
End If
If KeyCode = 40 And Timer2.Enabled = False Then
Timer4.Enabled = True
Timer2.Enabled = False
Timer3.Enabled = False
Timer1.Enabled = False
End If
End Sub
Private Sub Form_Load()
Randomize
x = Fix(10 * Rnd + 1)
y = Fix(10 * Rnd + 1)
Shape2.Left = x * 300
Shape2.Top = y * 300
For i = 1 To 300
Load Shape1(i)
Next i
End Sub
Private Sub Timer1_Timer()
Shape1(0).Left = Shape1(0).Left - 300
If Shape1(0).Left < -50 Then
Timer1.Enabled = False
MsgBox "游戏结束,你的得分是:" + Label1.Caption
End If
End Sub
Private Sub Timer2_Timer()
Shape1(0).Top = Shape1(0).Top - 300
If Shape1(0).Top < -50 Then
Timer2.Enabled = False
MsgBox "游戏结束,你的得分是:" + Label1.Caption
End If
End Sub
Private Sub Timer3_Timer()
Shape1(0).Left = Shape1(0).Left + 300
If Shape1(0).Left > 6050 Then
Timer3.Enabled = False
MsgBox "游戏结束,你的得分是:" + Label1.Caption
End If
End Sub
Private Sub Timer4_Timer()
Shape1(0).Top = Shape1(0).Top + 300
If Shape1(0).Top > 6050 Then
Timer4.Enabled = False
MsgBox "游戏结束,你的得分是:" + Label1.Caption
End If
End Sub
Private Sub Timer5_Timer()
If Shape1(0).Left <= Shape2.Left + 50 And Shape1(0).Left >= Shape2.Left - 50 And Shape1(0).Top <= Shape2.Top + 50 And Shape1(0).Top >= Shape2.Top - 50 Then
If Shape2.Visible = True Then
Label1.Caption = Label1.Caption + 5
Shape2.Visible = False
Randomize
x = Fix(10 * Rnd + 1)
y = Fix(10 * Rnd + 1)
Shape2.Left = x * 300
Shape2.Top = y * 300
Shape2.Visible = True
End If
End If
End Sub
Private Sub Timer6_Timer()
If Timer1.Enabled = True Or Timer2.Enabled = True Or Timer3.Enabled = True Or Timer4.Enabled = True Then
Label2.Caption = Label2.Caption - 1
End If
If Label2.Caption = 0 Then
Timer1.Enabled = False
Timer2.Enabled = False
Timer3.Enabled = False
Timer4.Enabled = False
Timer6.Enabled = False
Form1.KeyPreview = False
MsgBox "游戏结束,你的得分是:" + Label1.Caption
End If
End Sub
就是无法增加贪吃蛇的长度,会的朋友请留言指教!我想用控件数组增加长度 展开
shape2为果实,shape1为蛇身,shape1的index属性为0,通过创建控件数组来增加蛇身.label1显示当前分数,label2显示剩下时间,在窗体上添加6个时钟控件,用于控制蛇的速度与发布果实与减去时间.
代码如下
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 37 And Timer3.Enabled = False Then
Timer1.Enabled = True
Timer2.Enabled = False
Timer3.Enabled = False
Timer4.Enabled = False
End If
If KeyCode = 38 And Timer4.Enabled = False Then
Timer2.Enabled = True
Timer1.Enabled = False
Timer3.Enabled = False
Timer4.Enabled = False
End If
If KeyCode = 39 And Timer1.Enabled = False Then
Timer3.Enabled = True
Timer1.Enabled = False
Timer2.Enabled = False
Timer4.Enabled = False
End If
If KeyCode = 40 And Timer2.Enabled = False Then
Timer4.Enabled = True
Timer2.Enabled = False
Timer3.Enabled = False
Timer1.Enabled = False
End If
End Sub
Private Sub Form_Load()
Randomize
x = Fix(10 * Rnd + 1)
y = Fix(10 * Rnd + 1)
Shape2.Left = x * 300
Shape2.Top = y * 300
For i = 1 To 300
Load Shape1(i)
Next i
End Sub
Private Sub Timer1_Timer()
Shape1(0).Left = Shape1(0).Left - 300
If Shape1(0).Left < -50 Then
Timer1.Enabled = False
MsgBox "游戏结束,你的得分是:" + Label1.Caption
End If
End Sub
Private Sub Timer2_Timer()
Shape1(0).Top = Shape1(0).Top - 300
If Shape1(0).Top < -50 Then
Timer2.Enabled = False
MsgBox "游戏结束,你的得分是:" + Label1.Caption
End If
End Sub
Private Sub Timer3_Timer()
Shape1(0).Left = Shape1(0).Left + 300
If Shape1(0).Left > 6050 Then
Timer3.Enabled = False
MsgBox "游戏结束,你的得分是:" + Label1.Caption
End If
End Sub
Private Sub Timer4_Timer()
Shape1(0).Top = Shape1(0).Top + 300
If Shape1(0).Top > 6050 Then
Timer4.Enabled = False
MsgBox "游戏结束,你的得分是:" + Label1.Caption
End If
End Sub
Private Sub Timer5_Timer()
If Shape1(0).Left <= Shape2.Left + 50 And Shape1(0).Left >= Shape2.Left - 50 And Shape1(0).Top <= Shape2.Top + 50 And Shape1(0).Top >= Shape2.Top - 50 Then
If Shape2.Visible = True Then
Label1.Caption = Label1.Caption + 5
Shape2.Visible = False
Randomize
x = Fix(10 * Rnd + 1)
y = Fix(10 * Rnd + 1)
Shape2.Left = x * 300
Shape2.Top = y * 300
Shape2.Visible = True
End If
End If
End Sub
Private Sub Timer6_Timer()
If Timer1.Enabled = True Or Timer2.Enabled = True Or Timer3.Enabled = True Or Timer4.Enabled = True Then
Label2.Caption = Label2.Caption - 1
End If
If Label2.Caption = 0 Then
Timer1.Enabled = False
Timer2.Enabled = False
Timer3.Enabled = False
Timer4.Enabled = False
Timer6.Enabled = False
Form1.KeyPreview = False
MsgBox "游戏结束,你的得分是:" + Label1.Caption
End If
End Sub
就是无法增加贪吃蛇的长度,会的朋友请留言指教!我想用控件数组增加长度 展开
1个回答
展开全部
Private Sub Timer5_Timer()
If Shape1(0).Left <= Shape2.Left + 50 And Shape1(0).Left >= Shape2.Left - 50 And Shape1(0).Top <= Shape2.Top + 50 And Shape1(0).Top >= Shape2.Top - 50 Then
If Shape2.Visible = True Then
Label1.Caption = Label1.Caption + 5
Shape2.Visible = False
Randomize
x = Fix(10 * Rnd + 1)
y = Fix(10 * Rnd + 1)
Shape2.Left = x * 300
Shape2.Top = y * 300
Shape2.Visible = True
End If
End If
End Sub
整段代码中,这一段是判断“蛇”是否已经吃到“果实”,如果吃到“果实”就随机位置产生一个“果实”,但这段代码中没有增加蛇的长度的代码。在这一段中加进去就OK了。
例:
Private Sub Timer5_Timer()
If Shape1(0).Left <= Shape2.Left + 50 And Shape1(0).Left >= Shape2.Left - 50 And Shape1(0).Top <= Shape2.Top + 50 And Shape1(0).Top >= Shape2.Top - 50 Then
If Shape2.Visible = True Then ‘如果果实是有效的,
Label1.Caption = Label1.Caption + 5 ‘给玩家加分。
Shape2.Visible = False ’隐藏被吃掉的果实。
‘在这里加入代码增加蛇的长度
Randomize
x = Fix(10 * Rnd + 1)
y = Fix(10 * Rnd + 1)
Shape2.Left = x * 300
Shape2.Top = y * 300
Shape2.Visible = True
End If
End If
End Sub
If Shape1(0).Left <= Shape2.Left + 50 And Shape1(0).Left >= Shape2.Left - 50 And Shape1(0).Top <= Shape2.Top + 50 And Shape1(0).Top >= Shape2.Top - 50 Then
If Shape2.Visible = True Then
Label1.Caption = Label1.Caption + 5
Shape2.Visible = False
Randomize
x = Fix(10 * Rnd + 1)
y = Fix(10 * Rnd + 1)
Shape2.Left = x * 300
Shape2.Top = y * 300
Shape2.Visible = True
End If
End If
End Sub
整段代码中,这一段是判断“蛇”是否已经吃到“果实”,如果吃到“果实”就随机位置产生一个“果实”,但这段代码中没有增加蛇的长度的代码。在这一段中加进去就OK了。
例:
Private Sub Timer5_Timer()
If Shape1(0).Left <= Shape2.Left + 50 And Shape1(0).Left >= Shape2.Left - 50 And Shape1(0).Top <= Shape2.Top + 50 And Shape1(0).Top >= Shape2.Top - 50 Then
If Shape2.Visible = True Then ‘如果果实是有效的,
Label1.Caption = Label1.Caption + 5 ‘给玩家加分。
Shape2.Visible = False ’隐藏被吃掉的果实。
‘在这里加入代码增加蛇的长度
Randomize
x = Fix(10 * Rnd + 1)
y = Fix(10 * Rnd + 1)
Shape2.Left = x * 300
Shape2.Top = y * 300
Shape2.Visible = True
End If
End If
End Sub
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询