【问题标题】:Pygame is not working in pycharm just a black window pops upPygame 在 pycharm 中不起作用,只是弹出一个黑色窗口
【发布时间】:2019-08-17 18:17:21
【问题描述】:

我在 pycharm 中使用了这段代码,但事件的打印都不起作用(退出、鼠标按钮单击或键盘上的按键)

虽然我看到了 pygame 窗口,但事件不起作用。

我也使用了 get() 而不是 wait() 但仍然没有运气。

有什么想法吗?

import pygame

pygame.init()
screen = pygame.display.set_mode((800, 600))

while True:
    event = pygame.event.wait()

    if event.type == pygame.QUIT:
        print('Quit')

    if event.type == pygame.KEYDOWN:
        print('Key Down')
        print(event.key)
        print(event.unicode)

    if event.type == pygame.KEYUP:
        print('Key Up')
        print(event.key)

    if event.type == pygame.MOUSEBUTTONDOWN:
        print('Mouse Button Down')
        print(event.pos)
        print(event.button == pygame.BUTTON_RIGHT)
        print(event.button == pygame.BUTTON_LEFT)

    if event.type == pygame.MOUSEBUTTONUP:
        print('Mouse Button Up')
        print(event.pos)
        print(event.button == pygame.BUTTON_RIGHT)
        print(event.button == pygame.BUTTON_LEFT)

    if event.type == pygame.MOUSEMOTION:
        print('Mouse Motion')
        print(event.pos)
        print(event.rel)

更新 我发现这是pycharm的问题。 当我用 pygame 运行任何代码时,只会弹出一个黑色的 pygame 窗口,它不会运行任何其他代码(事件,用颜色填充窗口,...)。甚至 pygame 窗口也不是我给出的尺寸。

这里是另一个代码示例。

import pygame

pygame.init()
screen = pygame.display.set_mode((200, 200))

red = (255, 0, 0)
screen.fill(red)
pygame.display.update()
pygame.time.delay(10000)

当我在 VS 代码中运行它时:

Vs code pygame test

当我在 pycharm 中运行它时 :( :

Pycharm pygame test

我还在为 VS 代码和 pycharm 定义相同的解释器,并且我已经重新安装了 pygame 包。

【问题讨论】:

  • 您在 PyCharm 运行控制台中看到了什么?您的打印语句似乎在那里工作。
  • @RSH 它也对我有用。我希望你不要期望在 pygame 窗口中看到打印,因为打印会出现在 IDE 的控制台中。
  • @SagarGuptaFTW 不,我知道它必须出现在 IDE 控制台中。我发现它是 pycharm 的一个问题,因为我在 VS 代码中对其进行了测试并且它有效!
  • @RSH 很奇怪,我正在使用 PyCharm,为我工作。在你的情况下找到原因会很有趣:)
  • @DaveStSomeWhere 那里什么也没有打印出来!

标签: python events pygame


【解决方案1】:

您可以尝试使用完整的游戏循环,因为这会反复更新窗口。

试试:

import pygame

pygame.init()
screen = pygame.display.set_mode((200, 200))

red = (255, 0, 0)
running = True
while running:
    screen.fill(red)
    pygame.display.flip()
    for event in pygame.event.get():
        if event.type = pygame.QUIT:
            running = False
pygame.quit()

【讨论】:

    【解决方案2】:

    我使用的是 pycharm,但所有代码都没有运行,所以我用鼠标右键单击它并选择在 python cnsole 中运行。 或者 :::::::::::: SHIFT + ALT + E

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-22
      • 2017-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多