I made my first game today. The first error is a video error. pygame.error: video system not initialized
I'm getting a video system error in my Pygame source code. I'm writing a python file using VSCODE on a MacBook Pro.
When I run the file with the arrow symbol in the upper right corner of VSCODE, nothing happens.
This is a game where you have to avoid hitting the cave. The code is copied directly from the text Kindle, so there are no typos. Also, I'm getting a wavy line error in blit and xpos saying there is no value.
Can anyone tell me why? By the way, I was able to make the sample alien game from Pygame itself, and other home-made files.
When I run it in VSCODE debug mode, the window comes up, but it stops at the beginning. When I run it in normal mode, I still get a video error.
""" cave - Copyright 2016 """
import sys
from random import randint
import pygame
from pygame.locals import QUIT, Rect, KEYDOWN, K_SPACE
pygame.init()
pygame.key.set_repeat(5, 5)
SURFACE = pygame.display.set_mode((800, 600))
FPSCLOCK = pygame.time.Clock()
def main():
walls = 80
ship_y = 250
velocity = 0
score = 0
slope = randint(1, 6)
sysfont = pygame.font.SysFont(None, 36)
ship_image = pygame.image.load("ship.png")
bang_image = pygame.image.load("bang.png")
holes = []
for xpos in range(walls):
holes.append(Rect(xpos * 10, 100, 10, 400))
game_over = False
while True:
is_space_down = False
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_SPACE:
is_space_down = True
if not game_over:
score += 10
velocity += -3 if is_space_down else 3
ship_y += velocity
edge = holes[-1].copy()
test = edge.move(0, slope)
if test.top <= 0 or test.bottom >= 600:
slope = randint(1, 6) * (-1 if slope > 0 else 1)
edge.inflate_ip(0, -20)
edge.move_ip(10, slope)
holes.append(edge)
del holes[0]
holes = [x.move(-10, 0) for x in holes]
if holes[0].top > ship_y or \
holes[0].bottom < ship_y + 80:
game_over = True
SURFACE.fill((0, 255, 0))
for hole in holes:
pygame.draw.rect(SURFACE, (0, 0, 0), hole)
SURFACE.blit(ship_image, (0, ship_y))
score_image = sysfont.render("score is {}".format(score),
True, (0, 0, 225))
SURFACE.blit(score_image, (600, 20))
if game_over:
SURFACE.blit(bang_image, (0, ship_y-40))
pygame.display.update()
FPSCLOCK.tick(15)
if __name__ == '__main__':
main()
error
MacBook-Pro Pygame % /Library/Developer/CommandLineTools/usr/bin/python3 /Users/username/Pygame/cave.py
pygame 2.1.2 (SDL 2.0.18, Python 3.8.2)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "/Users/username/Pygame/cave.py", line 8, in <module>
pygame.key.set_repeat(5, 5)
pygame.error: video system not initialized