我正在做一个国际象棋引擎,需要一个切片函数来改变,这取决于有多少数字是遵循分裂的开始(或任何其他方法,实现相同)。bRbNbBbQbKbBbNbR8bP32EE8wPwRwNwBwQwKwBwNwR
这是我目前为止的代码;
chessEngine.py
colours = ["White", "Black"]
class gameState():
def __init__ (self):
self.board = "bRbNbBbQbKbBbNbR8bP32EE8wPwRwNwBwQwKwBwNwR"
self.moveColour = "White"
self.gameLog = ""
self.canCastle = True
chessMain.py
import pygame
import chessEngine
pygame.init()
WIDTH = HEIGHT = 512
DIMENSION = 8
SQ_SIZE = HEIGHT // DIMENSION
MAX_FPS = 5
IMAGES= {}
def loadImages():
pieces = ["bR", "bN", "bB", "bQ", "bK", "bP",
"wP", "wR", "wN", "wB", "wQ", "wK",]
for piece in pieces:
IMAGES[piece] = pygame.transform.scale(pygame.image.load("Assets/" + piece + ".png"), (SQ_SIZE,SQ_SIZE))
"""
We can now access an image by saying "IMAGES()"
eg; to get the image for a white pawn write "IMAGES["wP"]"
"""
def main():
screen = pygame.display.set_mode((WIDTH,HEIGHT))
clock = pygame.time.Clock()
screen.fill(pygame.Color("white"))
gs = chessEngine.gameState()
loadImages()
running = True
while running:
for x in pygame.event.get():
if x.type == pygame.QUIT:
running = False
drawGameState(screen, gs)
clock.tick(MAX_FPS)
pygame.display.flip()
def drawGameState(screen, gs):
drawBoard(screen)
drawPieces(screen, gs.board)
def drawBoard(screen):
colors = [pygame.Color(253,235,253), pygame.Color(75,160,60)] # White and green
for r in range(DIMENSION):
for c in range (DIMENSION):
color = colors[((r+c)%2)]
pygame.draw.rect(screen, color, pygame.Rect(c*SQ_SIZE, r*SQ_SIZE, SQ_SIZE, SQ_SIZE))
def drawPieces(screen, board):
squareCount = 0
listCount = 0
r = squareCount % DIMENSION
c = squareCount // DIMENSION
for z in range(DIMENSION^2):
x = slice(listCount, listCount + 1)
if board[x].isnumeric() == False:
v = slice(listCount, listCount + 2)
screen.blit(IMAGES[board[v]], pygame.Rect(r*SQ_SIZE, c*SQ_SIZE, SQ_SIZE, SQ_SIZE))
squareCount += 1
listCount += 2
else:
amount = int(board[x])
for y in range(amount):
v = slice(listCount, listCount + 2)
screen.blit(IMAGES[board[v]], pygame.Rect(r*SQ_SIZE, c*SQ_SIZE, SQ_SIZE, SQ_SIZE))
squareCount += 1
listCount += len(str(amount)) + 2
main()
1条答案
按热度按时间polhcujo1#
以下是如何对电路板进行游程编码:
输出: