Hey, I'm brand new to Python and I'm sorry if my code/question is sloppy and ridiculous. I had to make a snowman for homework, but now I have to make the entire snowman move 6 times wherever the user clicks. I used this at first, but it obviously only moves the snowman's head:

for i in range(6):
        p = win.getMouse()
        c = top.getCenter()
        dx = p.getX() - c.getX()
        dy = p.getY() - c.getY()
        top.move(dx,dy)

So how would I get all the pieces of the snowman to move at once?

from graphics import *

def main():
    
    win = GraphWin("Snowman",400,500)
    
    top = Circle(Point(200,115), 45)
    top.setFill("white")
    top.setOutline("white")
    top.draw(win)
    

    middle = Circle(Point(200,210), 65)
    middle.setFill("white")
    middle.setOutline("white")
    middle.draw(win)

    bottom = Circle(Point(200,345), 85)
    bottom.setFill("white")
    bottom.setOutline("white")
    bottom.draw(win)

    hat = Rectangle(Point(290,150),Point(200,160))
    hat.setFill("black")
    hat.move(-43,-86)
    hat.draw(win)
    
    tophat = Rectangle(Point(30,30),Point(70,70))
    tophat.setFill("black")
    tophat.move(150,-7)
    tophat.draw(win)

    eye = Circle(Point(180,100), 3.5)
    eye.setFill("black")
    eye.draw(win)

    eye2=eye.clone()
    eye2.move(40,0)
    eye2.draw(win)

    nose = Polygon(Point(180,90),Point(130,100),Point(180,100))
    nose.setFill("orange")
    nose.move(20,18)
    nose.draw(win)

    mouth = eye2.clone()
    mouth.move(-50,25)
    mouth.draw(win)

    mouth2 = mouth.clone()
    mouth2.move(15,10)
    mouth2.draw(win)

    mouth3 = mouth.clone()
    mouth3.move(30,15)
    mouth3.draw(win)

    mouth4 = mouth.clone()
    mouth4.move(45,10)
    mouth4.draw(win)

    mouth5 = mouth.clone()
    mouth5.move(60,0)
    mouth5.draw(win)

    button = mouth.clone()
    button.move(30,50)
    button.draw(win)

    button2 = mouth.clone()
    button2.move(30,80)
    button2.draw(win)

    button3 = mouth.clone()
    button3.move(30,110)
    button3.draw(win)

    leftarm = Line(Point(50,100),Point(125,125))
    leftarm.setFill("brown")
    leftarm.setWidth(2)
    leftarm.move(18,60)
    leftarm.draw(win)

    rightarm = Line(Point(50,-45),Point(125,-80))
    rightarm.setFill("brown")
    rightarm.setWidth(2)
    rightarm.move(205,225)
    rightarm.draw(win)

    snow = Circle(Point(100,100), 2)
    snow.setFill("white")
    snow.setOutline("white")
    snow.draw(win)

    snow2 = snow.clone()
    snow2.move(200,100)
    snow2.draw(win)

    snow3 = snow.clone()
    snow3.move(250,150)
    snow3.draw(win)
    
    snow4 = snow.clone()
    snow4.move(200,200)
    snow4.draw(win)

    snow5 = snow.clone()
    snow5.move(150,50)
    snow5.draw(win)

    snow6 = snow.clone()
    snow6.move(25,100)
    snow6.draw(win)

    snow7 = snow.clone()
    snow7.move(-15,50)
    snow7.draw(win)

    snow8 = snow.clone()
    snow8.move(-60,-20)
    snow8.draw(win)

    snow9 = snow.clone()
    snow9.move(-40,-70)
    snow9.draw(win)

    snow10 = snow.clone()
    snow10.move(275,30)
    snow10.draw(win)

    snow11 = snow.clone()
    snow11.move(230,-5)
    snow11.draw(win)

    snow12 = snow.clone()
    snow12.move(190,-50)
    snow12.draw(win)

    snow13 = snow.clone()
    snow13.move(50,-70)
    snow13.draw(win)

    snow14 = snow.clone()
    snow14.move(-40,250)
    snow14.draw(win)

    snow15 = snow.clone()
    snow15.move(-70,150)
    snow15.draw(win)
    
    win.getMouse()
    win.close()   

main()

Thank you for any help!!

Member Avatar for masterofpuppets

hi
this is probably not the best solution to the problem you have but here goes :)..Try to connect all parts of the snowman together using the same variables for x and y coordinates for the head, body...modified of course to actually create the snowman. What I mean is this:
if the head has a centre at x, y, the body can have a centre at x, y + 50 say, the same for the arms...... so if you modify x or y and redraw the snowman and it will be moved to the new coordinates. I'll make a small example:

from Tkinter import *

root = Tk()
c = Canvas( root, width = 300, height = 300 ); c.pack()
x, y = 150, 150
c.create_text( 150, 20, text = "Left-Click anywhere", font = "Arial" )
head = c.create_oval( x - 10, y - 10, x + 10, y + 10, fill = "white" )
body = c.create_oval( x - 15, y + 10, x + 15, y + 40, fill = "white" ) 
arm1 = c.create_oval( x - 20, y + 15, x - 10, y + 30, fill = "white" )
arm2 = c.create_oval( x + 20, y + 15, x + 10, y + 30, fill = "white" )

def snowman():
    global head, body, arm1, arm2, x, y
    c.delete( head, body, arm1, arm2 )
    head = c.create_oval( x - 10, y - 10, x + 10, y + 10, fill = "white" )
    body = c.create_oval( x - 15, y + 10, x + 15, y + 40, fill = "white" ) 
    arm1 = c.create_oval( x - 20, y + 15, x - 10, y + 30, fill = "white" )
    arm2 = c.create_oval( x + 20, y + 15, x + 10, y + 30, fill = "white" )

def changeCoordinates( event ):
    global x, y
    x = event.x
    y = event.y
    snowman()

c.bind( "<Button-1>", changeCoordinates )
mainloop()

something like this maybe? :)

Put the parts you want to move into a list and do this ...

parts = [top, middle, bottom, hat, tophat, eye, eye2,
        nose, mouth, mouth2, mouth3, mouth4, mouth5,
        button, button2, button3, leftarm, rightarm]
    for i in range(6):
        p = win.getMouse()
        c = top.getCenter()
        dx = p.getX() - c.getX()
        dy = p.getY() - c.getY()
        for part in parts:
            part.move(dx,dy)

Nice project for beginners!

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.