【问题标题】:Pygame draw.rect 2d object array black screenPygame draw.rect 2d对象数组黑屏
【发布时间】:2017-12-10 02:26:25
【问题描述】:

我的程序有问题。我正在尝试编写战舰游戏,问题就在这里:

class Battleship(object):

    def __init__(self):
      
        pygame.init()
        pygame.display.set_caption('battleship')
        self.gameDisplay = pygame.display.set_mode((DISPLAYWIDTH, DISPLAYHEIGHT))
        self.clock = pygame.time.Clock()

class Tile(object):
    def __init__(self):

        self.occupied = False
        self.shipID = 0
        self.playerID = 0

    def setOccupied(self, occupied):
        self.occupied = occupied

    def setShipID(self, shipID):
        self.shipID = shipID

    def setPlayerID(self, pID):
        self.playerID = pID

class Board:

    shipSizes = [1, 2, 3]
    sizeX = 25
    sizeY = 25

    def __init__(self, playerID):
  
        self.playerID = playerID

        self.playerShips = []

        for i in range(0, len(self.shipSizes)):
            
            self.playerShips.append(Ship(i, self.shipSizes[i]))

        self.tiles = [[Tile() for i in range(self.sizeX)] for j in range(self.sizeY)]

    def drawBoard(self):
        x = 0
        y = 0
        for row in self.visual:
            for col in row:
                pygame.draw.rect(Battleship().gameDisplay, black, (x, y, CASILLA, CASILLA), LINEWIDTH)
                x = x + CASILLA
            y = y + CASILLA
            x=0

我没有收到任何错误,但是该功能无法正常工作,只显示黑屏,检查了其他所有内容,问题肯定出在那部分。

编辑,我添加了战列舰

【问题讨论】:

  • 代码不足,无法找到错误。请提供minimal, complete and verifiable example
  • 您是否在终端/控制台中运行它以查看错误?
  • 使用print() 来检查你用来绘制的变量中的值——比如self.visual。顺便说一句:你为​​什么每次画rect()时都创建Battleship()
  • 感谢您的宝贵时间,我应该发布整个代码吗?另一件事是,我 100 % 确定我的代码中的问题在于绘图板功能,因为我测试了整个程序,其他一切正常
  • furas,是的,我运行了它,没有错误,它只是冻结

标签: python python-2.7 pygame


【解决方案1】:

我编写了这个最小的示例来向您展示我在 cmets 中描述的内容。 board 实例现在是Battleship 的一个属性,你可以在战舰的draw 方法中调用它的draw 方法。

import pygame as pg


GRAY = pg.Color('gray20')
BLUE = pg.Color('dodgerblue1')

class Battleship(object):

    def __init__(self):
        pg.init()
        pg.display.set_caption('battleship')
        self.display = pg.display.set_mode((800, 600))
        self.clock = pg.time.Clock()
        self.fps = 30
        self.done = False
        # Is an attribute of the Battleship now.
        self.board = Board()

    def run(self):
        while not self.done:
            self.handle_events()
            self.run_logic()
            self.draw()
            self.clock.tick(self.fps)

    def handle_events(self):
        for event in pg.event.get():
            if event.type == pg.QUIT:
                self.done = True

    def run_logic(self):
        pass

    def draw(self):
        self.display.fill(GRAY)
        # Call the board's draw method and pass the display.
        self.board.draw(self.display)
        pg.display.flip()


class Board:

    shipSizes = [1, 2, 3]
    sizeX = 25
    sizeY = 25

    def __init__(self):
        self.tiles = [['-' for i in range(self.sizeX)] for j in range(self.sizeY)]

    def draw(self, display):
        for y, row in enumerate(self.tiles):
            for x, col in enumerate(row):
                pg.draw.rect(
                    display, BLUE,
                    (x*self.sizeX, y*self.sizeY, self.sizeX, self.sizeY), 
                    2)


battleship = Battleship()
battleship.run()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多