繁体   English   中英

用于 Python 的 Ursina 游戏引擎在我添加一定数量的实体后不允许我全屏游戏

[英]Ursina Game Engine for Python won't let me fullscreen the game after I add a certain number of entities

我目前正在创建一个 minecraft 克隆,每当我尝试生成一个 16x * 32y * 16z 的块时,它会拒绝让我打开全屏 window 如果它不是全屏那么它会被偏移到屏幕的奇怪部分。 https://pastebin.com/XTncVhBM

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

user32 = ctypes.windll.user32
screensize = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)

gameApp = Ursina()


class Face(Button):
    def __init__(self, position=(float(0), float(0), float(0)), rotation=(0, 0, 0),
                 truePos=(float(0), float(0), float(0))):
        super().__init__(
            parent=scene,
            position=position,
            model='quad',
            scale=1,
            origin_y=1,
            rotation=rotation,
            texture='white_cube',
            color=color.white,
            highlight_color=color.lime
        )
        self.truePos = truePos

    def destroy(self):
        destroy(self)


class Block(object):
    def __init__(self, position=(float(0), float(0), float(0)), chunk=None):
        self.negativeZFace = Face(position=position + (0.0, 1.0, -0.5), rotation=(0, 0, 0), truePos=position)
        self.positiveZFace = Face(position=position + (0.0, -1.0, 0.5), rotation=(180, 0, 0), truePos=position)
        self.positiveYFace = Face(position=position + (0.0, 0.5, 1.0), rotation=(90, 0, 0), truePos=position)
        self.negativeYFace = Face(position=position + (0.0, -0.5, -1.0), rotation=(270, 0, 0), truePos=position)
        self.positiveXFace = Face(position=position + (0.5, 1.0, 0.0), rotation=(0, 270, 0), truePos=position)
        self.negativeXFace = Face(position=position + (-0.5, 1.0, 0.0), rotation=(0, 90, 0), truePos=position)
        self.position = position
        self.faces = [self.negativeYFace, self.negativeXFace, self.negativeZFace, self.positiveXFace,
                      self.positiveYFace, self.positiveZFace]
        self.chunk: Chunk = chunk
        self.optimize()

    def destroy(self):
        for face in self.faces:
            face.destroy()
        if self.chunk.genBlocks.keys().__contains__(str(tuple(map(operator.add, self.position, (1.0, 0, 0))))):
            self.chunk.genBlocks[
                str(tuple(map(operator.add, self.position, (1.0, 0, 0))))].regenFaces()
        if self.chunk.genBlocks.keys().__contains__(str(tuple(map(operator.add, self.position, (-1.0, 0, 0))))):
            self.chunk.genBlocks[
                str(tuple(map(operator.add, self.position, (-1.0, 0, 0))))].regenFaces()
        if self.chunk.genBlocks.keys().__contains__(str(tuple(map(operator.add, self.position, (0.0, 0, 1.0))))):
            self.chunk.genBlocks[
                str(tuple(map(operator.add, self.position, (0.0, 0, 1.0))))].regenFaces()
        if self.chunk.genBlocks.keys().__contains__(str(tuple(map(operator.add, self.position, (0.0, 0, -1.0))))):
            self.chunk.genBlocks[
                str(tuple(map(operator.add, self.position, (0.0, 0, -1.0))))].regenFaces()
        if self.chunk.genBlocks.keys().__contains__(
                str(tuple(map(operator.add, self.position, (0.0, 1.0, 0.0))))):
            self.chunk.genBlocks[
                str(tuple(map(operator.add, self.position, (0.0, 1.0, 0.0))))].regenFaces()
        if self.chunk.genBlocks.keys().__contains__(
                str(tuple(map(operator.add, self.position, (0.0, -1.0, 0.0))))):
            self.chunk.genBlocks[
                str(tuple(map(operator.add, self.position, (0.0, -1.0, 0.0))))].regenFaces()
        self.chunk.removeBlock(self.position)

    def regenFaces(self):
        self.negativeZFace = Face(position=self.position + (0.0, 1.0, -0.5), rotation=(0, 0, 0), truePos=self.position)
        self.positiveZFace = Face(position=self.position + (0.0, -1.0, 0.5), rotation=(180, 0, 0),
                                  truePos=self.position)
        self.positiveYFace = Face(position=self.position + (0.0, 0.5, 1.0), rotation=(90, 0, 0), truePos=self.position)
        self.negativeYFace = Face(position=self.position + (0.0, -0.5, -1.0), rotation=(270, 0, 0),
                                  truePos=self.position)
        self.positiveXFace = Face(position=self.position + (0.5, 1.0, 0.0), rotation=(0, 270, 0), truePos=self.position)
        self.negativeXFace = Face(position=self.position + (-0.5, 1.0, 0.0), rotation=(0, 90, 0), truePos=self.position)
        self.faces = [self.negativeYFace, self.negativeXFace, self.negativeZFace, self.positiveXFace,
                      self.positiveYFace, self.positiveZFace]
        self.optimize()

    def new(self):
        if self.chunk.genBlocks.keys().__contains__(str(tuple(map(operator.add, self.position, (1.0, 0, 0))))):
            self.chunk.genBlocks[
                str(tuple(map(operator.add, self.position, (1.0, 0, 0))))].regenFaces()
        if self.chunk.genBlocks.keys().__contains__(str(tuple(map(operator.add, self.position, (-1.0, 0, 0))))):
            self.chunk.genBlocks[
                str(tuple(map(operator.add, self.position, (-1.0, 0, 0))))].regenFaces()
        if self.chunk.genBlocks.keys().__contains__(str(tuple(map(operator.add, self.position, (0.0, 0, 1.0))))):
            self.chunk.genBlocks[
                str(tuple(map(operator.add, self.position, (0.0, 0, 1.0))))].regenFaces()
        if self.chunk.genBlocks.keys().__contains__(str(tuple(map(operator.add, self.position, (0.0, 0, -1.0))))):
            self.chunk.genBlocks[
                str(tuple(map(operator.add, self.position, (0.0, 0, -1.0))))].regenFaces()
        if self.chunk.genBlocks.keys().__contains__(
                str(tuple(map(operator.add, self.position, (0.0, 1.0, 0.0))))):
            self.chunk.genBlocks[
                str(tuple(map(operator.add, self.position, (0.0, 1.0, 0.0))))].regenFaces()
        if self.chunk.genBlocks.keys().__contains__(
                str(tuple(map(operator.add, self.position, (0.0, -1.0, 0.0))))):
            self.chunk.genBlocks[
                str(tuple(map(operator.add, self.position, (0.0, -1.0, 0.0))))].regenFaces()

    def optimize(self):
        if self.chunk.genBlocks.keys().__contains__(str(tuple(map(operator.add, self.position, (1.0, 0, 0))))):
            self.chunk.genBlocks[
                str(tuple(map(operator.add, self.position, (1.0, 0, 0))))].negativeXFace.destroy()
            self.positiveXFace.destroy()
        if self.chunk.genBlocks.keys().__contains__(str(tuple(map(operator.add, self.position, (-1.0, 0, 0))))):
            self.chunk.genBlocks[
                str(tuple(map(operator.add, self.position, (-1.0, 0, 0))))].positiveXFace.destroy()
            self.negativeXFace.destroy()
        if self.chunk.genBlocks.keys().__contains__(str(tuple(map(operator.add, self.position, (0.0, 0, 1.0))))):
            self.chunk.genBlocks[
                str(tuple(map(operator.add, self.position, (0.0, 0, 1.0))))].negativeZFace.destroy()
            self.positiveZFace.destroy()
        if self.chunk.genBlocks.keys().__contains__(str(tuple(map(operator.add, self.position, (0.0, 0, -1.0))))):
            self.chunk.genBlocks[
                str(tuple(map(operator.add, self.position, (0.0, 0, -1.0))))].positiveZFace.destroy()
            self.negativeZFace.destroy()
        if self.chunk.genBlocks.keys().__contains__(
                str(tuple(map(operator.add, self.position, (0.0, 1.0, 0.0))))):
            self.chunk.genBlocks[
                str(tuple(map(operator.add, self.position, (0.0, 1.0, 0.0))))].negativeYFace.destroy()
            self.positiveYFace.destroy()
        if self.chunk.genBlocks.keys().__contains__(
                str(tuple(map(operator.add, self.position, (0.0, -1.0, 0.0))))):
            self.chunk.genBlocks[
                str(tuple(map(operator.add, self.position, (0.0, -1.0, 0.0))))].positiveYFace.destroy()
            self.negativeYFace.destroy()


