Pi上的Pygame通过Putty运行,没有屏幕,没有输入 [英] Pygame on Pi running through Putty, no screen, no input

查看:48
本文介绍了Pi上的Pygame通过Putty运行,没有屏幕,没有输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Pygame 和 Python 2.7 来控制连接到 Raspberry Pi 的 Roomba.我通过 Putty 连接到 Pi 并通过 Putty 控制台控制它.我遇到的问题是我无法显示 Pygame 显示,并且 Pygame 键盘输入仅在 Pygame 屏幕具有焦点时才有效. 代码运行时没有打开窗口,Putty 的控制台只是坐在那里.有没有办法以这种方式打开 Pygame 窗口?

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?

我有不使用 Pygame 的工作代码,但它使用 getch 进行输入,因此您只能通过传入的字符切换移动,当您停止按住键时,您无法让它停止移动.

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.

这是我的基本代码,当我按下一个键时,我只是想让 Pygame 做任何事情:

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'

推荐答案

这听起来像是你需要he​​adless"运行 Pygame:

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))

这个答案也可能有用:Pygame headless setup

请注意,这些要求键盘直接连接到 Raspberry Pi 本身 - 而不是您运行 PuTTY 的计算机.

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

如果这是您真正想要的,这里的方法可能很有用:通过 SSH 的 Pygame 不注册击键 (Raspberry Pi 3)

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

这篇关于Pi上的Pygame通过Putty运行,没有屏幕,没有输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