1

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'
Bonzai
  • 23
  • 4

1 Answers1

1

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)

Community
  • 1
  • 1
glennji
  • 349
  • 1
  • 4
  • I've tried this as well (and I just tried it again now for good measure). Even with this all that happens when I press keys is that they type into Putty's console. I can't get keypress events to fire still. – Bonzai Mar 05 '17 at 00:23
  • Ooooooh, I get it now -- it's because it's PuTTY (SSH). I bet if you connected a keyboard to the RPi's USB port, it would work. I'll have a look and see if there's something you can do about remote keyboard input. – glennji Mar 05 '17 at 01:06
  • Otherwise, I think you need to pass the keys via another process ala http://stackoverflow.com/questions/38907681/pygame-through-ssh-does-not-register-keystrokes-raspberry-pi-3#38908145 – glennji Mar 05 '17 at 01:13
  • You're right, it's working with a keyboard connected directly to the Pi. I was able to get the movement code in now and that works perfectly too. The only downside is now I have to use a wireless keyboard and I can't use a gamepad unless I code that directly to the Pi, but it controls better now. Thanks a lot! – Bonzai Mar 05 '17 at 02:18