Color change with collision [SOLVED] | SoloLearn: Learn to code for FREE!


Color change with collision [SOLVED]

I’m trying to make a collision code with circles. The primary objective is to change the color of the circles when they collide, but somehow it doesn’t work. I can see the collision detection functions, but the colors change only when the circles are next to each other in the array. Can someone please have a look to see what I’m doing wrong?

6/25/2019 6:41:26 PM


11 Answers

New Answer


In the loop you are comparing each ball with all other balls. So you actually land in the else { balls[i].noChangeColor(); balls[j].noChangeColor(); } part all the time, and even if the balls changed color at some point they will change right back because likely no ball will ever collide with all other balls. Before the double loop you want a: for(let ball of balls) ball.noChangeColor(); to make them all green, and then just remove that `else` part. I hope that gets you a step closer.


🤔 Strange they should both change color... Let me make the same with vanilla js.


Schindlabua Thanks, that’s a great improvement! There is a color change with all collisions now, but why is only one of the circles changing and not both?


Schindlabua I think you believed it right 😃 It seems to be working now. Thanks so much, i’ve been wrecking my brains over this all evening 🤯🌹🌹🌷🌷


A bit late to the party but I did it:


Thanks Haris ( exams ) that looks great. I see you basically used a similar solution as Schindlabua , by first setting the color, draw and then reset. I moved my initial setting a bit based on your example now. Thank you both for yet another lesson today 😃🌹🌹


I'm not a 100% on that but I believe you want to `.show()` after you have calculated the collisions.


You're welcome :)


Jake Skutnik thank you, but this question is already solved 😊 I took the else statement out of the if else and used the noChangeColor() at another location. It’s working now 😃


else { balls [i].noChangeColor(); balls[j].NoChangeColor(); }


import turtle as t t.color ("green") (r)