2011-11-18 3 views
2

Je suis donc en train de coder Pacman en Python juste pour m'amuser et je veux créer le visage ouvert de pacman (quand il mange de la nourriture) en utilisant les graphiques Tkinter. Comment puis-je dessiner un visage ouvert? Je pensais à l'aide canvas.create_arc pour créer la bouche ouverte, mais il ne fonctionne pas correctement :(Dessin du visage de Pacman dans Tkinter

Toute aide? Merci!

+2

Qu'avez-vous essayé? Comment a-t-il échoué? – sarnold

+2

Plus important encore, comment dessinez-vous l'arc pour Mme Pacman? –

Répondre

2

Vous avez raison que vous utilisez la méthode create_arc. Régler le début de 45 et extent-270 et il ressemblera pacman est tourné vers la droite avec sa bouche ouverte.

Puisque vous êtes juste que je vais apprendre pas poster le code exact de sorte que vous aurez une chance de le comprendre

0

Cela fonctionnerait:

import sys 
if sys.hexversion > 0x02ffffff: 
    from tkinter import * 
else: 
    from Tkinter import * 

class Application(Frame): 

    #Creates an arc  
    def draw_pieslice(self, canv,x,y,rad): 
     return canv.create_arc(x-rad, y-rad, x+rad, y+rad, fill='yellow', style=PIESLICE, start=self.start_angle, extent=self.stop_angle) 

    #To toggle the start and extent values of the arc, such that the pacman opens and closes the mouth 
    #Initially the mouth will be completely closed, but as the value is toggled we will get a 45deg space (mouth opens) 
    def toggle_mouth(self): 
     if self.start_angle is 1: 
      self.start_angle = 45 
      self.stop_angle = 270 
     else:      
      self.start_angle = 1 
      self.stop_angle = 359 

    #moves the pacman body horizontally 
    def movecircle(self): 
     self.repeat = self.repeat - 1   #sets a limit to horizontal movement 
     self.canvas.move(self.PacMouth,1,0)  
     if (self.repeat % 10) is 0:    #Adjusting the value in here, will help to adjust the mouth movement speed with the horizontal body movement 
      self.toggle_mouth() 
      self.canvas.itemconfig(self.PacMouth, start = self.start_angle, extent = self.stop_angle) 
     if self.repeat is not 0: 
      self.after(10, self.movecircle)  #By adjusting the time, we can adjust the horizontal movement speed 

    def __init__(self, master=None): 
     Frame.__init__(self, master) 

     self.start_angle = 1 
     self.stop_angle = 359 

     self.canvas = Canvas(width=800, height=480, bg='black') 
     self.canvas.pack(expand=YES, fill=BOTH) 
     text = self.canvas.create_text(50,10, text="Pacman! Yeah!", fill="white") 
     self.PacMouth = self.draw_pieslice(self.canvas,100,240,20) 
     self.repeat = 600 

     self.movecircle() 


root = Tk() 
root.config(bg = "black") 
root.title("Im on stack overflow") 
root.geometry('{}x{}'.format(800, 480)) 
root.resizable(width=False, height=False) 

app = Application(master=root) 
app.mainloop()