如何在生成的板上移动角色?

时间:2018-12-03 20:06:56

标签: python python-3.x

几天前,开始制作我的简单棋盘游戏。首先,我为游戏创建一个董事会。看起来像这样: the gameboard generated for 13x13

其次,我将角色放置在板上,即“ A”: The player placed

我为此做了一个骰子,它会生成1到6之间的数字。

我现在的目标是使“ A”字符在骰子上沿“ *”符号移动,直到到达左上角为止: I need to get here by the dice

这是我尝试的代码:

import math
import random

import os
board= []
def generator(boardsize):
    for row in range(boardsize+1):
            brow = []
            for column in range(boardsize+1):
                if row == column == 0:
                    brow.append(' ')
                elif row==0:
                    brow.append(str(column-1)[-1])
                elif column==0:
                    brow.append(str(row-1)[-1])
                elif ((math.ceil(boardsize/2)-1 )<= column) and(column <= (math.ceil(boardsize/2)+1)) or ((math.ceil(boardsize/2)-1 )<= row) and(row <= (math.ceil(boardsize/2)+1)):
                    if row == 1 or column == 1 or row == boardsize or column == boardsize: 
                        brow.append('*')

                    else:
                        if row == (math.ceil(boardsize/2)) and column == (math.ceil(boardsize/2)):
                            brow.append('X')
                        elif row == (math.ceil(boardsize/2)) or column == (math.ceil(boardsize/2)): 
                            brow.append('D')
                        else:
                            brow.append('*') 
                else:
                    brow.append(' ')
            board.append(brow)

    return board

def print_table(x):
    os.system('cls')
    for x in board:
        print(' '.join(x))
number_from_dice= []
def dice():
    min = 1
    max = 6
    x = random.randint(min, max)
    number_from_dice[:]= [x]
    return number_from_dice


def player1(x):
    generator(x)
    prev_char_y = 1
    prev_char_x = math.ceil(x/2)+1
    char_y= 1
    char_x= math.ceil(x/2)+1
    board[char_y][char_x] = "A"
    print_table(x)
    dice()
    f = number_from_dice[0]
    for i in range(f):
       if(char_y<x):
        if (board[char_y+1][char_x]) == '*':

            char_y= char_y +1
            board[char_y][char_x] = "A"
            board[prev_char_y][prev_char_x] = '*'
            prev_char_x = char_x
            prev_char_y = char_y
            print_table(x)
        else:
            if(char_x!=x):
                char_x2 = char_x
                if (board[char_y][char_x+1]=='*'):
                    char_x = char_x +1
                    board[char_y][char_x] = "A"
                    board[prev_char_y][prev_char_x] = '*'
                    prev_char_x = char_x
                    prev_char_y = char_y
                    print_table(x)
                else:
                    if (board[char_y+1][char_x]) == '*':
                                    char_y= char_y +1
                                    board[char_y][char_x] = "A"
                                    board[prev_char_y][prev_char_x] = '*'
                                    prev_char_x = char_x
                                    prev_char_y = char_y
                                    print_table(x)
            else:
                        if (board[char_y][char_x2-1]) == '*':
                                    char_x2 = char_x2 -1
                                    board[char_y][char_x2] = "A"
                                    board[prev_char_y][prev_char_x] = '*'
                                    prev_char_x = char_x2
                                    prev_char_y = char_y
                                    print_table(x)
                        else:
                            if (board[char_y+1][char_x2]) == '*':
                                    char_y = char_y +1
                                    board[char_y][char_x2] = "A"
                                    board[prev_char_y][prev_char_x] = '*'
                                    prev_char_x = char_x2
                                    prev_char_y = char_y
                                    print_table(x)












    print('Number from dice: ', end='')
    print(f)

player1(13)

我使用的技术有潜力吗?还是太复杂了?你会怎么做?

1 个答案:

答案 0 :(得分:0)

从一般意义上讲,您已经使其变得过于复杂。

考虑一下-就运动而言,木板只是一组有序的空间。

但是现在,您掌握了有关如何将板作为播放器代码的一部分进行创建的信息。

最好将其分开,您会发现事情变得简单了。

相反,让玩家简单地跟踪它的进度,换句话说,它在什么空间上编号。

然后您可以生成棋盘,并在知道空格编号的情况下查看它是否与玩家的位置匹配。

然后将其进一步(并且更简单),将板子绘制在2D阵列上,然后输出该板子,而不是在逐行移动时试图找出板子。