#๐ I need to optimise my code, help me (I use bubble algorithm)
36 messages ยท Page 1 of 1 (latest)
@ember scarab
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
So, the problem is, i want to optimise this algorythm because it's too slow, how can I make it faster but still checking all colisions?
what is this "bubble" algorithm
doesnt pygame already let you handle collisions with other sprites?
Ok so what I mean by bubble algorithm is this:
for i in range(1000):
for j in range(1000):
So this way you arechecking colisions by iterating through every single element in the lists. an example would be, you have 10 objects in one list and 10 in another, so you would check if the first element of the first list collides with one of the 10 in the other list, and then you would go to check if the second element would collide with one of the 10 in the other list. This makes it be very slow, bc it checks things 1000 * 1000 times
Real question is : do you need to check all these collisions ?
You can simplify by putting your objects in chunks: for example, if you divide your map in 4 corners, you check only the collision of entities in the same corner
Ok so first, no, obviously if the objects are in opposite corners, it's not a good idea to check if they collide (obviously they don't). Now that we have divided the map in 4, I got 2 questions.
- What do I do if they are in 2, 3 or 4 corners at the same time?
- How do I put them in different lists to check colisions depending if they are on the first or the 4th corner?
no idea if pygame provides specific solutions
if not, then look into for example, kd trees
When then move, check their position, if they changed corner, then remove them from a list and put them in another
How can they be ? Like if they are at the border of two chunks ?
Keep it in the list of both chunks ig
and a simple more question now that it's done and that it works
is there any way to check the time spent before and after this modification? I want to calculate the exact number of squares that work best for different situations. I will explain this to you. Imagine you got 1000m in X and 1000m in Y, so you could make 4 squares by dividing the screen(1000, 1000) in squares of 500m of height and width. However, if my characters are too big, maybe they tend to go occupie too much suquares, and maybe dividing it in 2 rectangles of 500meters of width and 1000 of height is better. How can I check the speed of the code?
Basically I want to make a function that throughout the game knows what's better for them and changes the number of squares if necessary
You can measure the time spent in a piece of code by using the time module or the pygame's clock
Ok and another question
If one list is 200 long
And thye other is 50 let's say
how should I do the bubble algorithm, for i in range (200): for j in range(50):
or
for i in range(50): for j in range(200)
the order doesnt matter
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.