2016-04-28 10 views
0

Wie bewegt man Augen in Python (wenn der Geist nach links wendet, bleibt das Auge auf der rechten Seite). Ich versuchte mit c.move (Auge, -10, 0), als ich die linke Bewegung definierte, habe ich auch versucht, Zustand für das Auge verborgen und schuf ein left_eye mit Zustand normal, aber es funktioniert nicht. Danke.tkinter - move shapes

from tkinter import * 
HEIGHT = 500 
WIDTH = 800 
window = Tk() 
window.title('Fetita arunca mingea') 
c = Canvas (window, width = WIDTH, height = HEIGHT, bg = 'grey') 
c.pack() 
school= c.create_rectangle (200, 50,600, 150, fill = 'orange') 
school_window1 = c.create_rectangle (250,80, 300, 100, fill= 'yellow') 
school_window2 = c.create_rectangle (360,80, 420, 100, fill= 'yellow') 
school_window2 = c.create_rectangle (500,80, 550, 100, fill= 'yellow') 
body =c.create_polygon (0, 450, 250, 450, 100, 240,fill ='blue') 
head = c.create_oval (50, 250, 150, 150, fill = 'ivory') 
eye =c.create_oval (120 ,180, 125, 185,fill ='blue') 
#eye_left =c.create_oval (75 ,180, 80, 185,fill ='yellow') 
eye_left = c.move(eye,-10,0) 
mouth = c.create_line (145, 220, 120, 220,fill = 'red') 
#mouth_left = c.create_line (55, 220, 80, 220,fill = 'blue') 
hand =c.create_line (110, 300, 200, 220, fill = 'black') 
GIRL_step = 10 
def move_girl(event): 
    if event.keysym == 'Up': 
     c.move (body, 0, - GIRL_step) 
     c.move (head, 0, - GIRL_step) 
     c.move (eye, 0, - GIRL_step) 
     c.move (mouth, 0, - GIRL_step) 
     c.move (hand, 0, - GIRL_step) 
    elif event.keysym== 'Down': 
     c.move (body, 0, GIRL_step) 
     c.move (head, 0, GIRL_step) 
     c.move (eye, 0, GIRL_step) 
     c.move (mouth, 0, GIRL_step) 
     c.move (hand, 0, GIRL_step) 
    elif event.keysym== 'Left': 
     c.move(eye,-10, 0) 
     c.move (body, - GIRL_step, 0) 
     c.move (head, - GIRL_step, 0) 
     c.move (eye, - GIRL_step, 0) 
     c.move (mouth, _GIRL_step, 0) 
     c.move (hand, - GIRL_step, 0) 
    elif event.keysym== 'Right': 
     c.move (body, GIRL_step, 0) 
     c.move (head, GIRL_step, 0) 
     c.move (eye, GIRL_step, 0) 
     c.move (mouth, GIRL_step, 0) 
     c.move (hand, GIRL_step, 0) 
c.bind_all ('<Key>', move_girl) 
+0

Sie Beispiel nicht funktioniert, weil von einer Art zu beseitigen (unterstreichen anstelle von Minuszeichen für Auge in Bewegung "Links"). Es funktioniert nach Korrektur des Tippfehlers. Zusätzlich sollten Sie die Bewegung des Gesichts überprüfen, x für das Auge ist kleiner als der Kopf x-Radius des Kopfes für die Bewegung nach links. –

Antwort

1

Sie können auch eine Verwendung für(), um die Dinge einfacher

for body_part in [body, head, eye, mouth, hand]: 
     c.move (body_part, - GIRL_step, 0) 

und eine Funktion redundanten Code

from tkinter import * 
HEIGHT = 500 
WIDTH = 800 
window = Tk() 
window.title('Fetita arunca mingea') 
c = Canvas (window, width = WIDTH, height = HEIGHT, bg = 'grey') 
c.pack() 
school= c.create_rectangle (200, 50,600, 150, fill = 'orange') 
school_window1 = c.create_rectangle (250,80, 300, 100, fill= 'yellow') 
school_window2 = c.create_rectangle (360,80, 420, 100, fill= 'yellow') 
school_window2 = c.create_rectangle (500,80, 550, 100, fill= 'yellow') 
body =c.create_polygon (0, 450, 250, 450, 100, 240,fill ='blue') 
head = c.create_oval (50, 250, 150, 150, fill = 'ivory') 
eye =c.create_oval (120 ,180, 125, 185,fill ='blue') 
#eye_left =c.create_oval (75 ,180, 80, 185,fill ='yellow') 
eye_left = c.move(eye,-10,0) 
mouth = c.create_line (145, 220, 120, 220,fill = 'red') 
#mouth_left = c.create_line (55, 220, 80, 220,fill = 'blue') 
hand =c.create_line (110, 300, 200, 220, fill = 'black') 
GIRL_step = 10 

def move_common(move_x, move_y): 
    for body_part in [body, head, eye, mouth, hand]: 
     c.move (body_part, move_x, move_y) 

def move_girl(event): 
    if event.keysym == 'Up': 
     move_common(0, -GIRL_step) 
    elif event.keysym== 'Down': 
     move_common(0, GIRL_ste) 
    elif event.keysym== 'Left': 
     c.move(eye,-10, 0) 
     move_common(-GIRL_step, 0) 
    elif event.keysym== 'Right': 
     move_common(GIRL_step, 0) 
c.bind_all ('<Key>', move_girl) 

window.mainloop() 
+0

Ihre Antwort beginnt mit "Auch". "Auch" zu was? Die Antwort bedeutet, dass es einen Teil 1 und einen Teil 2 gibt, wo dies Teil 2 ist. Ist Teil 1 der Kommentar, den Sie gemacht haben? –