Pygame窗口在Mac上未收到键盘事件 [英] Pygame window not receiving keyboard events on Mac

查看:71
本文介绍了Pygame窗口在Mac上未收到键盘事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Pygame上运行这个非常简单的教程:

I am running this very simple tutorial on Pygame:

https://www.youtube.com/watch?v=xh4SV3kF-zk

我无法使键盘事件被游戏窗口识别.我已经阅读了许多与此类似的问题,但答案似乎不适用于Mac.

I can't get keyboard events to be recognised by the game window. I have read a number of similar questions about this, but the answers don't seem right for Macs.

我正在使用miniconda,可能是我需要退出虚拟环境吗?我不知道该怎么做.或者也许我需要将焦点设置到我的窗口上-但我也不知道该怎么做.这一定是很多人在运行El Capitan的Mac上遇到的问题.我的Mac机太旧,无法正确升级到Sierra.有没有办法让键盘输入起作用?

I am using miniconda and it might be that I need to quit the virtualenvironment? I don't understand how to do that tho. Or maybe I need to set the focus to my window - but I don't know how to do that either. This must be an issue lots of people are having on Macs running El Capitan. My Mac is too old to upgrade properly to Sierra. Is there a way to get the keyboard input to work?

import pygame

pygame.init()

display_width = 1200
display_height = 800

black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)


gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('A Bit Racey')
clock = pygame.time.Clock()

speederImg = pygame.image.load('speeder.png')

def speeder(x, y):
    gameDisplay.blit(speederImg, (x, y))

x = (display_width * 0.4)
y = (display_height * 0.2)

x_change = 0

crashed = False

while not crashed:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x_change = -5
            elif event.key == pygame.K_RIGHT:
                x_change = 5

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                x_change = 0

    x += x_change

    gameDisplay.fill(white)
    speeder(x, y)
        # print(event)


    pygame.display.update()

    clock.tick(60)

pygame.quit()
quit()

推荐答案

对于其他试图在Mac上使用Python3从终端运行的人来说,问题似乎出在窗口焦点上.要使其正常运行而不是键入:

For anybody else trying to run from the terminal using Python3 on a Mac, it seems the problem is the window focus. To get it to run properly instead of typing:

python mygame.py

改为

键入:

pythonw mygame.py

请参见 https://github.com/pygame/pygame/issues/359 了解更多信息

这篇关于Pygame窗口在Mac上未收到键盘事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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