Advertisement
Fenny_Theo

CSC1002_5

Apr 22nd, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. from turtle import Turtle, Screen, mainloop
  2. import turtle
  3. import random
  4. import math
  5. import time
  6. head=Turtle()
  7. screen=Screen()
  8. tails=Turtle()
  9. monster=Turtle()
  10. text=Turtle()
  11. food=Turtle()
  12. KEY_UP,KEY_DOWN,KEY_LEFT,KEY_RIGHT="Up","Down","Left","Right"
  13.  
  14. #global variable:
  15. pausing=True
  16. gameReady=False
  17. eating=False
  18. caught=False
  19. food_x=[]
  20. food_y=[]
  21. food_q=[]
  22.  
  23.  
  24.  
  25. def setWrite():
  26. text.up()
  27. text.setpos(-245,225)
  28. text.write("Welcom to Fenny's snake game",move=False, align="left", font=("Arial", 10, "normal"))
  29. text.setpos(-245,205)
  30. text.write("You are going to use the four arrow keys to move the head around the the screen",\
  31. move=False, align="left", font=("Arial", 10, "normal"))
  32. text.setpos(-245,185)
  33. text.write("trying to consume all the food items before the monster catches you... ",\
  34. move=False, align="left", font=("Arial", 10, "normal"))
  35. text.setpos(-245,165)
  36. text.write("Click anywhere on the screen to start the game,have fun!",\
  37. move=False, align="left", font=("Arial", 10, "normal"))
  38. text.ht()#hide the turtle
  39.  
  40. def setSnake():
  41. head.up()
  42. head.setpos(0,0)
  43. head.shape("square")
  44. head.fillcolor("red")
  45.  
  46.  
  47. def setMonster():
  48. monster.up()
  49. monster.shape("square")
  50. monster.fillcolor("purple")
  51. x1=random.randint(-200,-100)
  52. x2=random.randint(100,200)
  53. x=random.random()
  54. if x<1/2:
  55. x=x1
  56. else:
  57. x=x2
  58. y=random.randint(-200,-100)
  59. monster.setpos(x,y)
  60.  
  61. def gameBegin(m,n):
  62. -250<=m<=250
  63. -250<=n<=250
  64. global gameReady
  65. global pausing
  66. if (gameReady):
  67. pausing=False
  68. screen.onscreenclick(None)
  69.  
  70. def setFood():
  71. global food_q
  72. global food_x
  73. global food_y
  74. for i in range(1,10):
  75. x=int(220*random.random())
  76. c=random.random()
  77. if c<1/2:
  78. x=(-1)*x
  79. y=int(220*random.random())
  80. d=random.random()
  81. if d<1/2:
  82. y=(-1)*y
  83. food.up()
  84. food.ht()
  85. food.setpos(x,y)
  86. food.write(i)
  87. food_x.append(x)
  88. food_y.append(y)
  89. food_q.append(i)
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97. #main
  98. #set Screen
  99. screen.setup(width=500, height=500, startx=None, starty=None)
  100. turtle.title("Fenny's Game")
  101. setWrite()
  102. setSnake()
  103. setMonster()
  104. gameReady=True #前面默认设置完成
  105. screen.onscreenclick(gameBegin)
  106. while True:#停在第一个界面直到click(限制在这个循环中)
  107. if (pausing):
  108. pass
  109. else:
  110. break
  111. text.clear()
  112. setFood()
  113. turtle.title("Snake")
  114.  
  115.  
  116.  
  117. screen.listen()
  118. screen.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement