태지쌤

로봇 & 코딩교육 No.1 크리에이터

파이썬

Google Colab에서 pygame 사용 가능할까?

태지쌤 2022. 7. 18. 13:48
반응형

How can we use pygame on Google Colab?

pygame 모듈 설치는 됩니다.

!pip install pygame

설치 후 불러오는 것도 잘 됩니다.

import pygame

하지만...  게임 화면이 만들어지지는 않습니다.

from pygame.locals import *

pygame.init()

display_width = 600
display_height = 800
DISPLAY = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption("게임 제작")
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-6-863bff6e499f> in <module>()
      5 display_width = 600
      6 display_height = 800
----> 7 DISPLAY = pygame.display.set_mode((display_width, display_height))
      8 pygame.display.set_caption("게임 제작")

error: No available video device

 

스택오버플로우에 동일한 사례가 질문으로 올라와 있습니다.


PyGame can use only local video card and local monitor.

When you run code on server then it try to use video card installed in server and monitor connected to this server which you can't see.

But you can't even run it because usually servers don't have video card and monitor because nobody could see this monitor (except server's admin).


There is a way to run pygame in Colab with some limitations.

  1. Fool the system to think there is a video device
  2. Capture the pygame screen and copy it to another interface compatible with Colab like OpenCV or matplotlib
  3. Use output library to clear the cell.

Not the most elegant way, but it gets it done.

Take a look at this notebook I created: (https://colab.research.google.com/drive/1xtiBrGeRHmXY3KSOixkZBf_rJIgBImJu?usp=sharing)

Please notice there are limitations:

  1. Can't read inputs from keyboard or mouse
  2. Can't play audios

In my case I was interested to use pygame for animation purposes so I wasn't too fuzzed about it.


세상엔 참 똑똑한 사람이 많습니다.

코랩에서 파이게임 실행하는 방법이 있긴 합니다.

대신 절차가 아래와 같이 복잡할 뿐입니다.

해보니 되긴 합니다.

그런데 이럴 바에는 그냥.... 기본 파이썬 IDLE 사용하는게 낫겠죠~?^^;;;

import cv2
from google.colab.patches import cv2_imshow
from google.colab import output
import time 
import os, sys
# set SDL to use the dummy NULL video driver, 
#   so it doesn't need a windowing system.
os.environ["SDL_VIDEODRIVER"] = "dummy"
import pygame

pygame.init()
screen = pygame.display.set_mode((400, 300))
done = False
is_blue = True
x = 30
y = 30

while not done:
        for event in pygame.event.get():
                if event.type == pygame.QUIT:
                        done = True
        
        color = (255, 100, 0)
        pygame.draw.rect(screen, color, pygame.Rect(x, y, 60, 60))
        
        pygame.display.flip()

        #convert image so it can be displayed in OpenCV
        view = pygame.surfarray.array3d(screen)

        #  convert from (width, height, channel) to (height, width, channel)
        view = view.transpose([1, 0, 2])

        #  convert from rgb to bgr
        img_bgr = cv2.cvtColor(view, cv2.COLOR_RGB2BGR)

        #Display image, clear cell every 0.5 seconds
        cv2_imshow(img_bgr)
        time.sleep(0.5)
        output.clear()

https://colab.research.google.com/drive/1xtiBrGeRHmXY3KSOixkZBf_rJIgBImJu?usp=sharing#scrollTo=EMk8HL92ATLL

반응형