I have to write a program in Python that creates two circles colliding to each other and to the sides of the window. Here is what I have so far, but the problem is that they don't collide and they don't bump against the walls.
Please tell me what i'm doing wrong or if i'm missing something. Thank you

from time import *

from graphics import *
def getCircle():
    windowWidth = 500
    windowHeight = 500
    win = GraphWin('Animation', windowWidth, windowHeight)
    center = win.getMouse()
    radius = windowWidth/10
    shape  = Circle(center, radius)
    shape.setFill('green')
    shape.draw(win)
    shape.move(dx = 1,dy = 1)
    while win.checkMouse()==None:
        sleep(0.04)
        shape.move(dx = 1,dy = 1)
    center = win.getMouse()
    radius = windowWidth/10
    shape = Circle(center,radius)
    shape.setFill('red')
    shape.draw(win)
    shape.move(dx = 2, dy = 2)
    while win.checkMouse()==None:
        sleep(0.04)
        shape.move(dx = 2,dy =2)
    (c1.getX(), c1.getY())
    (c2.getX(), c2.getY())
    win.getMouse()
    win.close()

getCircle()

Recommended Answers

All 7 Replies

You are missing the (CODE) button before pasting your code, so that we can read your code (tabulation otherwise lost). Where is you distance/collision logic? What is c1, you have not defined it anywhere in your posted code?

Define the math first. The two circles are colliding if the distance between the centers is less than or equal to the (radius of circle one + the radius of circle two. Dry it out on paper first).

I have to write a program in Python that creates two circles colliding to each other and to the sides of the window. Here is what I have so far, but the problem is that they don't collide and they don't bump against the walls.
Please tell me what i'm doing wrong or if i'm missing something. Thank you

from time import *

from graphics import *

Hi there!
I was going to copy/paste this into Python to see how your code was so far (even though you said it doesn't really work the way you want it to) and then see if I could work it out (despite the fact that I'm a Python newb). But... Python says it doesn't have a module named "graphics." This is 2.7.1, BTW. Do I need an add-on? Sorry about my newby-ism. :P

graphics.py is just simple file, which people usually place in same directory as files from DaniWeb questions, as almost nobody uses it to their own programs and so it need not be installed in PATH or PYTHONPATH.

Some professors force their students to use graphics.py, it presumably cuts down on cheating.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.