首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Tkinter中绘制吃豆人的脸

问在Tkinter中绘制吃豆人的脸
EN

Stack Overflow用户
提问于 2011-11-18 09:10:05
回答 2查看 2.6K关注 0票数 2

所以我用Python编写Pacman只是为了好玩,我想用Tkinter图形创建pacman的张开的脸(当他在吃东西的时候)。我到底该如何画一个开放的脸呢?我在考虑用canvas.create_arc来创建张开的嘴巴,但它不能正常工作:

有什么帮助吗?谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-11-19 05:02:46

您使用create_arc方法是正确的。将start设置为45,将extent设置为270,看起来就像是吃豆人张着嘴面对着右边一样。

由于您刚刚开始学习,我不会发布确切的代码,因此您将有机会自己弄清楚。

票数 2
EN

Stack Overflow用户

发布于 2017-08-18 20:06:53

这将会起作用:

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8176599

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档