Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2019
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.19 KB | None | 0 0
  1. # Importing from turtle import Screen, Turtle saves time and make code look cleaner!
  2. from turtle import Screen, Turtle
  3. from random import randint
  4.  
  5. DELAY = 100 # Milliseconds
  6.  
  7.  
  8. def up():
  9.     global direction
  10.     direction = 'up'
  11.     turtle.setheading(90)
  12.  
  13. def down():
  14.     global direction
  15.     direction = 'down'
  16.     turtle.setheading(270)
  17.  
  18. def left():
  19.     global direction
  20.     direction = 'left'
  21.     turtle.setheading(180)
  22.  
  23. def right():
  24.     global direction
  25.     direction = 'right'
  26.     turtle.setheading(0)
  27.  
  28. def move():
  29.     global direction
  30.  
  31.     x, y = old_position = turtle.position()
  32.  
  33.     if direction == 'up':
  34.         turtle.sety(y + 20)
  35.     elif direction == 'down':
  36.         turtle.sety(y - 20)
  37.     elif direction == 'left':
  38.         turtle.setx(x - 20)
  39.     elif direction == 'right':
  40.         turtle.setx(x + 20)
  41.  
  42.     # Basically saying if the direction of turtle is up
  43.     # Then y is equal to our y coordinates which is up
  44.     # Saying the direction of our turtle that ycor is going up 20 pixles
  45.  
  46.  
  47.     if direction != 'stop':
  48.  
  49.         if sections:
  50.             if any(turtle.distance(section) < 20 for section in sections):# Check for collison with the body sections
  51.                 turtle.goto(0, 0)#Return to middle of screen once it colldies with it's self
  52.                 direction = 'stop'
  53.                 # Hide the sections
  54.                 for section in sections:
  55.                     section.hideturtle()# Once it body colldies with it's self body parts will hide then clear
  56.                 # Clear the section list
  57.                 sections.clear()#will clear our list of body parts
  58.             else:
  59.                 last_position = sections[-1].position()
  60.                 # Move the sections in Reverse order
  61.                 if len(sections) > 1:
  62.                     for index in range(len(sections) - 1, 0, -1):
  63.                         sections[index].goto(sections[index - 1].position())
  64.  
  65.                 sections[0].goto(old_position)
  66.  
  67.                 old_position = last_position
  68.  
  69.         if not (-390 < turtle.xcor() < 390 and -390 < turtle.ycor() < 390):
  70.             turtle.goto(0, 0)# Basically saying if i turn left while going right it make me restart from the middle of the screen or if i do it the other way around
  71.             direction = 'stop'
  72.  
  73.             for section in sections:
  74.                 section.hideturtle()
  75.  
  76.             sections.clear()
  77.  
  78.  
  79.         if turtle.distance(food) < 20:
  80.             food.goto(randint(-280, 280), randint(-280, 280))
  81.             # Adding sections
  82.             new_section = Turtle()# Turtle() is the same as turtle.Turtle()
  83.             new_section.shape('square')
  84.             new_section.speed(0)
  85.             new_section.color('orange', 'grey')
  86.             new_section.penup()
  87.             new_section.goto(old_position)
  88.             sections.append(new_section)# Adds new section of body at the end of body
  89.  
  90.             # Score
  91.             score = 0
  92.             high_score = 0
  93.  
  94.             # Increase the score
  95.             score =+ 10
  96.  
  97.             if score > high_score:
  98.                 high_score = score
  99.             pen.clear()
  100.             pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))
  101.  
  102.     screen.update()
  103.     screen.ontimer(move, DELAY)
  104. # Screen set up
  105. screen = Screen()
  106. screen.title("Snakebite mini Game by Rafa94")
  107. screen.bgcolor('brown')
  108. screen.setup(width=800, height=700)
  109. screen.tracer(0)
  110.  
  111. # Player
  112. turtle = Turtle()
  113. turtle.shape('turtle')
  114. turtle.speed(0)
  115. turtle.color('orange', 'grey')
  116. turtle.penup()
  117.  
  118. # Snake food
  119. food = Turtle()
  120. food.shape('circle')
  121. food.speed(0)
  122. food.color('red', 'yellow')
  123. food.penup()
  124. food.goto(randint(-280, 280), randint(-280, 280))
  125. #
  126. sections = []
  127.  
  128. # Pen/ Score board
  129. pen = Turtle()
  130. pen.speed()
  131. pen.shape("square")
  132. pen.color("white")
  133. pen.penup()
  134. pen.hideturtle()
  135. pen.goto(0, 260)
  136. pen.write("Score: 0 High Score: 0", align="center", font=("Courier", 24, "normal"))
  137.  
  138.  
  139. direction = 'stop'
  140. # Keyboard bindings
  141. screen.onkey(up, 'Up')
  142. screen.onkey(down, 'Down')
  143. screen.onkey(left, 'Left')
  144. screen.onkey(right, 'Right')
  145. screen.listen()
  146.  
  147. # main game loop
  148. move()
  149.  
  150. screen.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement