1
votes

My first game, more precisely, its initial appearance (without especially beautiful icons, avatars, backgrounds and objects), was almost ready, and started perfectly, until I wanted to add a start menu, in which it would be possible to change the game parameters and, in fact, launch its button 'Start'. However, after a few changes to make the menu appear, it turned out that I had some kind of problems with int game () and a black screen. I deleted the previously made changes and tried to start the game without a menu, which it originally was, but the black screen still followed me, and i saw this thing:

Traceback (most recent call last):
  File "C:\Users\olive\AppData\Local\Temp\main.py\debug.py", line 40, in <module>
    keys = pygame.key.get_pressed()
pygame.error: video system not initialized

Tired of looking for answers on foreign and local forums, I decided to contact the inhabitants of stackover. I really hope for your help. Here, in fact, is the code itself (which I compressed as much as I could):

Import pygame, sys

pygame.init()
pygame.font.init()

window = pygame.display.set_mode((1000, 690))

screen = pygame.Surface((1000, 690))

player = pygame.Surface((60, 60))

zet = pygame.Surface((60, 60))

arrow = pygame.Surface((20, 40))

count = 0

a_x = 1000
a_y = 1000

strike = False

z_x = 0
z_y = 0

x_p = 0
y_p = 640


z_right = True


done = False

while done == False:
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            pygame.quit()
            done = True
        keys = pygame.key.get_pressed() #ошибка здесь
        if keys[pygame.K_w]:
            y_p -= 5
        if keys[pygame.K_s]:
            y_p += 5
        if keys[pygame.K_a]:
            x_p -= 5
        if keys[pygame.K_d]:
            x_p += 5 
        if e.type == pygame.KEYDOWN and e.key == pygame.K_SPACE:
            if strike == False:
                strike = True
                a_x = x_p
                a_y = y_p - 40
            if strike:
                a_y -= 1.5
                if a_y < 0:
                    strike = False
                    a_y = 1000
                    a_x = 1000

            if inter(a_x, a_y, z_x, z_y, 20, 40):
                count += 1
                strike = False
                a_y = 1000
                a_x = 1000

            if z_right:
                z_x += 1
                if z_x > 960:
                    z_x -=1
                z_right = False
            else:
                z_x -=1
                if z_x < 0:
                     z_x += 1
                z_right = True


    string = myfont.render('Счёт: '+str(count), 0, (255,0,0))
    screen.fill((0,225,0))
    screen.blit(string, (0, 430))
    screen.blit(arrow, (a_x, a_y))
    screen.blit(zet, (z_x, z_y))
    screen.blit(player, (x_p, y_p))
    window.blit(screen, (0, 0))
    pygame.display.update()

pygame.quit()
exit()

By the way, I'm not sure I have saw all the questions on the forums and the answers I could, so I might have missed some solution. So, I do not deny that my question may be similar to the others on this site, do not consider it a copy-paste or flood.

1
Please fix the indentation in the code you posted. As it stands it won't run. - BoarGules

1 Answers

0
votes

This entire part can be changed

        keys = pygame.key.get_pressed() #ошибка здесь
        if keys[pygame.K_w]:
            y_p -= 5
        if keys[pygame.K_s]:
            y_p += 5
        if keys[pygame.K_a]:
            x_p -= 5
        if keys[pygame.K_d]:
            x_p += 5 
    
    if e.type == pygame.KEYDOWN and e.key == pygame.K_SPACE:
        # some code

To

        if e.type == pygame.KEYDOWN:
            if e.key == pygame.K_SPACE:
                # some code
            if e.key == pygame.K_w:
                y_p -= 5
            if e.key == pygame.K_s:
                y_p += 5
            if e.key == pygame.K_a:
                x_p -= 5
            if e.key == pygame.K_d:
                x_p += 5 

You can make a lot of changes to your code

Import pygame, sys 
# to
import pygame, sys
# few variables
a_x = 1000
a_y = 1000
# to
a_x = a_y = 1000 

# next in the main loop
while done == False:
# this is not good
while not done:
# this is better practice

# and lastly
        if e.type == pygame.QUIT:
            pygame.quit()
            done = True
# you don't have to do pygame.quit inside the main loop becoz you already called it outside
pygame.quit()
exit()
# also change exit() to
sys.exit()