1

Let's say I have a simple program as shown below that I want to run on the Cloud IDE Codespaces in GitHub.

import pygame
from pygame.locals import *
pygame.init()
pygame.display.init()

Now, if I run the program, I get a bunch of errors:

/home/codespace/.python/current/bin/python3 /workspaces/Rian/help/testing.py
pygame 2.1.2 (SDL 2.0.16, Python 3.10.4)
Hello from the pygame community. https://www.pygame.org/contribute.html
ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4732:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4732:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
ALSA lib conf.c:4732:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5220:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM default
Traceback (most recent call last):
  File "/workspaces/Rian/help/testing.py", line 4, in <module>
    pygame.display.init()
pygame.error: No available video device

I want to shift the focus of this question to the last error as the ALSA errors don't seem to be that fatal. (Code still works with ALSA errors and there are plenty questions about these ones already)

How do I resolve the display error?

I understand that this error occurs because SDL is executed on a Cloud IDE and not a standard machine. I have browsed SO and it seems that this is frequently occurring on Linux machines. The solutions often include changing the display environment variables (I think) to resolve the problem. I checked with echo $DISPLAY that there is currently no variable set like ":0" or ":0.0". I don't quite understand the concept of this approach because I don't know what these are for nor do I know how to set them properly. I tried both export DISPLAY=:0 and export DISPLAY=:0.0 but the error persists.
This answer suggests using

import os
os.environ["SDL_VIDEODRIVER"] = "dummy"

I tried that (despite it being a workaround according to the comments) and the error does disappear but somehow it just freezes a normal pygame program. So not a solution either.

There was no question on SO specifically for Codespaces and pygame, which is why I created this question. However, I found a question with Codespaces and mathplotlib (which I unfortunately can't find right now) that had a similar problem. One of the answers shows that there is an integrated function for mathplotlib if the OS isn't XWindows (if I understand this correctly) that solves the problem:

mathplotlib.use("Agg")

Is there some equivalent method for pygame? If so, that would be an easy fix.

This is a similar question with another online coding website. The answers said that they "can't think of any options for running pygame windows online". So maybe this is just not possible for anything that doesn't run locally? This answer to another question further suggests that.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Blyfh
  • 43
  • 5
  • Was asked before: https://stackoverflow.com/questions/74943815/how-to-run-a-pygame-projects-on-a-github-codespace – Rabbid76 Jan 19 '23 at 15:51
  • @Rabbid76 Thanks for the link! I didn't find any posts regarding pygame on Codespaces so I feel very dumb now. But that answers my question. – Blyfh Jan 19 '23 at 16:09

0 Answers0