0

我正在使用 pygame 在 Python 中编写基于回合的策略,例如 Fire Emblem,但我遇到了玩家移动的问题。它的工作方式是您选择一个玩家,它会向您显示以蓝色突出显示的所有允许的移动,但我的问题是我在列出所有可能移动的列表时遇到问题。

def showPerson(tilex, tiley, personAtTile):
global ALLOWEDMOVES
ALLOWEDMOVES = []
prepare = {k:v for v,k in PLAYERSPOSITION.items()}
z = PLAYERDISTANCE[personAtTile]
#get all coords for the possible moves
currentNewSpots = []
oldSpots = []
a = PLAYERSPOSITION[personAtTile][0]
b = PLAYERSPOSITION[personAtTile][1]
c = a + 1
test = findTileLetter(c, b)
if test:
    currentNewSpots.append((c, b))
    ALLOWEDMOVES.append((c, b))
c = a -1
test = findTileLetter(c, b)
if test:
    currentNewSpots.append((c, b))
    ALLOWEDMOVES.append((c, b))
c = b + 1
test = findTileLetter(a, c)
if test:
    currentNewSpots.append((a, c))
    ALLOWEDMOVES.append((a, c))
c = b - 1
test = findTileLetter(a, c)
if test:
    currentNewSpots.append((a, c))
    ALLOWEDMOVES.append((a, c))

for x in range(PLAYERDISTANCE[prepare[(tilex, tiley)]]):
    for y in range(len(currentNewSpots) - 1):
        a = currentNewSpots[y][0]
        b = currentNewSpots[y][1]
        c = a + 1
        test = findTileLetter(c, b)
        if test and ((c, b)) not in ALLOWEDMOVES:
            currentNewSpots.append((c, b))
            ALLOWEDMOVES.append((c, b))
        c = a -1
        test = findTileLetter(c, b)
        if test and ((c, b)) not in ALLOWEDMOVES:
            currentNewSpots.append((c, b))
            ALLOWEDMOVES.append((c, b))
        c = b + 1
        test = findTileLetter(a, c)
        if test and ((c, b)) not in ALLOWEDMOVES:
            currentNewSpots.append((a, c))
            ALLOWEDMOVES.append((a, c))
        c = b - 1
        test = findTileLetter(a, c)
        if test and ((c, b)) not in ALLOWEDMOVES:
            currentNewSpots.append((a, c))
            ALLOWEDMOVES.append((a, c))
4

1 回答 1

1

因为我不知道您的想法,所以我将在以下假设下进行:

  • findTileLetter(x, y)告诉我在的广场(x, y)是否可以通行。也就是说,它告诉我一个单位是否可以通过或结束他们在那个方格上的回合。
  • 单位PLAYERDISTANCE[unit] + 1每回合最多可以加倍。每个步骤都可以向上、向下、向左或向右。不允许对角阶梯,而是必须通过阶梯来完成,例如向左和向上。

考虑到这一点,我们注意到在线

for y in range(len(currentNewSpots) - 1):

您迭代的元素比您应该currentNewSpots的一个元素,因此x您省略的每个循环都从上一个循环中最后添加currentNewSpots的元素开始。因此,您忽略了潜在的目标方格。x

将线路更改为

for y in range(len(currentNewSpots))

修复了这个问题。

此外,循环中的 delta-y 测试y并不完全正确:

    c = b + 1
    test = findTileLetter(a, c)
    if test and ((c, b)) not in ALLOWEDMOVES: ### <--- should be (a, c)
        currentNewSpots.append((a, c))
        ALLOWEDMOVES.append((a, c))

工作测试代码块如下。定义了一个瓷砖的grid世界:0瓷砖是不可通行的,而1瓷砖是可以通行的。MY_XMY_Y定义我们从哪里搜索。在每一步之后,我们将地图输出到标准输出,说明到目前为止我们找到了哪些方块。

import sys

MY_X = 3
MY_Y = 4
MY_RNG = 2

grid = [
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
    [0, 0, 1, 1, 1, 1, 0, 0, 1, 0],
    [0, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [0, 0, 1, 1, 0, 1, 1, 0, 0, 1],
    [0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
    [0, 0, 0, 0, 1, 1, 1, 1, 1, 0],
    [0, 0, 0, 1, 1, 1, 1, 0, 0, 0],
    [0, 1, 1, 1, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]

def findTileLetter(x, y):
    return grid[y][x]

class Person:
    pass

def showMap():
    for y in range(len(grid)):
        for x in range(len(grid[y])):
            if grid[y][x] == 0:
                sys.stdout.write(' ')
            elif x == MY_X and y == MY_Y:
                sys.stdout.write('x')
            elif (x, y) in ALLOWEDMOVES:
                sys.stdout.write('o')
            else:
                sys.stdout.write('-')
        sys.stdout.write('\n')

me = Person()

ALLOWEDMOVES = []

PLAYERDISTANCE = {}
PLAYERDISTANCE[me] = MY_RNG

PLAYERSPOSITION = {}
PLAYERSPOSITION[me] = (MY_X, MY_Y)

def showPerson(tilex, tiley, personAtTile):
    global ALLOWEDMOVES
    ALLOWEDMOVES = []
    prepare = {k:v for v,k in PLAYERSPOSITION.items()}
    z = PLAYERDISTANCE[personAtTile]
    #get all coords for the possible moves
    currentNewSpots = []
    oldSpots = []
    a = PLAYERSPOSITION[personAtTile][0]
    b = PLAYERSPOSITION[personAtTile][1]
    c = a + 1
    test = findTileLetter(c, b)
    if test:
        currentNewSpots.append((c, b))
        ALLOWEDMOVES.append((c, b))
    c = a -1
    test = findTileLetter(c, b)
    if test:
        currentNewSpots.append((c, b))
        ALLOWEDMOVES.append((c, b))
    c = b + 1
    test = findTileLetter(a, c)
    if test:
        currentNewSpots.append((a, c))
        ALLOWEDMOVES.append((a, c))
    c = b - 1
    test = findTileLetter(a, c)
    if test:
        currentNewSpots.append((a, c))
        ALLOWEDMOVES.append((a, c))

    showMap()

    for x in range(PLAYERDISTANCE[prepare[(tilex, tiley)]]):
        for y in range(len(currentNewSpots)):
            a = currentNewSpots[y][0]
            b = currentNewSpots[y][1]
            c = a + 1
            test = findTileLetter(c, b)
            if test and ((c, b)) not in ALLOWEDMOVES:
                currentNewSpots.append((c, b))
                ALLOWEDMOVES.append((c, b))
            c = a - 1
            test = findTileLetter(c, b)
            if test and ((c, b)) not in ALLOWEDMOVES:
                currentNewSpots.append((c, b))
                ALLOWEDMOVES.append((c, b))
            c = b + 1
            test = findTileLetter(a, c)
            if test and ((a, c)) not in ALLOWEDMOVES:
                currentNewSpots.append((a, c))
                ALLOWEDMOVES.append((a, c))
            c = b - 1
            test = findTileLetter(a, c)
            if test and ((a, c)) not in ALLOWEDMOVES:
                currentNewSpots.append((a, c))
                ALLOWEDMOVES.append((a, c))
        showMap()

showPerson(MY_X, MY_Y, me)
print ALLOWEDMOVES
于 2013-10-24T23:50:01.863 回答