m_durnell
asked on
Writing and moving a paddle to hit a "ball"
Hi
A few years ago when computer games first came out I had two handsets connected to a TV set which had buttons on it which enabled two users to play a game (Tennis I think)
This game is very primitive by todays standards but I quite liked it. So my question is this:
How can I program a screen (with boundaries) with a paddle that moves up and down by pressing two keys (say m and k)and a ball (I think called a diode) which bounces off the paddle and the boundaries (there should be a section of the boundarie that is open and if the diode "hits" this will count as a goal (score).
If I get help with this part of the program I should then (hopefully) be able to do a "mirror" program for the opposite side of the game (oponent).
Any help appreciated
Regards
Mark
A few years ago when computer games first came out I had two handsets connected to a TV set which had buttons on it which enabled two users to play a game (Tennis I think)
This game is very primitive by todays standards but I quite liked it. So my question is this:
How can I program a screen (with boundaries) with a paddle that moves up and down by pressing two keys (say m and k)and a ball (I think called a diode) which bounces off the paddle and the boundaries (there should be a section of the boundarie that is open and if the diode "hits" this will count as a goal (score).
If I get help with this part of the program I should then (hopefully) be able to do a "mirror" program for the opposite side of the game (oponent).
Any help appreciated
Regards
Mark
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The square shape has a property to set to make it a circle.
The "bounce" code could look like this:
Option Explicit
Private miXDirection As Integer
Private miYDirection As Integer
Private Const miSpeed As Integer = 30
Private Sub Form_Load()
miXDirection = 1
miYDirection = 1
End Sub
Private Sub tmrMove_Timer()
Dim iNextLeft As Integer
Dim iNextTop As Integer
Dim iPaddleNumber As Integer
Dim iWallNumber As Integer
iNextLeft = shpBall.Left + miXDirection * miSpeed
iNextTop = shpBall.Top + miYDirection * miSpeed
' Check to see if ball hit anything
For iPaddleNumber = 0 To 1
If iNextLeft > shpPaddle(iPaddleNumber).L eft _
And iNextLeft < shpPaddle(iPaddleNumber).L eft + shpPaddle(iPaddleNumber).W idth _
And iNextTop > shpPaddle(iPaddleNumber).T op _
And iNextTop < shpPaddle(iPaddleNumber).T op + shpPaddle(iPaddleNumber).H eight Then
' Hit paddle...change X direction
miXDirection = -miXDirection
End If
Next iPaddleNumber
For iWallNumber = 0 To 1
If iNextLeft > shpWall(iWallNumber).Left _
And iNextLeft < shpWall(iWallNumber).Left + shpWall(iWallNumber).Width _
And iNextTop > shpWall(iWallNumber).Top _
And iNextTop < shpWall(iWallNumber).Top + shpWall(iWallNumber).Heigh t Then
' Hit paddle...change X direction
miYDirection = -miYDirection
End If
Next iWallNumber
shpBall.Left = iNextLeft
shpBall.Top = iNextTop
End Sub
--
The bouncing should be slightly modified to take into account the direction of the ball. i.e. if the ball is moving right, you should check the right edge of the ball against the left edge of the paddles. if the ball is moving down, you should check the bottom edge of the ball against the top edge of the wall.
Noet that I set the walls as filled rectangles rather than lines so that the boundaries can be better measured; otherwise you have to check to see if the ball went PAST the lines.
The "bounce" code could look like this:
Option Explicit
Private miXDirection As Integer
Private miYDirection As Integer
Private Const miSpeed As Integer = 30
Private Sub Form_Load()
miXDirection = 1
miYDirection = 1
End Sub
Private Sub tmrMove_Timer()
Dim iNextLeft As Integer
Dim iNextTop As Integer
Dim iPaddleNumber As Integer
Dim iWallNumber As Integer
iNextLeft = shpBall.Left + miXDirection * miSpeed
iNextTop = shpBall.Top + miYDirection * miSpeed
' Check to see if ball hit anything
For iPaddleNumber = 0 To 1
If iNextLeft > shpPaddle(iPaddleNumber).L
And iNextLeft < shpPaddle(iPaddleNumber).L
And iNextTop > shpPaddle(iPaddleNumber).T
And iNextTop < shpPaddle(iPaddleNumber).T
' Hit paddle...change X direction
miXDirection = -miXDirection
End If
Next iPaddleNumber
For iWallNumber = 0 To 1
If iNextLeft > shpWall(iWallNumber).Left _
And iNextLeft < shpWall(iWallNumber).Left + shpWall(iWallNumber).Width
And iNextTop > shpWall(iWallNumber).Top _
And iNextTop < shpWall(iWallNumber).Top + shpWall(iWallNumber).Heigh
' Hit paddle...change X direction
miYDirection = -miYDirection
End If
Next iWallNumber
shpBall.Left = iNextLeft
shpBall.Top = iNextTop
End Sub
--
The bouncing should be slightly modified to take into account the direction of the ball. i.e. if the ball is moving right, you should check the right edge of the ball against the left edge of the paddles. if the ball is moving down, you should check the bottom edge of the ball against the top edge of the wall.
Noet that I set the walls as filled rectangles rather than lines so that the boundaries can be better measured; otherwise you have to check to see if the ball went PAST the lines.
ASKER
Copied and pasted code into program, altered names of ball, and paddles. Changed the property of ball to circle. Ran the program nothing happened.
Any ideas? I have probably done something wrong.
Regards
Mark
Any ideas? I have probably done something wrong.
Regards
Mark
did you add the timer control?
Set its properties to something like 100ms and enabled=true
Set its properties to something like 100ms and enabled=true
Hi m_durnell,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:
Accept rspahitz's comment(s) as an answer.
m_durnell, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you. DO NOT accept this comment as an answer.
EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:
Accept rspahitz's comment(s) as an answer.
m_durnell, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you. DO NOT accept this comment as an answer.
EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
Comment from expert accepted as answer
Computer101
E-E Admin
Computer101
E-E Admin
ASKER
For rspahitz
I really need to know what the code is for moving the "ball" and also for the timer control.
I did the form as you suggested and found the line control in the standard tools - Is that right or not ? The circle however I could not find so I settled for a "square" shape instead.
Thanks for your time.
Regards
Mark