| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 490 人关注过本帖
标题:[分享]菜鸟编程之--------个性贪食蛇(VB版)
只看楼主 加入收藏
bowen666
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2007-7-15
收藏
 问题点数:0 回复次数:3 
[分享]菜鸟编程之--------个性贪食蛇(VB版)

在网上搜了一篇不错不文章,拿出来分享一下!



Dim runway As Integer '蛇的方向
Dim runstep As Integer '定义每一步走多远
Dim maxlong As Integer '蛇的长度
Private Type snakelocal '一个结构,定义蛇的位置
x As Long
y As Long
End Type
Dim snakeinitl(3) As snakelocal '初使蛇的位置
Dim snakel As snakelocal
Private Sub Form_Load()
Dim i As Integer
Dim shudu As Integer
runway = 2 '蛇向右行
runstep = 900 '每一步走蛇的宽度那么大
maxlong = 3
For i = 0 To 3
snakeinitl(i).x = Shape1(i).Left
snakeinitl(i).y = Shape1(i).Left
Next
initgame '子和序初使化程序
Label1.Caption = "***B键开始游戏***A键退出游戏***"
Label1.Visible = True
End Sub
Private Sub form_keydown(keycode As Integer, shift As Integer) '按键
Select Case keycode
Case 37 '左
If runway <> 2 Then runway = 0
Case 38 '上
If runway <> 3 Then runway = 1
Case 39 '右
If runway <> 0 Then runway = 2
Case 40 '下
If runway <> 1 Then runway = 3
Case 65 'A
End
Case 66 'B
If Timer1.Enabled = False Then '如果timer1.enabled = false 则起动游戏
initgame
Timer1.Enabled = True
Timer2.Enabled = True
Timer3.Enabled = True
End If
End Select
End Sub
Private Sub Timer1_Timer() '起动游戏
snakel.x = Shape1(0).Left
snakel.y = Shape1(0).Top
Select Case runway '方向
Case 0
Shape1(0).Left = Shape1(0).Left - runstep
checkwinne '检察输没有
checkbean '检察吃到豆没有
movesnake '身体跟着头移动
Case 1
Shape1(0).Top = Shape1(0).Top - runstep
checkwinne
checkbean
movesnake
Case 2
Shape1(0).Left = Shape1(0).Left + runstep
checkwinne
checkbean
movesnake
Case 3
Shape1(0).Top = Shape1(0).Top + runstep
checkwinne
checkbean
movesnake
End Select
End Sub
Private Sub Timer2_Timer() '随机产生豆子一
Dim x As Long
Dim y As Long
Randomize Timer '每次产生的随机数不一样
x = Rnd * Form1.Width - 901
y = Rnd * Form1.Height - 901
Shape2.Top = y
Shape2.Left = x
Shape2.Visible = True
Timer2.Enabled = False
End Sub
Private Sub Timer3_Timer() '随机产生豆子二
Dim x As Long
Dim y As Long
Randomize Timer
x = Rnd * Form1.Width - 901
y = Rnd * Form1.Height - 901
Shape3.Top = y
Shape3.Left = x
Shape3.Visible = True
Timer3.Enabled = False
End Sub
Private Sub checkwinne() '检察输没有
Dim i As Integer
If Shape1(0).Left + Shape1(0).Width < 0 Or Shape1(0).Left > Form1.ScaleWidth Or Shape1(0).Top + Shape1(0).Width < 0 Or Shape1(0).Top > Form1.ScaleHeight Then
Timer1.Enabled = False
Timer2.Enabled = False
Timer3.Enabled = False
Label1.Visible = True
Label1.Caption = "***B键开始游戏***A键退出游戏***"
End If
For i = 1 To maxlong
If Shape1(0).Left = Shape1(i).Left And Shape1(0).Top = Shape1(i).Top Then
Timer1.Enabled = False
Timer2.Enabled = False
Timer3.Enabled = False
Label1.Visible = True
Label1.Caption = "***B键开始游戏***A键退出游戏***"
Exit For
End If
Next
End Sub
Private Sub movesnake() '身体跟着头移动
Dim tmpx As Long
Dim tmpy As Long
For i = 1 To maxlong
tmpx = Shape1(i).Left
tmpy = Shape1(i).Top
Shape1(i).Left = snakel.x
Shape1(i).Top = snakel.y
snakel.x = tmpx
snakel.y = tmpy
Next
End Sub
Private Sub checkbean() '检察吃到豆没有 每个方向都要检察
If Shape2.Left > Shape1(0).Left And Shape2.Left < (Shape1(0).Left + Shape1(0).Width) And Shape2.Top > Shape1(0).Top And Shape2.Top < Shape1(0).Top + Shape1(0).Height Then
Shape2.Visible = False
Timer2.Enabled = True
maxlong = maxlong + 1
fenshu
Load Shape1(maxlong)
Shape1(maxlong).Top = Shape1(maxlong - 1).Top + Shape1(maxlong).Height
Shape1(maxlong).Left = Shape1(maxlong - 1).Left + Shape1(maxlong).Width
Shape1(maxlong).FillColor = &HFF0000
Shape1(maxlong).Shape = 0
Shape1(maxlong).Visible = True
ElseIf Shape3.Left > Shape1(0).Left And Shape3.Left < (Shape1(0).Left + Shape1(0).Width) And Shape3.Top > Shape1(0).Top And Shape3.Top < Shape1(0).Top + Shape1(0).Height Then
Shape3.Visible = False
Timer3.Enabled = True
maxlong = maxlong + 1
fenshu
Load Shape1(maxlong)
Shape1(maxlong).Top = Shape1(maxlong - 1).Top + Shape1(maxlong).Height
Shape1(maxlong).Left = Shape1(maxlong - 1).Left + Shape1(maxlong).Width
Shape1(maxlong).FillColor = &HFF0000
Shape1(maxlong).Shape = 0
Shape1(maxlong).Visible = True
ElseIf Shape1(0).Left > Shape2.Left And Shape1(0).Left < (Shape2.Left + Shape2.Width) And Shape1(0).Top > Shape2.Top And Shape1(0).Top < Shape2.Top + Shape2.Height Then
Shape2.Visible = False
Timer2.Enabled = True
maxlong = maxlong + 1
fenshu
Load Shape1(maxlong)
Shape1(maxlong).Top = Shape1(maxlong - 1).Top + Shape1(maxlong).Height
Shape1(maxlong).Left = Shape1(maxlong - 1).Left + Shape1(maxlong).Width
Shape1(maxlong).FillColor = &HFF0000
Shape1(maxlong).Shape = 0
Shape1(maxlong).Visible = True
ElseIf Shape1(0).Left > Shape3.Left And Shape1(0).Left < (Shape3.Left + Shape3.Width) And Shape1(0).Top > Shape3.Top And Shape1(0).Top < Shape3.Top + Shape3.Height Then
Shape3.Visible = False
Timer3.Enabled = True
maxlong = maxlong + 1
fenshu
Load Shape1(maxlong)
Shape1(maxlong).Top = Shape1(maxlong - 1).Top + Shape1(maxlong).Height
Shape1(maxlong).Left = Shape1(maxlong - 1).Left + Shape1(maxlong).Width
Shape1(maxlong).FillColor = &HFF0000
Shape1(maxlong).Shape = 0
Shape1(maxlong).Visible = True
ElseIf Shape2.Left > Shape1(0).Left And Shape2.Left < (Shape1(0).Left + Shape1(0).Width) And Shape1(0).Top > Shape2.Top And Shape1(0).Top < Shape2.Top + Shape2.Height Then
Shape2.Visible = False
Timer2.Enabled = True
maxlong = maxlong + 1
fenshu
Load Shape1(maxlong)
Shape1(maxlong).Top = Shape1(maxlong - 1).Top + Shape1(maxlong).Height
Shape1(maxlong).Left = Shape1(maxlong - 1).Left + Shape1(maxlong).Width
Shape1(maxlong).FillColor = &HFF0000
Shape1(maxlong).Shape = 0
Shape1(maxlong).Visible = True
ElseIf Shape3.Left > Shape1(0).Left And Shape3.Left < (Shape1(0).Left + Shape1(0).Width) And Shape1(0).Top > Shape3.Top And Shape1(0).Top < Shape3.Top + Shape3.Height Then
Shape3.Visible = False
Timer3.Enabled = True
maxlong = maxlong + 1
fenshu
Load Shape1(maxlong)
Shape1(maxlong).Top = Shape1(maxlong - 1).Top + Shape1(maxlong).Height
Shape1(maxlong).Left = Shape1(maxlong - 1).Left + Shape1(maxlong).Width
Shape1(maxlong).FillColor = &HFF0000
Shape1(maxlong).Shape = 0
Shape1(maxlong).Visible = True
ElseIf Shape1(0).Left > Shape2.Left And Shape1(0).Left < (Shape2.Left + Shape2.Width) And Shape2.Top > Shape1(0).Top And Shape2.Top < Shape1(0).Top + Shape1(0).Height Then
Shape2.Visible = False
Timer2.Enabled = True
maxlong = maxlong + 1
fenshu
Load Shape1(maxlong)
Shape1(maxlong).Top = Shape1(maxlong - 1).Top + Shape1(maxlong).Height
Shape1(maxlong).Left = Shape1(maxlong - 1).Left + Shape1(maxlong).Width
Shape1(maxlong).FillColor = &HFF0000
Shape1(maxlong).Shape = 0
Shape1(maxlong).Visible = True
ElseIf Shape1(0).Left > Shape3.Left And Shape1(0).Left < (Shape3.Left + Shape3.Width) And Shape3.Top > Shape1(0).Top And Shape3.Top < Shape1(0).Top + Shape1(0).Height Then
Shape3.Visible = False
Timer3.Enabled = True
maxlong = maxlong + 1
fenshu
Load Shape1(maxlong)
Shape1(maxlong).Top = Shape1(maxlong - 1).Top + Shape1(maxlong).Height
Shape1(maxlong).Left = Shape1(maxlong - 1).Left + Shape1(maxlong).Width
Shape1(maxlong).FillColor = &HFF0000
Shape1(maxlong).Shape = 0
Shape1(maxlong).Visible = True
ElseIf Shape1(0).Left = Shape2.Left And Shape1(0).Top = Shape2.Top Then
Shape2.Visible = False
Timer2.Enabled = True
maxlong = maxlong + 1
fenshu
Load Shape1(maxlong)
Shape1(maxlong).Top = Shape1(maxlong - 1).Top + Shape1(maxlong).Height
Shape1(maxlong).Left = Shape1(maxlong - 1).Left + Shape1(maxlong).Width
Shape1(maxlong).FillColor = &HFF0000
Shape1(maxlong).Shape = 0
Shape1(maxlong).Visible = True
ElseIf Shape1(0).Left = Shape3.Left And Shape1(0).Top = Shape3.Top Then
Shape3.Visible = False
Timer3.Enabled = True
maxlong = maxlong + 1
fenshu
Load Shape1(maxlong)
Shape1(maxlong).Top = Shape1(maxlong - 1).Top + Shape1(maxlong).Height
Shape1(maxlong).Left = Shape1(maxlong - 1).Left + Shape1(maxlong).Width
Shape1(maxlong).FillColor = &HFF0000
Shape1(maxlong).Shape = 0
Shape1(maxlong).Visible = True
End If
End Sub
Private Sub initgame()
Dim i As Integer
For i = 0 To maxlong
If i >= 4 Then Unload Shape1(i)
If i <= 3 Then
Shape1(i).Left = snakeinitl(i).x
Shape1(i).Top = snakeinitl(i).y
End If
Next
maxlong = 3
runway = 2
Label1.Top = Form1.Height / 4 - Label1.Height / 2
Label1.Left = Form1.Width / 2 + Label1.Width / 8
End Sub
Private Sub fenshu()
Label1.Caption = "你的现在的分数是:" & (maxlong - 3) * 500 & "分!!"
Label1.Visible = True
For i = 1 To 500000
DoEvents
Next
Label1.Visible = False
End Sub


mSrP4pgn.rar (78.46 KB) 菜鸟编程之--------个性贪食蛇(VB版)


[此贴子已经被作者于2007-9-2 16:52:37编辑过]

搜索更多相关主题的帖子: 分享 
2007-09-02 15:43
multiple1902
Rank: 8Rank: 8
等 级:贵宾
威 望:42
帖 子:4881
专家分:671
注 册:2007-2-9
收藏
得分:0 
2007-09-02 16:22
cc0214
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-9-4
收藏
得分:0 

2007-09-04 14:34
guiyangl015
Rank: 1
等 级:新手上路
帖 子:125
专家分:0
注 册:2006-5-21
收藏
得分:0 

站的高,才能看的远!!
2007-09-04 15:39
快速回复:[分享]菜鸟编程之--------个性贪食蛇(VB版)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020505 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved