创建任意数量的矩形,能够检查我创建了多少个,并检查它们的坐标

2024-04-16 15:15:39 发布

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

好吧,我在棋盘上做一个益智游戏。到目前为止,为特定的事物以及它们如何与其他事物交互编写代码相对容易,但我遇到了添加不可通行的空间的问题,因为我似乎无法用一种方法来创建一个、一个也不能,或者20个不可通行的对象。因为接下来我需要将图像blit到rect上,然后根据rect的坐标防止移动。如果需要的话,我可以说得更具体一些。只需提出意见。提前谢谢你的帮助。你知道吗

编辑:有人要求提供更多信息。。。。这是我到目前为止所知道的。。。有人帮我写了一个函数,为电路板上的每个空格生成矩形。。。你知道吗

def MakeBoard(upper_x=100, upper_y=100, size=100):
global ChessBoard
ChessBoard = []
for y in range(8):
    row = []
    for x in range(8):
        coords = (upper_x + x * size, upper_y + y * size)
        row.append(pygame.Rect(coords, (size, size)))
    ChessBoard.append(row)
return ChessBoard

所以这个函数在电路板上产生空间。在这种类型的函数中,移动是被禁止的,它会生成可供您使用的移动。基本上,它检查目标坐标是否在(1,1)和(8,8)之间,因此是否在电路板上。它可以被棋盘[y][x]引用

def RookMove(RXM, RYM):
VarReset()
if (RXM + 3) >= 1 and (RXM + 3) <= 8:
    global ROption1Exists
    ROption1Exists = True
if (RXM-3) >= 1 and (RXM-3) <= 8:
    global ROption2Exists
    ROption2Exists = True
if (RYM+3) >= 1 and (RYM+3) <= 8:
    global ROption3Exists
    ROption3Exists = True
if (RYM-3) >= 1 and (RYM-3) <= 8:
    global ROption4Exists
    ROption4Exists = True

为了禁止移动,我想在If语句中添加另一个条件,类似于。。。有一个不可通行物体的坐标列表,并检查以确保您必须遍历的所有坐标都与列表中的元素不匹配。你知道吗

不过,我想我自己也能弄明白。我遇到的主要问题是生成列表时,没有为电路板上的每个空格生成64个if语句之类的巨大函数。你知道吗

基本上,我要找的是如何,相对简单地

A.在一些坐标上显示图像,这些坐标表示无法通行的地形

B.用不可通行地形的坐标填充一个列表,然后检查以确保玩家将行驶的坐标不在列表中。或者,以其他方式,在我提供的示例函数的上下文中,禁止玩家移动,如果移动选项将使他在无法通行的地形中移动。你知道吗


Tags: and函数true列表size棋盘ifupper
1条回答
网友
1楼 · 发布于 2024-04-16 15:15:39

你似乎有两个问题,这应该是两个独立的问题。你知道吗

  1. 确定一个游戏棋子的合法移动列表。你知道吗
  2. 在pygame中显示游戏板。你知道吗

下面是一些简单的代码,用于测试给定棋盘位置是否对象棋车有效。它并不完美,但它可能会给你一些如何处理这个问题的想法。注意,在真正的国际象棋游戏中,您还需要检查目标方格的路径中是否没有其他棋子。你知道吗

还要注意的是,通常应该避免使用global变量,这里当然不需要它们。相反,请尝试让函数返回一个有用的结果,您可以从提供的参数中计算该结果,而不直接更改其外部的任何数据(“无副作用”)。你知道吗

from collections import namedtuple

BoardPosition = namedtuple('BoardPosition', ['x', 'y'])

def isOnBoard(position):
    # return True if position is on the board, or else False
    return (position.x >= 0
        and position.x < 8
        and position.y >= 0
        and position.y < 8)

def sameX(posA, posB):
    # return True if both positions are on the same column
    return posA.x == posB.x

def sameY(posA, posB):
    # return True if both positions are on the same row
    return posA.y == posB.y

def isRookMove(rookPos, targetPos):
    # return True if the target position is on the game board
    # AND it is a valid move for the supplied rook position.
    return ((sameX(rookPos, targetPos) or sameY(rookPos, targetPos))
            and isOnBoard(targetPos))


# Now to test the code...
myRookPos = BoardPosition(0, 0)
print isRookMove(myRookPos, BoardPosition(0, 4)) # True  (same column)
print isRookMove(myRookPos, BoardPosition(2, 4)) # False 
print isRookMove(myRookPos, BoardPosition(0, 8)) # False (off the board)

# Here is one way to produce a list of all the valid moves for the test rook...
allLegalMoves = [BoardPosition(x, y) for x in range(8) for y in range(8)
        if isRookMove(myRookPos, BoardPosition(x, y))]
print allLegalMoves

相关问题 更多 >