基于回合的运动算法

时间:2013-10-24 23:11:27

标签: python pygame

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

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))

1 个答案:

答案 0 :(得分:1)

因为我不知道你的想法,我将按照以下假设继续:

  • findTileLetter(x, y)告诉我(x, y)的方格是否合格。也就是说,它告诉我一个单位是否可以通过或结束他们在该广场上的转弯。
  • 每回合可以提升PLAYERDISTANCE[unit] + 1次。每个步骤都可以向上,向下,向左或向右。不允许使用对角线步骤,而是必须通过例如步进来完成向左和向上。

考虑到这一点,我们注意到了

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

你迭代currentNewSpots少于元素,因此每个x循环都会忽略currentNewSpots中添加的元素前一个x循环中的 last 。因此,您遗漏了潜在的目的地广场。

将行更改为

for y in range(len(currentNewSpots))

解决了这个问题。

此外,y循环中的delta-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))

随后是工作测试代码的Blob。 grid定义了拼贴世界:0拼贴无法通行,而1拼贴可以通过。 MY_XMY_Y定义了我们搜索的位置。在每一步之后,我们将地图输出到stdout,说明我们到目前为止找到的方块。

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