基于转弯的运动算法

2024-05-26 17:44:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用pygame编程一个基于回合的策略,比如Fire-symbol,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))

Tags: andintestforifnotprepareappend
1条回答
网友
1楼 · 发布于 2024-05-26 17:44:11

因为我不知道你在想什么,我将在以下假设下继续:

  • findTileLetter(x, y)告诉我(x, y)处的正方形是否可以通过。也就是说,它告诉我一个单位是否可以通过或结束他们在广场上的回合。在
  • 单位每回合最多可步进PLAYERDISTANCE[unit] + 1次。每一步都可以上、下、左或右。不允许使用斜踏步,但必须通过向左和向上跨步来完成。在

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

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

迭代currentNewSpots的一个元素,因此每一个x循环都会忽略前面x中最后一个添加到{}循环中的元素的步进。因此,你忽略了潜在的目的地广场。在

换行

^{pr2}$

修复此问题。在

此外,您在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))

工作测试代码如下。grid定义了一个瓷砖的世界:0瓷砖是不可通行的,而1瓷砖是可以通行的。MY_X和{}定义了我们的搜索位置。在每一步之后,我们将地图输出到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

相关问题 更多 >

    热门问题