import turtle
import time
import random

camara = turtle.Screen()
camara.title('bienvenido al juego de la serpiente.')
camara.setup(width=600, height=600)
camara.bgcolor("blue")
score = 0
total_score = 0
#texto
text = turtle.Turtle()

text.penup()
text.hideturtle()
text.color('red')
text.speed(0)
text.setpos(0, 250)
text.write(f'actual score {score} total score {total_score}', move=False, align='center', font=('candara', 24, 'bold'))

#cabeza

head = turtle.Turtle()

head.speed(1.4)

head.direction = 'stop'
head.shape('square')
head.shapesize(stretch_wid=1.1, stretch_len=1.1, outline=None)
head.color('red')
head.penup()
head.setpos(0, 0)

def arriba():
    while head.direction != 'down':
        head.direction = 'up'
        y = head.ycor()
        head.sety(y + 20)

def abajo():
    while head.direction != 'up':
        head.direction = 'down'
        y = head.ycor()
        head.sety(y - 20)

def izquierda():
    while head.direction != 'right':
        head.direction = 'left'
        x = head.xcor()
        head.setx(x - 20)

def derecha():
    while head.direction != 'left':
        head.direction = 'right'
        x = head.xcor()
        head.setx(x + 20)


camara.listen()
camara.onkeypress(arriba, 'w')
camara.onkeypress(izquierda, 'a')
camara.onkeypress(abajo, 's')
camara.onkeypress(derecha, 'd')
#food

food = turtle.Turtle()
food.penup()
food.shape('square')
food.speed(0)
x1 = random.randint(-270, 270)
y1 = random.randint(-270, 270)
food.setpos(x1, y1)

if food.distance(head) < 20:
    food.penup()
    food.shape('square')
    food.speed(0)
    x1 = random.randint(-270, 270)
    y1 = random.randint(-270, 270)
    food.setpos(x1, y1)

camara.mainloop()


大家好,我正在试着做一个《蛇的游戏》,我做了一个算法,当蛇接触到食物时,食物消失,然后再次出现在 map 上的随机位置,但不知何故,它似乎不起作用,因为当蛇接触食物时,食物仍然保持在相同的位置,所以在那之后,我做了这个算法(添加一个While True):

food = turtle.Turtle()
while True:
    food.penup()
    food.shape('square')
    food.speed(0)
    x1 = random.randint(-270, 270)
    y1 = random.randint(-270, 270)
    food.setpos(x1, y1)

为了弄清楚食物是否随机地看起来足够好,确实是这样,但当我开始移动蛇头时,循环停止运行,(蛇继续移动),食物保持不变.我是说... 当我执行这些代码行时:

def arriba():
    while head.direction != 'down':
        head.direction = 'up'
        y = head.ycor()
        head.sety(y + 20)

def abajo():
    while head.direction != 'up':
        head.direction = 'down'
        y = head.ycor()
        head.sety(y - 20)

def izquierda():
    while head.direction != 'right':
        head.direction = 'left'
        x = head.xcor()
        head.setx(x - 20)

def derecha():
    while head.direction != 'left':
        head.direction = 'right'
        x = head.xcor()
        head.setx(x + 20)


camara.listen()
camara.onkeypress(arriba, 'w')
camara.onkeypress(izquierda, 'a')
camara.onkeypress(abajo, 's')
camara.onkeypress(derecha, 'd')

可以这么说,代码的其余部分停止运行. 那么这里的问题是什么呢?我应该怎么做才能让食物在每次蛇接触食物时都随机出现?谢谢.

推荐答案

碰撞判断只在游戏开始时进行一次,而不是在每一帧上.

您可以将逻辑放入函数中,并在每一步的每个移动循环中调用它:

def eat_food_if_close():
    if food.distance(head) < 20:
        food.penup()
        food.shape('square')
        food.speed(0)
        x1 = random.randint(-270, 270)
        y1 = random.randint(-270, 270)
        food.setpos(x1, y1)

呼叫每个方向,如下所示:

def derecha():
    while head.direction != 'left':
        head.direction = 'right'
        x = head.xcor()
        head.setx(x + 20)
        eat_food_if_close() # <---

另一种可能的解决方案是使用ontimer触发测试,比如每30毫秒运行一次:

# ...

def tick():
    camara.ontimer(tick, 30)

    if food.distance(head) < 20:
        food.penup()
        food.shape('square')
        food.speed(0)
        x1 = random.randint(-270, 270)
        y1 = random.randint(-270, 270)
        food.setpos(x1, y1)

tick()
camara.mainloop()  # blocks forever, don't add code below this

如果你禁用内部的Turtle循环,并使用ontimer循环来运行turtle.update()旁边的所有东西,这款应用可能会运行得更流畅.例如here,但这可能是一个太大的重构,现在还不能强迫您进入,所以我将它作为一个概念保留下来.

Python相关问答推荐

使用FASTCGI在IIS上运行Django频道

Python daskValue错误:无法识别的区块管理器dask -必须是以下之一:[]

为什么sys.exit()不能与subproccess.run()或subprocess.call()一起使用

如何在WSL2中更新Python到最新版本(3.12.2)?

什么是最好的方法来切割一个相框到一个面具的第一个实例?

使用Python和文件进行模糊输出

如何从列表框中 Select 而不出错?

在pandas数据框中计算相对体积比指标,并添加指标值作为新列

try 检索blob名称列表时出现错误填充错误""

如何在达到end_time时自动将状态字段从1更改为0

如何检测鼠标/键盘的空闲时间,而不是其他输入设备?

在Docker容器(Alpine)上运行的Python应用程序中读取. accdb数据库

根据Pandas中带条件的两个列的值创建新列

在我融化极点数据帧之后,我如何在不添加索引的情况下将其旋转回其原始形式?

多个矩阵的张量积

Pandas:将值从一列移动到适当的列

遍历列表列表,然后创建数据帧

具有不匹配列的2D到3D广播

#将多条一维曲线计算成其二维数组(图像)表示

使用Django标签显示信息