Pong question

mpie

Member
Joined
Jun 20, 2003
Messages
6
Im trying to program pong in vb.net but I cant get the ball on the screen. I use the solidbrush method to draw a ball and the timer event with refresh. I know it has something to do with the paint event but I dont know the exact code. Ive got something like this :
Code:
    Private dirUp As Boolean
    Private dirLeft As Boolean
    Private ballHeight As Integer = 10
    Private ballWidth As Integer = 10
    Private ballColor As New SolidBrush(Color.DarkKhaki)
    Private ballX As Integer
    Private ballY As Integer
    Private maxHeight As Integer
    Private maxWidth As Integer

    Sub Tekenbal(ByVal e As System.Windows.Forms.DrawItemEventArgs)

        Me.CreateGraphics.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias
        Me.CreateGraphics.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighSpeed

        e.Graphics.FillEllipse(ballColor, 10, 10, 10, 10)
    End Sub

    Sub Beweegbal()
        If dirUp = True Then
            ballY = ballY - 10
        Else
            ballY = ballY + 10
        End If

        If dirLeft = True Then
            ballX = ballX - 10
        ElseIf Not dirLeft Then
            ballX = ballX + 10
        End If

        If ballHeight + ballY >= maxHeight Then
            dirUp = True Weer naar boven
        Else
            dirUp = False
        End If

        If ballWidth + ballX >= maxWidth Then
            dirLeft = True Naar links
        Else
            dirLeft = False
        End If

        If ballY <= 0 Then
            dirUp = True Naar beneden
        End If

        If ballX <= 0 Then
            dirLeft = True Naar rechts
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Me.Refresh()
        Tekenbal(e) It says System.Windows.Forms.PaintEventArgs cannot be converted to System.Windows.Forms.DrawItemEventArgs
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        maxWidth = Me.Width
        maxHeight = Me.Height - 30
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Tekenbal(e) It says System.Windows.Forms.PaintEventArgs cannot be converted to System.Windows.Forms.DrawItemEventArgs
        Beweegbal()
    End Sub
 
Make your Tekenbal sub accept PaintEventArgs, not draw item event args. Dont use Me.CreateGraphics.smoothingmode. Use e.Graphics.SmoothingMode, after you pass in the paint event args from Paint event. Also in your timer, you try to pass in e, which I dont see you declaring anywhere in that sub, but you dont need this, Me.Refresh will call your paint event which contains the same thing.
 
Originally posted by mutant
Make your Tekenbal sub accept PaintEventArgs, not draw item event args. Dont use Me.CreateGraphics.smoothingmode. Use e.Graphics.SmoothingMode, after you pass in the paint event args from Paint event. Also in your timer, you try to pass in e, which I dont see you declaring anywhere in that sub, but you dont need this, Me.Refresh will call your paint event which contains the same thing.
Thank you very much I can see the ball now :)
 
Is that dirUp or dirLeft is supposed to do?

Use the KeyDown event on your form to capture keys, then check what key was pressed like this:
Code:
If e.KeyCode = Keys.Left Then for example if you want
to check if left arrow was pressed.
And you also have to change the coordinates of your ellipse, right now you are drawing in the same place all the time, (10,10). Make those two values a variable, and increase it or decrease it when someone presses a button and then refresh your form.
 
Originally posted by mutant
Is that dirUp or dirLeft is supposed to do?

Use the KeyDown event on your form to capture keys, then check what key was pressed like this:
Code:
If e.KeyCode = Keys.Left Then for example if you want
to check if left arrow was pressed.
And you also have to change the coordinates of your ellipse, right now you are drawing in the same place all the time, (10,10). Make those two values a variable, and increase it or decrease it when someone presses a button and then refresh your form.
The ball is now moving. The dirUp and dirLeft is to check if the ball hits the wall.
My source is now :
Code:
Private dirUp As Boolean
    Private dirLeft As Boolean
    Private ballHeight As Integer = 10
    Private ballWidth As Integer = 10
    Private ballColor As New SolidBrush(Color.MediumSlateBlue)
    Private ballXpositie As Integer = 20
    Private ballYpositie As Integer = 20
    Private maxHeight As Integer
    Private maxWidth As Integer

    Sub Tekenbal(ByVal e As System.Windows.Forms.PaintEventArgs)
        e.Graphics.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias
        e.Graphics.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighSpeed
        e.Graphics.FillEllipse(ballColor, ballXpositie, ballYpositie, ballHeight, ballWidth)
    End Sub

    Sub Beweegbal()

        If dirUp = True Then
            ballYpositie -= 10
        Else
            ballYpositie += 10
        End If

        If dirLeft = True Then
            ballXpositie -= 10
        Else
            ballXpositie += 10
        End If

        If ballHeight + ballYpositie >= maxHeight Then
            dirUp = True going up
        Else
            dirUp = False
        End If

        If ballWidth + ballXpositie >= maxWidth Then
            dirLeft = True going left
        Else
            dirLeft = False
        End If

        If ballYpositie <= 0 Then
            dirUp = False going down
        Else
            dirUp = True
        End If

        If ballXpositie <= 0 Then
            dirLeft = False going right
        Else
            dirLeft = True
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Me.Refresh()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        maxWidth = Me.Width
        maxHeight = Me.Height - 30
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Tekenbal(e)
        Beweegbal()
    End Sub
The ball keeps bouncing up/left and down/right without hitting the wall. And when I change the true and false with each other the ball went straight through the wall. :eek: Im getting frustrated here.
 
It hurts to see this kinda code.

consider:

Assume user capable of creating Ball and Wall objects
private theBall as Ball
private theWall as Wall


Timer event painting removed for clarity.. moving on to direction setting.

If (theBall.X <= theWall.Left) OR (theWall.Right <= theBall.X) then
theBall.XDirection = theBall.XDirection * -1
End If

If (theBall.Y <= theWall.Top) OR (theWall.Bottom <= theBall.Y) Then
theball.YDirection = theBall.YDirection * -1
End If
 
Also consider :

Getting a copy of .NET Game Programming With DirectX 9.0

It starts off by covering stuff you are doing not prior to getting stuck into how to use DirectX.

Ive said it before and Ill say it again.....its a bloody good book:)
 
Originally posted by hog
Also consider :

Getting a copy of .NET Game Programming With DirectX 9.0

It starts off by covering stuff you are doing not prior to getting stuck into how to use DirectX.

Ive said it before and Ill say it again.....its a bloody good book:)
Thanx Ill look for it at our bookstore
 
Back
Top