当前位置: 首页 > 知识库问答 >
问题:

Pygame空白(黑色)屏幕

夹谷烨赫
2023-03-14

我以前也发布过同样的问题,但我现在再次发布,因为我在代码中发现了更多的错误并更正了它们。然而,我仍然面临着和以前一样的问题!

原始帖子:我几周前刚开始学习Python,我正在学习一个教程,用pygame构建一个数独解算器!

我现在面临的问题是,每当我尝试运行代码时,只会弹出一个黑色的空白窗口。我已经一遍又一遍地检查我的代码,但我似乎找不到问题。。。我不确定我的代码中到底是哪里出了问题,所以请原谅我在下面列出了整个脚本

import pygame
pygame.font.init()

#--------------------------------------------------------------------------------------------------
#Begin constructing GUI

class Single_Cell:

    rows = 9
    columns = 9
    def __init__(self, value, row, column, width, height):
        self.value = value
        self.row = row
        self.column = column
        self.width = width
        self.height = height
        self.temporary_value = 0
        self.selectedcell = False

    def insert_temporary_value(self, value):
        self.temporary_value = value

    def insert_value(self, value):
        self.value = value

    def draw_single_cell(self, screen):
        cell_measuremt = self.width / 9
        cell_x = self.column * cell_measuremt
        cell_y = self.row * cell_measuremt
        gray = (128, 128, 128)
        some_blue = (0, 170, 204)
        white = (255, 255, 255)
        fnt = pygame.font.SysFont("comicsans", 32)

        if self.selectedcell == True:
            pygame.draw.rect(screen, some_blue, (cell_x, cell_y, cell_measuremt, cell_measuremt), 3)

        if self.temporary_value != 0 and self.value == 0:
            text = fnt.render(str(self.temporary_value), 1, gray)
            screen.blit(text, (cell_x + 5, cell_y + 5))

        elif self.value != 0:
            text = fnt.render(str(self.value), 1, white)
            screen.blit(text, (cell_x + (cell_measuremt/2 - text.get_width()/2), cell_y + (cell_measuremt/2 - text.get_height()/2)))

    def draw_outcome(self, screen, outcome):
        aqua = (77, 255, 255)
        fuchsia = (255, 0, 255)
        black = (0, 0, 0)
        white = (255, 255, 255)
        fnt = pygame.font.SysFont("comicsans", 32)
        cell_measuremt = self.width / 9
        cell_x = self.column * cell_measuremt
        cell_y = self.row * cell_measuremt
        pygame.draw.rect(screen, black, (cell_x, cell_y, cell_measuremt, cell_measuremt), 0)

        text = fnt.render(str(self.value), 1, white)
        screen.blit(text, (cell_x + (cell_measuremt/2 - text.get_width()/2), cell_y + (cell_measuremt/2 - text.get_height()/2)))

        if outcome == True:
            pygame.draw.rect(screen, aqua, (cell_x, cell_y, cell_measuremt, cell_measuremt), 3)
        else:
            pygame.draw.rect(screen, fuchsia, (cell_x, cell_y, cell_measuremt, cell_measuremt), 3)




class Global_Grid:

    sudoku_board = [
    [5, 0, 0, 1, 8, 0, 0, 7, 0],
    [0, 0, 7, 2, 9, 0, 5, 4, 3],
    [0, 0, 0, 0, 0, 4, 0, 0, 1],
    [0, 5, 0, 6, 0, 0, 9, 8, 0],
    [7, 0, 0, 9, 0, 0, 3, 0, 4],
    [0, 9, 6, 3, 0, 8, 2, 1, 5],
    [0, 0, 5, 8, 0, 0, 0, 0, 6],
    [1, 4, 0, 0, 6, 0, 0, 0, 2],
    [0, 3, 0, 0, 2, 7, 1, 0, 0]]

    def __init__(self, rows, columns, width, height, screen):
        self.rows = rows
        self.columns = columns
        self.width = width
        self.height = height
        self.screen = screen
        self.singlecells = [[Single_Cell(self.sudoku_board[row][column], row, column, width, height) for row in range(rows)] for column in range(columns)]
        self.updatedgrid = None
        self.update_board()
        self.selected = None

    def solve_sudoku(self):
        emptycell_location = find_empty_cells(self.updatedgrid)
        if not emptycell_location:
            return True
        else:
            (row, column) = emptycell_location
        for n in range(1, 10):
            if harmony(self.updatedgrid, n, emptycell_location) == True:
                self.updatedgrid[row][column] = n


                if self.solve_sudoku() == True:
                    return True

                self.updatedgrid[row][column] = 0

        return False


    def update_board(self):
        self.updatedgrid = [[self.singlecells[row][column].value for row in range(self.columns)] for column in range(self.rows)]

    def commit_value(self, value):
        (row, column) = self.selected
        if self.singlecells[row][column].value == 0:
            self.singlecells[row][column].insert_value(value)
            self.update_board()

            if harmony(self.updatedgrid, value, (row, column)) and solve_sudoku(self.updatedgrid) == True:
                return True

            else:
                self.singlecells[row][column].insert_value(0)
                self.singlecells[row][column].insert_temporary_value(0)
                self.update_board()
                return False

    def clear_cell(self):
        (row, column) = self.selected
        if self.singlecells[row][column].value == 0:
            self.singlecells[row][column].insert_temporary_value(0)

    def sketch_cell(self, value):
        (row, column) = self.selected
        self.singlecells[row][column].insert_temporary_value(value)

    def draw_grid(self):
        white = (255, 255, 255)
        gray = (128, 128, 128)
        cell_width = (self.width / 9)
        for n in range(self.column + 1):
            if n % 3 == 0:
                line_width = 3
            else:
                line_width = 1
            if line_width == 3:
                pygame.draw.line(self.screen, white, ((cell_width * n), 0), ((cell_width * n), self.height), line_width)
                pygame.draw.line(self.screen, white, (0, (cell_width * n)), (self.width, (cell_width * n)), line_width)
            elif line_width == 1:
                pygame.draw.line(self.screen, gray, ((cell_width * n), 0), ((cell_width * n), self.height), line_width)
                pygame.draw.line(self.screen, gray, (0, (cell_width * n)), (self.width, (cell_width * n)), line_width)

        for row in range(self.rows):
            for column in rang(self.columns):
                self.singlecells[row][column].draw_single_cell(self.screen)

    def reset_and_select(self, row, column):
        for x in range(self.rows):
            for y in range(self.columns):

                self.singlecells[x][y].selectedcell = False

        self.singlecells[row][column].selectedcell = True
        self.selected = (row, column)

    def user_click(self, position):
        if position[0] < self.width and position[1] < self.height:
            cell_width = (self.width / 9)
            row = int (position[0] // cell_width)
            column = int (position[1] // cell_width)
            return (column, row)
        else:
            return None

    def check_complete(self):
        for row in range(self.rows):
            for column in range(self.columns):
                if self.singlecells[row][column].value == 0:
                    return False
        return True

    def solve_sudoku_GUI(self):
        emptycell_location = find_empty_cells(self.updatedgrid)
        if not emptycell_location:
            return True
        else:
            (row, column) = emptycell_location
        for n in range(1, 10):
            if harmony(self.updatedgrid, n, emptycell_location) == True:
                self.updatedgrid[row][column] = n
                self.singlecells[row][column].insert_value(n)
                self.singlecells[row][column].draw_outcome(self.screen, True)
                self.update_board()
                pygame.display.update()
                pygame.time.delay(100)

                if self.solve_sudoku_GUI() == True:
                    return True

                self.updatedgrid[row][column] = 0
                self.singlecells[row][column].insert_value(0)
                self.update_board()
                self.singlecells[row][column].draw_outcome(self.screen, False)
                pygame.display.update()
                pygame.time.delay(100)
        return False



def main_window():

    screenwidth = 540
    screenheight = 600
    screen = pygame.display.set_mode((screenwidth, screenheight))
    board = Global_Grid(9, 9, 500, 500, screen)

    key = None
    mainloop = True

    #construct pygame events handler:
    while mainloop:

        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                mainloop = False

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_1:
                    key = 1
                if event.key == pygame.K_2:
                    key = 2
                if event.key == pygame.K_3:
                    key = 3
                if event.key == pygame.K_4:
                    key = 4
                if event.key == pygame.K_5:
                    key = 5
                if event.key == pygame.K_6:
                    key = 6
                if event.key == pygame.K_7:
                    key = 7
                if event.key == pygame.K_8:
                    key = 8
                if event.key == pygame.K_9:
                    key = 9

                if event.key == pygame.K_DELETE:
                    board.clear_cell()
                    key = None

                if event.key == pygame.K_RETURN:
                    (row, column) = board.selected
                    if board.singlecells[row][column].temporary_value != 0:
                        if board.commit_value(board.singlecells[row][column].temporary_value) == True:
                            print ("Passed.")
                        else:
                            print ("Failed.")
                        key = None

                        if board.check_complete() == True:
                            print ("Sudoku is completed. Thanks for testing the program.")

                if event.key == pygame.K_SPACE:
                    board.solve_sudoku_GUI()
                    print ("Try harder next time.")

            if event.type == pygame.MOUSEBUTTONDOWN:
                position = pygame.mouse.get_pos()
                cell_position = board.user_click(position)
                if cell_position:
                    board.reset_and_select(cell_position[0], cell_position[1])
                    key = None

        if board.selected and key != None:
            board.sketch_cell(key)

    pygame.display.update()

main_window()
pygame.quit()

共有1个答案

裴俊能
2023-03-14

你永远不会调用板。绘制网格()

self。列应为self。列

你需要缩进...更新()调用,使其在'main循环'

 类似资料:
  • 这是我的代码,请记住,我在几天前学习了python,所以我的代码可能制作不正确,等等。我正在尝试制作一个窗口,该窗口将显示一些文本(测试版),并将显示两个小矩形,我想成为按钮。

  • 我是python新手,我想用pygame在屏幕上可视化一个算法。这是我的代码: 但这总是显示一个黑色或有点深灰色的屏幕。有人知道我做错了什么吗?我尝试了多个教程,它们都给了我相同的屏幕。 我正在使用MacOS 10.14

  • 我运行以下代码: 当我运行代码时,pygame窗口打开,但它是一个空白(黑色)屏幕。我还收到以下错误消息:Traceback(最近一次呼叫last): 文件"C:/用户/Draco/OneDrive/文档/编程/graphics.py",第13行,screen.blit(img(0,0))TypeError:'pyplay.Surface'对象不可调用 我试图打开的图像保存为JPG文件。图像保存在

  • 我做了一些研究,看看我是否可以用这种方式解决这个问题,但似乎没有找到任何解决我问题的方法。我发现了这两个问题:为什么我的pygame显示器没有显示任何东西?对为什么PyGame显示屏是黑屏感到困惑。我试图用评论中的建议来解决我的问题,但没有成功,或者问题的原因与我的不同。 当我运行代码时,pygame窗口会显示,但完全是黑色的,但不会调用任何错误。

  • 我正在为平台游戏做教程,但我的Pygame窗口显示一个黑屏。 我不知道怎么解决这个问题。 我尝试再次关闭和打开Visual Studio Code,但没有任何结果。

  • 我的有问题,当我在手机上启动应用程序时,它会显示约0.5秒的白色屏幕。扩展了,在中,我声明为启动器,肖像模式为。 代码: XML: