1 Replies - 1658 Views - Last Post: 16 April 2010 - 03:35 PM Rate Topic: -----

#1 Chainsaw_Matt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 03-November 08

Help adding features to a pong game

Posted 16 April 2010 - 01:41 PM

So I actually found a fairly good pong tutorial on this site which I've linked to at the bottom of this post. I used the code as my base and I'm starting to implement my own new features. I'm currently working on making the ball move faster after each hit of the paddle, but I can't get it working! I thought it would be as simple as adding 1 to the 'speed' variable during the test to see if the ball has hit the paddle. Doesn't work! I also tried various other methods, including adding an extra timer to the form and having the speed increase a certain number every tick on the timer. Didn't work! Is there some special way that I need to reference that variable in order to manipulate it? Code below.
Public Class pongMain
    Dim compScore As Integer = 0
    Dim playerScore As Integer = 0
    Dim turn As Integer = 0
    Dim speed As Single = 10                  ' Ball Speed
    Dim rndInst As New Random()                 ' Random instance
    Dim xVel As Single = Math.Cos(rndInst.Next(5, 10)) * speed + turn
    Dim yVel As Single = Math.Sin(rndInst.Next(5, 10)) * speed + turn
    Private Sub pongMain_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        If e.Y > 5 And e.Y < Me.Height - 40 - paddlePlayer.Height Then                      'Allows for paddle to follow mouse movement
            paddlePlayer.Location = New Point(paddlePlayer.Location.X, e.Y)
        End If
    End Sub
    Private Sub gameTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gameTimer.Tick
        Dim ballSpeed As Single = speed
        gameBall.Location = New Point(gameBall.Location.X + xVel, gameBall.Location.Y + yVel)   'starts ball movement
        If gameBall.Location.Y > 5 And gameBall.Location.Y < Me.Height - 40 _
        - paddlePlayer.Height Then                                                              'Set the computer player to move according to the ball's position.
            paddleComp.Location = New Point(paddleComp.Location.X, gameBall.Location.Y)
        End If

        If gameBall.Location.Y < 0 Then                                                         'Check for top wall collision
            gameBall.Location = New Point(gameBall.Location.X, 0)
            yVel = -yVel
        End If
        If gameBall.Location.Y > Me.Height - gameBall.Size.Height - 45 Then                     'Check for bottom wall collision
            gameBall.Location = New Point(gameBall.Location.X, Me.Height - gameBall.Size.Height - 45)
            yVel = -yVel
        End If
        If gameBall.Bounds.IntersectsWith(paddlePlayer.Bounds) Then                             'Check for player paddle collision
            gameBall.Location = New Point(paddlePlayer.Location.X - gameBall.Size.Width, _
            gameBall.Location.Y)
            xVel = -xVel
            turn = turn + 10
        End If
        If gameBall.Bounds.IntersectsWith(paddleComp.Bounds) Then                               'Check for computer paddle collision
            gameBall.Location = New Point(paddleComp.Location.X + paddleComp.Size.Width + 1, _
            gameBall.Location.Y)
            xVel = -xVel
            turn = turn + 10
        End If
        If gameBall.Location.X < 0 Then                                                         'Add one to player score if ball passes compPaddle
            playerScore += 1
            gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
            playerScoreLabel.Text = Convert.ToString(playerScore)
            gameTimer.Enabled = False
            startButton.Visible = True
        End If
        If gameBall.Location.X > Me.Width - gameBall.Size.Width - paddlePlayer.Width Then       'Add one to comp score if ball passes playerPaddle
            compScore += 1
            gameBall.Location = New Point(Me.Size.Width / 2, Me.Size.Height / 2)
            compScoreLabel.Text = Convert.ToString(compScore)
            gameTimer.Enabled = False
            startButton.Visible = True
        End If
        Debug.WriteLine(turn)

    End Sub

    Private Sub startButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startButton.Click
        gameTimer.Enabled = True
        startButton.Visible = False
    End Sub

    Private Sub speedTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
End Class


http://www.dreaminco...onal-pong-game/

Is This A Good Question/Topic? 0
  • +

Replies To: Help adding features to a pong game

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1533
  • View blog
  • Posts: 4,919
  • Joined: 25-September 09

Re: Help adding features to a pong game

Posted 16 April 2010 - 03:35 PM

Your xVel and yVel are both set when they are declared and are never looked at again. No matter what you do to the value of turn. Where you are increasing the value of turn, you will need to set the value of xVel and yVel there.

One thing to note, the speed is based on moving to a new position, when the number is set too high, it can pass right over the paddle without intersecting. In other words, if you paddle is 10 wide and the ball is 5 pixels away and the speed is 25 the next position of the ball will be 10 pixels behind the paddle and will not trigger the change in movement.

This post has been edited by CharlieMay: 16 April 2010 - 03:44 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1