Python 在尝试渲染文本时,脚本每隔一次初始化时就会崩溃

Python 在尝试渲染文本时,脚本每隔一次初始化时就会崩溃,python,text,pygame,crash,render,Python,Text,Pygame,Crash,Render,所以我尝试自学Python,作为一个项目,我尝试使用pygame创建游戏Connect 4。我已经实现了一个调试函数,它显示所有分幅的坐标,但是当我激活它时(将变量'debug'设置为'1',每当我尝试初始化脚本时,它就会变黑并崩溃。其他50%的情况下,它工作正常。当我禁用文本呈现时,它不会发生。但是,我不知道为什么会发生这种情况,并且我的控制台中没有收到任何错误报告 我通过Anaconda3发行版在Spyder中使用PyGame2.0.1编写Python 3.7 这是代码。有问题的行似乎是41

所以我尝试自学Python,作为一个项目,我尝试使用pygame创建游戏Connect 4。我已经实现了一个调试函数,它显示所有分幅的坐标,但是当我激活它时(将变量'debug'设置为'1',每当我尝试初始化脚本时,它就会变黑并崩溃。其他50%的情况下,它工作正常。当我禁用文本呈现时,它不会发生。但是,我不知道为什么会发生这种情况,并且我的控制台中没有收到任何错误报告

我通过Anaconda3发行版在Spyder中使用PyGame2.0.1编写Python 3.7

这是代码。有问题的行似乎是41-43,也许是75-79,但我不知道可能是什么错误。有人知道可能是什么问题吗?不同的字体也会出现这种情况

import sys
import pygame

# ==== Settings ====

scalingFactor = 2 #Must be an integer

displayWidth = 320 * scalingFactor
displayHeight = 375 * scalingFactor
displaySize = [displayWidth, displayHeight]

Black = (0, 0, 0)
White = (255, 255, 255)
Grey = (255, 255, 255)
Red = (255, 20, 20)
Blue = (80, 80, 255)

player1 = Red
player2 = Blue
turnColor = player1 #Starting color

frameBackgroundColor = White
frameLatticeColor = Black
frameMargin = 5 * scalingFactor
gridWidth = 40 * scalingFactor
gridHeight = 40 * scalingFactor
columns = 7
rows = 6

fontColor = Black

debug = 1

# ==== Code ====

pygame.init()
screen = pygame.display.set_mode(displaySize)
clock = pygame.time.Clock()
pygame.display.set_caption('Connect Four')

if debug == 1: #Line 43 starts here
    #This somehow makes the script unstable and won't allow it to run every other time I try to initialize it.
    font = pygame.font.Font(None, 24)

running = True

grid = []
for row in range(rows):
    grid.append([])
    for column in range(columns):
        grid[row].append(0)

def drawGrid():
    for row in range(rows):
        for column in range(columns):
            color = White
            pygame.draw.rect(screen, 
                             color,
                             [(frameMargin + gridWidth) * column + frameMargin,
                              (frameMargin + gridHeight) * row + frameMargin,
                              gridWidth, gridHeight])
            
            if grid[row][column] == 1:
                pygame.draw.circle(screen, 
                                   player1, 
                                   [(frameMargin + gridWidth) * column + frameMargin + gridWidth // 2, 
                                    (frameMargin + gridHeight) * row + frameMargin + gridHeight // 2], 
                                    gridWidth // 2 - 5)
            if grid[row][column] == 2:
                pygame.draw.circle(screen, 
                                   player2, 
                                   [(frameMargin + gridWidth) * column + frameMargin + gridWidth // 2, 
                                    (frameMargin + gridHeight) * row + frameMargin + gridHeight // 2], 
                                    gridWidth // 2 - 5)
            if debug == 1: #Line 75 starts here
                textString = str(row) + "," + str(column)
                textRender = font.render(textString, True, fontColor)
                screen.blit(textRender,((frameMargin + gridWidth) * column + frameMargin + 8, 
                                        (frameMargin + gridHeight) * row + frameMargin + 8))

def validTile_check(validTile, pos, column, row):
    
    # Checks if registered click is on tile or on frame lattice
    if pos[0] % (gridWidth + frameMargin) <= frameMargin or pos[1] % (gridWidth + frameMargin) <= frameMargin:
        print("Click registered on", pos, ". Not on tile.")
    # Checks if registered click is within grid
    elif row > rows - 1 or column > columns - 1:
        print("Click registered on", pos, ". With grid coordinates:", row, column, ". Not a tile.")
        validTile = 0
    # Checks if registered click is on bottom tile
    elif grid[row][column] == 0 and row == rows - 1:
        validTile = 1
    # Checks if registered click is on tile with placed piece in tile below
    elif grid[row][column] == 0 and grid[row + 1][column] > 0:
        validTile = 1
    else: 
        validTile = 0
        
    return validTile

def winCheck(winCondition, grid, pos, column, row):
    
    winCondition = 0
    chain = 1     
    
    for n in range(1, 3):
        if chain == 4:
            winCondition = 1
            break
        if row + n < rows:
            if grid[row][column] == grid[row + n][column]:
                chain += 1
            else:
                break

    return winCondition

while running:
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            
            pos = pygame.mouse.get_pos()
            column = pos[0] // (gridWidth + frameMargin)
            row = pos[1] // (gridHeight + frameMargin)
            validTile = 0
            winCondition = 0
            
            validTile = validTile_check(validTile, pos, column, row)
            
            if validTile == 0:
                print("Click registered on", pos, ". With grid coordinates:", row, column, ". Not a valid tile.")
            # Marks tile if clicked tile was unmarked.
            elif grid[row][column] == 0:
                if turnColor == player1:
                    grid[row][column] = 1
                    turnColor = player2
                elif turnColor == player2:
                    grid[row][column] = 2
                    turnColor = player1
                print("Click registered on", pos, ". With grid coordinates:", row, column, ". Tile marked.")
            # Unmarks tile if clicked tile was marked.
            elif grid[row][column] > 0:
                grid[row][column] = 0
                print("Click registered on", pos, ". With grid coordinates:", row, column, ". Tile unmarked.")
                
            winCheck(winCondition, grid, pos, column, row)
            
            if winCondition == 1:
                print("Winner!")
        
    screen.fill(Black)
    drawGrid()
    pygame.display.update()
    
    clock.tick(60)
    

pygame.quit()

当它显示“Hello from the pygame community.”时,它会工作,当它不显示任何内容时,或者只显示“runfile()时'行,它崩溃了。

我仍然不知道到底出了什么问题,但似乎不是代码。我将Python版本更新为3.9.5,并通过另一个下载程序(而不是Anaconda3)下载了我正在使用的IDE的新版本。这些问题都可能解决了,但它不再崩溃了!

它是如何崩溃的?是一个例外吗正在引发ion?如果是这样,请包含完整的错误消息和回溯。第41-43行和第75-79行是哪行?当我运行脚本时,它要么工作,要么窗口保持黑色几秒钟并退出。没有错误消息,没有任何内容。我将在原始帖子中添加行号,并包含控制台日志的注释摘录。我无法重现此问题。您的操作系统是什么?Windows 10 Home 64位。其他一些规格:英特尔(R)Core(TM)i5-9600K,16GB RAM。我建议使用而不是:
font=pygame.font.SysFont(无,24)
   In [2]: runfile('C:/Users/Admin/Documents/Connect4/Game/gameFrame.py', wdir='C:/Users/Admin/Documents/Connect4/Game')
    
   In [1]: runfile('C:/Users/Admin/Documents/Connect4/Game/gameFrame.py', wdir='C:/Users/Admin/Documents/Connect4/Game')
    pygame 2.0.1 (SDL 2.0.14, Python 3.7.4)
    Hello from the pygame community. https://www.pygame.org/contribute.html
    
   In [2]: runfile('C:/Users/Admin/Documents/Connect4/Game/gameFrame.py', wdir='C:/Users/Admin/Documents/Connect4/Game')
    pygame 2.0.1 (SDL 2.0.14, Python 3.7.4)
    Hello from the pygame community. https://www.pygame.org/contribute.html
    
   In [1]:
    
   In [2]: runfile('C:/Users/Admin/Documents/Connect4/Game/gameFrame.py', wdir='C:/Users/Admin/Documents/Connect4/Game')