Python 制作排序应用程序时未初始化Pygame视频系统
我一直在用python开发一个排序应用程序,使用pygame进行可视化处理,现在我遇到了一个错误,我不知道如何修复。我的代码运行得非常好,然后我开始出现一个错误,它说“pygame.error:video system not initialized”。这是我的密码:Python 制作排序应用程序时未初始化Pygame视频系统,python,pygame,Python,Pygame,我一直在用python开发一个排序应用程序,使用pygame进行可视化处理,现在我遇到了一个错误,我不知道如何修复。我的代码运行得非常好,然后我开始出现一个错误,它说“pygame.error:video system not initialized”。这是我的密码: import pygame import random import keyboard pygame.font.init() lines = [random.randint(1,25) for i in range(100)
import pygame
import random
import keyboard
pygame.font.init()
lines = [random.randint(1,25) for i in range(100)]
def line_draw():
array_items = len(lines)
line_color = (0,0,0)
line_width = int(650 / array_items)
list_pos = 1
for i in lines:
start_x = (800 / (array_items + 1)) * list_pos
start_y = 600
end_x = start_x
end_y = 600 - (i * 20)
pygame.draw.line(gameDisplay, line_color, (start_x, start_y), (end_x, end_y), line_width)
list_pos = list_pos + 1
def refill():
gameDisplay.fill((255,255,255))
line_draw()
pygame.display.update()
pygame.time.delay(3)
def bubble_sort():
exchange = True
elements = len(lines)
passes_remaining = elements - 1
while passes_remaining > 0 and exchange:
exchange = False
pygame.event.pump()
for i in range(passes_remaining):
if lines[i] > lines[i+1]:
exchange = True
temp = lines[i]
lines[i] = lines[i+1]
lines[i+1] = temp
refill()
passes_remaining = passes_remaining - 1
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_o:
bubble_sort()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
lines = [random.randint(1,25) for i in range(100)]
line_draw()
gameDisplay.fill((255, 255, 255))
line_draw()
pygame.display.update()
您必须初始化为pygame模块(例如),并且在应用程序循环之前,您必须初始化用于显示的窗口或屏幕:
导入pygame
# [...]
pygame.init()
pygame.display.set_模式((640480))
# [...]
运行=真
运行时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
# [...]
pygame.quit()
并且是missing@Rabbid76谢谢我刚刚意识到我不小心删除了显示启动。