如何根据分配的 ID 使对象表现不同

问题描述 投票:0回答:1

我正在尝试创建棋子,而不是为每种类型的棋子使用不同的类,我想给它们一个 ID,女巫将允许我根据该值更改它们的移动方式,但我不确定如何去做吧。

#ID is the type of peice, queen = 6 rook = 5 king = 4 bishop = 3 knight = 2 pawn = 1
ID = [5,2,3,6,4,3,2,5,1,1,1,1,1,1,1,1,5,2,3,4,6,3,2,5,1,1,1,1,1,1,1,1]

class createpeice(pygame.sprite.Sprite):
  def __init__(self, color, x, y, n):
    super().__init__()
    self.id = n
    self.image = pygame.Surface([peice_width, peice_height])
    self.image.fill(color)
    self.rect = self.image.get_rect()
    self.rect.x = x
    self.rect.y = y

  def move(self):
    if n == 1:
        print("1")
    if n== 2:
        print("2")
    if n == 3:
        print("3")
    if n == 4:
        print("4")
    if n == 5:
        print("5")
    if n == 6:
        print("6")

def placepeices():
    global top2,n,n2,count
    for row in range(4):
        for column in range(8):
            n2 = ID[count]
            peice = createpeice(color2[n],column * (peice_width + 50) + 75, top2 + 25, n2)
            peices.add(peice)
            allsprites.add(peice)
            count += 1
            if count == 16:
                n += 1

        top2 += peice_height + 50
        if row == 1:
            top2 += 400
placepeices()

当我调用第一个对象时,

createpeice.move(0)
,在我的主循环中它返回 1,但是我需要它返回 5,因为这是第一块的 ID。

python class pygame
1个回答
0
投票

我根据 OOP 原则、逻辑和修复拼写错误来组织你的代码。

如你所愿,Rook(id=5)先移动。但是,这只是您的代码的 1 次迭代。您可以从这里继续开发它。

代码:

import pygame

piece_width = 50
piece_height = 50
color_white = (255, 255, 255)
color_black = (0, 0, 0)
top2 = 0
count = 0

ID = [5, 2, 3, 6, 4, 3, 2, 5, 1, 1, 1, 1, 1, 1, 1, 1, 5, 2, 3, 4, 6, 3, 2, 5, 1, 1, 1, 1, 1, 1, 1, 1]

class ChessPiece(pygame.sprite.Sprite):
    def __init__(self, color, x, y):
        super().__init__()
        self.image = pygame.Surface([piece_width, piece_height])
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

    def move(self):
        raise NotImplementedError("Subclasses must implement the move method")

class Pawn(ChessPiece):
    def move(self):
        print("Pawn moves forward")

class Knight(ChessPiece):
    def move(self):
        print("Knight moves in an L-shape")

class Bishop(ChessPiece):
    def move(self):
        print("Bishop moves diagonally")

class Rook(ChessPiece):
    def move(self):
        print("Rook moves horizontally or vertically")

class Queen(ChessPiece):
    def move(self):
        print("Queen moves horizontally, vertically, or diagonally")

class King(ChessPiece):
    def move(self):
        print("King moves one square in any direction")

def create_piece(color, x, y, piece_type):
    if piece_type == 1:
        return Pawn(color, x, y)
    elif piece_type == 2:
        return Knight(color, x, y)
    elif piece_type == 3:
        return Bishop(color, x, y)
    elif piece_type == 4:
        return King(color, x, y)
    elif piece_type == 5:
        return Rook(color, x, y)
    elif piece_type == 6:
        return Queen(color, x, y)
    else:
        raise ValueError("Invalid piece type")

def place_pieces():
    global top2, count
    for row in range(4):
        for column in range(8):
            piece_type = ID[count]
            color = color_white if row < 2 else color_black
            piece = create_piece(color, column * (piece_width + 50) + 75, top2 + 25, piece_type)
            pieces.add(piece)
            allsprites.add(piece)
            count += 1

        top2 += piece_height + 50
        if row == 1:
            top2 += 400

pygame.init()
pieces = pygame.sprite.Group()
allsprites = pygame.sprite.Group()
place_pieces()

piece_to_move = pieces.sprites()[0]  # call move on the first piece
piece_to_move.move()

输出:

Hello from the pygame community. https://www.pygame.org/contribute.html
Rook moves horizontally or vertically
© www.soinside.com 2019 - 2024. All rights reserved.