【发布时间】:2021-06-20 15:28:03
【问题描述】:
所以我正在尝试用python制作一个蛇游戏,我达到了蛇必须吃掉水果/点才能成长的地步。所以我做了两只乌龟。一个是蛇头,一个是必须吃的果子。除了我不知道怎么做,所以当蛇头龟碰到水果时,水果会随机到另一个位置。 这是我到目前为止的代码:
import turtle
import random
import time
points = 0
game_over = False
# Create map
screen = turtle.Screen()
screen.bgcolor('black')
# Create turtles :)
snake = turtle.Turtle()
snake.color('white')
snake.penup()
point_master = turtle.Turtle()
point_master.color('white')
point_master.penup()
snake.penup()
# Code snake so it can move :D
snake_speed = 2
def travel():
snake.forward(snake_speed)
screen.ontimer(travel, 10)
screen.onkey(lambda: snake.setheading(90), 'Up')
screen.onkey(lambda: snake.setheading(180), 'Left')
screen.onkey(lambda: snake.setheading(0), 'Right')
screen.onkey(lambda: snake.setheading(270), 'Down')
# Here is where we code it so when the snake touches the fruit the fruit will relocate.
# Continues to make snake move :)
screen.listen()
travel()
screen.mainloop()
【问题讨论】:
-
那么,你知道如何确定海龟的位置吗?你知道如何确定两点之间的距离吗?你真正的问题是什么?
-
我可以确定乌龟的位置。我试过这样的东西:snake.distance(point_master)
-
"我尝试过这样的事情:snake.distance(point_master)
-
当我运行它并检查水果时它什么也没做
-
好的,你有没有试过检查
snake.distance(point_master)返回的内容,在蛇离水果足够近的地方?你试过检查它是否真的被调用了吗?
标签: python python-3.x module