ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

屏幕小宠物(python3.8.5版)

2021-11-20 09:07:29  阅读:449  来源: 互联网

标签:itemconfigure 宠物 toggle state tongue root 屏幕 python3.8 happy


有些人渴望有个小宠物,今天作者带了一个电子宠物——猫。

先把代码奉献给大家:

from tkinter import HIDDEN,NORMAL,Tk,Canvas
def toggle_eyes():
    current_color = c.itemcget(eye_left,'fill')
    new_color = c.color = c.body_color if current_color == 'white' else 'white'
    current_state = c.itemcget(pupil_left,'state')
    new_state = NORMAL if current_state == HIDDEN else HIDDEN
    c.itemconfigure(pupil_left, state=new_state)
    c.itemconfigure(pupil_right, state=new_state)
    c.itemconfigure(eye_left,fill=new_color)
    c.itemconfigure(eye_right,fill=new_color)

def blink():
    toggle_eyes()
    root.after(250,toggle_eyes)
    root.after(3000,blink)

def toggle_pupils():
    if not c.eyes_crossed:
        
        c.move(pupil_left,10,-5)
        c.move(pupil_right,-10,-5)
        c.eyes_crossed = True
    else:
        c.move(pupil_left,-10,5)
        c.move(pupil_right,10,5)
        c.eyes_crossed = False
       
        
def toggle_tongue():
    if not c.tongue_out:
        c.itemconfigure(tongue_tip,state=NORMAL)
        c.itemconfigure(tongue_main,state=NORMAL)
        c.tongue_out = True
    else:
        c.itemconfigure(tongue_tip,state=HIDDEN)
        c.itemconfigure(tongue_main,state=HIDDEN)
        c.tongue_out = False
        
def cheeky(event):
    toggle_tongue()
    toggle_pupils()
    hide_happy(event)
    root.after(1000,toggle_tongue)
    root.after(1000,toggle_pupils)
    return

def sad():
    if c.happy_level == 0:
        c.itemconfigure(mouth_happy,state=HIDDEN)
        c.itemconfigure(mouth_normal,state=HIDDEN)
        c.itemconfigure(mouth_sad,state=NORMAL)
    else:
        c.happy_level -= 1
    root.after(5000,sad)


def show_happy(event):
    if(20 <= event.x <= 350) and (20 <= event.y <=350):
        c.itemconfigure(cheek_left,state=NORMAL)
        c.itemconfigure(cheek_right,state=NORMAL)
        c.itemconfigure(mouth_happy,state=NORMAL)
        c.itemconfigure(mouth_normal,state=HIDDEN)
        c.itemconfigure(mouth_sad,state=HIDDEN)
        c.happy_level = 10
        
        return

def hide_happy(event):
    c.itemconfigure(cheek_left,state=HIDDEN)
    c.itemconfigure(cheek_right,state=HIDDEN)
    c.itemconfigure(mouth_happy,state=HIDDEN)
    c.itemconfigure(mouth_normal,state=NORMAL)
    c.itemconfigure(mouth_sad,state=HIDDEN)

    return

root = Tk()
c = Canvas(root,width=400,height=400)
c.configure(bg='dark blue',highlightthickness=0)
c.body_color = 'orange1'
body = c.create_oval(35,20,365,350,outline=c.body_color,fill=c.body_color)
ear_left = c.create_polygon(75,80,75,10,165,70,outline=c.body_color,fill=c.body_color)
ear_right = c.create_polygon(255,45,325,10,320,70,outline=c.body_color,\
                             fill=c.body_color)
foot_left = c.create_oval(65,320,145,360,outline=c.body_color,fill=c.body_color)
foot_right = c.create_oval(250,320,330,360,outline=c.body_color,fill=c.body_color)


eye_left = c.create_oval(130,110,160,170,outline='black',fill='white')
pupil_left = c.create_oval(140,145,150,155,outline='black',fill='black')
eye_right = c.create_oval(230,110,260,170,outline='black',fill='white')
pupil_right = c.create_oval(240,145,250,155,outline='black',fill='black')
mouth_normal = c.create_line(170,250,200,272,230,250,smooth=1,width=2,state=NORMAL)
mouth_happy = c.create_line(170,250,200,282,230,250,smooth=1,width=2,state=HIDDEN)
mouth_sad = c.create_line(170,250,200,232,230,250,smooth=1,width=2,state=HIDDEN)
tongue_main = c.create_rectangle(170,250,230,290,outline='red',fill='red',state=HIDDEN)
tongue_tip = c.create_oval(170,285,230,300,outline='red',fill='red',state=HIDDEN)


cheek_left =  c.create_oval(70,180,120,230,outline='pink',fill='pink',state=HIDDEN)
cheek_right =  c.create_oval(280,180,330,230,outline='pink',fill='pink',state=HIDDEN)
c.pack()

c.bind('<Motion>',show_happy)
c.bind('<Leave>',hide_happy)
c.bind('<Double-1>',cheeky)
c.happy_level = 10
c.eyes_crossed = False
c.tongue_out = False

root.after(1000,blink)
root.after(5000,sad)
root.mainloop()

代码是不是很多呀!

给你们看看效果!

它的正常模式

 

它被抚摸的模式 ,鼠标划过去即可

你不理他,伤心的模式 ,给它抚摸模式就会重新开心起来!

标签:itemconfigure,宠物,toggle,state,tongue,root,屏幕,python3.8,happy
来源: https://blog.csdn.net/qq_48942747/article/details/121434726

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有