【问题标题】:Pygame: Generate x random and different coordinates in a definited boardPygame:在确定的棋盘上生成x个随机和不同的坐标
【发布时间】:2019-07-07 16:19:18
【问题描述】:

对于一个小型 rpg 游戏,我会创建包含特定数量值的棋盘。

让我介绍一个例子:

board =[[0, 0, 0, 15, 0, 0, 0, 0, 0, 0],
[0, 0, 15, 0, 15, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 15, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 15, 0, 0, 0, 0, 15, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 15, 0, 0, 0, 0, 0, 15, 0, 0],
[0, 0, 0, 0, 0, 15, 0, 0, 15, 0],
[15, 0, 15, 15, 0, 0, 0, 0, 0, 0]]

这是一个 10 x 10 的棋盘,其中 13 乘以 15。

为了得到这个结果,我使用了一个在棋盘上给出随机坐标的程序。

b2x = []
b2y = []
for i in range(B):
    x = random.randint(0,L-1)
    y = random.randint(0,H-1)
    b2x.append(x)
    b2y.append(y)
    board[x][y] = 15
    print('x : ',x, ' and y : ', y)

这里一个明显的问题是我们可以得到两个相似的坐标,这将减少一个值的整数。

事实上,在第一个例子的板子中,我不会有值的数量,因为我向程序询问了 15 个值,它返回我 13。

所以我尝试在没有坐标检查的情况下解决这个问题,现在似乎无法正常工作。

    for j in range(len(bo2x)):
        if (x == b2x[j-1]) and (y == b2y[j-1]):
            i -= 1 # affect the for i in range(B) loop

完整代码:

b2x = []
b2y = []
for i in range(B):
    x = random.randint(0,L-1)
    y = random.randint(0,H-1)
    for j in range(len(bo2x)):
        if (x == b2x[j-1]) and (y == b2y[j-1]):
            i -= 1 # affect the for i in range(B) loop
    b2x.append(x)
    b2y.append(y)
    board[x][y] = 15
    print('x : ',x, ' and y : ', y)

结果,经过多次尝试,完全没有变化:

random generation
x :  5  and y :  4
x :  1  and y :  3
x :  7  and y :  7
x :  7  and y :  5
x :  0  and y :  7
x :  0  and y :  1
x :  6  and y :  2
x :  3  and y :  6
x :  9  and y :  4
x :  5  and y :  9
x :  6  and y :  8
x :  6  and y :  7
x :  3  and y :  6
x :  3  and y :  7
x :  7  and y :  5
[Finished in 0.2s]

如您所见,x : 7 and y : 5 行和x : 3 and y : 6 行在生成中出现了两次。

有人可以帮助我达到预期的结果吗?

预期结果(概念):

x :  5  and y :  4
x :  1  and y :  3
x :  7  and y :  7
x :  7  and y :  5
x :  0  and y :  7
x :  0  and y :  1
x :  6  and y :  2
x :  3  and y :  6
x :  9  and y :  4
x :  5  and y :  9
x :  6  and y :  8
x :  6  and y :  7
x :  3  and y :  6
this line already exist !
x :  3  and y :  7
x :  7  and y :  5
this line already exist !
x :  1  and y :  3 
x :  9  and y :  4

board :
[[0, 15, 15, 0, 0, 0, 0, 15, 0, 0],
[0, 0, 0, 15, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 15, 15, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 15, 0, 0, 0, 0, 15],
[0, 0, 15, 0, 0, 0, 0, 15, 15, 0],
[0, 0, 0, 0, 0, 15, 0, 15, 0, 0],
[0, 0, 0, 15, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 15, 0, 0, 0, 0, 0]]

【问题讨论】:

标签: python python-3.x


【解决方案1】:

你想创建一个 LxB 板。

L, H = 10, 10

这可以通过一行代码来实现:

board = [[0] * L for _ in range(H)] 

您想在板上的不同位置放置某个数字 15 次。
生成随机坐标,但如果数字已分配给索引字段,则跳过坐标:

count = 15
number = 15
b = []
while count > 0:
    x, y = random.randint(0,L-1), random.randint(0,H-1)
    if board[y][x] != number:
        board[y][x] = number
        b.append((x, y))
        count -= 1

for row in board:
    print(row)
print(b)

【讨论】:

  • @Edhyjox 我虽然你需要索引元组。我已经不需要它们了,那么b 没用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-09
  • 2022-01-18
  • 2023-01-07
  • 1970-01-01
  • 2021-10-03
  • 2017-06-14
相关资源
最近更新 更多