Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. import turtle, math, random, winsound
  2.  
  3. screen = turtle.Screen()
  4. screen.bgcolor("black")
  5. screen.setup(600,600,700,0)
  6.  
  7. #borders
  8. b = turtle.Pen()
  9. b.penup()
  10. b.speed(0)
  11. b.pencolor("orange")
  12. b.setpos(-235,-235)
  13. for i in range(4):
  14. b.pendown()
  15. b.forward(470)
  16. b.left(90)
  17.  
  18.  
  19.  
  20. # Snake
  21. s = turtle.Turtle()
  22. s.color("orange")
  23. s.fillcolor("black")
  24. s.shape("square")
  25. s.shapesize(0.5)
  26. s.penup()
  27. s.speed(0)
  28. speed = 1
  29. direction = "stop"
  30. tail = 30
  31. print(tail)
  32.  
  33. # Apple
  34. a = turtle.Turtle()
  35. a.color("red")
  36. a.fillcolor('black')
  37. a.shape("circle")
  38. a.shapesize(0.2)
  39. a.penup()
  40. a.speed(0)
  41. a.setpos(random.randrange(-220,200), random.randrange(-220,200))
  42. print(a.xcor())
  43.  
  44. def move():
  45. global speed
  46. global direction
  47. global tail
  48. if direction =="left":
  49.  
  50. s.goto(s.xcor() - speed, s.ycor())
  51. s.stamp()
  52. if s.stamp() > tail:
  53. s.clearstamps(2)
  54. if is_contact(s, a):
  55. winsound.Beep(1000,40)
  56. speed += 0.5
  57. a.setpos(random.randrange(-220,200), random.randrange(-220,200))
  58. if s.xcor() < -220:
  59. direction = "stop"
  60. elif direction =="right":
  61.  
  62. s.goto(s.xcor() + speed, s.ycor())
  63. s.stamp()
  64. if s.stamp() > tail:
  65. s.clearstamps(2)
  66. if is_contact(s, a):
  67. winsound.Beep(1000,40)
  68. speed += 0.5
  69. a.setpos(random.randrange(-220,200), random.randrange(-220,200))
  70. if s.xcor() > 220:
  71. direction = "stop"
  72. elif direction == "up":
  73.  
  74. s.goto(s.xcor() , s.ycor() + speed)
  75. s.stamp()
  76. if s.stamp() > tail:
  77. s.clearstamps(2)
  78. if is_contact(s, a):
  79. winsound.Beep(1000,40)
  80. a.setpos(random.randrange(-220,200), random.randrange(-220,200))
  81. speed += 0.5
  82. if s.ycor() > 220:
  83. direction = "stop"
  84. elif direction == "down":
  85.  
  86. s.goto(s.xcor(), s.ycor() -speed)
  87. s.stamp()
  88. if s.stamp() > tail:
  89. s.clearstamps(2)
  90. if is_contact(s, a):
  91. winsound.Beep(1000,40)
  92. a.setpos(random.randrange(-220,200), random.randrange(-220,200))
  93. speed += 0.5
  94. if s.ycor() < -220:
  95. direction = "stop"
  96. else:
  97. s.goto(s.xcor(), s.ycor())
  98. def move_left():
  99. global direction
  100. direction = "left"
  101. def move_right():
  102. global direction
  103. direction = "right"
  104. def move_up():
  105. global direction
  106. direction = "up"
  107. def move_down():
  108. global direction
  109. direction = "down"
  110.  
  111. turtle.listen()
  112. turtle.onkey(move_left,"Left")
  113. turtle.onkey(move_right,"Right")
  114. turtle.onkey(move_up,"Up")
  115. turtle.onkey(move_down,"Down")
  116.  
  117. def is_contact(object_1, object_2):
  118. a = object_1.xcor() - object_2.xcor()
  119. b = object_1.ycor() - object_2.ycor()
  120. distance = math.sqrt((a ** 2) + (b ** 2))
  121. if distance < 10:
  122. return True
  123. else:
  124. return False
  125. stamp_list = []
  126. while True:
  127. move()
  128. #print(s.xcor(),s.ycor())
  129. #print(s.stamp())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement