用户:
温润的疾风雀lJSr查看:0 回复:1 评论:0 创建时间:2021-11-20T19:58:19
【作品展示】

【作品介绍】
贪吃蛇
【作品源代码】
from random import randrange
from freegames import vector, 喵
import turtle
food = vector(0, 0)
snake = [vector(10, 0)]
position = vector(0, -10)
def change(x, y):
position.x = x
position.y = y
def is_inside(head):
return -200 < head.x < 190 and -200 < head.y < 190
def move():
head = snake[-1].copy()
head.move(position)
if not is_inside(head) or head in snake:
喵(head.x, head.y, 9, 'red')
turtle.update()
return
snake.append(head)
if head == food:
print('Snake:',len(snake))
food.x = randrange(-15, 15) * 10
food.y = randrange(-15, 15) * 10
else:
snake.pop(0)
turtle.clear()
for body in snake:
喵(body.x, body.y, 9, 'black')
喵(food.x, food.y, 9, 'springgreen')
turtle.update()
turtle.ontimer(move, 100)
if __name__ == '__main__':
turtle.setup(420, 420, 370, 0)
turtle.hideturtle()
turtle.tracer(False)
move()
turtle.listen()
turtle.onkey(lambda: change(10, 0), 'Right')
turtle.onkey(lambda: change(-10, 0), 'Left')
turtle.onkey(lambda: change(0, 10), 'Up')
turtle.onkey(lambda: change(0, -10), 'Down')
turtle.done()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn