0

Только изучаю питон и поэтому прошу не ругаться на кривой код.

Такая проблема: после переноса массивов с картинками (для анимации персонажа) в другой файл и вызова этих массивов появилась такая ошибка:

File "C:\Users\...\Desktop\pm\dog-run\player_anim.py", line 10, in <module>
    pygame.image.load('img/dog/idle/idle1.png').convert_alpha(),
pygame.error: No video mode has been set

Main файл:

import pygame

import player_anim

clock = pygame.time.Clock()

pygame.init()

display_width = 1750                                             # <== Display settings
display_height = 793
screen = pygame.display.set_mode((display_width, display_height))

pygame.display.set_caption("Dog-run")                          # <== IMG Stuff
icon = pygame.image.load('img/icon.png').convert_alpha()
pygame.display.set_icon(icon)

bg = pygame.image.load('img/bg/Preview/Background.png').convert_alpha()

slime = pygame.image.load('img/slime/idle/slime1.png').convert_alpha()          # <== slime

slime_list_in_game = [

]


slime_x = 750
slime_y = 700

player = pygame.image.load('img/dog/idle/idle1.png').convert_alpha()               # <== Player Settings
player_speed = 3

player_x = 150
player_y = 700

is_jump = False
jump_count = 7


bg_music = pygame.mixer.Sound('music/music 1.mp3')               # <== bg music
bg_music.play()

slime_timer = pygame.USEREVENT + 1
pygame.time.set_timer(slime_timer, 2600)

running = True
while running:

    screen.blit(bg, (player_anim.bg_x, 0))                                      # <== Object Display
    screen.blit(bg, (player_anim.bg_x + 928, 0))

    player_rect = player_anim.player_right[1].get_rect(topleft=(player_x, player_y))

    if slime_list_in_game:
        for el in slime_list_in_game:
            screen.blit(slime, el)
            el.x -= 10

            if player_rect.colliderect(el):
                print("You Lose!")

    keys = pygame.key.get_pressed()

    if keys[pygame.K_d]:
        screen.blit(player_anim.player_right[player_anim.player_anim_count], (player_x, player_y))

    else:
        screen.blit(player_anim.player_idle[player_anim.player_anim_count], (player_x, player_y))

    if keys[pygame.K_d] and player_speed < 200:
        player_x += player_speed

    elif keys[pygame.K_a] and player_x > 50:
        player_x -= player_speed

    if not is_jump:
        if keys[pygame.K_SPACE]:
            is_jump = True
    else:
        if jump_count >= -7:
            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 = 7

    if player_anim.player_anim_count == 4:                                         # <== Animation of player object
        player_anim_count = 0
    else:
        player_anim.player_anim_count += 1

    player_anim.bg_x -= 2

    if player_anim.bg_x == -918:                                                    # <== Bg animation (If i need)
        player_anim.bg_x = 0

    pygame.display.update()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            pygame.quit()
        if event.type == slime_timer:
            slime_list_in_game.append(slime.get_rect(topleft=(1990, 700)))

    clock.tick(13) `




player anim файл:
`import pygame


clock = pygame.time.Clock()

pygame.init()


player_idle = [
    pygame.image.load('img/dog/idle/idle1.png').convert_alpha(),
    pygame.image.load('img/dog/idle/idle2.png').convert_alpha(),
    pygame.image.load('img/dog/idle/idle3.png').convert_alpha(),
    pygame.image.load('img/dog/idle/idle4.png').convert_alpha(),
    pygame.image.load('img/dog/idle/idle5.png').convert_alpha(),
]


player_right = [
     pygame.image.load('img/dog/walk/walk1.png').convert_alpha(),
     pygame.image.load('img/dog/walk/walk2.png').convert_alpha(),
     pygame.image.load('img/dog/walk/walk3.png').convert_alpha(),
     pygame.image.load('img/dog/walk/walk4.png').convert_alpha(),
     pygame.image.load('img/dog/walk/walk5.png').convert_alpha(),
    ]


player_anim_count = 3
bg_x = 0

Объясните, пожалуйста, что здесь не так, и как это исправить.

1 ответ 1

0
Ошибка "No video mode has been set" возникает, когда вы пытаетесь загрузить изображение, не установив перед этим видео режим. В вашем случае, проблема заключается в том, что файл player_anim.py использует pygame модуль для загрузки изображений без предварительного создания окна игры с определенными настройками.

Для решения этой проблемы, нужно удалить строки импорта и использования pygame в файле player_anim.py. Вы можете загрузить массивы с картинками в основном файле игры и затем использовать их при обновлении игры.

import pygame

clock = pygame.time.Clock()

pygame.init()

display_width = 1750                                             # <== Display settings
display_height = 793
screen = pygame.display.set_mode((display_width, display_height))

pygame.display.set_caption("Dog-run")                          # <== IMG Stuff
icon = pygame.image.load('img/icon.png').convert_alpha()
pygame.display.set_icon(icon)

bg = pygame.image.load('img/bg/Preview/Background.png').convert_alpha()

slime = pygame.image.load('img/slime/idle/slime1.png').convert_alpha()

slime_list_in_game = []

slime_x = 750
slime_y = 700

player_idle = [
    pygame.image.load('img/dog/idle/idle1.png').convert_alpha(),
    pygame.image.load('img/dog/idle/idle2.png').convert_alpha(),
    pygame.image.load('img/dog/idle/idle3.png').convert_alpha(),
    pygame.image.load('img/dog/idle/idle4.png').convert_alpha(),
    pygame.image.load('img/dog/idle/idle5.png').convert_alpha(),
]

player_right = [
     pygame.image.load('img/dog/walk/walk1.png').convert_alpha(),
     pygame.image.load('img/dog/walk/walk2.png').convert_alpha(),
     pygame.image.load('img/dog/walk/walk3.png').convert_alpha(),
     pygame.image.load('img/dog/walk/walk4.png').convert_alpha(),
     pygame.image.load('img/dog/walk/walk5.png').convert_alpha(),
]

player = player_idle[0]               # <== Player Settings
player_speed = 3

player_x = 150
player_y = 700

is_jump = False
jump_count = 7

bg_music = pygame.mixer.Sound('music/music 1.mp3')               # <== bg music
bg_music.play()

slime_timer = pygame.USEREVENT + 1
pygame.time.set_timer(slime_timer, 2600)

running = True
while running:

    screen.blit(bg, (0, 0))                                      # <== Object Display
    screen.blit(bg, (928, 0))

    player_rect = player.get_rect(topleft=(player_x, player_y))

    if slime_list_in_game:
        for el in slime_list_in_game:
            screen.blit(slime, el)
            el.x -= 10

            if player_rect.colliderect(el):
                print("You Lose!")

    keys = pygame.key.get_pressed()

    if keys[pygame.K_d]:
        screen.blit(player_right[player_anim_count], (player_x, player_y))
    else:
        screen.blit(player_idle[player_anim_count], (player_x, player_y))

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

    if not is_jump:
        if keys[pygame.K_SPACE]:
            is_jump = True
    else:
        if jump_count >= -7:
            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 = 7

    if player_anim_count == 4:                                         # <== Animation of player object
        player_anim_count = 0
    else:
        player_anim_count += 1

    pygame.display.update()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            pygame.quit()
        if event.type == slime_timer:
            slime_list_in_game.append(slime.get_rect(topleft=(1990, 700)))

    clock.tick(13)

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.