Results 1 to 3 of 3

Thread: Help with my pong code

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    1

    Help with my pong code

    I need to make my Computer Paddle defeatable. Right now i have everything coded it is just impossible to win.
    Pong Code:
    1. Public Class uipongform
    2. #Region "End Game on Escape Press"
    3.     ' Escape the game when escape has been pressed.
    4.     Private Sub pongMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    5.         If e.KeyValue = Keys.Escape Then
    6.             Me.Close()
    7.         End If
    8.     End Sub
    9. #End Region
    10. #Region "Globals"
    11.     Dim speed As Single = 10 ' Ball Speed
    12.     Dim rndInst As New Random() ' Random instance
    13.     Dim xVel As Single = Math.Cos(rndInst.Next(5, 10)) * speed
    14.     Dim yVel As Single = Math.Sin(rndInst.Next(5, 10)) * speed
    15.     ' The player's scores.
    16.     Dim compScore As Integer = 0
    17.     Dim plrScore As Integer = 0
    18.  
    19. #End Region
    20.  
    21.  
    22.  
    23. #Region "Move the paddle according to the mouse"
    24.     ' Move the paddle according to the mouse position.
    25.     Private Sub pongMain_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
    26.  
    27.         If e.Y > 5 And e.Y < Me.Height - 40 - paddlePlayer.Height Then _
    28.         paddlePlayer.Location = New Point(paddlePlayer.Location.X, e.Y)
    29.  
    30.     End Sub
    31. #End Region
    32.  
    33. #Region "Main Timer"
    34.     Private Sub gameTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gameTimer.Tick
    35.         'Set the computer player to move according to the ball's position."
    36.         If gameBall.Location.Y > 5 And gameBall.Location.Y < Me.Height - 40 _
    37.         - paddlePlayer.Height Then _
    38.         paddleComputer.Location = New Point(paddleComputer.Location.X, gameBall.Location.Y)
    39.         ' Move the game ball.
    40.         gameBall.Location = New Point(gameBall.Location.X + xVel, gameBall.Location.Y + yVel)
    41.         ' Check for top wall.
    42.         If gameBall.Location.Y < 0 Then
    43.             gameBall.Location = New Point(gameBall.Location.X, 0)
    44.             yVel = -yVel
    45.         End If
    46.         ' Check for bottom wall.
    47.         If gameBall.Location.Y > Me.Height - gameBall.Size.Height - 45 Then
    48.             gameBall.Location = New Point(gameBall.Location.X, Me.Height - gameBall.Size.Height - 45)
    49.             yVel = -yVel
    50.         End If
    51.         ' Check for player paddle.
    52.         If gameBall.Bounds.IntersectsWith(paddlePlayer.Bounds) Then
    53.             gameBall.Location = New Point(paddlePlayer.Location.X - gameBall.Size.Width, _
    54.             gameBall.Location.Y)
    55.             xVel = -xVel
    56.         End If
    57.         ' Check for computer paddle.
    58.         If gameBall.Bounds.IntersectsWith(paddleComputer.Bounds) Then
    59.             gameBall.Location = New Point(paddleComputer.Location.X + paddleComputer.Size.Width + 1, _
    60.             gameBall.Location.Y)
    61.             xVel = -xVel
    62.         End If
    63.         ' Check for left wall.
    64.         If gameBall.Location.X < 0 Then
    65.             plrScore += 1
    66.             gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
    67.             plrScoreDraw.Text = Convert.ToString(plrScore)
    68.         End If
    69.         ' Check for right wall.
    70.         If gameBall.Location.X > Me.Width - gameBall.Size.Width - paddlePlayer.Width Then
    71.             compScore += 1
    72.             gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
    73.             compScoreDraw.Text = Convert.ToString(compScore)
    74.         End If
    75.     End Sub
    76. #End Region
    77.  
    78.  
    79.     Private Sub uiexitbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uiexitbutton.Click
    80.         Me.Close()
    81.     End Sub
    82.  
    83. End Class

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Help with my pong code

    Here is one simple idea. Use a random offset. If you are centering the paddle on the ball, a random offset will add/subtract from that center position; thereby changing the position of the paddle. Depending on the range of the random offset and size of the paddle, the paddle may partially/completely hit the ball or miss altogether.

    Edited: For example, the range may be -PaddleHeight to +PaddleHeight.
    Last edited by LaVolpe; Feb 7th, 2010 at 01:38 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Help with my pong code

    Another possibility, have a delay in reaction time movement. Don't make the computer paddle constantly follow the ball. After it hits the ball, let it idle and wait for the player to hit it. If the player hits it, start a random delay period and after that delay period, then move the paddle at some velocity to intercept the ball. Either it'll get there (a slow moving ball) or it won't (delay was too long and ball was too fast).
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

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