Python Pygame空白(黑色)屏幕
我以前也发布过同样的问题,但我现在再次发布,因为我在代码中发现了更多的错误并更正了它们。然而,我仍然面临着和以前一样的问题 原职: 几周前我刚开始学习Python,现在我正在学习一个教程,用pygame构建一个数独解算器 我现在面临的问题是,每当我尝试运行代码时,只会弹出一个黑色的空白窗口。我已经一遍又一遍地检查我的代码,但我似乎找不到问题。。。我不确定我的代码中到底是哪里出了问题,所以请原谅我在下面列出了整个脚本Python Pygame空白(黑色)屏幕,python,pygame,Python,Pygame,我以前也发布过同样的问题,但我现在再次发布,因为我在代码中发现了更多的错误并更正了它们。然而,我仍然面临着和以前一样的问题 原职: 几周前我刚开始学习Python,现在我正在学习一个教程,用pygame构建一个数独解算器 我现在面临的问题是,每当我尝试运行代码时,只会弹出一个黑色的空白窗口。我已经一遍又一遍地检查我的代码,但我似乎找不到问题。。。我不确定我的代码中到底是哪里出了问题,所以请原谅我在下面列出了整个脚本 import pygame pygame.font.init() #-----
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()
导入pygame
pygame.font.init()
#--------------------------------------------------------------------------------------------------
#开始构建GUI
类单_单元:
行=9
列=9
def uuu init uuu(self、value、row、column、width、height):
自我价值=价值
self.row=行
self.column=列
self.width=宽度
自我高度=高度
self.temporary_值=0
self.selectedcell=False
def插入临时值(自身,值):
self.temporary_值=值
def插入_值(自身,值):
自我价值=价值
def draw_单电池(自身,屏幕):
单元测量值t=自身宽度/9
cell_x=self.column*cell_measuremt
cell_y=self.row*cell_measuremt
灰色=(128128128)
一些蓝色=(0170204)
白色=(255,255,255)
fnt=pygame.font.SysFont(“comicsans”,32)
如果self.selectedcell==True:
pygame.draw.rect(屏幕,一些蓝色,(单元格x,单元格y,单元格measuremt,单元格measuremt),3)
如果self.temporary_值!=0和self.value==0:
text=fnt.render(str(self.temporary_值),1,灰色)
blit(文本,(单元格x+5,单元格y+5))
elif self.value!=0:
text=fnt.render(str(self.value),1,白色)
blit(text,(cell_x+(cell_measuremt/2-text.get_width()/2),cell_y+(cell_measuremt/2-text.get_height()/2)))
def绘图结果(自身、屏幕、结果):
水=(77255,255)
紫红色=(255,0255)
黑色=(0,0,0)
白色=(255,255,255)
fnt=pygame.font.SysFont(“comicsans”,32)
单元测量值t=自身宽度/9
cell_x=self.column*cell_measuremt
cell_y=self.row*cell_measuremt
pygame.draw.rect(屏幕,黑色,(单元格x,单元格y,单元格measuremt,单元格measuremt),0)
text=fnt.render(str(self.value),1,白色)
blit(text,(cell_x+(cell_measuremt/2-text.get_width()/2),cell_y+(cell_measuremt/2-text.get_height()/2)))
如果结果==真:
pygame.draw.rect(屏幕,水,(单元格x,单元格y,单元格度量,单元格度量),3)
其他:
pygame.draw.rect(屏幕,紫红色,(单元格x,单元格y,单元格度量,单元格度量),3)
类全局网格:
数独板=[
[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]]
定义初始化(自身、行、列、宽度、高度、屏幕):
self.rows=行
self.columns=列
self.width=宽度
自我高度=高度
self.screen=屏幕
self.singlecells=[[Single_Cell(self.sudoku_board[row][column],行、列、宽度、高度)表示范围内的行(rows)]表示范围内的列(columns)]
self.updateGrid=None
self.update_board()
self.selected=无
def solve_数独(self):
emptycell\u location=查找\u空\u单元格(self.updateGrid)
如果未清空呼叫单元位置:
返回真值
其他:
(行、列)=清空单元格位置
对于范围(1,10)内的n:
如果harmony(self.updateGrid,n,emptycell_位置)=True:
self.updateGrid[行][列]=n
如果self.solve_sudoku()=真:
返回真值
self.updateGrid[行][列]=0
返回错误
def更新板(自身):
self.updateGrid=[[self.singlecells[行][列]。范围(self.columns)中的行的值]范围(self.rows)中的列的值]
def commit_值(自身,值):
(行、列)=自选
如果self.singlecells[行][列].value==0:
self.singlecells[行][列].插入_值(值)
self.update_board()
如果和谐(self.updategrid,value,(row,column))和solve_数独(self.updategrid)==True:
返回真值
其他:
self.singlecells[行][列].插入_值(0)
self.singlecells[行][列].插入临时值(0)
self.update_board()
返回错误
def清除单元(自身):
(行、列)=自选
如果self.singlecells[行][列].value==0:
self.singlecells[行][列].插入临时值(0)
def草图_单元(自身,值):
(行、列)=自选
self.singlecells[行][列].插入临时值(值)
def绘制网格(自):
白色=(255,255,255)
灰色=(128128128)
单元格宽度=(self.width/9)
对于范围内的n(self.column+1):
如果n%3==0:
线宽=3
其他:
线宽=1
如果线宽==3:
pygame.draw.line(self.screen,白色,((单元格宽度*n),0),((单元格宽度*n),self.height),线宽)
pygame.draw.line(self.screen,白色,(0,(单元格宽度*n)),(self.width,(单元格宽度*n)),线宽)
elif线宽==1:
pygame.draw.line(自身屏幕,灰色,((单元格宽度*n),