minigolf issue - ball movement

Started by
0 comments, last by jnmacd 12 years, 7 months ago
[font="Arial, Helvetica, sans-serif"][color="#333333"]
Hi,

So as stated in a previous question, I'm creating a basic minigolf game, as test.
So I managed to move the ball and bounce it off walls.



'create simple form with small picturebox as "gameBall" and timer as "gameTimer"

Public Class Main

Dim pt As Point
Dim speed As Double = 0.07
Dim xVel As Double
Dim yVel As Double

Private Sub Main_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
xVel = (pt.X - gameBall.Location.X) * speed
yVel = (pt.Y - gameBall.Location.Y) * speed
gameTimer.Enabled = True
End Sub

Private Sub Main_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
pt = New Point(e.X, e.Y)
Me.Refresh()
End Sub

Private Sub Main_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.DrawLine(Pens.Black, gameBall.Location.X,
gameBall.Location.Y, pt.X, pt.Y)
End Sub

Private Sub gameTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles gameTimer.Tick
'move ball
gameBall.Location = New Point(gameBall.Location.X + xVel,
gameBall.Location.Y + yVel)

' Check for top wall.
If gameBall.Location.Y < 0 Then
gameBall.Location = New Point(gameBall.Location.X, 0)
yVel = -yVel
End If

' Check for bottom wall.
If gameBall.Location.Y > Me.Height - gameBall.Size.Height Then
gameBall.Location = New Point(gameBall.Location.X, Me.ClientSize.Height - gameBall.Size.Height)
yVel = -yVel
End If

' Check for left wall.
If gameBall.Location.X < 0 Then
gameBall.Location = New Point(0, gameBall.Location.Y)
xVel = -xVel
End If

' Check for right wall.
If gameBall.Location.X > Me.Width - gameBall.Size.Width Then
gameBall.Location = New Point(Me.ClientSize.Width - gameBall.Size.Width, gameBall.Location.Y)
xVel = -xVel
End If

'stop ball
If speed = 0 Then
gameTimer.Enabled = False
End If
End SubEnd Class


But I come across small problems:

1. when you move the mouse a few pixels away from the corner at for example a tad more than 90° you'll see that the ball wont move or that it moves straight upwards or downwards and not a few degrees off.
2. There seems to be a problem with the bottom and right wall... the ball keeps bouncing off outside the form although i use the me.clientsize control. Any ideas why this is happening?


Regards[/font]
Advertisement
Well this seems odd... If gameBall.Location.Y > Me.Height - gameBall.Size.Height Then gameBall.Location = New Point(gameBall.Location.X, Me.ClientSize.Height - gameBall.Size.Height)You check for one condition, then set it to some other size... why not always use Me.Height? or Me.ClientSize.Height?

This topic is closed to new replies.

Advertisement