[英]Pygame blank (black) screen
I posted the same question before but I'm posting again because I found more errors in my code and corrected them.我之前发布了同样的问题,但我再次发布,因为我在代码中发现了更多错误并更正了它们。 However I'm still facing the same problem as before!
但是我仍然面临与以前相同的问题!
Original post: I just started learning Python a couple of weeks ago and I'm following a tutorial to build a Sudoku Solver with pygame!原帖:几周前我刚开始学习 Python,我正在学习使用 pygame 构建数独求解器的教程!
The problem I'm facing now is whenever I try to run my code, only a blank black window pops up.我现在面临的问题是每当我尝试运行我的代码时,只会弹出一个空白的黑色 window 。 I've been going through my code over and over and over again but I can't seem to find the problem... I'm not sure where exactly is the problem in my code so please forgive me for putting out the entire script below!
我一遍又一遍地检查我的代码,但我似乎找不到问题......我不确定我的代码到底哪里出了问题,所以请原谅我把整个脚本都放出来了以下!
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()
You never call board.draw_grid()
你永远不会调用
board.draw_grid()
self.column
should be self.columns
self.column
应该是self.columns
and you need to indent the ...update()
call so it's inside the 'mainloop'
并且您需要缩进
...update()
调用,使其位于'mainloop'
内
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.