Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3280832
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:44:55+00:00 2026-05-17T19:44:55+00:00

I have been reading through this tutorial . I am in a chapter where

  • 0

I have been reading through this tutorial. I am in a chapter where you can build a pong game. However I am having trouble trying to make a function that can detect when the ball hits the paddle?

It says I should do this:

Make a boolean function hit(bx, by, r, px, py, h) that returns True when the vertical coordinate of the ball (by) is between the bottom and top of the paddle, and the horizontal location of the ball (bx) is less than or equal to the radius (r) away from the front of the paddle.

I have tried this so far:

def hit(bx, by, r, px, py, h):
    if by > py and by <= h:
        if bx <= r:
            return True
    else:
        return False

…

# If this returns True then change the direction of the ball (dx).
if hit(ball_x, ball_y, radius, paddle_x, paddle_y, height):
    dx *= -1

I’m having trouble translating the quoted paragraph into code. What am I doing wrong?

Note: The expected output of this function would be:

hit(760, 100, 10, 780, 100, 100)
    False

hit(770, 100, 10, 780, 100, 100)
    True

hit(770, 200, 10, 780, 100, 100)
    True

hit(770, 210, 10, 780, 100, 100)
    False
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:44:56+00:00Added an answer on May 17, 2026 at 7:44 pm

    You’re not accounting for the actual locations of the items – the coordinates you have refer to a specific point on the ball and paddle. What your code does is check that the ball is above the paddle and has a bigger radius than the distance from the y-origin to the paddle, then check that it’s wider than the distance from the x-origin to the paddle.

    Assuming that (px, py) is the top-left corner of the paddle, you could try something like this:

    def hit(bx, by, r, px, py, h):
        if by >= py and by <= py + h:
            print "Y satisfied."
            if bx <= px + r:
                print "HIT"
                return True
            print "X not satisfied."
        print "not hit."
        return False
    

    Bear in mind this doesn’t account for the ball being round (or whatever other shape you have in mind).

    EDIT:
    If you’re having issues with making it work, you could try sticking in some print statements to let you know what the values of your parameters are and what the return value is. This should give you some insights into what is actually going on.

    EDIT AGAIN:
    Realised that there was a possibility that the function didn’t return a value. Also, this code doesn’t pass all the test cases – it counts the first one as a hit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been reading through the C++ FAQ and was curious about the friend
I have been reading through the CodePlex supported open source licenses, i couldn't quite
I have been reading up on this, and it seems that if you use
OK, I have just been reading and trying for the last hour to import
I have been reading the MSDN documentation on subclassing and I have been successful
I have been reading about the differences between Table Variables and Temp Tables and
I have been reading the proper article in MSDN, Strong-Named Assemblies and a related
On Stackers' recommendation, I have been reading Crockford's excellent Javascript: The Good Parts .
I have been hearing and reading about Agile for years. I own a book
I have been doing a little reading on Flow Based Programming over the last

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.