Pygame 通过 SSH 不注册击键 (Raspberry Pi 3) [英] Pygame through SSH does not register keystrokes (Raspberry Pi 3)
问题描述
所以我得到了 raspi 3 和简单的 8x8 LED 矩阵.在玩了一段时间之后,我决定用 pygame 的事件制作一个简单的蛇游戏(显示在该矩阵上),我之前没有使用 pygame 的经验.除了led矩阵外,没有连接任何屏幕/显示器.
So I got raspi 3 and simple 8x8 LED matrix. After some playing with it I decided to make a simple snake game (displaying on that matrix) with pygame's events, I have no prior experience with pygame. There is no screen/display connected besides the led matrix.
所以一开始的问题是pygame.error: video system not initialized",虽然我认为我通过设置环境变量来解决它:os.putenv('DISPLAY', ':0.0')
现在我让它工作了,我运行它......没有任何反应,就像没有注册击键一样.就是这个垃圾",不知道怎么称呼 LED 上的点矩阵不动.如果我在循环中的某处改变蛇的 x 或 y 位置,它会按预期移动.
So the problem at first was "pygame.error: video system not initialized", though I think i got it fixed by setting an env variable:
os.putenv('DISPLAY', ':0.0')
Now that I got it working I run it...and nothing happens, like no keystrokes are registered. Just this "junk", I don't know how to call it The dot on LED matrix is not moving. If i alter the snake's x or y position somewhere in the loop it moves as intended.
我的代码:
#!/usr/bin/python2
import pygame
import max7219.led as led
from max7219.font import proportional, SINCLAIR_FONT, TINY_FONT, CP437_FONT
import numpy as nqp
import os
SIZE = (8, 8)
class Board:
def __init__(self, size, snake):
"Board object for snake game"
self.matrix = np.zeros(size, dtype=np.int8)
self.device = led.matrix()
self.snake = snake
def draw(self):
#add snake
self.matrix = np.zeros(SIZE, dtype=np.int8)
self.matrix[self.snake.x][self.snake.y] = 1
for x in range(8):
for y in range(8):
self.device.pixel(x, y, self.matrix[x][y], redraw=False)
self.device.flush()
def light(self, x, y):
"light specified pixel"
self.matrix[x][y] = 1
def dim(self, x, y):
"off specified pixel"
self.matrix[x][y] = 0
class Snake:
def __init__(self):
"Object representing an ingame snake"
self.length = 1
self.x = 3
self.y = 3
if __name__=="__main__":
os.putenv('DISPLAY', ':0.0')
pygame.init()
snake = Snake()
board = Board(SIZE, snake)
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
snake.y -= 1
elif event.key == pygame.K_DOWN:
snake.y += 1
elif event.key == pygame.K_LEFT:
snake.x -= 1
elif event.key == pygame.K_RIGHT:
snake.x += 1
board.draw()
我正在使用 pygame,因为我不知道其他任何东西(好吧,我也不能使用 pygame,但我只是不知道任何替代方案).如果它可以做得更简单,我会很乐意去做.提前谢谢你!
I'm using pygame because I don't know anything else (Well I can't use pygame either but I just don't know of any alternatives). If it can be done simpler I will be happy to do it. Thank You in advance!
推荐答案
您应该能够使用curses.这是一个简单的例子:
You should be able to use curses. Here's a simple example:
import curses
def main(screen):
key = ''
while key != 'q':
key = screen.getkey()
screen.addstr(0, 0, 'key: {:<10}'.format(key))
if __name__ == '__main__':
curses.wrapper(main)
您会看到您的按键已注册 - 它们只是字符串.
You'll see that your key presses are registered - they're just strings.
然而,它以阻塞模式运行.假设你的代码需要做其他事情,你可以开启nodelay
:
However, this runs in blocking mode. Assuming that your code needs to do other things, you can turn nodelay
on:
def main(screen):
screen.nodelay(True)
key = ''
while key != 'q':
try:
key = screen.getkey()
except curses.error:
pass # no keypress was ready
else:
screen.addstr(0, 0, 'key: {:<10}'.format(key))
在您的场景中,您可能会将其放入绘制到 8x8 显示器的游戏循环中,因此它看起来像这样:
In your scenario you probably would put this inside your game loop that's drawing out to your 8x8 display, so it would look something like this:
game = SnakeGame()
while game.not_done:
try:
key = screen.getkey()
except curses.error:
key = None
if key == 'KEY_UP':
game.turn_up()
elif key == 'KEY_DOWN':
game.turn_down()
elif key == 'KEY_LEFT':
game.turn_left()
elif key == 'KEY_RIGHT':
game.turn_right()
game.tick()
需要注意的一点 - 这种方法将占用您的 CPU 的 100%,因此如果您没有其他方法来限制您的应用程序正在执行的操作,则可能会导致一些问题.您可以使用线程/多处理来扩展这种方法,如果您发现它是您需要的东西.
One thing to note - this approach will take 100% of your CPU, so if you don't have some other way to limit what your app is doing it can cause you some problems. You could extend this approach using threading/multiprocessing, if you find that to be something that you need.
这篇关于Pygame 通过 SSH 不注册击键 (Raspberry Pi 3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!