【问题标题】:Properly close the window using mouse in Ursina在 Ursina 中使用鼠标正确关闭窗口
【发布时间】:2021-12-04 09:54:28
【问题描述】:

在过去的几天里,我一直在玩Ursina Engine in Python,用于创建 3D 和 2D 游戏。但是我在制作 3D 游戏时一直面临这个引擎的反复出现的问题,就是我无法正确关闭窗口。发生这种情况是因为在游戏内部使用鼠标来控制玩家,所以如果我尝试点击关闭按钮,鼠标将始终停留在游戏中。解决方法是移动到另一个窗口,将鼠标放在窗口之外,然后最后点击关闭按钮。但这对用户来说是很多工作,只需关闭窗口即可。

这里有一些简单的代码来演示:

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
import random

game = Ursina()

class Block(Button):
    def __init__(self, position = (0,0,0)):
        super().__init__(parent = scene, position = position, model = 'cube', color = color.white)

for z in range(20):
    for x in range(20):
        block = Block(position = (x, 0, z))

player = FirstPersonController()
game.run()

我相信这个 import 语句导致了这个:

from ursina.prefabs.first_person_controller import FirstPersonController

如何在 Ursina 中正确关闭窗口?

【问题讨论】:

  • 我会在游戏中创建关闭按钮 - 直接在游戏中或在某些菜单中作为Quit

标签: python window mouse ursina


【解决方案1】:

解决这个问题的一个方法是为您创建一个按键退出游戏的方法。

def input(key):
    if key == 'escape':
        quit()

使用此代码,您可以按“退出”键关闭游戏。

【讨论】:

    【解决方案2】:

    快速解决方案,Shift+Q(如果启用了 exit_button)。

    通常在第一人称射击游戏中,您会制作一个暂停菜单并在那里放置一个退出按钮。 FirstPersonController 隐藏鼠标光标并将位置锁定在屏幕中心。要扭转这种情况,请执行以下操作:

    mouse.locked = False
    mouse.visible = True
    

    【讨论】:

      【解决方案3】:

      我通常按“Windows”键。按下此按钮可打开 windows 菜单,但它也会使鼠标可见。

      另一个提示,你可以说:

      window.exit_button.visible = False
      

      在您的代码中使关闭窗口更容易。

      【讨论】:

      • 首先,我不在 Windows 上。我在mac上。我知道它在文档中没有说在 mac 上兼容,但不知何故我还是尝试了它,它奏效了。其次,我收到以下错误:AttributeError: 'Ursina' object has no attribute 'exit_button'
      • 那我恐怕帮不了你了:(
      猜你喜欢
      • 2012-02-08
      • 1970-01-01
      • 2021-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 2020-01-19
      相关资源
      最近更新 更多