Snake on Python 使用 Turtle 模块
Snake on Python Using the Turtle Module
我正在尝试使用 Turtle 模块自己制作蛇。考虑到我几乎没有编码经验,我已经走了很远。我目前正在尝试在吃完一块食物后将 body 添加到蛇身上。我做了这样当你吃一块食物时,一块body会出现,但我不知道怎么做,所以body会跟随蛇的头.
谢谢!
import turtle
import random
turtle.title("snake?")
turtle.getscreen()
turtle.setup(500, 520)
#character
player = turtle
player.shape("square")
player.bgcolor("lightgreen")
player.color("red")
player.penup()
player.goto(0, 0)
player.speed(0)
player.dy = -1
player.dx = 0
#definng how to move the player
move = 10
turn = 90
def w():
player.dx *= 0
player.dy = 2
def a():
player.dy *= 0
player.dx = -2
def s():
player.dx *= 0
player.dy = -2
def d():
player.dy *= 0
player.dx = 2
def moving():
turtle.onkey(w, "w")
turtle.onkey(a, "a")
turtle.onkey(s, "s")
turtle.onkey(d, "d")
#the food
x = 10 * random.randint(0, 20)
y = 10 * random.randint(0, 20)
food = player.clone()
food.shape("square")
food.color("black")
food.goto(x, y)
score = 1
#the test to see if the player touches the block as well
#as actually moving the player
while True:
player.sety(player.ycor() + player.dy)
player.setx(player.xcor() + player.dx)
if player.xcor() < -235:
break
if player.xcor() > 234:
break
if player.ycor() < -243:
break
if player.ycor() > 245:
break
if player.distance(food) < 20:
x = 10 * random.randint(-20, 20)
y = 10 * random.randint(-20, 20)
food.goto(x, y)
score = score + 1
body = turtle.Turtle()
body.speed(0)
body.shape("square")
body.color("red")
body.penup()
moving()
turtle.listen()
turtle.update()
print("Score: " + str(score))
试试这个:
import turtle
import random
turtle.title("snake?")
turtle.getscreen()
turtle.setup(500, 520)
#character
player = turtle
player.shape("square")
player.bgcolor("lightgreen")
player.color("red")
player.penup()
player.goto(0, 0)
player.speed(0)
player.dy = 0
player.dx = 10
#definng how to move the player
move = 10
turn = 90
def w():
player.dx *= 0
player.dy = 10
def a():
player.dy *= 0
player.dx = -10
def s():
player.dx *= 0
player.dy = -10
def d():
player.dy *= 0
player.dx = 10
def moving():
turtle.onkey(w, "w")
turtle.onkey(a, "a")
turtle.onkey(s, "s")
turtle.onkey(d, "d")
#the food
x = 10 * random.randint(0, 20)
y = 10 * random.randint(0, 20)
food = player.clone()
food.shape("square")
food.color("black")
food.goto(x, y)
score = 1
bodyparts = []
def move_body():
for position in range(len(bodyparts)-1,0,-1):
x = bodyparts[position-1].xcor()
y = bodyparts[position-1].ycor()
bodyparts[position].goto(x,y)
if len(bodyparts) > 0:
x = player.xcor()
y = player.ycor()
bodyparts[0].goto(x,y)
#the test to see if the player touches the block as well
#as actually moving the player
while True:
player.sety(player.ycor() + player.dy)
player.setx(player.xcor() + player.dx)
if player.xcor() < -235:
break
if player.xcor() > 234:
break
if player.ycor() < -243:
break
if player.ycor() > 245:
break
if player.distance(food) < 20:
x = 10 * random.randint(-20, 20)
y = 10 * random.randint(-20, 20)
food.goto(x, y)
score = score + 1
body = turtle.Turtle()
body.speed(0)
body.shape("square")
body.color("red")
body.penup()
bodyparts.append(body)
move_body()
moving()
turtle.listen()
turtle.update()
print("Score: " + str(score))
我正在尝试使用 Turtle 模块自己制作蛇。考虑到我几乎没有编码经验,我已经走了很远。我目前正在尝试在吃完一块食物后将 body 添加到蛇身上。我做了这样当你吃一块食物时,一块body会出现,但我不知道怎么做,所以body会跟随蛇的头.
谢谢!
import turtle
import random
turtle.title("snake?")
turtle.getscreen()
turtle.setup(500, 520)
#character
player = turtle
player.shape("square")
player.bgcolor("lightgreen")
player.color("red")
player.penup()
player.goto(0, 0)
player.speed(0)
player.dy = -1
player.dx = 0
#definng how to move the player
move = 10
turn = 90
def w():
player.dx *= 0
player.dy = 2
def a():
player.dy *= 0
player.dx = -2
def s():
player.dx *= 0
player.dy = -2
def d():
player.dy *= 0
player.dx = 2
def moving():
turtle.onkey(w, "w")
turtle.onkey(a, "a")
turtle.onkey(s, "s")
turtle.onkey(d, "d")
#the food
x = 10 * random.randint(0, 20)
y = 10 * random.randint(0, 20)
food = player.clone()
food.shape("square")
food.color("black")
food.goto(x, y)
score = 1
#the test to see if the player touches the block as well
#as actually moving the player
while True:
player.sety(player.ycor() + player.dy)
player.setx(player.xcor() + player.dx)
if player.xcor() < -235:
break
if player.xcor() > 234:
break
if player.ycor() < -243:
break
if player.ycor() > 245:
break
if player.distance(food) < 20:
x = 10 * random.randint(-20, 20)
y = 10 * random.randint(-20, 20)
food.goto(x, y)
score = score + 1
body = turtle.Turtle()
body.speed(0)
body.shape("square")
body.color("red")
body.penup()
moving()
turtle.listen()
turtle.update()
print("Score: " + str(score))
试试这个:
import turtle
import random
turtle.title("snake?")
turtle.getscreen()
turtle.setup(500, 520)
#character
player = turtle
player.shape("square")
player.bgcolor("lightgreen")
player.color("red")
player.penup()
player.goto(0, 0)
player.speed(0)
player.dy = 0
player.dx = 10
#definng how to move the player
move = 10
turn = 90
def w():
player.dx *= 0
player.dy = 10
def a():
player.dy *= 0
player.dx = -10
def s():
player.dx *= 0
player.dy = -10
def d():
player.dy *= 0
player.dx = 10
def moving():
turtle.onkey(w, "w")
turtle.onkey(a, "a")
turtle.onkey(s, "s")
turtle.onkey(d, "d")
#the food
x = 10 * random.randint(0, 20)
y = 10 * random.randint(0, 20)
food = player.clone()
food.shape("square")
food.color("black")
food.goto(x, y)
score = 1
bodyparts = []
def move_body():
for position in range(len(bodyparts)-1,0,-1):
x = bodyparts[position-1].xcor()
y = bodyparts[position-1].ycor()
bodyparts[position].goto(x,y)
if len(bodyparts) > 0:
x = player.xcor()
y = player.ycor()
bodyparts[0].goto(x,y)
#the test to see if the player touches the block as well
#as actually moving the player
while True:
player.sety(player.ycor() + player.dy)
player.setx(player.xcor() + player.dx)
if player.xcor() < -235:
break
if player.xcor() > 234:
break
if player.ycor() < -243:
break
if player.ycor() > 245:
break
if player.distance(food) < 20:
x = 10 * random.randint(-20, 20)
y = 10 * random.randint(-20, 20)
food.goto(x, y)
score = score + 1
body = turtle.Turtle()
body.speed(0)
body.shape("square")
body.color("red")
body.penup()
bodyparts.append(body)
move_body()
moving()
turtle.listen()
turtle.update()
print("Score: " + str(score))