473,396 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,396 developers and data experts.

Mouse Tail...

Ali Rizwan
925 512MB
Hi all,
Use this code to make mouse tail using some image.

You need to add an image control and a timer control to form.
Load some image to image control.

Image name : ImgBall
Index : 0 (important)

Under timercontrol use this code
Expand|Select|Wrap|Line Numbers
  1. Private Sub Timer1_Timer()
  2.     Animate
  3. End Sub
Add this code under form load event::::
Expand|Select|Wrap|Line Numbers
  1.  Private Sub Form_Load()
  2.  
  3. Call MUSICSND
  4.  
  5.  Dim I As Integer
  6.     For I = ImgBall.UBound + 1 To 7
  7.         Load ImgBall(I)
  8.         ImgBall(I).Visible = True
  9.         ImgBall(I).Top = ImgBall(I - 1).Top + 11
  10.     Next I
  11.     ImgBall(0).Visible = False
  12.     Call InitVal
  13.     Call InitBall
  14.     Timer1.Interval = 20
  15.     Timer1.Enabled = True
  16.  
  17. End Sub
Add this code under form mousemove event
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  2.     MoveHandler CLng(X), CLng(Y)
  3.     Animate
  4. End Sub
Now add a module and write this code in module :::::

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Public Type Vec2D
  4.     X As Long
  5.     Y As Long
  6. End Type
  7.  
  8. Public Type AnimBall
  9.     Vec As Vec2D
  10.     dx As Double
  11.     dy As Double
  12.     Img As Image
  13. End Type
  14.  
  15. Dim nBalls As Integer
  16. Dim Xpos, Ypos
  17. Dim DeltaT As Double
  18. Dim SegLen
  19. Dim SpringK
  20. Dim Mass
  21. Dim Gravity
  22. Dim Resistance
  23. Dim StopVel As Double
  24. Dim StopAcc As Double
  25. Dim DotSize As Long
  26. Dim Bounce As Double
  27. Dim bFollowM As Boolean
  28. Dim balls() As AnimBall
  29.  
  30. Function InitVal()
  31. ' Some of the variables are still unknown to me
  32.     nBalls = 7          ' numbers of ball
  33.     Xpos = Ypos = 0     ' evaluate position
  34.     DeltaT = 0.01       '
  35.     SegLen = 10#        ' it seem like the distance between the
  36.                         ' mouse pointer and the ball
  37.                         ' it's quite intersting to change the value
  38.                         ' and see the effect
  39.     SpringK = 11       ' spring constant,
  40.                        ' if large, the longer and higher the tail
  41.                        ' will swing
  42.     Mass = 1            'mass of the ball
  43.     Gravity = 40        ' gravity coeff,
  44.                         ' if large, the balls are more difficult
  45.                         ' to move upward
  46.     Resistance = 9     ' resistivity of the ball to move itself
  47.                         ' from a location, the larger the more difficult to
  48.                         ' move
  49.     StopVel = 0.1
  50.     StopAcc = 0.1
  51.     DotSize = 11        ' the size of the ball in pixel
  52.     Bounce = 0.95       ' bouncing coeff,
  53.     bFollowM = True     ' animation flag
  54. End Function
  55.  
  56.  
  57. ' must only be called after load all imgBall
  58. Function InitBall()
  59.     Dim I As Integer
  60.     ReDim balls(nBalls)
  61.  
  62.     For I = 0 To nBalls
  63.         balls(I) = BallSet(MAINPAGE.ImgBall(I))
  64.     Next I
  65.  
  66.     For I = 0 To nBalls
  67.         balls(I).Img.Left = balls(I).Vec.X
  68.         balls(I).Img.Top = balls(1).Vec.Y
  69.     Next I
  70. End Function
  71.  
  72. ' initialize a ball
  73. Function BallSet(Img As Image) As AnimBall
  74.     BallSet.Vec.X = Xpos
  75.     BallSet.Vec.Y = Ypos
  76.     BallSet.dx = BallSet.dy = 0
  77.     Set BallSet.Img = Img
  78. End Function
  79.  
  80. ' initialize a vector variable
  81. Function VecSet(X As Long, Y As Long) As Vec2D
  82.     VecSet.X = X
  83.     VecSet.Y = Y
  84. End Function
  85.  
  86. ' update position when mouse move
  87. Function MoveHandler(X As Long, Y As Long)
  88.     Xpos = X
  89.     Ypos = Y
  90. End Function
  91.  
  92. ' calculate the spring force of the balls chain
  93. Function SpringForce(I As Integer, J As Integer, ByRef spring As Vec2D)
  94.     Dim tempdx, tempdy, tempLen, springF
  95.     tempdx = balls(I).Vec.X - balls(J).Vec.X
  96.     tempdy = balls(I).Vec.Y - balls(J).Vec.Y
  97.     tempLen = Sqr(tempdx * tempdx + tempdy * tempdy)
  98.     If (tempLen > SegLen) Then
  99.         springF = SpringK * (tempLen - SegLen)
  100.         spring.X = spring.X + (tempdx / tempLen) * springF
  101.         spring.Y = spring.Y + (tempdy / tempLen) * springF
  102.     End If
  103. End Function
  104.  
  105. ' main routine of this animated balls
  106. ' call on mouse move or every 20ms
  107. Function Animate()
  108.     Dim iH, iW
  109.     Dim start As Integer
  110.     Dim I As Integer
  111.     Dim spring As Vec2D
  112.     Dim resist As Vec2D
  113.     Dim accel As Vec2D
  114.     ' enable the animation
  115.     If (bFollowM) Then
  116.         balls(0).Vec.X = Xpos
  117.         balls(0).Vec.Y = Ypos
  118.         start = 1
  119.     End If
  120.  
  121.     For I = start To nBalls
  122.         spring = VecSet(0, 0)
  123.  
  124.         If (I > 0) Then
  125.             Call SpringForce(I - 1, I, spring)
  126.         End If
  127.  
  128.         If (I < (nBalls - 1)) Then
  129.             Call SpringForce(I + 1, I, spring)
  130.         End If
  131.         resist = VecSet(-balls(I).dx * Resistance, -balls(I).dy * Resistance)
  132.         accel = VecSet((spring.X + resist.X) / Mass, _
  133.                         (spring.Y + resist.Y) / Mass + Gravity)
  134.  
  135.         balls(I).dx = balls(I).dx + DeltaT * accel.X
  136.         balls(I).dy = balls(I).dy + DeltaT * accel.Y
  137.  
  138.         If (Abs(balls(I).dx) < StopVel And _
  139.             Abs(balls(I).dy) < StopVel And _
  140.             Abs(accel.X) < StopAcc And _
  141.             Abs(accel.Y) < StopAcc) Then
  142.             balls(I).dx = 0
  143.             balls(I).dy = 0
  144.         End If
  145.  
  146.         balls(I).Vec.X = balls(I).Vec.X + balls(I).dx
  147.         balls(I).Vec.Y = balls(I).Vec.Y + balls(I).dy
  148.  
  149.         ' checking for boundary conditions
  150.         iW = MAINPAGE.ScaleWidth
  151.         iH = MAINPAGE.ScaleHeight
  152.  
  153.         ' check bottom
  154.         If (balls(I).Vec.Y >= iH - DotSize - 1) Then
  155.             If (balls(I).dy > 0) Then
  156.                 balls(I).dy = Bounce * (-balls(I).dy)
  157.             End If
  158.             balls(I).Vec.Y = iH - DotSize - 1
  159.         End If
  160.  
  161.         ' check right
  162.         If (balls(I).Vec.X >= iW - DotSize) Then
  163.             If (balls(I).dx > 0) Then
  164.                 balls(I).dx = Bounce * (-balls(I).dx)
  165.             End If
  166.             balls(I).Vec.X = iW - DotSize - 1
  167.         End If
  168.  
  169.         ' check left
  170.         If (balls(I).Vec.X < 0) Then
  171.             If (balls(I).dx < 0) Then
  172.                 balls(I).dx = Bounce * (-balls(I).dx)
  173.             End If
  174.             balls(I).Vec.X = 0
  175.         End If
  176.         ' check top
  177.         If (balls(I).Vec.Y < 0) Then
  178.             If (balls(I).dy < 0) Then
  179.                 balls(I).dy = Bounce * (-balls(I).dy)
  180.             End If
  181.             balls(I).Vec.Y = 0
  182.         End If
  183.  
  184.         balls(I).Img.Left = balls(I).Vec.X
  185.         balls(I).Img.Top = balls(I).Vec.Y
  186.     Next I
  187. End Function
