Making snake in Python, can't get bodies to follow head etc

拈花ヽ惹草 提交于 2020-01-06 06:07:33

问题


I wanted to make a snake game in turtle but I can't seem to get the tail segments to follow the leader. As if to say I can't get the first body part to follow the head, and then the second body part to follow the first body, etc.

I tried using what maths I have but I can't seem to get it to calculate where the head just was or where the body in front of it just was.

here is my code:

    #libraries
import turtle
import random
import math

#screen
the_screen = turtle.Screen()
the_screen.bgcolor("lightgreen")
the_screen.title("Catapillar")

#border
border_writer = turtle.Turtle()
border_writer.speed(0)
border_writer.color("green")
border_writer.penup()
border_writer.setposition(-275, -275)
border_writer.pendown()
border_writer.pensize(7)
for side in range(4):
    border_writer.fd(550)
    border_writer.lt(90)
border_writer.hideturtle()

#player
player = turtle.Turtle()
player.color("yellow")
player.shape("circle")
player.penup()
player.speed(0)
player.setposition(0,0)
player.setheading(0)
playerspeed = 2

#fruit
fruit = 1
fruit = turtle.Turtle()
fruit.color("red")
fruit.shape("circle")
fruit.penup()
fruit.speed(0)
fruit.shapesize(0.6, 0.6)
x = random.randint(-200, 200)
y = random.randint(100, 250)
fruit.setposition(x, y) 

#moving and collision checker
def turnleft():
    player.left(24.5)
def turnright():
    player.right(24.5)  
def increasespeed():
    global playerspeed
    playerspeed += 1
def isCollision(t1, t2):
    distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(), 2) + math.pow(t1.ycor()-t2.ycor(), 2))
    if distance < 24:
        return True
    else:
        return False    

    #making the tail(s) and hiding them
tail1 = turtle.Turtle()
tail1.hideturtle()
tail1.color("yellow")
tail1.shape("circle")
tail1.penup()
tail1.speed(0)
tail1.setposition(+700,700)
tail1.shapesize(0.6, 0.6)
tail1state = 'off'

#key presses
turtle.listen()
turtle.onkeypress(turnleft, "Left")
turtle.onkeypress(turnright, "Right")
turtle.onkeypress(increasespeed, "Up")  

#main loop player always moves forward, out of bound set, reset fruit 
     #randomly when collided, create tail1...
while True:
    player.forward(playerspeed)
               #gameovers'
    if (player.xcor() > 275) or (player.xcor() < -275):
        playerspeed += 7
        print("GAME OVER")
    if (player.ycor() > 275) or (player.ycor() < -275):
        playerspeed += 7
        print("GAME OVER")
              #collision check between fruit and head
    if isCollision(player, fruit):
        #resets the fruit, moves it randomly
        fruit.hideturtle()
        if tail1state == 'off':
            uuu = player.xcor()
            vvv = player.ycor()
            tail1.setposition(uuu,vvv)
            tail1.showturtle()
            tail1state = 'on'
        #reset the fruit
        x = random.randint(-200, 200)
        y = random.randint(-100, 250)
        fruit.setposition(x, y) 
        fruit.showturtle()
        #playerspeed +=1

If you have any idea on how you think I should approach it, please let me know what you think.

Thank you.


回答1:


Since this comes up over and over (but effort to effect it is rarely shown) here's my minimal, generic, slithering around a screen effect that you should be able to adapt for your own purposes:

from turtle import Turtle, Screen

SNAKE_LENGTH = 10
SNAKE_SIZE = 15
WIDTH, HEIGHT = 375, 375
CURSOR_SIZE = 20

def slither():
    segment = snake[-1].clone() if len(snake) < SNAKE_LENGTH else snake.pop(0)

    screen.tracer(False)  # segment.hideturtle()
    segment.setposition(snake[-1].position())
    segment.setheading(snake[-1].heading())
    segment.forward(SNAKE_SIZE)

    while not -WIDTH/2 < segment.xcor() < WIDTH/2 or not -HEIGHT/2 < segment.ycor() < HEIGHT/2:
        segment.undo()
        segment.right(95)
        segment.forward(SNAKE_SIZE)

    screen.tracer(True)  # segment.showturtle()
    snake.append(segment)

    screen.ontimer(slither, 100)

boundary = Turtle(shape='square', visible=False)
boundary.shapesize(HEIGHT / CURSOR_SIZE, WIDTH / CURSOR_SIZE)
boundary.fillcolor("white")
boundary.stamp()

head = Turtle("circle")
head.shapesize(SNAKE_SIZE / CURSOR_SIZE)
head.penup()

snake = [head]

screen = Screen()

slither()

screen.exitonclick()



来源:https://stackoverflow.com/questions/50295255/making-snake-in-python-cant-get-bodies-to-follow-head-etc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!