Results 1 to 14 of 14

Thread: moving the ball in PONG

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    66

    moving the ball in PONG

    vb Code:
    1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick, MyBase.Move
    2.         Dim movepix As Integer
    3.         movepix = 5
    4.         Ball = Me.CreateGraphics
    5.         Ball.Clear(Color.Gray)
    6.  
    7.         If y = 230 Then
    8.             movepix = (movepix * -1)
    9.  
    10.         End If
    11.         If y = 0 Then
    12.             movepix = (movepix * -1)
    13.  
    14.         End If
    15.         If x = 230 Then
    16.             movepix = (movepix * -1)
    17.  
    18.         End If
    19.         If x = 0 Then
    20.             movepix = (movepix * -1)
    21.  
    22.         Else
    23.             x += movepix
    24.             y += movepix
    25.         End If
    26.  
    27.  
    28.         Ball.FillEllipse(Brushes.Yellow, x, y, 20, 20)
    29.     End Sub


    this is what i came up with for moving the ball in a pong game i am creating.....all the ball does is travel at a diagonal until it gets stuck in the lower-right corner.....i can't get the balll to richochet or move around the dimensions of the window.....also, i cant find a good was of clear the ball graphic without changing the backfround color.

  2. #2
    Lively Member
    Join Date
    Jul 2002
    Posts
    86

    Re: moving the ball in PONG

    The problem that you are having is that you are using the same variable for moving the ball in the x and y directions.

    What you should do is have 2 variables for the balls speed, like xSpeed and ySpeed and set them to whatever. Then when the ball's x position is less than 0, make xSpeed = -xSpeed, when the x position+width of ball is greater than the right side of the screen, again make xSpeed = -xSpeed. Similarly for the y bounds and ySpeed. Then in the timer, do the bound checks then at the end do: ball.x += xSpeed and ball.y +=ySpeed.

    Just taking your code and changing it:
    Code:
          Dim xSpeed As Integer       'set the values for these
          Dim ySpeed As Integer
     
          Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick, MyBase.Move
                  Ball = Me.CreateGraphics
                  Ball.Clear(Color.Gray)
    
                  If (y + Ball.height)>= 230 Then
                      ySpeed = -ySpeed
                  End If
                  If y <= 0 Then
                      ySpeed = -ySpeed
                  End If
                  If (x + Ball.width)>= 230 Then
                      xSpeed = -xSpeed
                  End If
                  If x <= 0 Then
                      xSpeed = -xSpeed
                  End If
                  x += xSpeed
                  y += ySpeed
          
                  Ball.FillEllipse(Brushes.Yellow, x, y, 20, 20)
              End Sub
    Those conditions may have to change for the Y, I forget if VB starts counting the Y coordinate from the top or the bottom.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    66

    Re: moving the ball in PONG

    that's kinda of a general problem i have been running in to....... the .height() and .width() are not members of the System.Drawing.Graphics class.....

  4. #4
    Lively Member
    Join Date
    Jul 2002
    Posts
    86

    Re: moving the ball in PONG

    Ok, well if they aren't members of that class, just use constants instead of calling those functions/methods. I see that in the Ball.FillEllipse line, that you are using 20 for the height and width, so just change the conditions to be x + 20 and y + 20.

    EDIT
    I just looked as MSDN site and that is really odd, and kind of stupid, that the class doesn't have width and height functions OR member variables. lol
    Last edited by fartman_900; Apr 23rd, 2007 at 12:47 PM.

  5. #5
    Hyperactive Member singularis's Avatar
    Join Date
    Nov 2006
    Location
    Over There!
    Posts
    372

    Re: moving the ball in PONG

    There is nothing strange about the graphics class having to member varibles. This is because the graphics class does not mean a 'graphic' as such but the graphics class is an interface to draw shapes.
    If what I said was helpful, give me rep!

    My Complete Games: -- 2D Zone (Space Shooter game) || _2D Zone 2_ || Ninja Blob (2D platformer) || Dren (Co-op up to 4 player base defence game)

    My Projects: -- The Dread Engine (2D VB game Engine) || A* Path Finding


    An excellent site for learning DirectX7, 8 & 9 (for VB6, C# & VB.net) would be: directx4vb.vbgamer.com --- For my projects and games see: pieper.freehostia.com

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    66

    Re: moving the ball in PONG

    well, what is the best way to manipulate their location...re-draw them every Timer1_Tick()?

  7. #7
    Hyperactive Member singularis's Avatar
    Join Date
    Nov 2006
    Location
    Over There!
    Posts
    372

    Re: moving the ball in PONG

    That or use a managed game loop. It is a lot simpler than it sounds. Just make a while loop and put the DoEvents in it. Usually when you start a loop, windows forgets to breathe until it finishes and your computer crashes. DoEvents stops this from happening and tells windows to breathe.

    Note that this code is classic VB code but the principles should remain.
    Code:
    Dim bLoop as Boolean
    bLoop = true
    While(bLoop)
    '<- Put game code here
    System.DoEvents
    wend
    Avoid using timers as much as you can !
    If what I said was helpful, give me rep!

    My Complete Games: -- 2D Zone (Space Shooter game) || _2D Zone 2_ || Ninja Blob (2D platformer) || Dren (Co-op up to 4 player base defence game)

    My Projects: -- The Dread Engine (2D VB game Engine) || A* Path Finding


    An excellent site for learning DirectX7, 8 & 9 (for VB6, C# & VB.net) would be: directx4vb.vbgamer.com --- For my projects and games see: pieper.freehostia.com

  8. #8
    Lively Member
    Join Date
    Jul 2002
    Posts
    86

    Re: moving the ball in PONG

    Quote Originally Posted by singularis
    There is nothing strange about the graphics class having to member varibles. This is because the graphics class does not mean a 'graphic' as such but the graphics class is an interface to draw shapes.
    Oh, ok. I guess I didn't read the document like I should have lol, thanks.

  9. #9
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994

    Re: moving the ball in PONG

    You might also want to look into 2D vectors if you plan on doing more 2D programming(or 3D for that matter) - they help a lot when calculating and understanding objects in a coordinate system.
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    66

    Re: moving the ball in PONG

    vb Code:
    1. Dim movepix As Integer
    2.         Dim negmovepix As Integer
    3.         Dim go As Boolean
    4.         go = False
    5.         movepix = 5
    6.         negmovepix = -5
    7.         Ball = Me.CreateGraphics
    8.         Ball.Clear(Me.BackColor)
    9.         While go
    10.             x += 5
    11.             y += 5
    12.             Ball.FillEllipse(Brushes.Yellow, x, y, 20, 20)
    13.             Application.DoEvents()
    14.         End While



    I put that in, but when i run it, the program just flashed a ball and bar and then disappears, how do i get the program to move my ball around and my bar to NOT disappear. also i want to be able to run my keypress...

    vb Code:
    1. Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles Me.KeyPress
    2.         Bar = Me.CreateGraphics
    3.         Select Case e.KeyChar
    4.             Case ","c
    5.                 Ball.Clear(Me.BackColor)
    6.                 Bar.FillRectangle(Brushes.Black, (x1 - 10), 295, 80, 15)
    7.                 x1 = x1 - 10
    8.             Case "<"c
    9.                 Ball.Clear(Me.BackColor)
    10.                 Bar.FillRectangle(Brushes.Black, (x1 - 10), 295, 80, 15)
    11.                 x1 = x1 - 10
    12.             Case "."c
    13.                 Ball.Clear(Me.BackColor)
    14.                 Bar.FillRectangle(Brushes.Black, (x1 - 10), 295, 80, 15)
    15.                 x1 = x1 + 10
    16.             Case ">"c
    17.                 Ball.Clear(Me.BackColor)
    18.                 Bar.FillRectangle(Brushes.Black, (x1 - 10), 295, 80, 15)
    19.                 x1 = x1 + 10
    20.             Case "s"c
    21.  
    22.                 Timer1.Start()
    23.                 Ball.FillEllipse(Brushes.Yellow, x, y, 20, 20)
    24.                 Bar.FillRectangle(Brushes.Black, x1, 295, 80, 15)
    25.         End Select
    26.     End Sub

  11. #11
    Lively Member
    Join Date
    Jul 2002
    Posts
    86

    Re: moving the ball in PONG

    The first thing that is wrong is that you set go to false, when it should be true. So the loop is executing once then ending. Also, inside the while loop you need to put the collision statements (or a method call to check for collisions). As for the key press, I'm not entirely sure (I don't code in VB at all) but I thought that the KeyPress method gets called automatically when a key is pressed so it might be fine. Just another thing about the case statements, all of them move the bar down, just change two of them to + 10 instead of - 10.

  12. #12
    Hyperactive Member singularis's Avatar
    Join Date
    Nov 2006
    Location
    Over There!
    Posts
    372

    Re: moving the ball in PONG

    @kylekocak: Read Fartman's post. If you want nice smooth keyboard input then I suggest trying a little trick:

    You create a varible remembering the velocity of the paddle
    e.g. Dim Vel as Long

    Then in keypress event set the velocity to 1 if they press right and -1 if they press left.

    Then in the code in the loop you tell the ball to move depending on its velocity
    e.g. x1 = x1 + Vel

    That way the paddle will move smoothly
    If what I said was helpful, give me rep!

    My Complete Games: -- 2D Zone (Space Shooter game) || _2D Zone 2_ || Ninja Blob (2D platformer) || Dren (Co-op up to 4 player base defence game)

    My Projects: -- The Dread Engine (2D VB game Engine) || A* Path Finding


    An excellent site for learning DirectX7, 8 & 9 (for VB6, C# & VB.net) would be: directx4vb.vbgamer.com --- For my projects and games see: pieper.freehostia.com

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    66

    Re: moving the ball in PONG

    bb Code:
    1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick, MyBase.Move
    2.         Ball = Me.CreateGraphics
    3.         Ball.Clear(Me.BackColor)
    4.         If y >= 230 Then
    5.             Do
    6.                 y -= 5
    7.                 Ball.FillEllipse(Brushes.Yellow, x, y, 20, 20)
    8.                 Application.DoEvents()
    9.             Loop Until y <= 0
    10.         Else
    11.             x += 5
    12.             y += 5
    13.             Ball.FillEllipse(Brushes.Yellow, x + 5, y + 5, 20, 20)
    14.         End If
    15.         If x >= 230 Then
    16.             Do
    17.                 x -= 5
    18.                 Ball.FillEllipse(Brushes.Yellow, x, y, 20, 20)
    19.                 Application.DoEvents()
    20.             Loop Until x <= 0
    21.         Else
    22.             x += 5
    23.             y += 5
    24.             Ball.FillEllipse(Brushes.Yellow, x, y, 20, 20)
    25.         End If
    26.     End Sub[HIGHLIGHT=vb]
    [/HIGHLIGHT]

    This code gets the ball to move around the screen, problem is once it hits the 230 for the x and y coordinates, it "transports" to the x or y =0 respectively.....and yes, it is because it is in a Do loop, no need to point that out....i thought the application.DoEvents would have taken care of that.....how do i get the ball to SLOWLY move?

  14. #14
    Hyperactive Member singularis's Avatar
    Join Date
    Nov 2006
    Location
    Over There!
    Posts
    372

    Re: moving the ball in PONG

    Lower the value from 5 to 0.1 !
    If what I said was helpful, give me rep!

    My Complete Games: -- 2D Zone (Space Shooter game) || _2D Zone 2_ || Ninja Blob (2D platformer) || Dren (Co-op up to 4 player base defence game)

    My Projects: -- The Dread Engine (2D VB game Engine) || A* Path Finding


    An excellent site for learning DirectX7, 8 & 9 (for VB6, C# & VB.net) would be: directx4vb.vbgamer.com --- For my projects and games see: pieper.freehostia.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width