[ad_1]
I’ve observed just a few unsuitable issues on the collision strains:
def examineCollisions1():
world rating
if ball['y'] >= paddle1['y'] and ball['y'] <= paddle1['y'] + paddle1['height']:
if ball['x'] + ball['radius'] >= paddle1['x']:
ball['velocity_x'] = -ball['velocity_x']
ball['x'] = paddle1['x'] - ball['radius']
def examineCollisions2():
world rating
if ball['x'] >= paddle2['x'] and ball['x'] <= paddle2['x'] + paddle2['width']:
if ball['x'] + ball['radius'] >= paddle2['x']:
ball['velocity_x'] = +ball['velocity_x']
ball['x']
ball['x'] = paddle2['x'] + ball['radius']
First, on examineCollisions1, you examine if ball['x'] + ball['radius'] >= paddle1['x']: and if that is true, you set the ball’s place like this: ball['x'] = paddle1['x'] - ball['radius']. This will not be unsuitable, if the ball strikes on the X axis each body with out having its velocity ever hit 0. But if for some purpose that operate runs twice with out the ball shifting on the X axis, it’ll set off collision twice.
You talked about the primary collision works correctly, in order that’s most likely not a giant concern, however one thing value being conscious of. One advised resolution is altering the if situation to make use of > as an alternative of >=, in order that when collision occurs, the ball will likely be positioned ready the place collision can not occur, until one of many two objects strikes nearer to one another.
On the second collision there are a selection of issues unsuitable. Firstly, on the primary if situation, you examine the ball’s and paddle’s X axis, opposite to the primary collision the place you examine the Y axis first.
First collision:
if ball['y'] >= paddle1['y'] and ball['y'] <= paddle1['y'] + paddle1['height']:
Second collision:
if ball['x'] >= paddle2['x'] and ball['x'] <= paddle2['x'] + paddle2['width']:
This will not be sufficient, because it’s the identical as the primary collision, so you should change a bit little bit of the code like this:
if ball['x'] <= paddle2['x'] and ball['x'] >= paddle2['x'] - paddle2['width']:
This appears to be an error as on the subsequent line you examine the ball’s X and paddle’s X once more, so they need to be modified to the Y axis, and width ought to change to peak.
On the subsequent line, I am unable to inform if it was a copy-paste error, or it is a syntax error (I’ve restricted information on python), however this line appears unsuitable:
ball['x']
That’s all I discovered from a fast look, let me know if the issue nonetheless happens.
[ad_2]