So i have my code and I want to appear a cube under a cube i will click. Then i want to check if there are cubes around this cube if in some place there isnt a cube then create one but i cant do this. Can someone help me?
from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
app = Ursina(borderless = False)
player = FirstPersonController(model='none', position=(0,5,0), scale=2)
Sky()
overworld_ground = Entity(
model = 'cube',
scale = (32,2,18),
position = (0,0,0),
collider = 'mesh',
color = color.rgb(0, 128, 0),
texture = 'grass'
)
class Voxel(Button):
def __init__(self, position = (0,0,0)):
super().__init__(
parent = scene,
position = self.position,
model = 'cube',
collider = 'box',
rotation = (0,0,0),
color = color.rgb(0, random.uniform(162,182), 0),
scale = (2, 2, 2))
def input(self, key):
if self.hovered:
if key == 'left mouse down':
voxel = Voxel(self.position.y - 2)
destroy(self)
for z in range(8):
for x in range(8):
voxel = Voxel(position = (x * 2 - 11,-1,z * 2 + 10))
app.run()
>Solution :
I see two problems with your code. When I fix both of these, I get something reasonable that I think will put you in a place where you can proceed with your development:
-
I get an error when I try to run your code, involving the line
position = self.position,. This line makes no sense. Here, you reference thepositionattribute of the object that you’re just starting to construct. Also, you aren’t using thepositionparameter passed to the constructor. To resolve both of these issues, I expect that what you want here isposition = positionto simply pass the passed inpositionalong to the superclass’s constructor. -
This line looks suspicious as well:
voxel = Voxel(self.position.y - 2). You’re taking just theycomponent of an object’s position and passing that to theVoxelconstructor that expects a 3-d position.
I fixed both of these issues, and played around with where the new Voxels are created so that I could see them. Once I did this, clicking on an existing Voxel caused a new one to appear and that one to disappear, which seems like what you want to see happening.
So here’s my fixed version of your code that behaves more "properly":
from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
app = Ursina(borderless = False)
player = FirstPersonController(model='none', position=(0,5,0), scale=2)
Sky()
overworld_ground = Entity(
model = 'cube',
scale = (32,2,18),
position = (0,0,0),
collider = 'mesh',
color = color.rgb(0, 128, 0),
texture = 'grass'
)
class Voxel(Button):
def __init__(self, position = (0,0,0)):
super().__init__(
parent = scene,
position = position, # <- was self.position
model = 'cube',
collider = 'box',
rotation = (0,0,0),
color = color.rgb(0, random.uniform(162,182), 0),
scale = (2, 2, 2))
def input(self, key):
if self.hovered:
if key == 'left mouse down':
p = self.position
p.z += 20
voxel = Voxel(position=p)
destroy(self)
for z in range(8):
for x in range(8):
voxel = Voxel(position = (x * 2 - 11,-1,z * 2 + 10))
app.run()