basic problem

sunnypalsingh

Active member
Joined
Jul 24, 2005
Messages
41
Programming Experience
Beginner
i have a simple problem which i am not able to figure out....i am not asking for anyone to write a code for me...just a basic logic in 2-3 line if possible

problem:
Timer that makes a ball go around a frame and bounce back
 
Let's say you want to make the ball move from Left to Right and bounce back as soon as it reachs the edge...

First, you need to know in what direction the ball is moving, if from left to right than you have to increase X location, else, decrease it. From left to right, you need to know at what coordinate is the right side of the ball (ball.left + ball.width). If ball_right_side < right_edge, then ball.left += 1.

Once the ball reaches the right edge, the calculation should be ball.left -+ 1. The same logic applies to top and bottom edge.

Hope this gives you the basic idea.
 
It can't be explained in 2-3 lines :(

VB.NET:
[size=2][color=#0000ff]Protected[/color][/size][size=2] [/size][size=2][color=#0000ff]Overridable[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] TimerOnTick([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] obj [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] ea [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] tmrA.Tick
[/size][size=2][color=#008000]' Obtain the Graphics object exposed by the Form.

[/color][/size][size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] grfx [/size][size=2][color=#0000ff]As[/color][/size][size=2] Graphics = CreateGraphics()

[/size][size=2][color=#008000]'Draw the bitmap containing the ball on the Form.

[/color][/size][size=2]grfx.DrawImage(bitmap, _

[/size][size=2][color=#0000ff]CInt[/color][/size][size=2](ballPositionX - ballBitmapWidth / 2), _

[/size][size=2][color=#0000ff]CInt[/color][/size][size=2](ballPositionY - ballBitmapHeight / 2), _

ballBitmapWidth, ballBitmapHeight)

grfx.Dispose()

[/size][size=2][color=#008000]' Increment the ball position by the distance it has

[/color][/size][size=2][/size][size=2][color=#008000]' moved in both X and Y after being redrawn.

[/color][/size][size=2]ballPositionX += ballMoveX

ballPositionY += ballMoveY

[/size][size=2][color=#008000]' Reverse the ball's direction when it hits a boundary.

[/color][/size][size=2][/size][size=2][color=#0000ff]If[/color][/size][size=2] ballPositionX + ballRadiusX >= ClientSize.Width _

[/size][size=2][color=#0000ff]Or[/color][/size][size=2] ballPositionX - ballRadiusX <= 0 [/size][size=2][color=#0000ff]Then

[/color][/size][size=2]ballMoveX = -ballMoveX

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If

[/color][/size][size=2][/size][size=2][color=#008000]' Set the Y boundary at 40 instead of 0 so the ball does not bounce

[/color][/size][size=2][/size][size=2][color=#008000]' into controls on the Form.

[/color][/size][size=2][/size][size=2][color=#0000ff]If[/color][/size][size=2] ballPositionY + ballRadiusY >= ClientSize.Height _

[/size][size=2][color=#0000ff]Or[/color][/size][size=2] ballPositionY - ballRadiusY <= 1 [/size][size=2][color=#0000ff]Then

[/color][/size][size=2]ballMoveY = -ballMoveY

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub [/color][/size]


Regards ;)
 
looping problem

i know its difficult to write the code in 2-3 lines..that is why i said simple logic....anyways thanx a lot for giving the code but basically i had to code this in VB rather than VB.NET...that is why i stressed on logic ....and i know very less commands of VB...(I learnt VB.NET before VB...but since VB is in my course curriculam i had to learn VB too )

I just figured out a very basic code which i am attaching here....still there is one problem...i am trying to figure out how to continously loop it??....
 

Attachments

  • TimerBall.zip
    1.5 KB · Views: 37
full solution

i just figured out the looping problem.....there was no need for a do while loop....

thanx
 

Attachments

  • TimerBall.zip
    1.5 KB · Views: 29
Move a ball around with GDI+

Actually very bad solution ... The shape in your form is moving very unnatural if i may

Here is how it should look like ... run the project bellow and catch some tips ... all that features could be easily implemented in vb6 too.

Regards ;)
 

Attachments

  • AnimateCircle.zip
    23.9 KB · Views: 37
Back
Top