-1

我试图重制游戏 Snake(在 Python 中)只是为了好玩,但我遇到了一个错误,无法弄清楚如何修复它。蛇头动作正确,能吃到食物,又给身体添了一块新的。然而,身体并没有正确地跟随。

我使用http://www.codeskulptor.org/进行编程,因为我是学生,我们无法在学校计算机上下载东西。

代码在运行时不会出现任何错误,但是当你吃东西时,身体会被创建,但只是被扔到头顶或消失。

我不确定我的代码中的问题出在哪里。首先,当你吃东西时,代码会运行 eat 函数,它会在蛇的相反方向上附加一块。然后,如果时间正确,则运行移动功能。这从蛇的末端一直延伸到前面,并使一块等于它前面的一块。然后,它向前移动头部,所以我不确定身体会发生什么。

有人可以帮我吗?

import simplegui, time, random

startTime = False
direction = None
snake = [[212.5, 212.5]]

def draw(canvas):
    global snake, food
    canvas.draw_polygon([(food[0] - 11, food[1] - 11), (food[0] + 11, food[1] - 11), (food[0] + 11, food[1] + 11), (food[0] - 11, food[1] + 11)], 1, 'red', 'red')
    for coords in snake:
        canvas.draw_polygon([(coords[0] - 11, coords[1] - 11), (coords[0] + 11, coords[1] - 11), (coords[0] + 11, coords[1] + 11), (coords[0] - 11, coords[1] + 11)], 1, 'green', 'green')
    if (snake[0] == food):
        eat()
    if (not startTime and direction != None):
        move()
    else:
        if (time.time() - startTime > 0.19 and time.time() - startTime < 0.23):
            move()

def key_handler(key):
    if (key == 37):
        left()
    elif (key == 38):
        up()
    elif (key == 39):
        right()
    elif (key == 40):
        down()

def move():
    global snake, direction, startTime
    for x in range(len(snake) - 1, 0, -1):
        snake[x] = snake[x-1]
    if (direction[0] == 0):
        snake[0][0] += direction[1]
    else:
        snake[0][1] += direction[1]
    startTime = time.time()

def eat():
    global snake, direction, food
    food = relocate()
    if (direction[0] == 0):
        snake.append([snake[len(snake) - 1][0] - direction[1], snake[len(snake) - 1][1]])
    else:
        snake.append([snake[len(snake) - 1][0], snake[len(snake) - 1][1] - direction[1]])

def relocate():
    global snake
    location = [random.randrange(13, 413, 25) - .5, random.randrange(13, 413, 25) - .5]
    for coords in snake:
        if (location == coords):
            location = relocate()
            break
    return location

def left():
    global direction
    direction = [0, -25]

def up():
    global direction
    direction = [1, -25]


def right():
    global direction
    direction = [0, 25]

def down():
    global direction
    direction = [1, 25]

food = relocate()

frame = simplegui.create_frame('Snake', 425, 425)
frame.set_keydown_handler(key_handler)
frame.set_draw_handler(draw)
frame.start()

更新:Codeskulptor 有这种称为 Viz 模式的模式,您可以在其中逐步运行您的代码。使用此代码:

snake = [[212.5, 212.5], [212.5, 237.5]]
direction = [0, 25]
for x in range(len(snake) - 1, 0, -1):
    snake[x] = snake[x-1]
if (direction[0] == 0):
    snake[0][0] += direction[1]
else:
    snake[0][1] += direction[1]
print (snake)

在 viz 模式下显示,当身体向前移动到头部时,它们成为同一个列表,所以当你移动头部时,它会将身体设置到头部的位置,因为它们是相同的数组。这是我的代码中的问题还是这只是 Codedeskulptor 的运行方式?

4

2 回答 2

0

问题是头部作为列表存储在内存中,然后主体也设置为该列表(相同的内存插槽),或者至少这就是 Codeskulptor 正在做的事情?因此,我没有将身体位置设置为头部位置,而是将身体 x 设置为头部 x,身体 y 设置为头部 y,并且它起作用了。

于 2019-09-13T20:12:24.357 回答
0

我做了一些更正: https ://py3.codeskulptor.org/#user304_7fINP16ZAD_0.py

主要错误是重复使用相同的坐标列表。蛇是坐标列表的列表。当您复制(坐标)列表时,它是相同的列表,而不是副本。所以你必须明确地复制它。就像我在上面代码的第 33 行中所做的那样。

于 2019-09-14T11:50:42.243 回答