I am trying to learn how to make a game of Snake in Python. I found a tutorial using pygame, but I'm having trouble making it work. Here's my code :
import os, pygame
os.environ["SDL_VIDEODRIVER"] = "dummy"
pygame.init()
dis = pygame.display.set_mode((400,300))
pygame.display.set_caption('Test')
while 1 :
pygame.display.update()
pygame.quit()
quit()
The problem is that the game window simply doesn't open. I get ALSA lib pcm.c:8424:(snd_pcm_recover) underrun occurred
errors but I think those are related to the sound. Other than that, no message in the console.
The code is running in a debian 11 VM inside a Windows 7 host.
CodePudding user response:
Delete the os.environ["SDL_VIDEODRIVER"] = "dummy"
You can use PyGame without opening a visible display (e.g. for testing, or for integrating with other frameworks that have their own displays) by using SDL environment variables before you initialise pygame. Environment variables can be set with the os.environ dict in python.
CodePudding user response:
As the documentation states, the dummy mode is designed to create an SDL environment without opening a window. Because in this case, we do actually want to create a window, You should remove the line os.environ["SDL_VIDEODRIVER"] = "dummy"
. This does also mean that you don't have to import the os
module.