Untitled

Anonymous
plain_text
02/13/2026 12:31 PM
3.6 KB
11
Indexable
from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController

# Initialize the engine
app = Ursina()

# --- Level Setup ---
# Create the ground
ground = Entity(
    model='plane',
    texture='grass',
    collider='box',
    scale=(100, 1, 100)
)

# Create walls to define the arena
wall_1 = Entity(model='cube', collider='box', position=(-50, 0, 0), scale=(1, 10, 100), color=color.gray)
wall_2 = Entity(model='cube', collider='box', position=(50, 0, 0), scale=(1, 10, 100), color=color.gray)
wall_3 = Entity(model='cube', collider='box', position=(0, 0, 50), scale=(100, 10, 1), color=color.gray)
wall_4 = Entity(model='cube', collider='box', position=(0, 0, -50), scale=(100, 10, 1), color=color.gray)

# --- Player Setup ---
player = FirstPersonController()
player.cursor.visible = False # Hide the mouse cursor

# Create the Gun (a simple cube attached to the camera)
gun = Entity(
    parent=camera.ui,
    model='cube',
    scale=(0.2, 0.2, 1),
    position=(0.5, -0.6), # Bottom right of screen
    rotation=(-5, -5, -5),
    color=color.dark_gray,
    on_cooldown=False
)

# --- Enemy Setup ---
enemies = []

def create_enemy():
    # Spawn an enemy at a random position
    x = random.uniform(-20, 20)
    z = random.uniform(10, 40)
    enemy = Entity(
        model='cube',
        color=color.red,
        scale=(2, 3, 2),
        position=(x, 1.5, z),
        collider='box',
        tag='enemy' # We tag it to identify it later
    )
    enemies.append(enemy)

# Create 5 initial enemies
for i in range(5):
    create_enemy()

# --- Gameplay Logic ---

def shoot():
    if not gun.on_cooldown:
        gun.on_cooldown = True
        gun.position = (0.5, -0.5)  # Recoil kick back
        gun.animate_position((0.5, -0.6), duration=0.1, curve=curve.linear) # Return to normal
        
        # Cast a ray from the center of the screen (camera) forward
        hit_info = raycast(camera.world_position, camera.forward, distance=100)
        
        if hit_info.hit:
            # If we hit an entity tagged as 'enemy'
            if hit_info.entity.tag == 'enemy':
                destroy(hit_info.entity)
                create_enemy() # Spawn a new one to keep the game going
                
        invoke(setattr, gun, 'on_cooldown', False, delay=0.15)

def input(key):
    if key == 'left mouse down':
        shoot()
    if key == 'escape':
        quit()

def update():
    # Basic enemy AI: make them look at the player
    for enemy in enemies:
        try: # Try/Except in case an enemy was just destroyed
            enemy.look_at(player)
            # Move slightly toward player
            enemy.position += enemy.forward * time.dt * 2
        except:
            pass

# Run the game
app.run()
Editor is loading...
Leave a Comment