# Detects if two "characters" moving around the screen collide. # # The character positions are represented with: # character1x, character1y # and # character2x, character2y # # Their movement is represented with: # character1x_speed and character2x_speed # # The characters are drawn as simple circles. # And they only move left / right. # # Collision is detected with the dist() command, and when they # "collide" the screen is flashed red for one frame. character1x = 175 character1y = 200 character2x = 195 character2y = 200 character1x_speed = 3 character2x_speed = -3 def setup(): size(400,400) def draw(): background(255) global character1x, character2x global character1x_speed, character2x_speed ellipse(character1x,character1y,25,25) ellipse(character2x,character2y,25,25) character1x = character1x + character1x_speed character2x = character2x + character2x_speed if character1x > width or character1x < 0: character1x_speed = character1x_speed * -1 if character2x > width or character2x < 0: character2x_speed = character2x_speed * -1 if dist(character1x,character1y, character2x,character2y) < 10: background(255,0,0)