在 Python 中制作蛇,无法让身体跟随头部等 [英] Making snake in Python, can't get bodies to follow head etc

查看:26
本文介绍了在 Python 中制作蛇,无法让身体跟随头部等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在乌龟中做一个蛇游戏,但我似乎无法让尾段跟随领导者.好像说我不能让第一个身体部位跟随头部,然后第二个身体部位跟随第一个身体,依此类推.

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.

这是我的代码:

    #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.

谢谢.

推荐答案

由于这个问题反复出现(但很少展示实现它的努力)这里是我的最小的、通用的、滑动的屏幕效果,你应该能够适应您自己的目的:

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()

这篇关于在 Python 中制作蛇,无法让身体跟随头部等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