Minecraft Clone Bug – Ursina Engine

Question:

I don’t know why my minecraft clone destroys blocks of the ground, where I don’t want. Here’s my code:

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

app = Ursina()
player = FirstPersonController()
Sky(color=color.azure,texture=None)
amp = 6
freq = 24
shells = []
shellWidth = 12
noise = PerlinNoise(octaves=2,seed=randrange(1, 100000000000000000000000000000000000))


for i in range(shellWidth*shellWidth):
    ent = Entity(model='cube', texture='ursina-tutorials-main/assets/grass', collider='box')
    shells.append(ent)
    
    
def respawn():
    player.y=5
    player.gravity=0
    
    
Entity1=None


def destruction(position: Vec3):
    try:
        collider_entity = Entity(
            model="cube",
            collider="box",
            visible=False,
            scale=Vec3(0.5, 0.5, 0.5),
            position=position
        )
        collider_entity.intersects(ignore=[collider_entity]).entity.color=color.clear
        collider_entity.intersects(ignore=[collider_entity]).entity.collider = None
        return collider_entity.position
    except:pass
    
    
destructionPos=None
TextureList=["ursina-tutorials-main/assets/grass","ursina-tutorials-main/assets/sandMinecraft.jfif"]
textureNumber=0
x1=0
z1=0


def input(key):
    global TextureList,textureNumber
    global Entity1, destructionPos, x1,z1
    x1=0
    z1=0
    amp = 6
    freq = 24
    position_x = player.x
    position_z = player.z
    if key == "w" or key == "w hold" or key == "s" or key == "s hold" or key == "a" or key == "a hold" or key == "d" or key == "d hold":
        x1 = abs(position_x - abs(player.x)) if player.x > position_x else -abs(position_x - abs(player.x))
        z1 = abs(position_z - abs(player.z)) if player.z > position_z else -abs(position_x - abs(player.x))
    for i in range(len(shells)):
        x = shells[i].x = floor((i / shellWidth) + player.x - 0.5 * shellWidth)
        z = shells[i].z = floor((i % shellWidth) + player.z - 0.5 * shellWidth)
        y = shells[i].y = floor(noise([x / freq, z / freq]) * amp)
        if key=="left mouse down" and shells[i].hovered and mouse.world_point:
            Entity1=(round(mouse.world_point.x), ceil(mouse.world_point.y)-1, round(mouse.world_point.z))
    if key=="right mouse down" and mouse.world_point:
        PlacedBlock = Entity(model='cube', texture=TextureList[textureNumber%2], color=color.white, collider='box', scale=(1, 1, 1),
                       position=(round(mouse.world_point.x), ceil(mouse.world_point.y), round(mouse.world_point.z)),on_click=lambda:destroy(PlacedBlock))
    if key=="g":
        textureNumber+=1
    if Entity1!=None:
        if destructionPos!=None:
            if distance(Entity(position=(destructionPos)),Entity(position=(Entity1)))>=1:
                ""
                
                
myDestructionList=[]


def update():
    global Entity1,x1,z1,destructionPos
    if player.y<-100:
        respawn()
    try:
        x000,y000,z000=Entity1
        myDestructionList.append(destructionPos)
        if player.x != x1 or player.z != z1:
            if (myDestructionList.__len__()+1)>3:
                destructionPos = destruction(position=(x1,y000,z1))
                destructionPos=destruction(destructionPos)
            else:
                ""
            print(destructionPos)
    except:pass
    
    
app.run()

When I clicked on the blocks, they destroyed behind me. And when I was on the place, where the block should get destroyed, I fell down.

Asked By: Raphi-2Code

||

Answers:

I have to do this, but the hole isn’t still displayed yet.

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
from random import *
from perlin_noise import *
#import pyautogui
app = Ursina()
player = FirstPersonController()
Sky(color=color.azure,texture=None)
amp = 6
freq = 24
shells = []
shellWidth = 12
noise = PerlinNoise(octaves=2,seed=randrange(1, 100000000000000000000000000000000000))
for i in range(shellWidth*shellWidth):
    '''def TextureEntity():
        ent.texture="ursina-tutorials-main/assets/TextureLess.png"'''
    ent = Entity(model='cube', texture='ursina-tutorials-main/assets/grass', collider='box')#, on_click=TextureEntity)
    shells.append(ent)
