ahmadfononi
معاونت انجمن
این برنامه بازی کرم متحرک را انجام می دهد - برای اجرا روی فرم خودتان 6 عدد shape قرار دهید و یک label و یک text و یک تایمر نیز اضافه نمایید حال رویدادهای زیر را برای اشیا بنویسید
کد:
کد:
Option Explicit
Dim s As Long
Dim x1 As Long, y1 As Long
Dim x2 As Long, y2 As Long
Dim speed As Long
Dim dx As Long, dy As Long
Private Sub Form_Activate()
'========================== ÊäÙíã ÎÇÕíÊ åÇí Çæáíå
Text1.Left = 10
Text1.Width = 80
Label1.Left = 10
Label1.Top = 40
Label1.Caption = "SCORE:"
Text1.Text = "0"
Text1.Top = 70
Form1.WindowState = 2
Form1.ScaleMode = 3
Shape1.Shape = 1
Shape2.Shape = 1
Shape3.Shape = 0
Shape4.Shape = 0
Shape5.Shape = 0
Shape6.Shape = 0
Shape1.FillColor = vbBlue
Shape2.FillColor = vbRed
Shape3.FillColor = vbYellow
Shape4.FillColor = vbYellow
Shape5.FillColor = vbYellow
Shape6.FillColor = vbYellow
Shape1.Width = 20
Shape1.Height = 20
Shape2.Width = 20
Shape2.Height = 20
Shape3.Left = 100
Shape3.Top = 0
Shape3.Height = Form1.ScaleHeight
Shape3.Width = 15
Shape4.Left = Form1.ScaleWidth - 15
Shape4.Top = 0
Shape4.Height = Form1.ScaleHeight
Shape4.Width = 15
Shape5.Left = 100
Shape5.Top = 0
Shape5.Height = 15
Shape5.Width = Form1.ScaleWidth - 100
Shape6.Left = 100
Shape6.Top = Form1.ScaleHeight - 15
Shape6.Height = 15
Shape6.Width = Form1.ScaleWidth - 100
x2 = 115 + Int(Rnd * (Form1.ScaleWidth - 15 - 115 + 1))
y2 = 15 + Int(Rnd * (Form1.ScaleHeight - 15 - 15 + 1))
Shape2.Left = x2
Shape2.Top = y2
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
'========================== ˜äÊÑá ÍÑ˜Ê ÊæÓØ ˜áíÏ åÇí ÝáÔ
If KeyCode = 37 Then
dy = 0
dx = -10
ElseIf KeyCode = 38 Then
dy = -10
dx = 0
ElseIf KeyCode = 39 Then
dx = 10
dy = 0
ElseIf KeyCode = 40 Then
dy = 10
dx = 0
End If
End Sub
Private Sub Form_Load()
'========================== ÊäÙíã ãÍá Çæáíå ãÇäÚ æ ãÊÍј æ ÓÑÚÊ
dx = 10
dy = 0
x1 = (Form1.ScaleWidth - 100) \ 2
y1 = Form1.ScaleHeight \ 2
speed = 100
s = 0
Text1.Text = Str(s)
End Sub
Private Sub Timer1_Timer()
Dim R As Boolean
'========================== ÊÛííÑ ãÍá ãÊÍј ÈÇ ÊÛííÑ ãÎÕÇÊ Çä
x1 = x1 + dx
y1 = y1 + dy
Shape1.Left = x1
Shape1.Top = y1
R = False
'========================== ˜äÊÑá ÈÑÎæÑÏ Èå ÏÈæÇÑå åÇ
If x1 <= 115 Then
R = True
ElseIf x1 > Form1.ScaleWidth - 25 Then
R = True
ElseIf y1 <= 15 Then
R = True
ElseIf y1 >= Form1.ScaleHeight - 25 Then
R = True
End If
If R = True Then
s = s - 5
Text1.Text = Str(s)
dx = 10
dy = 0
x1 = (Form1.ScaleWidth - 100) \ 2
y1 = Form1.ScaleHeight \ 2
End If
If s < -25 Then
MsgBox ("GAME OVER . . . ")
End
End If
'========================== ˜äÊÑá ÈÑÎæÑÏ ãÊÍј Èå ãÇäÚ
If x1 >= x2 - 20 And x1 <= x2 + 20 And y1 >= y2 - 20 And y1 <= y2 + 20 Then
s = s + 2
x2 = 115 + Int(Rnd * (Form1.ScaleWidth - 15 - 115))
y2 = 15 + Int(Rnd * (Form1.ScaleHeight - 15 - 15 + 1))
Shape2.Left = x2
Shape2.Top = y2
Text1.Text = Str(s)
speed = speed - 4
Timer1.Interval = speed
End If
End Sub
Dim s As Long
Dim x1 As Long, y1 As Long
Dim x2 As Long, y2 As Long
Dim speed As Long
Dim dx As Long, dy As Long
Private Sub Form_Activate()
'========================== ÊäÙíã ÎÇÕíÊ åÇí Çæáíå
Text1.Left = 10
Text1.Width = 80
Label1.Left = 10
Label1.Top = 40
Label1.Caption = "SCORE:"
Text1.Text = "0"
Text1.Top = 70
Form1.WindowState = 2
Form1.ScaleMode = 3
Shape1.Shape = 1
Shape2.Shape = 1
Shape3.Shape = 0
Shape4.Shape = 0
Shape5.Shape = 0
Shape6.Shape = 0
Shape1.FillColor = vbBlue
Shape2.FillColor = vbRed
Shape3.FillColor = vbYellow
Shape4.FillColor = vbYellow
Shape5.FillColor = vbYellow
Shape6.FillColor = vbYellow
Shape1.Width = 20
Shape1.Height = 20
Shape2.Width = 20
Shape2.Height = 20
Shape3.Left = 100
Shape3.Top = 0
Shape3.Height = Form1.ScaleHeight
Shape3.Width = 15
Shape4.Left = Form1.ScaleWidth - 15
Shape4.Top = 0
Shape4.Height = Form1.ScaleHeight
Shape4.Width = 15
Shape5.Left = 100
Shape5.Top = 0
Shape5.Height = 15
Shape5.Width = Form1.ScaleWidth - 100
Shape6.Left = 100
Shape6.Top = Form1.ScaleHeight - 15
Shape6.Height = 15
Shape6.Width = Form1.ScaleWidth - 100
x2 = 115 + Int(Rnd * (Form1.ScaleWidth - 15 - 115 + 1))
y2 = 15 + Int(Rnd * (Form1.ScaleHeight - 15 - 15 + 1))
Shape2.Left = x2
Shape2.Top = y2
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
'========================== ˜äÊÑá ÍÑ˜Ê ÊæÓØ ˜áíÏ åÇí ÝáÔ
If KeyCode = 37 Then
dy = 0
dx = -10
ElseIf KeyCode = 38 Then
dy = -10
dx = 0
ElseIf KeyCode = 39 Then
dx = 10
dy = 0
ElseIf KeyCode = 40 Then
dy = 10
dx = 0
End If
End Sub
Private Sub Form_Load()
'========================== ÊäÙíã ãÍá Çæáíå ãÇäÚ æ ãÊÍј æ ÓÑÚÊ
dx = 10
dy = 0
x1 = (Form1.ScaleWidth - 100) \ 2
y1 = Form1.ScaleHeight \ 2
speed = 100
s = 0
Text1.Text = Str(s)
End Sub
Private Sub Timer1_Timer()
Dim R As Boolean
'========================== ÊÛííÑ ãÍá ãÊÍј ÈÇ ÊÛííÑ ãÎÕÇÊ Çä
x1 = x1 + dx
y1 = y1 + dy
Shape1.Left = x1
Shape1.Top = y1
R = False
'========================== ˜äÊÑá ÈÑÎæÑÏ Èå ÏÈæÇÑå åÇ
If x1 <= 115 Then
R = True
ElseIf x1 > Form1.ScaleWidth - 25 Then
R = True
ElseIf y1 <= 15 Then
R = True
ElseIf y1 >= Form1.ScaleHeight - 25 Then
R = True
End If
If R = True Then
s = s - 5
Text1.Text = Str(s)
dx = 10
dy = 0
x1 = (Form1.ScaleWidth - 100) \ 2
y1 = Form1.ScaleHeight \ 2
End If
If s < -25 Then
MsgBox ("GAME OVER . . . ")
End
End If
'========================== ˜äÊÑá ÈÑÎæÑÏ ãÊÍј Èå ãÇäÚ
If x1 >= x2 - 20 And x1 <= x2 + 20 And y1 >= y2 - 20 And y1 <= y2 + 20 Then
s = s + 2
x2 = 115 + Int(Rnd * (Form1.ScaleWidth - 15 - 115))
y2 = 15 + Int(Rnd * (Form1.ScaleHeight - 15 - 15 + 1))
Shape2.Left = x2
Shape2.Top = y2
Text1.Text = Str(s)
speed = speed - 4
Timer1.Interval = speed
End If
End Sub