Regards
>> ALI <<
Mar 6 '08 #1
0 4208

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Stephen Thorne | last post by:
Decorators have been getting lots of air-time at the moment, but only really the syntax. After a short discussion on irc the other night I decided to download python2.4 from experimental and write...
10
by: Mel | last post by:
i need to create a unix like "tail" function on my site. the question: the text is displayed in a scrolled area and a new line is added at the end and the entire text is scrolled down so that...
12
by: s99999999s2003 | last post by:
hi I have a file which is very large eg over 200Mb , and i am going to use python to code a "tail" command to get the last few lines of the file. What is a good algorithm for this type of task...
1
by: Bill | last post by:
I'm using code from http://www.hypergurl.com/elasticcursor.html to make a small mouse tail with some text. When I originally designed my page using tables to format it the code worked perfectly. ...
19
by: Kay Schluehr | last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
30
by: Chas Emerick | last post by:
I looked around for an ElementTree-specific mailing list, but found none -- my apologies if this is too broad a forum for this question. I've been using the lxml variant of the ElementTree API,...
6
by: lak | last post by:
i want to write a program that implement tail and tail -f command in unix. can any one help me in this?
0
by: Miguel Perez | last post by:
Please critique this tail call optimizing decorator I've written. I've tried to fix the pitfalls of other proposed decorators, and the result is this one that supports mutual recursion, does not...
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.