-2

a youtube video told me to put what I've already put.

https://www.youtube.com/watch?v=bVx2nhB0t1o&feature=youtu.be

My window opens, but just stays at a black screen. When I close the window, then I get the error message:

Traceback (most recent call last):
  File "./invaders.py", line 23, in <module>
    keys = pygame.key.get_pressed()
pygame.error: video system not initialized

I don't really understand why this is happening and any help would be much appreciated.

import pygame
pygame.init()
win = pygame.display.set_mode((500, 500))

pygame.display.set_caption("Space Invaders")

x = 50
y = 50
width = 30
height = 30
vel = 5

run = True
while run:
    pygame.time.delay(100)

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

pygame.quit()

keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
    x -= vel
if keys[pygame.K_RIGHT]:
    x += vel
if keys[pygame.K_UP]:
    y -= vel
if keys[pygame.K_DOWN]:
    y += vel

win.fill(0)
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
pygame.display.update()
2
  • 4
    Your question was probably down-voted because you need to include: What you expected to happen, what did happen (including any error messages), and what you did to try to fix it. Whereas you've included some source code and a link to some video. It appears like you haven't made much effort in solving the problem yourself, yet you expect the people answering to spend their time on another site watching a video, so they can fix your code. Do you see how this might upset people?
    – Kingsley
    Oct 18, 2020 at 3:03
  • 3
    Stack Overflow is intended as a reference for others who may encounter the same problem in the future, not just for the person asking the question. Removing important information from the question prevents people from understanding the full context. If it's important for you to be the last editor for some reason, you can make some edit that doesn't remove the error message (which is important to understanding the problem) or complain about editors. Any edits that do either of those things, however, will be rolled back.
    – Ryan M
    Oct 26, 2020 at 22:45

1 Answer 1

2

Your code is mixed-up. Generally PyGame applications have a "main loop" which handles the event loop, processes any user input, and then re-draws the screen.

Your code has all of these elements, but a whole block of code is not inside the main loop, it's only executed after the window has been closed. You need to be careful of the placement and indentation.

This code below is basically your exact code, re-arranged with some tweaks.

import pygame

BLACK = (  0,  0,  0 )   # colours
RED   = (255,  0,  0 )

pygame.init()
win = pygame.display.set_mode((500, 500))

pygame.display.set_caption("Space Invaders")

x = 50
y = 50
width = 30
height = 30
vel = 5

clock = pygame.time.Clock()
run = True

# Main loop
while run:
    #pygame.time.delay(100)   # <-- don't use delays like this

    # handle the PyGame event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    # handle user movement
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        x -= vel
    if keys[pygame.K_RIGHT]:
        x += vel
    if keys[pygame.K_UP]:
        y -= vel
    if keys[pygame.K_DOWN]:
        y += vel

    # repaint the window
    win.fill( BLACK )
    pygame.draw.rect( win, RED, ( x, y, width, height ) )
    pygame.display.update()

    clock.tick( 10 )  # limit the FPS here

# main loop has ended, quit
pygame.quit()

Now the window painting, and user-input handling is now moved inside the scope of the main loop.

Also it's best not to add fixed time delays into PyGame code. Adjust the frame rate using a PyGame clock.

2
  • So basically, everything important in my game needs to be included in the main loop, right?
    – Shane
    Oct 18, 2020 at 4:17
  • @Shane - Yeah, pretty much. You can put parts in functions too, but the main loop is where everything is triggered from.
    – Kingsley
    Oct 18, 2020 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.