3

For the past couple of days, I have been playing around in the Ursina Engine in Python, used for creating both 3D and 2D games. But the recurring problem I have been facing with this engine when making 3D games, is that I can't close the window properly. This is happening because mouse is being used inside of the game, to control the player, so if I try to go to the close button, the mouse will always stay in the game. The workaround for this is to move to a different window, position the mouse so it's outside of the window, and then finally hit the close button. But this is a lot of work for the user to do, to simply close the window.

Here is some simple code to demonstrate:

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

I believe this import statement is causing this:

from ursina.prefabs.first_person_controller import FirstPersonController

How can I close the window properly in Ursina?

krmogi
  • 2,588
  • 1
  • 10
  • 26
  • 1
    I would create close button inside game - directly in game or as `Quit` in some menu. – furas Oct 16 '21 at 20:23

4 Answers4

5

One solution to this problem would be to create a way for you to exit the game by pressing a key.

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

With this code you can close the game by pressing 'escape'.

4

Quick solution, Shift+Q (if the exit_button is enabled).

Usually in first person shooters you make a pause menu and put an exit button there. The FirstPersonController hides the mouse cursor and locks the position to the center of the screen. To reverse this, do:

mouse.locked = False
mouse.visible = True
pokepetter
  • 1,383
  • 5
  • 8
2

I usually press the "Windows" key. Pressing this button open the windows menu, but it also makes the mouse visible.

Another tip, you can put:

window.exit_button.visible = False

In your code to make it easier to close the window.

Spiceinajar
  • 101
  • 1
  • 10
  • Firstly, I am not on windows. I am on mac. I know it doesn't say compatible on mac in the docs, but somehow I tried it anyway, and it worked. Second, I am getting the following error: `AttributeError: 'Ursina' object has no attribute 'exit_button'` – krmogi Oct 16 '21 at 22:40
  • I am afraid I cannot help you then :( – Spiceinajar Oct 16 '21 at 23:19
-1

Just disable the FPC (First Person Controller) player.disable()

or just

make the mouse unlocked mouse.locked = False, so it can move

Lixt
  • 201
  • 4
  • 19