class Chunk(object):
    def __init__(self, position=(float(0), float(0), float(0)), blocks=None):
        print("creating chunk")
        position = tuple(map(operator.mul, position, (16, 0, 16)))
        self.genBlocks = {}
        if blocks is None:
            blocks = []
        for blockPos in blocks:
            if not isinstance(blockPos, tuple):
                break
            else:
                genBlock = Block(position=tuple(map(operator.add, blockPos, position)), chunk=self)
                self.genBlocks[str(genBlock.position)] = genBlock
        print("finished chunk")

    def optimize(self):
        for block in self.genBlocks:
            block: Block = self.genBlocks[block]
            if self.genBlocks.keys().__contains__(str(tuple(map(operator.add, block.position, (1.0, 0, 0))))):
                self.genBlocks[str(tuple(map(operator.add, block.position, (1.0, 0, 0))))].negativeXFace.destroy()
            if self.genBlocks.keys().__contains__(str(tuple(map(operator.add, block.position, (-1.0, 0, 0))))):
                self.genBlocks[str(tuple(map(operator.add, block.position, (-1.0, 0, 0))))].positiveXFace.destroy()
            if self.genBlocks.keys().__contains__(str(tuple(map(operator.add, block.position, (0.0, 0, 1.0))))):
                self.genBlocks[str(tuple(map(operator.add, block.position, (0.0, 0, 1.0))))].negativeZFace.destroy()
            if self.genBlocks.keys().__contains__(str(tuple(map(operator.add, block.position, (0.0, 0, -1.0))))):
                self.genBlocks[str(tuple(map(operator.add, block.position, (0.0, 0, -1.0))))].positiveZFace.destroy()
            if self.genBlocks.keys().__contains__(str(tuple(map(operator.add, block.position, (0.0, 1.0, 0.0))))):
                self.genBlocks[str(tuple(map(operator.add, block.position, (0.0, 1.0, 0.0))))].negativeYFace.destroy()
            if self.genBlocks.keys().__contains__(str(tuple(map(operator.add, block.position, (0.0, -1.0, 0.0))))):
                self.genBlocks[str(tuple(map(operator.add, block.position, (0.0, -1.0, 0.0))))].positiveYFace.destroy()

    def addBlock(self, position):
        genBlock = Block(position=position, chunk=self)
        self.genBlocks[str(genBlock.position)] = genBlock

    def removeBlock(self, position):
        self.genBlocks.pop(str(position)).new()


