Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
481 views
in Technique[技术] by (71.8m points)

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

I'm currently creating a minecraft clone and whenever I try to generate a chunk that is 16x * 32y * 16z it'll refuse to let me open a full screen window and if it isn't fullscreened then it'll be offset to a weird part of the screen. 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_o

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It may be a bug in ursina. I made this change and it got a lot further:

Find the window.py file in site-packagesursina. For instance, mine was in:

C:Program FilesPython37Libsite-packagesursina

Then edit your window.py file, change the line:

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

to:

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

Things went much better after that. I don't know what the ramifications might be, but give it a shot.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...