Alx09

Untitled

May 3rd, 2019
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. import turtle
  2. import time
  3. import random
  4.  
  5. #variables
  6.  
  7. width = 600
  8. height = 600
  9. delay = 0.1
  10. ran1 = -290
  11. ran2 = 290
  12. Color = ["red","grey","blue","purple","yellow","pink","green","brown"]
  13.  
  14. pen = turtle.Turtle()
  15. pen.speed(0)
  16. pen.shape("square")
  17. pen.color("blue")
  18. pen.penup()
  19. pen.hideturtle()
  20. pen.goto(0,260)
  21. pen.write("Score: 0 High Score: 0",align = "center",font = ("Courier",24,"normal"))
  22.  
  23. #clases
  24.  
  25. class setup_screen:
  26. def __init__(self,t,c,w,h):
  27. self.wn = turtle.Screen()
  28. self.wn.title(t)
  29. self.wn.bgcolor(c)
  30. self.wn.setup(w,h)
  31. self.wn.tracer(0)
  32. def Listen(self):
  33. self.wn.listen()
  34. self.wn.onkeypress(snake.go_up,"w")
  35. self.wn.onkeypress(snake.go_down,"s")
  36. self.wn.onkeypress(snake.go_right,"d")
  37. self.wn.onkeypress(snake.go_left,"a")
  38.  
  39. class Snake(object):
  40. def __init__(self,Shape,Color):
  41. self.head = turtle.Turtle()
  42. self.head.speed(0)
  43. self.head.shape(Shape)
  44. self.head.color(Color)
  45. self.head.penup()#doesn't draw
  46. self.head.goto(0,0)#align center
  47. self.head.direction = "stop"
  48. self.segment = []
  49. self.score = 0
  50. self.high_score = 0
  51. def move(self):
  52. if self.head.direction == "up":
  53. self.y = self.head.ycor()
  54. self.head.sety(self.y + 20)
  55. if self.head.direction == "down":
  56. self.y = self.head.ycor()
  57. self.head.sety(self.y - 20)
  58. if self.head.direction == "right":
  59. self.x = self.head.xcor()
  60. self.head.setx(self.x + 20)
  61. if self.head.direction == "left":
  62. self.x = self.head.xcor()
  63. self.head.setx(self.x - 20)
  64. def go_up(self):
  65. if self.head.direction != "down":
  66. self.head.direction = "up"
  67. else:
  68. go_up()
  69.  
  70. def go_down(self):
  71. if self.head.direction != "up":
  72. self.head.direction = "down"
  73. else :
  74. go_down()
  75.  
  76. def go_right(self):
  77. if self.head.direction != "left":
  78. self.head.direction = "right"
  79. else:
  80. go_right()
  81.  
  82. def go_left(self):
  83. if self.head.direction != "right":
  84. self.head.direction = "left"
  85. else:
  86. go_left()
  87.  
  88. def Grow(self):
  89. if self.head.distance(FOOD.food) < 20 :
  90. self.x = random.randint(-289,289)
  91. self.y = random.randint(-289,289)
  92. if self.x % 2 == 1:
  93. self.x = self.x - 1
  94. if self.y % 2 == 1:
  95. self.y = self.y - 1
  96. FOOD.food.goto(self.x,self.y)
  97. self.score = self.score + 10
  98. if self.score > self.high_score :
  99. self.high_score = self.score
  100. pen.clear()
  101. pen.write("Score: {} High Score: {}".format(self.score,self.high_score),align = "center",font = ("Courier",24,"normal"))
  102.  
  103. self.new_segment = turtle.Turtle()
  104. self.new_segment.speed(0)
  105. self.new_segment.shape("square")
  106. self.new_segment.color(Color[random.randint(0,len(Color) - 1)])
  107. self.new_segment.penup()
  108. self.segment.append(self.new_segment)
  109.  
  110. for index in range(len(self.segment) - 1,0,-1):
  111. self.x = self.segment[index - 1].xcor()
  112. self.y = self.segment[index - 1].ycor()
  113. self.segment[index].goto(self.x,self.y)
  114.  
  115. if len(self.segment) > 0:
  116. self.x = self.head.xcor()
  117. self.y = self.head.ycor()
  118. self.segment[0].goto(self.x,self.y)
  119. def GameOver(self):
  120. if self.head.xcor() > 300 or self.head.ycor() > 300 or self.head.ycor() <-300 or self.head.xcor() <-300:
  121. time.sleep(1)
  122. self.head.goto(0,0)
  123. self.head.direction = "stop"
  124. for s in self.segment :
  125. s.goto(1000,1000)
  126. self.segment.clear()
  127. self.score = 0
  128. pen.clear()
  129. pen.write("Score: {} High Score: {}".format(self.score,self.high_score),align = "center",font = ("Courier",24,"normal"))
  130. for s in self.segment:
  131. if s.distance(self.head) < 20:
  132. time.sleep(1)
  133. self.head.goto(0,0)
  134. self.head.direction = "stop"
  135. for d in self.segment :
  136. d.goto(1000,1000)
  137. self.segment.clear()
  138. self.score = 0
  139. pen.clear()
  140. pen.write("Score: {} High Score: {}".format(self.score,self.high_score),align = "center",font = ("Courier",24,"normal"))
  141.  
  142. class Food(object):
  143. def __init__(self,Shape,Color):
  144. self.food = turtle.Turtle()
  145. self.food.speed(0)
  146. self.food.shape(Shape)
  147. self.food.color(Color)
  148. self.food.penup()
  149. self.food.goto(0,100)
  150.  
  151. FOOD = Food("square","red")
  152. snake = Snake("square","white")
  153. screen = setup_screen("Snake Game","black",width,height)
  154. screen.Listen()
  155.  
  156. while True:
  157. screen.wn.update()
  158.  
  159. snake.Grow()
  160.  
  161. snake.move()
  162.  
  163. snake.GameOver()
  164.  
  165. if snake.head.distance(FOOD.food) < 20:
  166. delay -= 0.005
  167.  
  168. if snake.score == 0:
  169. delay = 0.1
  170.  
  171. time.sleep(delay)
  172.  
  173. wn.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment