#python #pygame
#python #pygame
Вопрос:
Я работал над приложением сортировки на python, используя pygame для визуальных целей, и теперь у меня возникла ошибка, которую я не уверен, как исправить. Мой код работал отлично, а затем я начал получать сообщение об ошибке «pygame.error: видеосистема не инициализирована». Вот мой код:
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()
Комментарии:
1.
pygame.init()
иpygame.display.set_mode((width, height))
отсутствует2. @Rabbid76, спасибо! Я только что понял, что случайно удалил инициализацию отображения.
Ответ №1:
Вы должны инициализироваться модулями pygame (например, pygame.init()
), и вы должны инициализировать окно или экран для отображения с помощью pygame.display.set_mode
, перед циклом приложения:
import pygame
# [...]
pygame.init()
pygame.display.set_mode((640, 480))
# [...]
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# [...]
pygame.quit()