Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pong ball not rebounding off of paddles

I’m writing a pong game, and everything works fine – score detection, bouncing off walls, movement of paddles, etc. However, the pong ball won’t bounce off the paddles, instead going right through them. (For the record, I’m doing this on MicroPython, using the pimoroni-pico library, so that’s what the button_up.is_pressed is about).

x = 160
y = 120
p1point = 0
p2point = 0
x_speed = random.choice([-3.0,3.0])
y_speed = random.choice([-3.0,3.0])
aipad = 110
plpad = 110
width = 320
height = 240

while True:
    # Controls bouncing off walls
    if (x >= width) or (x <= 0):
        x_speed *= -1
    if (y >= height) or (y <= 0):
        y_speed *= -1

    # Supposed to control bouncing off paddles, and choose a slightly random rebound
    if (aipad > y > aipad+20 and x < 5) or (plpad > y > plpad+20 and x > 315):
        x_speed += random.random()
        y_speed += random.random()
        x_speed *= -1
        if x_speed > 5 or y_speed > 5:
            x_speed = 3
            y_speed = 3
        print("flip")

    # Adds score for respective player
    if x < 0:
        x_speed = 3
        y_speed = 3
        x = 160
        y = 120
        p2point += 1
        sleep(1)
    if x > 320:
        x_speed = -3
        y_speed = -3
        x = 160
        y = 120
        p1point += 1
        sleep(1)

    # AI player's ai
    if y < aipad:
        aipad = aipad - 4
    if y > aipad:
        aipad = aipad + 4

    # Human player's controls
    if button_up.is_pressed
        plpad = plpad - 3
    if button_down.is_pressed:
        plpad = plpad + 3

    # Stops the paddles from going too far off screen
    if plpad > 220:
        plpad = 220
    if aipad > 220:
        aipad = 220
    if plpad < 0:
        plpad = 0
    if aipad < 0:
        aipad = 0

    # Adds _speed to the respective positional variable, rounding down x_speed  
    x += int(x_speed)
    y += int(y_speed)

    # ... render play field ...

I’ve tried rewriting the rebound code in different ways, changing the inequality symbols from > to >=, changing the numbers, nothing. I also tried lowering the speed to 1 pixel per tick (it might have been going past the collision zone) but that didn’t work. One time it started vibrating on the spot randomly, away from the edges, but I don’t know how to reproduce that. It’s probably a stupid mistake, but I cannot find it.

Variables:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • x: X pos of ball
  • y: Y pos of ball
  • x_speed: amount of pixels the ball moves per tick on the x axis
  • y_speed: same as above, but for the y axis
  • aipad: origin position of left paddle*
  • plpad: origin position of right paddle*
  • width+height: width and height of screen

*The pad’s origin is 5 pixels away from the screen (X) and at it’s position variable (Y), and it goes across 1 pixel towards the closest edge of the screen (2 pixels wide) and down 20 (20 pixels tall).

>Solution :

You want to decide if y is between aipad (or plpad) and aipad+20. If you write:

1 > 2 > 3

you can obviously see that 1>2 is false, so that is not what you want. Instead, you need:

aipad < y < aipad+20
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading