Roaming Ralph in 4 lines of horrific code

@janEntikan1 issued a codegolf challenge today in the Discord server:

So here is a functional re-implementation of Roaming Ralph in 4 lines (I did not permit myself to use semicolons) of terrible, awful, no-good write-only code:

from direct.showbase.ShowBase import ShowBase, ModelPool, auto_bind, AnimControlCollection, CollisionNode, CollisionTraverser, CollisionHandlerQueue, CollisionRay
(base := ShowBase()).taskMgr.add(lambda task: models[1].set_pos_hpr(models[1], 0, (base.mouseWatcherNode.is_button_down('arrow_down') * 10 - base.mouseWatcherNode.is_button_down('arrow_up') * 20) * base.clock.dt, 0, (base.mouseWatcherNode.is_button_down('arrow_left') - base.mouseWatcherNode.is_button_down('arrow_right')) * 300 * base.clock.dt, 0, 0) or models[1].set_z(base.cTrav.get_handler(models[1].children[-1]).entries[0].get_surface_point(models[1].parent).z) or (base.mouseWatcherNode.is_button_down('arrow_down') or base.mouseWatcherNode.is_button_down('arrow_up') or (acc.stop_all() and acc.get_anim(0).pose(5))) and (acc.get_anim(1).is_playing() or acc.get_anim(1).loop(True)) or task.cont, sort=40)
models, base.cTrav, acc = [base.render.attach_new_node(ModelPool.load_model(model)) for model in ("models/world", "models/ralph", "models/ralph-run", "models/ralph-walk")], CollisionTraverser(), AnimControlCollection()
auto_bind(render.node(), acc, ~0) or models[1].set_pos_hpr_scale(*(models[0].find("**/start_point").get_pos() + (0, 0, 1.5)), 0, 0, 0, 0.2, 0.2, 0.2) or base.cam.reparent_to(models[1]) or base.cam.set_pos_hpr_scale(0, 50, 26.3163, 180, -25.9349, 0, 5, 5, 5) or base.cTrav.add_collider(models[1].attach_new_node(CollisionNode('cnode')), CollisionHandlerQueue()) or models[1].children[-1].node().add_solid(CollisionRay(0, 0, 9, 0, 0, -1)) or base.run()

Of course, there is no reason to ever write code like this. I just wanted to show how such a thing might be possible.

8 Likes

That is both horrifying and impressive to behold. 0_0;

4 Likes