from ursina import * from ursina.prefabs.first_person_controller import FirstPersonController app = Ursina() # Define block size block_size = 1 # Load textures brick_texture = 'brick_texture.jpg' grass_texture = 'grass_texture.png' # Create player player = FirstPersonController() # Set normal and sprint movement speed normal_speed = player.speed sprint_speed = normal_speed * 2 # You can adjust the sprint speed as needed # Size of one ground tile tile_size = 10 # Number of tiles in each direction from the player grid_size = 5 # Dictionary to hold ground tiles tiles = {} # Function to create a ground tile at a specific position def create_ground_tile(x, z): position = (x * tile_size, 0, z * tile_size) tile = Entity(model='plane', texture=grass_texture, texture_scale=(tile_size, tile_size), position=position, scale=(tile_size, 1, tile_size), collider='box') tiles[(x, z)] = tile # Function to update ground tiles based on the player's position def update_ground_tiles(): player_x = int(player.x // tile_size) player_z = int(player.z // tile_size) for x in range(player_x - grid_size, player_x + grid_size + 1): for z in range(player_z - grid_size, player_z + grid_size + 1): if (x, z) not in tiles: create_ground_tile(x, z) # Create initial grid of ground tiles update_ground_tiles() # List to hold block entities blocks = [] # Function to place a block def place_block(): # Get the position on the grid where the player is looking hit_info = raycast(camera.position, camera.forward, distance=10, ignore=[player]) if hit_info.hit: position = mouse.world_point # Snap the position to the grid position = round(position[0]), 0.5, round(position[2]) # Check if there's already a block at the target position while True: block_exists = False for block in blocks: if block.position == position: # If there's already a block, move the position up position = (position[0], position[1] + block_size, position[2]) block_exists = True break if not block_exists: # If no block exists at the position, break the loop and place the new block break # Create a block entity at the adjusted position block = Entity(model='cube', texture=brick_texture, position=position, scale=(block_size, block_size, block_size), collider='box') blocks.append(block) # Function to remove the closest block to the player's position def remove_block(): closest_block = None closest_distance = float('inf') for block in blocks: distance = (block.position - player.position).length() if distance < closest_distance: closest_block = block closest_distance = distance if closest_block: blocks.remove(closest_block) destroy(closest_block) # Input handling for removing blocks def input(key): if mouse.left: place_block() elif mouse.right: remove_block() # Update function to continuously update ground tiles and check for sprinting def update(): update_ground_tiles() player.speed = sprint_speed if held_keys['left shift'] else normal_speed # Check for collisions between player and blocks for block in blocks: if player.intersects(block): # If the player intersects with a block, prevent movement in that direction player.position -= player.world_up * time.dt * player.gravity # Set FOV camera.fov = 90 # Adding skybox sky = Sky() app.run()