简体   繁体   English

玩家在python中奇形怪状的网格周围的运动

[英]Movement of a player around an oddly shaped grid in python

I am currently trying to move a player's position around a grid, for a small game. 我目前正尝试在小游戏中围绕网格移动玩家的位置。 The task is to move him the number of places that he rolls the 2 dice. 任务是移动他掷出2个骰子的位置数。 I've made the 2 dimensional array for the grid, and it works perfectly fine, as well as the rolling of the dice, but when the dice are doubles, he moves backwards, however I have no idea how to make him move around the grid as its shaped really oddly. 我已经为网格制作了二维数组,它的工作原理以及骰子的滚动都很完美,但是当骰子翻倍时,他向后移动,但是我不知道如何使他在骰子周围移动网格的形状真的很奇怪。 Any assistance would be greatly appreciated. 任何帮助将不胜感激。

what i have so far: 我到目前为止所拥有的:

grid = [[43,44,45,46,47,48,49],
        [42,41,40,39,38,37,36],
        [29,30,31,32,33,34,35],
        [28,27,26,25,24,23,22],
        [15,16,17,18,19,20,21],
        [14,13,12,11,10,9,8],
        [1,2,3,4,5,6,7]]

dice1 = randint(1,6)
dice2 = randint(1,6)
if dice1 == dice2:
    doubles = True

I would approach it by having a row and col position and function for moving forward and backwards. 我将通过具有行和列的位置以及向前和向后移动的功能来实现它。 Here is how I would do forward movement, I will leave backwards to you: 这是我前进的方式,我会向后退一步:

row = 6
col = 0
def forward(num):
    if row % 2 == 0: # move right to go forward for even row
        if num > 6-col:
            row -= 1
            col = 6
            return forward(num-(6-col))
        col += num
    else:
        if num > col:
            row -= 1
            col = 0
            return forward(num-(col+1))
        col -= num

You'll need to add checks for when they get to the final position. 您需要在支票到达最终位置时添加支票。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM