from random import randint
import turtle
wn = turtle.Screen()
if snake.xcor and snake.ycor == apple.xcor and apple.ycor:
food = []
Main_Score += 1
pen.write("score:{}".format(Main_Score), align="center", font=("courier", 24, "normal"))
while food == []:
food = [randint(1, 18), randint(1, 58)]
if food in snake: food == []
wn.addch(food[0], food[1], '*')
我看了其他一些问题,由于值“ snake”不支持成员资格测试,因此if food in snake: food == []
行不起作用
答案 0 :(得分:0)
如果要比较位置,则应使用
food == snake.pos()
类似
snake.pos() == apple.pos()
或
(snake.xcor(), snake.ycor()) == (apple.xcor(), apple.ycor())
您的专线
if snake.xcor and snake.ycor == apple.xcor and apple.ycor:
是错误的,因为您在()
,xcor()
中忘记了ycor()
,因为这意味着
if (snake.xcor) and (snake.ycor == apple.xcor) and (apple.ycor):
所以and
不能正常工作。
代替
while food == []:
你可以写
while not food: