Creating a pong type game to played inside documents

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #16
    Well, I've developed a working sample. It's a form which contains a picturebox. A number of coloured dots bounce around inside the picturebox.

    There are buttons to add and remove dots, a scollbar to adjust the animation speed, a button to pause/continue the animation, and a checkbox to turn erasing of the dots on and off. If you turn the "tails" on, the dots aren't erased each time things are drawn, so they leave a trail behind. It help to show off the colour-cycling.

    Sorry, I got a bit carried away.

    Unfortunately, I'm not sure whether you will be able to make any use of it, but perhaps you can get some ideas from it.

    First, I'll see whether I can attach a copy of the zipped project. Stay tuned...

    How about that, it worked!

    The zip contains a compiled EXE you should be able to run, so you can see what I'm talking about, anyway. I hope the code may give you some ideas, though it's probably not very good.
    Attached Files

    Comment

    • I Hate My Computer
      New Member
      • Mar 2007
      • 44

      #17
      Originally posted by Killer42
      Well, I've developed a working sample. It's a form which contains a picturebox. A number of coloured dots bounce around inside the picturebox.

      There are buttons to add and remove dots, a scollbar to adjust the animation speed, a button to pause/continue the animation, and a checkbox to turn erasing of the dots on and off. If you turn the "tails" on, the dots aren't erased each time things are drawn, so they leave a trail behind. It help to show off the colour-cycling.

      Sorry, I got a bit carried away.

      Unfortunately, I'm not sure whether you will be able to make any use of it, but perhaps you can get some ideas from it.

      First, I'll see whether I can attach a copy of the zipped project. Stay tuned...

      How about that, it worked!

      The zip contains a compiled EXE you should be able to run, so you can see what I'm talking about, anyway. I hope the code may give you some ideas, though it's probably not very good.
      I didn't understand how you did the animation. Help is great.

      Thanks

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #18
        Originally posted by I Hate My Computer
        I didn't understand how you did the animation. Help is great.
        Although the code may look a bit daunting, the way the animation is done is actually quite simple.

        For each of the X (horizontal) and Y (vertical) axes (let's ignore the colour for now) you have a value, and a speed (which I referred to as "delta", often used to indicate a rate of change). At each tick of the timer, you simply add the delta to the current value, and like magic, you have movement.

        Then there is some extra code to bounce when it hits a boundary, by reversing the delta and adding it again (twice). And a bit more to randomly change the speed now and then.

        Comment

        • I Hate My Computer
          New Member
          • Mar 2007
          • 44

          #19
          Originally posted by Killer42
          Although the code may look a bit daunting, the way the animation is done is actually quite simple.

          For each of the X (horizontal) and Y (vertical) axes (let's ignore the colour for now) you have a value, and a speed (which I referred to as "delta", often used to indicate a rate of change). At each tick of the timer, you simply add the delta to the current value, and like magic, you have movement.

          Then there is some extra code to bounce when it hits a boundary, by reversing the delta and adding it again (twice). And a bit more to randomly change the speed now and then.
          So you have the value and the speed. The value is the X and Y. The speed is also the timer. The timer is based off of the scroll bar value. The higher the value the faster it is the lower the value the slower it is. When the timer changes you add the delta to the current value. I think I am starting to get how to do my code but how do you do the timer and make it add the delta. (code example with discription if possible) Thanks for spending your time helping me.

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #20
            Originally posted by I Hate My Computer
            So you have the value and the speed. The value is the X and Y. The speed is also the timer. The timer is based off of the scroll bar value. The higher the value the faster it is the lower the value the slower it is. When the timer changes you add the delta to the current value. I think I am starting to get how to do my code but how do you do the timer and make it add the delta. (code example with discription if possible) Thanks for spending your time helping me.
            The timer code in my program was as follows...
            Code:
            Private Sub Timer1_Timer()
              Timer1.Interval = SpeedDelay
              Static Busy As Boolean
              Static I As Long
              ' This is where all the animation happens...
              If Not Busy Then
                Busy = True
                With Picture1
                  If Check1.Value = 0 Then
                    .Cls
                  End If
                  For I = 1 To NumberOfObjects
                    ApplyDeltaWithBounce X_pos(I), X_Delta(I), 0, .ScaleWidth, 2, -10, 10
                    ApplyDeltaWithBounce Y_pos(I), Y_Delta(I), 0, .ScaleHeight, 2, -10, 10
                    ApplyDeltaWithBounce R(I), R_Delta(I), 0, 255, 5, -15, 15
                    ApplyDeltaWithBounce G(I), G_Delta(I), 0, 255, 5, -15, 15
                    ApplyDeltaWithBounce B(I), B_Delta(I), 0, 255, 5, -15, 15
                    Picture1.PSet (X_pos(I), Y_pos(I)), RGB(R(I), G(I), B(I))
                  Next
                  .Refresh
                  DoEvents
                End With
                Busy = False
              End If
            End Sub
            This code is basically the entire animation.

            It loops through all the dots (I probably shouldn't called them "objects", as it may cause confusion). For each one, it passes each value/delta pair to a generic routine called ApplyDeltaWithB ounce. This routine is designed to take the value and the delta, and apply the delta (amount of change, remember) to the value. It also accepts limits beyond which the value should "bounce". The last three parameters allow me to insert a bit of random change in the movement (if you watch you'll see that the dots sometimes change direction for no apparent reason) by saying "n% of the time, change the delta to something within these limtis".

            With this sort of "black box" approach you don't necessarily need to understand (at first) exactly how each part of the program works - just what effect it has. This routine simply changes the value by the delta amount, bouncing when it hits the specified limits. The Timer1_Timer event procedure does the animation.

            If you like, we can simplify the example by skipping the colour-cycling and the check for re-entry (the Busy flag). As I said, I got a bit carried away. Here's the simpler version
            Code:
            Private Sub Timer1_Timer()
              Timer1.Interval = SpeedDelay
              Static I As Long
              ' This is where all the animation happens...
              With Picture1
                If Check1.Value = 0 Then
                  .Cls
                End If
                For I = 1 To NumberOfObjects
                  ApplyDeltaWithBounce X_pos(I), X_Delta(I), 0, .ScaleWidth, 2, -10, 10
                  ApplyDeltaWithBounce Y_pos(I), Y_Delta(I), 0, .ScaleHeight, 2, -10, 10
                  Picture1.PSet (X_pos(I), Y_pos(I)), RGB(R(I), G(I), B(I))
                Next
                .Refresh
                DoEvents
              End With
            End Sub

            Comment

            • I Hate My Computer
              New Member
              • Mar 2007
              • 44

              #21
              Originally posted by Killer42
              Private Sub Timer1_Timer()
              Timer1.Interval = SpeedDelay
              Static I As Long
              ' This is where all the animation happens...
              With Picture1
              If Check1.Value = 0 Then
              .Cls
              End If
              For I = 1 To NumberOfObjects
              ApplyDeltaWithB ounce X_pos(I), X_Delta(I), 0, .ScaleWidth, 2, -10, 10
              ApplyDeltaWithB ounce Y_pos(I), Y_Delta(I), 0, .ScaleHeight, 2, -10, 10
              Picture1.PSet (X_pos(I), Y_pos(I)), RGB(R(I), G(I), B(I))
              Next
              .Refresh
              DoEvents
              End With
              End Sub
              Can I change the Timer1.Interval = SpeedDelay to have a set interval so you wouldn't need a variable speed and scroll bar?

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #22
                Originally posted by I Hate My Computer
                Can I change the Timer1.Interval = SpeedDelay to have a set interval so you wouldn't need a variable speed and scroll bar?
                Of course.

                I specifically put in the scrollbar and the SpeedDelay variable to allow me to adjust the speed on the fly. There's no reason you can't just set the timer interval at design time and leave it.

                Comment

                • I Hate My Computer
                  New Member
                  • Mar 2007
                  • 44

                  #23
                  Originally posted by Killer42
                  Of course.

                  I specifically put in the scroll bar and the Speed Delay variable to allow me to adjust the speed on the fly. There's no reason you can't just set the timer interval at design time and leave it.
                  If your scroll bar is at the fastest side what is you speed variable and how can I put that in the timer interval thing?

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #24
                    Originally posted by I Hate My Computer
                    If your scroll bar is at the fastest side what is you speed variable and how can I put that in the timer interval thing?
                    To be honest, I don't think it matters what values I used. You should work out what you want to do, and play with values until you get the performance you want.

                    Just keep in mind that the timer's .Interval property specifies the number of milliseconds to pause between "ticks".

                    I just thought of something! Of you're working in VBA, there isn't any timer control, is there? There was a thread here not long ago in which we tracked down some alternatives. You may need to run some searches for things like "VBA timer control" and so on. (Actually, if I manage to find it, I might add it to the Tips & Tricks at the top of the forum.)

                    Comment

                    • I Hate My Computer
                      New Member
                      • Mar 2007
                      • 44

                      #25
                      Originally posted by Killer42
                      To be honest, I don't think it matters what values I used. You should work out what you want to do, and play with values until you get the performance you want.

                      Just keep in mind that the timer's .Interval property specifies the number of milliseconds to pause between "ticks".

                      I just thought of something! Of you're working in VBA, there isn't any timer control, is there? There was a thread here not long ago in which we tracked down some alternatives. You may need to run some searches for things like "VBA timer control" and so on. (Actually, if I manage to find it, I might add it to the Tips & Tricks at the top of the forum.)
                      Your right I don't need your value I just was lazy but I might find a better one if I try myself. I do know this timer code that spins a picture around the document maybe it could help us make a timer like yours for VBA if we change it.

                      Code:
                      Option Explicit
                       
                      Sub Document_Open()
                          Application.OnTime When:=Now + TimeValue("00:00:02"), Name:="ShapeMover"
                      End Sub
                       
                      Sub ShapeMover()
                          Dim oSH As Shape
                          Set oSH = ActiveDocument.Shapes(1)
                          oSH.IncrementRotation 10
                           
                          Application.OnTime When:=Now + TimeValue("00:00:02"), Name:="ShapeMover"
                      End Sub
                      The TimeValue("00:0 0:02") should be useful.

                      Comment

                      • Killer42
                        Recognized Expert Expert
                        • Oct 2006
                        • 8429

                        #26
                        The OnTime technique sounds quite useful.

                        I plan to gather together these sort of Timer substitutes and add them into the Tips & Tricks. Will include this one also, if it's not already there.

                        Comment

                        • I Hate My Computer
                          New Member
                          • Mar 2007
                          • 44

                          #27
                          I found that the help in VBA said the you use an OnTime thing. As shown below.

                          Example
                          This example runs the macro named "Macro1" in the current module at 3:55 P.M.

                          Code:
                          Application.OnTime When:="15:55:00", Name:="Macro1"
                          This example runs the macro named "Macro1" 15 seconds from the time the example is run. The macro name includes the project and module name.

                          Code:
                          Application.OnTime When:=Now + TimeValue("00:00:15"), _
                              Name:="Project1.Module1.Macro1"
                          This example runs the macro named "Start" at 1:30 P.M. The macro name includes the project and module name.

                          Code:
                          Application.OnTime When:=TimeValue("1:30 pm"), _
                              Name:="VBAProj.Module1.Start"
                          So we would go:

                          Code:
                          Application.OnTime When:=Now + TimeValue("00:00:15"), _ Name:="J"
                          Where J is what we want to happen and we can change TimeValue("00:0 0:15") to the time that I want.

                          Comment

                          Working...