【发布时间】:2019-05-03 20:30:43
【问题描述】:
我正在尝试用 python 中的海龟制作蛇游戏。 我想要做的是当蛇进入圈子时,蛇会变长。
我也在尝试每次使用随机模块让圆圈进入不同的位置,我不希望蛇离开屏幕。
from turtle import Turtle, Screen
import random
wn = Screen()
wn.bgcolor('lightblue')
snake = Turtle()
snake.shape("square")
snake.shapesize(1, 1, 1)
snake.color('red')
snake.penup()
speed = 3
circle = Turtle()
circle.shape("circle")
circle.shapesize(0.8, 0.8, 0.8)
circle.color('blue')
def travel():
snake.forward(speed)
wn.ontimer(travel, 10)
#controls for using arrows with keyboard
wn.onkey(lambda: snake.setheading(90), 'Up')
wn.onkey(lambda: snake.setheading(180), 'Left')
wn.onkey(lambda: snake.setheading(0), 'Right')
wn.onkey(lambda: snake.setheading(270), 'Down')
wn.listen()
travel()
wn.mainloop()
【问题讨论】:
标签: python python-3.x turtle-graphics