class Voxel(Button):
    def __init__(self, position=(0, 0, 0)):
        super().__init__(
            parent=scene,
            position=position,
            model='cube',
            scale=(1, 1, 1),
            texture='white_cube',
            color=color.white,
            highlight_color=color.lime

        )

    def input(self, key):
        if self.hovered:
            if key == "right mouse down":
                voxel = Voxel(position=self.position + mouse.normal)
            if key == "left mouse down":
                destroy(self)


window.title = "PyCraft"

player = FirstPersonController()
player.name = "Player"
started = False


def update():
    global started
    global player
    if not started:
        blocks = []
        for x in range(16):
            for z in range(16):
                for y in range(10):
                    blocks.append((float(x), float(y * -1), float(z)))
        test_chunk = Chunk(position=(0, 0, 0), blocks=blocks)
        started = True
        player.y = 1
        player.x = 0
        player.z = 0

    try:
        print_on_screen(f"X: {str(round(player.x))}, Y: {str(round(player.y))}, Z: {str(round(player.z))}",
                        duration=time.dt)
    except:
        pass
    if held_keys["space"]:
        try:
            print_on_screen(f"X: {str(round(player.x))}, Y: {str(round(player.y))}, Z: {str(round(player.z))}",
                            duration=time.dt)
        except:
            pass


window.fullscreen_resolution = (screensize[0] + 1, screensize[1] + 1)
window.fullscreen = True
gameApp.run()

如果您需要任何参考,我的代码在那里,谢谢。 错误代码是这样的

:显示:windisplay(错误):视频卡在指定分辨率(1921 x 1081 x 32)下没有支持的显示分辨率:显示:windisplay(警告):切换到全屏模式失败!

在 Ursina() 之前设置 window 大小(或全屏)

这可能是 ursina 中的错误。 我做了这个改变,它得到了更多:

在 site-packages\ursina 中找到 window.py 文件。 例如,我的是:

C:\Program Files\Python37\Lib\site-packages\ursina

然后编辑您的 window.py 文件,更改行:

self.fullscreen_size = Vec2(self.screen_resolution[0]+1, self.screen_resolution[1]+1)

到:

self.fullscreen_size = Vec2(self.screen_resolution[0], self.screen_resolution[1])

之后事情就好多了。 我不知道后果可能是什么,但试一试。

在 Ursina() 为我修复后执行此操作

window.fullscreen_resolution = (1920, 1080)
window.fullscreen = True

仍然给出错误,但它是全屏的

我制作了一个名为CreateWindow的 function 并使用了这两行,它为我全屏弹出:

window.size = window.fullscreen_size
window.position = Vec2(0, 0)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM