controlling image movements

CJ ONILLA
06-01-2001, 07:55 AM
is it possible to have two images moving at the same time by the user. You see, im using keycode/keydown and it only allows one of my images to move at a time
i.e. when i press a one object moves, but when i press vbkeyUp the first object stops and this begins to move?

any solutions?

anhmytran
06-01-2001, 10:22 AM
Create a brand new project with the default form
Copy and paste the following code to the form code.
at run time, click the form to start, and press key A, B or C
to see the effect.
The project is not for serious game. It has no finish or end.
It just show how to programmatically draw objects on a
container that gives the impression of moving objects.

Option Explicit
Dim L As Long ' for Left calculation
Dim T As Long ' for Top calculation
Dim W As Long ' the screen's width
Dim H As Long ' the screen's height
Dim iA As Integer ' for A object
Dim iB As Integer ' for B object
Dim iC As Integer ' for C object
Dim XA As Long ' for A object
Dim YA As Long ' for A object
Dim XB As Long ' for B object
Dim YB As Long ' for B object
Dim XC As Long ' for C object
Dim YC As Long ' for C object

Private Sub Form_Click()
Timer1.Enabled = True ' Start moving objects
End Sub

Private Sub Form_KeyPress(myKey As Integer)
Select Case myKey
Case 65, 97 ' user press key A or a
iA = 1
iB = 0
iC = 0
Case 66, 98 ' user press key B or b
iA = 0
iB = 1
iC = 0
Case 67, 99 ' user press key C or c
iA = 0
iB = 0
iC = 1
Case Else ' do nothing
End Select
End Sub

Private Sub Form_Load()
Randomize
W = Me.Width
H = Me.Height
XA = CLng((W * Rnd) + 1)
YA = CLng((H * Rnd) + 1)
XB = CLng((W * Rnd) + 1)
YB = CLng((H * Rnd) + 1)
XC = CLng((W * Rnd) + 1)
YC = CLng((H * Rnd) + 1)
Timer1.Enabled = False
Timer1.Interval = 300 ' one tenth of a second
End Sub

Private Sub Timer1_Timer()
Dim lA As Long
If iA Then
lA = 9
Else
lA = 26
End If
MovingA lA
MovingC iC
End Sub

Private Sub MovingA(lA As Long)
Static stA As Integer
If stA > 90 Then
stA = stA - 90
Else
stA = stA + 7
End If
If XA > W Then
YA = CLng((H * Rnd) + 1)
XA = 0
Else
XA = XA + 80
End If
If YA > H Then
YA = H
ElseIf YA < 0 Then
YA = 0
Else:
YA = Sin(stA) * 1600 + lA * 111
End If
Me.Cls
Circle (XA, YA), 50, RGB(233, 0, 100)
MovingB iB
End Sub

Private Sub MovingB(iB As Integer)
Dim lB As Long
If iB Then
lB = 15
Else
lB = 30
End If
If XB > W Then
XB = CLng((W * Rnd) + 1)
XB = XB - lB * 3
Else:
XB = XB + lB * 3
End If
If YB < H Then
YB = CLng((H * Rnd) + 1)
YB = YB + lB * 2
Else:
YB = YB - lB * 2
End If
Circle (XB, YB), 50, RGB(33, 90, 200)
End Sub

Private Sub MovingC(iC As Integer)

End Sub


AnhMy_Tran

BillSoo
06-01-2001, 11:59 AM
Here is a quick example using 2 images on a form....


<PRE>
Option Explicit
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Private Const VK_LEFT = &H25
Private Const VK_RIGHT = &H27
Private Const VK_UP = &H26
Private Const VK_DOWN = &H28
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Dim bDone As Boolean

Private Sub Form_Load()
Me.Show
bDone = False
MainLoop
End Sub

Private Sub MainLoop()
Dim l%, t%
Const DELTAVEE = 10
Dim rc1 As RECT
Dim rc2 As RECT

Do
With Image1
l = .Left
t = .Top
If (GetKeyState(VK_LEFT) And &H8000) Then
l = l - DELTAVEE
If l < 0 Then l = 0
End If
If (GetKeyState(VK_RIGHT) And &H8000) Then
l = l + DELTAVEE
If (l + .Width) > Me.ScaleWidth Then l = Me.ScaleWidth - .Width
End If
If (GetKeyState(VK_UP) And &H8000) Then
t = t - DELTAVEE
If t < 0 Then t = 0
End If
If (GetKeyState(VK_DOWN) And &H8000) Then
t = t + DELTAVEE
If t > (Me.ScaleHeight - .Height) Then t = Me.ScaleHeight - .Height
End If
.Move l, t
rc1.Left = .Left
rc1.Top = .Top
rc1.Right = .Left + .Width
rc1.Bottom = .Top + .Height
End With
With Image2
l = .Left
t = .Top
If (GetKeyState(65) And &H8000) Then 'A
l = l - 5
If l < 0 Then l = 0
End If
If (GetKeyState(68) And &H8000) Then 'D
l = l + 5
If (l + .Width) > Me.ScaleWidth Then l = Me.ScaleWidth - .Width
End If
If (GetKeyState(87) And &H8000) Then 'W
t = t - 5
If t < 0 Then t = 0
End If
If (GetKeyState(88) And &H8000) Then 'X
t = t + 5
If t > (Me.ScaleHeight - .Height) Then t = Me.ScaleHeight - .Height
End If
.Move l, t
rc2.Left = .Left
rc2.Top = .Top
rc2.Right = .Left + .Width
rc2.Bottom = .Top + .Height
End With
If isCollision(rc1, rc2) Then
Image1.Move (Rnd() * (Me.ScaleWidth - Image1.Width)), (Rnd() * (Me.ScaleHeight - Image1.Height))
Image2.Move (Rnd() * (Me.ScaleWidth - Image2.Width)), (Rnd() * (Me.ScaleHeight - Image2.Height))
End If
DoEvents
Loop While Not bDone
End Sub

Private Function isCollision(rc1 As RECT, rc2 As RECT) As Boolean
'returns true if any part of rectangle rc1 overlaps rectangle rc2
isCollision = Not ((rc1.Left > rc2.Right) Or (rc1.Right < rc2.Left) Or (rc1.Top > rc2.Bottom) Or (rc1.Bottom < rc2.Top))
End Function

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
bDone = True
End Sub



</PRE>

"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum