Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I get an error when I log out of my game: (line 145 pygame.display.update(); pygame.error: video system not initialized) #4203

Open
Hronox3 opened this issue Apr 11, 2024 · 1 comment

Comments

@Hronox3
Copy link

Hronox3 commented Apr 11, 2024

import pygame

pygame.init()

screen = pygame.display.set_mode((1280, 748), pygame.FULLSCREEN)
pygame.display.set_caption("PixelVania")
icon = pygame.image.load('images/icon.png').convert_alpha()
pygame.display.set_icon(icon)
clock = pygame.time.Clock()

bg = pygame.image.load('images/bg.png').convert_alpha()
walk_left = [
pygame.image.load('Player_left/image1.png').convert_alpha(),
pygame.image.load('Player_left/image2.png').convert_alpha(),
pygame.image.load('Player_left/image3.png').convert_alpha(),
pygame.image.load('Player_left/image4.png').convert_alpha(),
]

walk_right = [
pygame.image.load('Player_Right/image5.png').convert_alpha(),
pygame.image.load('Player_Right/image6.png').convert_alpha(),
pygame.image.load('Player_Right/image7.png').convert_alpha(),
pygame.image.load('Player_Right/image8.png').convert_alpha(),
]

ghost = pygame.image.load('images/ghost.png').convert_alpha()
ghost_list_in_game = []

player_anim_count = 0
bg_x = 0

player_speed = 8
player_x = 100
player_y = 555

is_jump = False
jump_count = 9

bg_sound = pygame.mixer.Sound('sounds/bg.mp3')
bg_sound.play(-1)

ghost_timer = pygame.USEREVENT + 1
pygame.time.set_timer(ghost_timer, 3000)

label = pygame.font.Font('fonts/Lobster-Regular.ttf', 100)
lose_label = label.render('Игра окончена', False, (255, 255, 255))
restart_label = label.render('Играть снова', False, (0, 0, 0))
quit_label = label.render('Выйти', False, (0, 0, 0))
restart_label_rect = restart_label.get_rect(topleft=(390, 300))
quit_label_rect = quit_label.get_rect(topleft=(500, 450))

bullet = pygame.image.load('images/bullet.png').convert_alpha()
bullets = []
bullets_left = 15
gameplay = True

run = True
while run:
screen.blit(bg, (bg_x, 0))
screen.blit(bg, (bg_x + 1280, 0))

if gameplay:
    player_rect = walk_left[0].get_rect(topleft=(player_x, player_y))

    if ghost_list_in_game:
        for (i, el) in enumerate(ghost_list_in_game):
            screen.blit(ghost, el)
            el.x -= 10

            if el.x < -10:
                ghost_list_in_game.pop(i)

            if player_rect.colliderect(el):
                gameplay = False

    keys = pygame.key.get_pressed()

    if keys[pygame.K_a]:
        screen.blit(walk_left[player_anim_count], (player_x, player_y))
    else:
        screen.blit(walk_right[player_anim_count], (player_x, player_y))

    if keys[pygame.K_a] and player_x > 10:
        player_x -= player_speed
    elif keys[pygame.K_d] and player_x < 500:
        player_x += player_speed

    if not is_jump:
        if keys[pygame.K_SPACE]:
            is_jump = True
    else:
        if jump_count >= -9:
            if jump_count < 0:
                player_y += (jump_count ** 2) / 2
            else:
                player_y -= (jump_count ** 2) / 2
            jump_count -= 1
        else:
            is_jump = False
            jump_count = 9

    if player_anim_count == 3:
        player_anim_count = 0
    else:
        player_anim_count += 1

    bg_x -= 2
    if bg_x == -1280:
        bg_x = 0

    if bullets:
        for (i, el) in enumerate(bullets):
            screen.blit(bullet, (el.x, el.y))
            el.x += 4

            if el.x > 1285:
                bullets.pop(i)

            if ghost_list_in_game:
                for (index, ghost_el) in enumerate(ghost_list_in_game):
                    if el.colliderect(ghost_el):
                        ghost_list_in_game.pop(index)
                        bullets.pop(i)

else:
    screen.fill((87, 88, 89))
    screen.blit(lose_label, (380, 10))
    screen.blit(restart_label, restart_label_rect)
    screen.blit(quit_label, quit_label_rect)
    bg_sound.stop()

    mouse = pygame.mouse.get_pos()
    if restart_label_rect.collidepoint(mouse) and pygame.mouse.get_pressed()[0]:
        gameplay = True
        player_x = 100
        ghost_list_in_game.clear()
        bullets.clear()
        bullets_left = 15
        bg_sound.play(-1)

    if quit_label_rect.collidepoint(mouse) and pygame.mouse.get_pressed()[0]:
        run = False
        pygame.quit()

pygame.display.update()
clock.tick(15)

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        run = False
        pygame.quit()

    if event.type == ghost_timer:
        ghost_list_in_game.append(ghost.get_rect(topleft=(1282, 540)))
    if gameplay and event.type == pygame.KEYUP and event.key == pygame.K_b and bullets_left > 0:
        bullets.append(bullet.get_rect(topleft=(player_x + 40, player_y + 20)))
        bullets_left -= 1
@mikehooper
Copy link

mikehooper commented May 7, 2024

Your code is the issue. You are calling pygame.quit() then the code continues to the update() which no longer has a pygame instance to update.

Either move the pygame.quit outside the while run loop or force the code to exit after existing pygame.quit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants