pygame.error:video system not initialized

北城余情 提交于 2021-02-17 05:22:19

问题


So i wrote this code:

# Pygame development 4
# Focus on making code object oriented
# Introduce classes and objects into our code

# Gain access to the pygame library
import pygame

        # Size of the screen
        SCREEN_TITLE = 'Crossy RPG'
        SCREEN_WIDTH = 500
        SCREEN_HEIGHT = 500
        # Colors according to RGB codes
        WHITE_COLOR = (255, 255, 255)
        BLACK_COLOR = (0, 0 , 0)
        # Clock used to update game events and frames
        clock = pygame.time.Clock()
        pygame.font.init()
        font = pygame.font.SysFont('comicsans', 75)

class Game:

    # Typical rate of 60, equivalent to fps
    TICK_RATE = 60
    # Initializer for the game class to set up the width, height, and title
    def __init__(self, title, width, height):
        self.title = title
        self.width = width
        self.height = height

        # Create the window of specified size in white to display the game
        self.game_screen = pygame.display.set_mode((width, height))
        # Set the game window color to white
        self.game_screen.fill(WHITE_COLOR)
        pygame.display.set_caption(title)

    def run_game_loop(self):
        is_game_over = False


        # Main game loop, used to update all gameplay suh as movement, check, and graphics
        # Runs unit is_game_over = True
        while not is_game_over:
        # A loop to get a;l of the events occuring at any given time
        # Events are most often mouse movement, mouse and button clicks, or eit events
          for event in pygame.event.get():
            # If we have a quite type event(exit out) then exit out of the game loop
            if event.type == pygame.QUIT:
                is_game_over = True

                print(event)


    # Update all game graphics
    pygame.display.update()
    # Tick the clock to update everything within the game
    clock.tick(self.TICK_RATE)

pygame.init()

new_game = Game(SCREEN_TITLE, SCREEN_WIDTH, SCREEN_HEIGHT)
new_game.run_game_loop()



pygame.quit()
quit()

Right now I am learning to code with python so im following a course online and since I couldn't get help from the forums of that website I thought I might ask the question here! So I've looked at the code multiple times to check for spelling mistakes but I couldn't find any and anyway i think that it's' not about something missing but it has something to do with pygame.display.update ! Can somebody pls help me?


回答1:


Without running your code or having a stack trace of where the problem happens, we need to debug the code for you first. So it would be beneficial to add a full stack trace to your questions. I'm pretty confident however that there's two issues that you should work out.

pygame.display.update() should be correctly indented to be in the while loop of your main game event loop. Secondly, the pygame.init() should be run before any other initialization (or at least so I've been taught over the years and every example points to)

Try this out, I think it solves your problem:

# Pygame development 4
# Focus on making code object oriented
# Introduce classes and objects into our code

# Gain access to the pygame library
import pygame

pygame.init()

# Size of the screen
SCREEN_TITLE = 'Crossy RPG'
SCREEN_WIDTH = 500
SCREEN_HEIGHT = 500
# Colors according to RGB codes
WHITE_COLOR = (255, 255, 255)
BLACK_COLOR = (0, 0 , 0)
# Clock used to update game events and frames
clock = pygame.time.Clock()
pygame.font.init()
font = pygame.font.SysFont('comicsans', 75)

class Game:

    # Typical rate of 60, equivalent to fps
    TICK_RATE = 60
    # Initializer for the game class to set up the width, height, and title
    def __init__(self, title, width, height):
        self.title = title
        self.width = width
        self.height = height

        # Create the window of specified size in white to display the game
        self.game_screen = pygame.display.set_mode((width, height))
        # Set the game window color to white
        self.game_screen.fill(WHITE_COLOR)
        pygame.display.set_caption(title)

    def run_game_loop(self):
        is_game_over = False


        # Main game loop, used to update all gameplay suh as movement, check, and graphics
        # Runs unit is_game_over = True
        while not is_game_over:
        # A loop to get a;l of the events occuring at any given time
        # Events are most often mouse movement, mouse and button clicks, or eit events
            for event in pygame.event.get():
                # If we have a quite type event(exit out) then exit out of the game loop
                if event.type == pygame.QUIT:
                    is_game_over = True

                    print(event)


                # Update all game graphics
                pygame.display.update()
                # Tick the clock to update everything within the game
                clock.tick(self.TICK_RATE)

new_game = Game(SCREEN_TITLE, SCREEN_WIDTH, SCREEN_HEIGHT)
new_game.run_game_loop()

pygame.quit()

This also seams to be a school assignment and not a online course (but I might be wrong here), never the less I'll leave this piece of advice if I'm right. I strongly suggest that if you bump into problems, ask your teacher for guidance. As there's always a reason for teachers giving you a challenge/problem to solve. It teaches you the latest techniques you've learned in class, and if you can't solve the problem with the tools that you've been given - you've most likely haven't learned the fundamentals that has been taught out - and you should really re-do some steps.



来源:https://stackoverflow.com/questions/62027477/pygame-errorvideo-system-not-initialized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!