Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Importing from turtle import Screen, Turtle saves time and make code look cleaner!
- from turtle import Screen, Turtle
- from random import randint
- DELAY = 100 # Milliseconds
- def up():
- global direction
- direction = 'up'
- turtle.setheading(90)
- def down():
- global direction
- direction = 'down'
- turtle.setheading(270)
- def left():
- global direction
- direction = 'left'
- turtle.setheading(180)
- def right():
- global direction
- direction = 'right'
- turtle.setheading(0)
- def move():
- global direction
- x, y = old_position = turtle.position()
- if direction == 'up':
- turtle.sety(y + 20)
- elif direction == 'down':
- turtle.sety(y - 20)
- elif direction == 'left':
- turtle.setx(x - 20)
- elif direction == 'right':
- turtle.setx(x + 20)
- # Basically saying if the direction of turtle is up
- # Then y is equal to our y coordinates which is up
- # Saying the direction of our turtle that ycor is going up 20 pixles
- if direction != 'stop':
- if sections:
- if any(turtle.distance(section) < 20 for section in sections):# Check for collison with the body sections
- turtle.goto(0, 0)#Return to middle of screen once it colldies with it's self
- direction = 'stop'
- # Hide the sections
- for section in sections:
- section.hideturtle()# Once it body colldies with it's self body parts will hide then clear
- # Clear the section list
- sections.clear()#will clear our list of body parts
- else:
- last_position = sections[-1].position()
- # Move the sections in Reverse order
- if len(sections) > 1:
- for index in range(len(sections) - 1, 0, -1):
- sections[index].goto(sections[index - 1].position())
- sections[0].goto(old_position)
- old_position = last_position
- if not (-390 < turtle.xcor() < 390 and -390 < turtle.ycor() < 390):
- 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
- direction = 'stop'
- for section in sections:
- section.hideturtle()
- sections.clear()
- if turtle.distance(food) < 20:
- food.goto(randint(-280, 280), randint(-280, 280))
- # Adding sections
- new_section = Turtle()# Turtle() is the same as turtle.Turtle()
- new_section.shape('square')
- new_section.speed(0)
- new_section.color('orange', 'grey')
- new_section.penup()
- new_section.goto(old_position)
- sections.append(new_section)# Adds new section of body at the end of body
- # Score
- score = 0
- high_score = 0
- # Increase the score
- score =+ 10
- if score > high_score:
- high_score = score
- pen.clear()
- pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))
- screen.update()
- screen.ontimer(move, DELAY)
- # Screen set up
- screen = Screen()
- screen.title("Snakebite mini Game by Rafa94")
- screen.bgcolor('brown')
- screen.setup(width=800, height=700)
- screen.tracer(0)
- # Player
- turtle = Turtle()
- turtle.shape('turtle')
- turtle.speed(0)
- turtle.color('orange', 'grey')
- turtle.penup()
- # Snake food
- food = Turtle()
- food.shape('circle')
- food.speed(0)
- food.color('red', 'yellow')
- food.penup()
- food.goto(randint(-280, 280), randint(-280, 280))
- #
- sections = []
- # Pen/ Score board
- pen = Turtle()
- pen.speed()
- pen.shape("square")
- pen.color("white")
- pen.penup()
- pen.hideturtle()
- pen.goto(0, 260)
- pen.write("Score: 0 High Score: 0", align="center", font=("Courier", 24, "normal"))
- direction = 'stop'
- # Keyboard bindings
- screen.onkey(up, 'Up')
- screen.onkey(down, 'Down')
- screen.onkey(left, 'Left')
- screen.onkey(right, 'Right')
- screen.listen()
- # main game loop
- move()
- screen.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement