当前位置:   article > 正文

用tkinter写一个桌面宠物_桌宠咋写的

桌宠咋写的

目录

桌宠是什么

tkinter是什么

代码

桌宠是什么

桌宠,即桌面宠物,又称电子宠物。属于休闲类小游戏,可存在于任何人的电脑桌面上,饲养它,和它玩耍,看它成长,很有乐趣,无论小孩大人都会喜欢的小型休闲娱乐游戏。

tkinter是什么

Python的标准Tk GUI工具包的接口

Tkinter模块("Tk 接口")是Python的标准Tk GUI工具包的接口.

Tk和Tkinter可以在大多数的Unix平台下使用,同样可以应用在WindowsMacintosh系统里.Tk8.0的后续版本可以通过ttk实现本地窗口风格,并良好地运行在绝大多数平台中.

此外,tkinter.tix提供了更丰富的窗口组件。

代码

  1. import tkinter
  2. import os
  3. import random
  4. from platform import system
  5. class Pet:
  6. def __init__(self):
  7. self.root = tkinter.Tk() # create window
  8. self.delay = 170 # delay in ms
  9. self.pixels_from_right = 200 # change to move the pet's starting position
  10. self.pixels_from_bottom = 200 # change to move the pet's starting position
  11. self.move_speed = 6 # change how fast the pet moves in pixels
  12. # initialize frame arrays
  13. self.animation = dict(
  14. idle=[tkinter.PhotoImage(file=os.path.abspath('gifs/idle.gif'), format='gif -index %i' % i) for i in
  15. range(5)],
  16. idle_to_sleep=[
  17. tkinter.PhotoImage(file=os.path.abspath('gifs/idle-to-sleep.gif'), format='gif -index %i' % i)
  18. for i in range(8)],
  19. sleep=[tkinter.PhotoImage(file=os.path.abspath('gifs/sleep.gif'), format='gif -index %i' % i) for i in
  20. range(3)] * 3,
  21. sleep_to_idle=[
  22. tkinter.PhotoImage(file=os.path.abspath('gifs/sleep-to-idle.gif'), format='gif -index %i' % i)
  23. for i in range(8)],
  24. walk_left=[tkinter.PhotoImage(file=os.path.abspath('gifs/walk-left.gif'), format='gif -index %i' % i)
  25. for i in range(8)],
  26. walk_right=[tkinter.PhotoImage(file=os.path.abspath('gifs/walk-right.gif'), format='gif -index %i' % i)
  27. for i in range(8)]
  28. )
  29. # window configuration
  30. self.root.overrideredirect(True) # remove UI
  31. if system() == 'Windows':
  32. self.root.wm_attributes('-transparent', 'black')
  33. else: # platform is Mac/Linux
  34. # https://stackoverflow.com/questions/19080499/transparent-background-in-a-tkinter-window
  35. self.root.wm_attributes('-transparent', True) # do this for mac, but the bg stays black
  36. self.root.config(bg='systemTransparent')
  37. self.root.attributes('-topmost', True) # put window on top
  38. # self.root.bind("<Button-1>", self.onLeftClick)
  39. # self.root.bind("<Button-2>", self.onRightClick)
  40. # self.root.bind("<Button-3>", self.onRightClick)
  41. self.root.bind("<Key>", self.onKeyPress)
  42. self.label = tkinter.Label(self.root, bd=0, bg='black') # borderless window
  43. if system() != 'Windows':
  44. self.label.config(bg='systemTransparent')
  45. self.label.pack()
  46. screen_width = self.root.winfo_screenwidth() # width of the entire screen
  47. screen_height = self.root.winfo_screenheight() # height of the entire screen
  48. self.min_width = 10 # do not let the pet move beyond this point
  49. self.max_width = screen_width - 110 # do not let the pet move beyond this point
  50. # change starting properties of the window
  51. self.curr_width = screen_width - self.pixels_from_right
  52. self.curr_height = screen_height - self.pixels_from_bottom
  53. self.root.geometry('%dx%d+%d+%d' % (100, 100, self.curr_width, self.curr_height))
  54. def update(self, i, curr_animation):
  55. # print("Curently: %s" % curr_animation)
  56. self.root.geometry('%dx%d+%d+%d' % (100, 100, self.curr_width, self.curr_height))
  57. self.root.attributes('-topmost', True) # put window on top
  58. animation_arr = self.animation[curr_animation]
  59. frame = animation_arr[i]
  60. self.label.configure(image=frame)
  61. # move the pet if needed
  62. if curr_animation in ('walk_left', 'walk_right'):
  63. self.move_window(curr_animation)
  64. i += 1
  65. if i == len(animation_arr):
  66. # reached end of this animation, decide on the next animation
  67. next_animation = self.getNextAnimation(curr_animation)
  68. self.root.after(self.delay, self.update, 0, next_animation)
  69. else:
  70. self.root.after(self.delay, self.update, i, curr_animation)
  71. def onLeftClick(self, event):
  72. print("detected left click")
  73. def onRightClick(self, event):
  74. self.quit()
  75. def onKeyPress(self, event):
  76. if event.char in ('q', 'Q') or event.char == "":
  77. self.quit()
  78. if event.char in ("w", "W"):
  79. if self.curr_height >= 0:
  80. self.curr_height -= 1
  81. if event.char in ("s", "S"):
  82. if self.curr_height <= 750:
  83. self.curr_height += 1
  84. if event.char in ("a", "A"):
  85. if self.curr_width >= 0:
  86. self.curr_width -= 1
  87. if event.char in ("d", "D"):
  88. if self.curr_width <= self.max_width:
  89. self.curr_width += 1
  90. def move_window(self, curr_animation):
  91. if curr_animation == 'walk_left':
  92. if self.curr_width > self.min_width:
  93. self.curr_width -= self.move_speed
  94. elif curr_animation == 'walk_right':
  95. if self.curr_width < self.max_width:
  96. self.curr_width += self.move_speed
  97. self.root.geometry('%dx%d+%d+%d' % (100, 100, self.curr_width, self.curr_height))
  98. def getNextAnimation(self, curr_animation):
  99. if curr_animation == 'idle':
  100. return random.choice(['idle', 'idle_to_sleep', 'walk_left', 'walk_right'])
  101. elif curr_animation == 'idle_to_sleep':
  102. return 'sleep'
  103. elif curr_animation == 'sleep':
  104. return random.choice(['sleep', 'sleep_to_idle'])
  105. elif curr_animation == 'sleep_to_idle':
  106. return 'idle'
  107. elif curr_animation == 'walk_left':
  108. return random.choice(['idle', 'walk_left', 'walk_right'])
  109. elif curr_animation == 'walk_right':
  110. return random.choice(['idle', 'walk_left', 'walk_right'])
  111. def run(self):
  112. self.root.after(self.delay, self.update, 0, 'idle') # start on idle
  113. self.root.mainloop()
  114. def quit(self):
  115. self.root.destroy()
  116. if __name__ == '__main__':
  117. pet = Pet()
  118. pet.run()

GitHub上一位大佬写的

https://github.com/tommyli3318/desktop-pet/blob/master/main.py

运行效果:

...

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/657550
推荐阅读
相关标签
  

闽ICP备14008679号