from ursina import * from random import randint # Initialize the game engine app = Ursina() # Create player entity (cube for simplicity) player = Entity(model='cube', color=color.orange, scale=(1, 2, 1), position=(0, 1, 5), collider='box') # Create ground ground = Entity(model='plane', color=color.green, scale=(100, 1, 100), position=(0, 0, 0), collider='box') # Create some obstacles (simulate buildings like Fortnite) obstacles = [] for _ in range(5): x = randint(-20, 20) z = randint(-20, 20) y = randint(1, 5) obs = Entity(model='cube', color=color.red, scale=(5, y, 5), position=(x, y / 2, z), collider='box') obstacles.append(obs) # Camera setup (player will follow the camera) camera.position = (0, 5, -10) camera.rotation = (30, 0, 0) # Player movement and jumping speed = 5 jump_height = 5 gravity = 1 y_velocity = 0 is_jumping = False # Make sure this is initialized globally # Smooth camera follow def update(): global y_velocity, is_jumping # Declare is_jumping as global here # Player movement player.rotation_y += mouse.velocity[0] * 0.1 player.x += (held_keys['d'] - held_keys['a']) * speed * time.dt player.z += (held_keys['w'] - held_keys['s']) * speed * time.dt # Jumping mechanics if is_jumping: y_velocity = jump_height is_jumping = False if not ground.intersects(player).hit: y_velocity -= gravity * time.dt else: y_velocity = 0 player.y = ground.y + 1 player.y += y_velocity * time.dt # Camera movement camera.position = Vec3(player.x, player.y + 5, player.z - 10) camera.look_at(player) # Shooting mechanism: fire bullet on right-click if held_keys['right mouse']: shoot() # Handle input (jumping) def input(key): global is_jumping if key == 'space' and not is_jumping and ground.intersects(player).hit: is_jumping = True # Shooting mechanic bullet_speed = 20 def shoot(): bullet = Entity(model='sphere', color=color.blue, scale=(0.3, 0.3, 0.3), position=player.position + Vec3(0, 2, 0), collider='sphere') # Add task to move the bullet task.add(lambda: move_bullet(bullet), 'move_bullet_task') def move_bullet(bullet): while bullet.x < 100: # Stop the bullet after it moves 100 units bullet.x += bullet_speed * time.dt yield bullet.disable() # Disable the bullet after it travels # Run the game window.size = (1280, 720) app.run()