Search code examples
pythonpython-2.7pygameraspberry-pi

Pygame on Pi running through Putty, no screen, no input


I'm trying to use Pygame and Python 2.7 to control a Roomba connected to a Raspberry Pi. I'm connected to the Pi through Putty and controlling it through the Putty console. The problem I'm having is that I can't get a Pygame display to show, and Pygame keyboard input only works when the Pygame screen has focus. There's just no window opened when the code runs and Putty's console just sits there. Is there a way to open the Pygame window this way?

I have working code that doesn't use Pygame, but it uses a getch for input so you can only toggle movement through incoming characters, you can't get it to stop moving when you stop holding down the key.

Here is my basic code where I'm just trying to get Pygame to do ANYTHING when I press a key:

import pygame, sys
from pygame.locals import *

pygame.init()

screen = pygame.display.set_mode((600, 400))
pygame.display.flip()

while 1:
key=pygame.key.get_pressed()
if key[pygame.K_w]:print'w'

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        sys.exit()
    elif event.type == KEYDOWN and event.key == K_ESCAPE:
        sys.exit()
    elif event.type == KEYDOWN and event.key == K_s:
        print's'

Solution

  • This sounds like you need to run Pygame "headless":

    http://pygame.org/wiki/HeadlessNoWindowsNeeded

    import os
    import pygame.display
    
    os.environ["SDL_VIDEODRIVER"] = "dummy" # or maybe 'fbcon'
    pygame.display.init()
    screen = pygame.display.set_mode((1,1))
    

    This answer may be useful too: Pygame headless setup

    Note that these require the keyboard to be connected directly to the Raspberry Pi itself - not the computer you're running PuTTY from.

    If this is what you actually want, the approach in here could be useful: Pygame through SSH does not register keystrokes (Raspberry Pi 3)