It showed video system not initialized

Viewed 36

So i jsut started learning python but right now when i run this and try to jump it loads this error and also the rectangle dont move if i hold it it only moves onces i just started and i really dont know what the problem is

This is what it showed: keys = pygame.key.get_pressed() pygame.error: video system not initialized

import pygame
pygame.init()


win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("Hello")

x = 100
y = 100
width = 50
height = 50
vel = 10

jump_count = 10
jump = False
run = True

while run:
    pygame.draw.rect(win, (255, 255, 255), (x, y, width, height))
    pygame.display.update()
    for event in pygame.event.get():


    if event.type == pygame.QUIT:
        pygame.quit()

    keys = pygame.key.get_pressed()


    if keys[pygame.K_RIGHT] and x < 450:
        x += vel
    if keys[pygame.K_LEFT] and x > 0:
        x -= vel
    if not jump:
        if keys[pygame.K_DOWN] and y < 450:
            y += vel
        if keys[pygame.K_UP] and y > 0:
            y -= vel
        if keys[pygame.K_SPACE]:
            jump = True
    else:
        if jump_count >= -10:
            neg = 1
            if jump_count < 0:
                neg = -1

            y -= (jump_count ** 2) /2 * neg
            jump_count = -1
        else:
            jump = False
            jump_count = 10


    win.fill((0, 0, 0))
1 Answers

It is a matter of Indentation. Correct the indentation of your code and it will work fine.

Don't uninitialize all pygame modules in the event loop. Terminate the application loop and call pygame.quit() after the application loop:

run = True

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

    # [...]

pygame.quit()

Use pygame.time.Clock to control the frames per second and thus the game speed.

The method tick() of a pygame.time.Clock object, delays the game in that way, that every iteration of the loop consumes the same period of time.
That means that the loop:

clock = pygame.time.Clock()
run = True
while run:
   clock.tick(60)

runs 60 times per second.

In addition, your code contains a typo. You have to decrement the variable jump_count than setting it to -1:

jump_count = -1

jump_count -= 1

Working code:

import pygame
pygame.init()

win = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
pygame.display.set_caption("Hello")

x = 100
y = 100
width = 50
height = 50
vel = 10

jump_count = 10
jump = False
run = True

while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_RIGHT] and x < 450:
        x += vel
    if keys[pygame.K_LEFT] and x > 0:
        x -= vel
    if not jump:
        if keys[pygame.K_DOWN] and y < 450:
            y += vel
        if keys[pygame.K_UP] and y > 0:
            y -= vel
        if keys[pygame.K_SPACE]:
            jump = True
    else:
        if jump_count >= -10:
            neg = 1
            if jump_count < 0:
                neg = -1

            y -= (jump_count ** 2) /2 * neg
            jump_count -= 1
        else:
            jump = False
            jump_count = 10

    win.fill((0, 0, 0))
    pygame.draw.rect(win, (255, 255, 255), (x, y, width, height))
    pygame.display.update()

pygame.quit()
Related