Advertisement
Fenny_Theo

final version

Apr 25th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.85 KB | None | 0 0
  1. from turtle import Turtle, Screen, mainloop
  2. import turtle
  3. import random
  4. import math
  5. import time
  6. KEY_UP,KEY_DOWN,KEY_LEFT,KEY_RIGHT="Up","Down","Left","Right"
  7. foods=[]
  8. food_x=[]
  9. food_y=[]
  10. food_q=[]
  11. foodNumber=0
  12. g_direction=None
  13. foodMove=0
  14. caught=False
  15. pausing=False
  16. contacting=0
  17. timeBegin=0
  18. timeNow=0
  19. moveTrace=[(0,0)]
  20. moveTime=0
  21. screen=Screen()
  22. head=Turtle()
  23. text=Turtle()
  24. monster=Turtle()
  25.  
  26.  
  27.  
  28.  
  29. def setWrite():
  30. screen.setup(width=500, height=500, startx=None, starty=None)
  31. turtle.title("Fenny's Game")
  32. text.up()
  33. text.setpos(-245,225)
  34. text.write("Welcom to Fenny's head game",move=False, align="left", font=("Arial", 10, "normal"))
  35. text.setpos(-245,205)
  36. text.write("You are going to use the four arrow keys to move the head around the the screen",\
  37. move=False, align="left", font=("Arial", 10, "normal"))
  38. text.setpos(-245,185)
  39. text.write("trying to consume all the food items before the monster catches you... ",\
  40. move=False, align="left", font=("Arial", 10, "normal"))
  41. text.setpos(-245,165)
  42. text.write("Click anywhere on the screen to start the game,have fun!",\
  43. move=False, align="left", font=("Arial", 10, "normal"))
  44. text.ht()#hide the turtle
  45.  
  46. def setSnake():
  47. head.up()
  48. head.setpos(0,0)
  49. head.shape("square")
  50. head.fillcolor("red")
  51.  
  52. def setMonster():
  53. monster.up()
  54. monster.shape("square")
  55. monster.fillcolor("purple")
  56. x1=random.randint(-200,-100)
  57. x2=random.randint(100,200)
  58. x=random.random()
  59. if x<1/2:
  60. x=x1
  61. else:
  62. x=x2
  63. y=random.randint(-200,-100)
  64. monster.setpos(x,y)
  65.  
  66. def setFood():
  67. global foods
  68. global food_q
  69. global food_x
  70. global food_y
  71. food1=Turtle()
  72. food2=Turtle()
  73. food3=Turtle()
  74. food4=Turtle()
  75. food5=Turtle()
  76. food6=Turtle()
  77. food7=Turtle()
  78. food8=Turtle()
  79. food9=Turtle()
  80. food10=Turtle()
  81. foods.append(food1)
  82. foods.append(food2)
  83. foods.append(food3)
  84. foods.append(food4)
  85. foods.append(food5)
  86. foods.append(food6)
  87. foods.append(food7)
  88. foods.append(food8)
  89. foods.append(food9)
  90. foods.append(food10)
  91. for i in range(10):
  92. foods[i].ht()
  93. food_x.append(0)
  94. food_y.append(0)
  95. food_q.append(5)
  96. for i in range(1,10):
  97. x=int(220*random.random())
  98. c=random.random()
  99. if c<1/2:
  100. x=(-1)*x
  101. y=int(220*random.random())
  102. d=random.random()
  103. if d<1/2:
  104. y=(-1)*y
  105. foods[i].up()
  106. foods[i].ht()
  107. foods[i].setpos(x,y)
  108. foods[i].write(i)
  109. food_x.append(x)
  110. food_y.append(y)
  111. food_q.append(i)
  112.  
  113.  
  114. def moveUp():
  115. global g_direction
  116. if g_direction!=270:
  117. g_direction=90
  118.  
  119. def moveDown():
  120. global g_direction
  121. if g_direction!=90:
  122. g_direction=270
  123.  
  124. def moveLeft():
  125. global g_direction
  126. if g_direction!=0:
  127. g_direction=180
  128.  
  129. def moveRight():
  130. global g_direction
  131. if g_direction!=180:
  132. g_direction=0
  133.  
  134. def pause():
  135. global pausing
  136. pausing=not pausing
  137.  
  138. def screenControl():
  139. screen.onkey(moveUp,KEY_UP)
  140. screen.onkey(moveDown,KEY_DOWN)
  141. screen.onkey(moveLeft,KEY_LEFT)
  142. screen.onkey(moveRight,KEY_RIGHT)
  143. turtle.onkey(pause, "space")
  144.  
  145.  
  146. def remove():
  147. global foodMove
  148. foods[foodMove].clear()
  149. food_x.remove(food_x[foodMove])
  150. food_y.remove(food_y[foodMove])
  151. food_q.remove(food_q[foodMove])#先remove 前面那个5==出现错误
  152. foods.remove(foods[foodMove])
  153.  
  154. def headMove(x,y):
  155. global g_direction
  156. if g_direction==90 and y>=230:#head的运动
  157. return 0
  158. if g_direction==180 and x<=-230:
  159. return 0
  160. if g_direction==270 and y<=-230:
  161. return 0
  162. if g_direction==0 and x>=230:
  163. return 0
  164. else:
  165. head.setheading(g_direction)
  166. head.forward(20)
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173. def appear(x,y):
  174. head.color("blue","black")
  175. head.stamp()
  176. head.color("red")
  177. headMove(x,y)
  178.  
  179. def moveForward(x,y):
  180. if (g_direction!=90 or y<=230) and (g_direction!=180 or x>=-230) and \
  181. (g_direction!=270 or y>=-230) and (g_direction!=0 or x<=230):
  182. head.color("blue","black")
  183. head.stamp()
  184. head.color("red")
  185. headMove(x,y)
  186. head.clearstamp(head.stampItems[0])
  187.  
  188.  
  189. def snakeMove():
  190. global caught
  191. global foodMove
  192. global food_x
  193. global food_y
  194. global food_q
  195. global foodNumber
  196. global pausing
  197. if g_direction!=None and pausing!=True:
  198. x,y=head.pos()
  199. i=0
  200. while i<len(foods):
  201. if abs(x-food_x[i])<20 and abs(y-food_y[i])<20:
  202. foodMove=i
  203. foodNumber=food_q[i]+foodNumber
  204. remove()
  205. i=i+1
  206. if foodNumber>0:
  207. appear(x,y)
  208. foodNumber-=1
  209. else:
  210. moveForward(x,y)
  211. if caught==True:
  212. return
  213. if len(head.stampItems)==50:
  214. text.up()
  215. text.ht()
  216. text.setpos(x,y)
  217. text.color("red")
  218. text.write("Winner!!!",font=("Arial", 20, "bold"))
  219. return
  220. elif pausing==True:
  221. head.forward(0)
  222. screen.ontimer(snakeMove,500)
  223.  
  224. def monsterMove():
  225. global g_direction
  226. global caught
  227. global pausing
  228. if g_direction!=None and pausing!=True:
  229. x,y=head.pos()
  230. a,b=monster.pos()
  231. delta1 = 2/math.sqrt(abs(x-a)*abs(x-a)+abs(y-b)*abs(y-b))
  232. delta2 = random.random() + 1
  233. if (abs(a-x)>20) or (abs(b-y)>20):
  234. monster.setx(a + (x - a)*delta1*delta2) # new pos
  235. monster.sety(b + (y- b)*delta1*delta2)
  236. else:
  237. caught=True
  238. x,y=head.pos()
  239. text.up()
  240. text.ht()
  241. text.setpos(x,y)
  242. text.color("yellow")
  243. text.write("Game Over!!!",font=("Arial", 20, "bold"))
  244. return
  245. if len(head.stampItems)==50:
  246. return
  247. elif pausing==True:
  248. monster.forward(0)
  249. screen.ontimer(monsterMove,100)
  250.  
  251. def contact():
  252. global g_direction
  253. global moveTrace
  254. global contacting
  255. global pausing
  256. if g_direction!=None and pausing!=True:
  257. length=len(head.stampItems)
  258. x,y=head.pos()
  259. a,b=monster.pos()
  260. if (x,y)!=moveTrace[-1]:
  261. moveTrace.append((x,y))
  262. for i in range(1,length+1):
  263. m,n=moveTrace[-i]
  264. if (abs(a-m)<20) and (abs(b-n)<20):
  265. contacting+=1
  266. screen.ontimer(contact,500)#speeed=snake
  267.  
  268.  
  269.  
  270. def timing():
  271. global g_direction
  272. global timeBegin
  273. global timeNow
  274. global moveTime
  275. if g_direction!=None:
  276. timeNow = time.time()
  277. moveTime=timeNow-timeBegin
  278. screen.ontimer(timing,500)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement