简体   繁体   中英

any way to increase speed every time it hits paddle in brick breaker game with pygame

I'm making a brick breaker game and I want to increase speed every time it hits the paddle. But when I add 1 in the speed for some reason it increases multiple times. here's my code

 if ball.colliderect(paddle):
        y_positive = False
        y_negative = True
        ball_speed += 1

That's because it's happening every clocktick that their collisionbox overlaps. They might be touching for a few frames before they are far enough apart to no longer be considered colliding. You need to put in logic to allow only one increment per volley.

volley = False  ##  only allow increment once ball crosses midpoint of screen
if ball.x > screen_width /2: volley = True

if ball.colliderect( paddle ) and volley:
    y_positive = False
    y_negative = True
    ball_speed += 1
    volley = False

Alternatively, once the ball touches, nudge it far enough away from paddle to ensure that their collisionboxes don't touch.

if ball.colliderect( paddle ):
    y_positive = False
    y_negative = True
    ball_speed += 1
    ball.x += ball.width /2

Nudge it however far it needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM