[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Writing and moving a paddle to hit a "ball"

Posted on 2002-07-18
7
Medium Priority
?
182 Views
Last Modified: 2010-05-02
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
0
Comment
Question by:m_durnell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 22

Accepted Solution

by:
rspahitz earned 400 total points
ID: 7163411
How about this:

Create a form, possible with fixed borders.

At the top and bottom put line controls: linBorder(0), linBorder(1) (spanning the entire length of the form.

Add two rectangle shapes to represent the paddles, rctPaddle(0), rctPaddle(1), and place one on each end of the form.

Add a circle shape to represent the ball, shpDiode, and place it somewhere in the middle of the form.

Add a timer control to handle the movement mechanism of the ball.

--
Now, when the game begins, simply start moving the ball in a predefined direction (controlled by an X-direction and Y-direction variable (such as -100, +20 to represent moving left at a weak horizontal angle.)

In the KeyPress event, check for certain keys, and if pressed, move the appropriate paddle a pre-set number of pixels for both Y1 and Y2 properties of the correct paddle.

In the timer event, determine the next position of the ball.  If the ball's left/top/right/bottom are within the same coordinates of any line or shape, reverse the sign of the X- or Y-direction variable.

--
Is that enough, or do you need more help?
0
 
LVL 1

Author Comment

by:m_durnell
ID: 7164310
Hi,

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
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7165503
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).Left _
    And iNextLeft < shpPaddle(iPaddleNumber).Left + shpPaddle(iPaddleNumber).Width _
    And iNextTop > shpPaddle(iPaddleNumber).Top _
    And iNextTop < shpPaddle(iPaddleNumber).Top + shpPaddle(iPaddleNumber).Height 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).Height 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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:m_durnell
ID: 7165859
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
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7169954
did you add the timer control?
Set its properties to something like 100ms and enabled=true
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 8014022
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
0
 
LVL 1

Expert Comment

by:Computer101
ID: 8095940
Comment from expert accepted as answer

Computer101
E-E Admin
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question