m65马蹄袖:怎样在VB中编出这样程序呢?急。

来源:百度文库 编辑:神马品牌网 时间:2024/04/28 15:12:17
我想做一个小游戏。
游戏里用shape1组出一个数组。共27个。把这个Shape1数组的图做成障碍物挡在路上。
游戏中通过Keydown事件按上下左右键控制人物走动。怎样才能让人物碰到障碍物边缘的时候就让人物停住呢?
目前的keydown是这样做的:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyRight Then
If boy.Left + a < 9840 Then
boy.Left = boy.Left + a
End If
ElseIf KeyCode = vbKeyLeft Then
If boy.Left - a > 0 Then
boy.Left = boy.Left - a
End If
ElseIf KeyCode = vbKeyUp Then
If boy.Top - a > 0 Then
boy.Top = boy.Top - a
End If
ElseIf KeyCode = vbKeyDown Then
If boy.Top + a < 7320 Then
boy.Top = boy.Top + a
End If
End If
End Sub
它只能让我在窗口边缘处停住不走。怎么解决这个障碍物的问题啊?急呀。

将以下代码保存为form1.frm然后用VB打开直接运行就可以了。
因为我不知道你要Shape1数组组成一个什么图,所以我是随机生成的图,不知道是否满足你的意思!
另外我的QQ是110807087,有事可以联系!

'————————form1.frm————————
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3090
ClientLeft = 1395
ClientTop = 1785
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3090
ScaleWidth = 4680
Begin VB.PictureBox boy
Height = 500
Left = 1740
ScaleHeight = 435
ScaleWidth = 435
TabIndex = 0
Top = 1305
Width = 500
End
Begin VB.Shape Shape2
Height = 495
Left = 2625
Top = 1095
Width = 1215
End
Begin VB.Shape Shape1
FillStyle = 0 'Solid
Height = 495
Index = 0
Left = 2100
Top = 540
Visible = 0 'False
Width = 495
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim wall(1 To 27, 1 To 2) As Integer
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim a As Integer
a = boy.Width
If KeyCode = vbKeyRight Then
If boy.Left + a <= 9000 Then
If (check(boy.Left + a, boy.Top)) Then
boy.Left = boy.Left + a
End If
End If
ElseIf KeyCode = vbKeyLeft Then
If boy.Left - a >= 0 Then
If (check(boy.Left - a, boy.Top)) Then
boy.Left = boy.Left - a
End If
End If
ElseIf KeyCode = vbKeyUp Then
If boy.Top - a >= 0 Then
If (check(boy.Left, boy.Top - a)) Then
boy.Top = boy.Top - a
End If
End If
ElseIf KeyCode = vbKeyDown Then
If boy.Top + a <= 6500 Then
If (check(boy.Left, boy.Top + a)) Then
boy.Top = boy.Top + a
End If
End If
End If
End Sub

Private Sub Form_Load()
Dim i As Integer
Dim X As Integer, Y As Integer
Me.KeyPreview = True
Me.Width = 10000
Me.Height = 7800
With Shape2
.Top = 0
.Left = 0
.Width = 9500
.Height = 7000
End With
boy.Left = 0
boy.Top = 0
Randomize
For i = 1 To 27
Load Shape1(i)
X = Int(Rnd * 19) * 500
Y = Int(Rnd * 14) * 500
Shape1(i).Left = X
Shape1(i).Top = Y
wall(i, 1) = X
wall(i, 2) = Y
Shape1(i).Visible = True
Next i
End Sub
Private Function check(ByVal nextL As Integer, ByVal nextT As Integer) As Boolean
Dim i As Integer
For i = 1 To 27
If nextL = wall(i, 1) And nextT = wall(i, 2) Then
check = False
Exit Function
End If
Next i
check = True
End Function

检测物体碰撞对吧?
关键是检测坐标,细心一点,没什么难的呀!
建议:在草稿上画一画,然后再写代码。

不容易!

什么游戏