当前位置:   article > 正文

打地鼠python程序设计说明,打地鼠游戏界面设计_如何用turtl做游戏

如何用turtl做游戏

大家好,小编来为大家解答以下问题,打地鼠python程序设计说明,打地鼠游戏界面设计,现在让我们一起来看看吧!

大家好,给大家分享一下python打地鼠游戏代码pycharm,很多人还不知道这一点。下面详细解释一下熟练掌握python就业是不是好一点。现在让我们来看看!

老鼠和锤子都是自己画的

应用了turtle的监听模块

可以通过按下键盘一些按键改变显示的图形,进行打地鼠游戏的编程

主要应用知识点:监听:本次写的是上下左右键,执行函数操作

  1. turtle.listen()
  2. turtle.onkey(lambda :shang(),'Up')
  3. turtle.onkey(lambda :xia(),'Down')
  4. turtle.onkey(lambda :zuo(),'Left')
  5. turtle.onkey(lambda :you(),'Right')

锤子的图像和老鼠的图像都是自己画的,可能不是很好看嘿嘿

按键后出现锤子,然后老鼠和锤子同时消失,使用的就是画了一个黑色的圆遮住

知识点

import turtle
turtle.bgcolor("red")#设置背景颜色为红色
turtle.penup()#抬笔
turtle.pendown()#落笔
turtle.goto(100,100)#前往坐标(100,100)
turtle.pensize(5)#画笔粗细为5
turtle.pencolor("blue")#画笔颜色为白色
turtle.forward(100)#顺着海龟方向画单位为100的直线
turtle.begin_fill()#开始填充
turtle.fillcolor("black")#填充颜色为黑色
turtle.end_fill()#结束填充 将需要填充的形状写在两者中间
turtle.circle(100)#画半径为100的圆
turtle.left(90)#海龟方向向左旋转90度
turtle.right(90)#海龟方向向右旋转90度

完整代码

  1. import turtle
  2. import random
  3. import time
  4. turtle.bgcolor('black')
  5. turtle.hideturtle()
  6. turtle.speed(0)
  7. turtle.delay(0)
  8. turtle.penup()
  9. turtle.goto(320,180)
  10. turtle.pendown()
  11. turtle.pencolor("white")
  12. turtle.write("游戏说明:\n控制键:\n↑ 打上面地鼠\n↓ 打下面地鼠\n← 打左面地鼠\n→ 打右面地鼠\n",align="right",font=("楷体", 14,"normal"))
  13. turtle.pencolor("black")
  14. def chuizi():
  15. turtle.hideturtle()
  16. turtle.begin_fill()
  17. turtle.fillcolor("#804000")
  18. turtle.left(135)
  19. turtle.forward(50)
  20. turtle.left(90)
  21. turtle.forward(30)
  22. turtle.right(90)
  23. turtle.forward(30)
  24. turtle.right(90)
  25. turtle.forward(65)
  26. turtle.right(90)
  27. turtle.forward(30)
  28. turtle.right(90)
  29. turtle.forward(30)
  30. turtle.left(90)
  31. turtle.forward(50)
  32. turtle.right(90)
  33. turtle.forward(5)
  34. turtle.end_fill()
  35. turtle.penup()
  36. turtle.home()
  37. def laoshu():
  38. turtle.begin_fill()
  39. turtle.fillcolor("#D3D3D3")
  40. turtle.forward(50)
  41. turtle.left(90)
  42. turtle.forward(60)
  43. turtle.circle(25, 180)
  44. turtle.forward(60)
  45. turtle.right(180)
  46. turtle.forward(60)
  47. turtle.right(90)
  48. turtle.end_fill()
  49. turtle.penup()
  50. turtle.forward(15)
  51. turtle.pendown()
  52. turtle.begin_fill()
  53. turtle.fillcolor("black")
  54. turtle.circle(3)
  55. turtle.end_fill()
  56. turtle.penup()
  57. turtle.forward(18)
  58. turtle.pendown()
  59. turtle.begin_fill()
  60. turtle.fillcolor("black")
  61. turtle.circle(3)
  62. turtle.end_fill()
  63. turtle.penup()
  64. turtle.right(180)
  65. turtle.forward(6)
  66. turtle.left(90)
  67. turtle.forward(5)
  68. turtle.pendown()
  69. turtle.right(90)
  70. turtle.forward(6)
  71. turtle.right(180)
  72. turtle.forward(3)
  73. turtle.right(90)
  74. turtle.forward(5)
  75. turtle.circle(3, 180)
  76. turtle.penup()
  77. turtle.left(90)
  78. turtle.forward(12)
  79. turtle.left(90)
  80. turtle.pendown()
  81. turtle.circle(3, 180)
  82. turtle.penup()
  83. turtle.home()
  84. def shang():
  85. turtle.penup()
  86. turtle.goto(100,200)
  87. chuizi()
  88. time.sleep(0.5)
  89. turtle.pendown()
  90. turtle.pencolor('black')
  91. turtle.color('black')
  92. turtle.begin_fill()
  93. turtle.circle(150)
  94. turtle.end_fill()
  95. turtle.penup()
  96. turtle.home()
  97. def xia():
  98. turtle.penup()
  99. turtle.goto(100,-200)
  100. turtle.pendown()
  101. chuizi()
  102. time.sleep(0.5)
  103. turtle.penup()
  104. turtle.goto(50, -300)
  105. turtle.pendown()
  106. turtle.pencolor('black')
  107. turtle.color('black')
  108. turtle.begin_fill()
  109. turtle.circle(150)
  110. turtle.end_fill()
  111. turtle.penup()
  112. turtle.home()
  113. def zuo():
  114. turtle.penup()
  115. turtle.goto(-100,0)
  116. turtle.pendown()
  117. chuizi()
  118. time.sleep(0.5)
  119. turtle.penup()
  120. turtle.goto(-100,-50)
  121. turtle.pendown()
  122. turtle.pencolor('black')
  123. turtle.color('black')
  124. turtle.begin_fill()
  125. turtle.circle(130)
  126. turtle.end_fill()
  127. turtle.penup()
  128. turtle.home()
  129. def you():
  130. turtle.penup()
  131. turtle.goto(300,0)
  132. turtle.pendown()
  133. chuizi()
  134. time.sleep(0.5)
  135. turtle.penup()
  136. turtle.goto(250,-100)
  137. turtle.pendown()
  138. turtle.pencolor('black')
  139. turtle.color('black')
  140. turtle.begin_fill()
  141. turtle.circle(150)
  142. turtle.end_fill()
  143. turtle.penup()
  144. turtle.home()
  145. turtle.listen()
  146. turtle.onkey(lambda :shang(),'Up')
  147. turtle.onkey(lambda :xia(),'Down')
  148. turtle.onkey(lambda :zuo(),'Left')
  149. turtle.onkey(lambda :you(),'Right')
  150. def huayuan():
  151. colors = random.choice(["pink", "yellow", "white", "red", "green"])
  152. x=random.choice([0,-200,200])
  153. if x==0:
  154. y=random.choice([-200,200])
  155. else:
  156. y=0
  157. turtle.penup()
  158. turtle.goto(x,y)
  159. turtle.pendown()
  160. laoshu()
  161. for i in range(10):
  162. huayuan()
  163. time.sleep(2)
  164. turtle.penup()
  165. turtle.home()
  166. turtle.done()

文章知识点与官方知识档案匹配,可进一步学习相关知识
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/122868
推荐阅读
相关标签
  

闽ICP备14008679号