def respawn():
    player.y=5
    player.gravity=0
Entity1=None


def destruction(position: Vec3):
    try:
        collider_entity = Entity(
            model="cube",
            collider="box",
            visible=False,
            scale=Vec3(0.5, 0.5, 0.5),
            position=position
        )
        #collider_entity.intersects(ignore = [collider_entity]).entity.color=color.clear
        collider_entity.intersects(ignore=[collider_entity]).entity.color=color.clear
        collider_entity.intersects(ignore=[collider_entity]).entity.collider = None
        return collider_entity.position
    except:pass

destructionPos=None

TextureList=["ursina-tutorials-main/assets/grass","ursina-tutorials-main/assets/sandMinecraft.jfif"]
textureNumber=0
x1=0
z1=0
def input(key):
    global TextureList,textureNumber
    global Entity1, destructionPos, x1,z1
    x1=0
    z1=0
    amp = 6
    freq = 24
    position_x = player.x
    position_z = player.z
    if key == "w" or key == "w hold" or key == "s" or key == "s hold" or key == "a" or key == "a hold" or key == "d" or key == "d hold":
        x1 = abs(position_x - abs(player.x)) if player.x > position_x else -abs(position_x - abs(player.x))
        z1 = abs(position_z - abs(player.z)) if player.z > position_z else -abs(position_x - abs(player.x))

    for i in range(len(shells)):
        x = shells[i].x = floor((i / shellWidth) + player.x - 0.5 * shellWidth)
        z = shells[i].z = floor((i % shellWidth) + player.z - 0.5 * shellWidth)
        y = shells[i].y = floor(noise([x / freq, z / freq]) * amp)
        if key=="left mouse down" and shells[i].hovered and mouse.world_point:
            Entity1=(round(mouse.world_point.x), ceil(mouse.world_point.y)-1, round(mouse.world_point.z))
    if key=="right mouse down" and mouse.world_point:
        PlacedBlock = Entity(model='cube', texture=TextureList[textureNumber%2], color=color.white, collider='box', scale=(1, 1, 1),
                       position=(round(mouse.world_point.x), ceil(mouse.world_point.y), round(mouse.world_point.z)),on_click=lambda:destroy(PlacedBlock))
    if key=="g":
        textureNumber+=1
    if Entity1!=None:
        if destructionPos!=None:
            if distance(Entity(position=(destructionPos)),Entity(position=(Entity1)))>=1:
                ""
                #xee,yee,zee=destructionPos
                #xeee,yeee,zeee=Entity1
                #entity=Entity(model='cube',collider='box',texture=TextureList[textureNumber%2],color=color.white,position=((abs(xee - abs(xeee)) if xeee > xee else -abs(xee - abs(xeee))),yeee,(abs(zee - abs(zeee)) if zeee > zee else -abs(zee - abs(zeee)))))
                #print(entity.position)
myDestructionList=[]
def update():
    global Entity1,x1,z1,destructionPos
    if player.y<-100:
        respawn()
    try:
        x000,y000,z000=Entity1
        myDestructionList.append(destructionPos)
        if player.x != x1 or player.z != z1:
            if (myDestructionList.__len__()+1)>1:
                destructionPos = destruction(position=(x1,y000,z1))
                destructionPos=destruction(destructionPos)-Vec3(sqrt(player.x)-destructionPos.x,sqrt(player.y)-destructionPos.y,sqrt(player.z)-destructionPos.z)
            else:
                ""
            print(destructionPos)
    except:pass
app.run()
Answered By: Raphi-2Code

I tried this code:

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
from random import *
from perlin_noise import *
#import pyautogui
app = Ursina()
player = FirstPersonController()
Sky(color=color.azure,texture=None)
shells = []
shellWidth = 12
noise = PerlinNoise(octaves=2,seed=randrange(1, 100000000000000000000000000000000000))
for i in range(shellWidth*shellWidth):
    '''def TextureEntity():
        ent.texture="ursina-tutorials-main/assets/TextureLess.png"'''
    ent = Entity(model='cube', texture='ursina-tutorials-main/assets/grass', collider='box')#, on_click=TextureEntity)
    shells.append(ent)
def respawn():
    player.y=5
    player.gravity=0

TextureList=["ursina-tutorials-main/assets/grass","ursina-tutorials-main/assets/sandMinecraft.jfif"]
textureNumber=0
def input(key):
    global TextureList,textureNumber
    if key=="right mouse down" and mouse.world_point:
        PlacedBlock = Entity(model='cube', texture=TextureList[textureNumber%2], color=color.white, collider='box', scale=(1, 1, 1),
                       position=(round(mouse.world_point.x), ceil(mouse.world_point.y), round(mouse.world_point.z)),on_click=lambda:destroy(PlacedBlock))
    if key=="g":
        textureNumber+=1
destroylist=[]
amp = 6
freq = 24
def update():
    global amp,freq,destroylist
    for i in range(len(shells)):
        longString=f"{floor((i / shellWidth) + player.x - 0.5 * shellWidth)},{floor((i % shellWidth) + player.z - 0.5 * shellWidth)}, {floor(noise([floor((i / shellWidth) + player.x - 0.5 * shellWidth) / freq,floor((i % shellWidth) + player.z - 0.5 * shellWidth) / freq]) * amp)}"
        position = shells[i].position = Vec3(floor((i / shellWidth) + player.x - 0.5 * shellWidth),floor((i % shellWidth) + player.z - 0.5 * shellWidth),floor(noise([floor((i / shellWidth) + player.x - 0.5 * shellWidth) / freq, floor((i % shellWidth) + player.z - 0.5 * shellWidth) / freq]) * amp))
            if not longString in destroylist else None
        if held_keys["left mouse"] and shells[i].hovered:
            destroylist.append(longString)
            shells[i].position=None
    print(destroylist)
app.run()

But it only destroys when I’m walking, and it doesn’t generate terrain anymore?

Answered By: Raphi-2Code

That works!

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
from random import *
from perlin_noise import *
#import pyautogui
app = Ursina()
player = FirstPersonController()
Sky(color=color.azure,texture=None)
shells = []
shellWidth = 12
noise = PerlinNoise(octaves=2,seed=randrange(1, 100000000000000000000000000000000000))
for i in range(shellWidth*shellWidth):
    '''def TextureEntity():
        ent.texture="ursina-tutorials-main/assets/TextureLess.png"'''
    ent = Entity(model='cube', texture='ursina-tutorials-main/assets/grass', collider='box')#, on_click=TextureEntity)
    shells.append(ent)
def respawn():
    player.y=5
    player.gravity=0

TextureList=["ursina-tutorials-main/assets/grass","ursina-tutorials-main/assets/sandMinecraft.jfif"]
textureNumber=0
def input(key):
    global TextureList,textureNumber
    if key=="right mouse down" and mouse.world_point:
        PlacedBlock = Entity(model='cube', texture=TextureList[textureNumber%2], color=color.white, collider='box', scale=(1, 1, 1),
                       position=(round(mouse.world_point.x), ceil(mouse.world_point.y), round(mouse.world_point.z)),on_click=lambda:destroy(PlacedBlock))
    if key=="g":
        textureNumber+=1
destroylist=[]
amp = 6
freq = 24
def update():
    global amp,freq,destroylist
    for i in range(len(shells)):
        x = shells[i].x = floor((i / shellWidth) + player.x - 0.5 * shellWidth)
        z = shells[i].z = floor((i % shellWidth) + player.z - 0.5 * shellWidth)
        y = shells[i].y = floor(noise([x / freq, z / freq]) * amp)
        shellsPosition=shells[i].position
        if shells[i].position in destroylist:
            shells[i].x=3000
        if held_keys["left mouse"] and shells[i].hovered:
            destroylist.append(shellsPosition)
            shells[i].position=None
    print(destroylist)
app.run()
Answered By: Raphi-2Code
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.