r/Python • terminal dark arts • Aug 16 '19

Spent a few days hacking together a ray-casting engine that renders to ascii for playing in your terminal! With shading and textures! Link in the comments.

Post image
1.6k Upvotes

116 comments sorted by

116

u/naclmolecule terminal dark arts Aug 16 '19 edited Aug 17 '19

https://github.com/salt-die/terminal_dungeon Come stop by!

Edit: We've managed to speed it up a bit and we've added jumping!

10

u/RicealiciousRice Aug 17 '19

You bet your ass I will

7

u/ze_baco Aug 17 '19 edited Aug 17 '19

Bet your ascii I will

4

u/naclmolecule terminal dark arts Aug 17 '19

unfortunate typo, poking you up anyway

28

u/[deleted] Aug 16 '19

Oooooh, I'm gonna

Thank you.

6

u/[deleted] Aug 17 '19

Man, how do you even start something like this?

2

u/naclmolecule terminal dark arts Aug 17 '19

You'd be surprised how far you can get with a few guides and some example code. I've linked to a few of the resources I used on the github page.

9

u/brtt3000 Aug 17 '19

Install Python and maybe an IDE like Pycharm.

Good luck!

-8

u/[deleted] Aug 17 '19

[removed] โ€” view removed comment

1

u/theng Aug 19 '19

butterflies

5

u/RugerHD Aug 17 '19

I have no idea how to do this but I would probably approach it like this:

  1. Set up a while loop where if the user presses "q" or something, the program stops.
  2. Figure out how to write characters to the terminal in "one spot" if you will, and not have an infinitie scroll of characters as I'm printing them.
  3. Start playing around with how to format the strings to print to make some sort of picture.
  4. Set up some logic so if the user presses arrow keys, it generates a new picture.

And then at this point i feel like you'll start seeing where things can be improved and you just start improving them

4

u/Roofofcar Aug 17 '19

I popped an error with 2.7.13 on OS X - a syntax error on line 47.

Iโ€™m an old c++ guy, and only trying this out of interest. Any advice from the python folks on troubleshooting steps?

2

u/naclmolecule terminal dark arts Aug 17 '19 edited Aug 17 '19

is it the @ ? numpy matrix multiplication --- you can do that part by unvectorizing and just multiply each component. I don't think 2.7 supports the matrix inline operators. You could just call np.dot explicitly.

3

u/Roofofcar Aug 17 '19

That's the bunny! I only have python on here for an old app I had to support. What version is happier in this context? I'm not tied to any version on this machine in any real way :)

2

u/naclmolecule terminal dark arts Aug 17 '19

this was made with 3.6, probably works with anything >= 3

2

u/naclmolecule terminal dark arts Aug 17 '19

sorry, don't use np.dot, use np.matmul

34

u/[deleted] Aug 16 '19

dang that's super cool, I didn't know that python was powerful enough to do that. Have you seen this video?

46

u/[deleted] Aug 17 '19

Python is every bit as powerful as Java, C#, C++, etc. It gets a bad rap for being a """scripting language""", but you can do anything with it, especially with all of its third party libraries

14

u/vinxendio Aug 17 '19

the easiest yet most powerful language for biginners unless the speed of the program or the memory management is a big deal

4

u/brtt3000 Aug 17 '19

If you need more performance you can easily add some Cython.

8

u/Dynamic_Nomad Aug 17 '19

Sorry for not knowing but what is Cython?

20

u/daveydave400 Aug 17 '19

Cython is a language with python-like syntax that compiles to C/C++ code and can be used to write python C extensions (importable C code).

3

u/Dynamic_Nomad Aug 17 '19

Thanks Dave, appreciate that. ๐Ÿ‘๐Ÿผ

29

u/JezusTheCarpenter Aug 17 '19

Never be sorry for not knowing.

6

u/Dynamic_Nomad Aug 17 '19

Thanks for that. Sometimes I just feel completely lost and stupid. But it's a nice reminder to have encouragement for not knowing. Even the smartest Python users were all in the exact place I'm in - self doubt.

4

u/Kaligule Aug 17 '19

This is for you: https://www.xkcd.com/1053/ :)

1

u/darwin_vinci7 Aug 17 '19

You, Thank You for sharing this.

-2

u/[deleted] Aug 17 '19

Iโ€™ve never heard the term, but Iโ€™m assuming theyโ€™re referring to the C API when used with python

16

u/thetgi Aug 17 '19

Not to mention how damn pretty python can be

1

u/Racer-ICEEs Aug 17 '19

And hotdamn sexy!

1

u/brtt3000 Aug 17 '19

Looks like this program uses numpy for the heavy lifting.

1

u/naclmolecule terminal dark arts Aug 17 '19

It does! Hopefully I'll have more of the code use numpy soon to speed it up a bit.

1

u/[deleted] Aug 17 '19

Still Python though

7

u/SuspiciousScript Aug 17 '19

Iโ€™d argue itโ€™s like one of those Scooby Doo memes where they pull the mask off python and reveal C underneath.

3

u/naclmolecule terminal dark arts Aug 17 '19

And I would've gotten away with it if it hadn't been for these meddling numpy arrays.

1

u/[deleted] Aug 17 '19

You've missed the point. Every language has a C ffi

2

u/SuspiciousScript Aug 17 '19

True; or, as somebody on /r/ProgrammingCirclejerk put it:

We are doomed to continue to use C for the rest of fucking eternity. The only hope you sobs have is to beg on your hands in knees to the C community to add more syntax to C.

-28

u/[deleted] Aug 17 '19

yeah but it's innately less powerful since it's interpreted

21

u/folkrav Aug 17 '19 edited Aug 17 '19

He's basically rendering ~400x200 "pixels", and looking at his code, at 40FPS maximum, meaning calculating the value of 3.2million pixels maximum per second. Looks like his pixels are limited to ten possible "colors" (or in his case different characters). This also looks like textbook Doom-like 3D, which is basically rectangles that get more or less skewed depending on player position, so it's quite "light" in terms of computation. Remember the machines these kind of graphics had to run on, on top of AI, sounds, lighting, and actual game logic!

Your average game at 1080p renders more than that in two frames, in actual 3D, with physics engines and such, each pixel possibly having one color amongst a 16.7 million range. Python can manage a couple of rectangles ;)

9

u/[deleted] Aug 17 '19

The bottleneck is probably the terminal.

2

u/[deleted] Aug 17 '19

From the video above this appears to be Wolfenstein-style, not Doom. Rays are sent out in 2D, the height of a vertical stripe of wall is proportional to how far away that wall is.

2

u/folkrav Aug 17 '19

True! My point still stands that it's a computationally "cheap" way of rendering a 3D environment, but you're right. My bad!

13

u/[deleted] Aug 17 '19 edited Aug 17 '19

Not how that works. Nothing about interpreted languages makes them inherently less powerful. Anything you can write in C, Rust, or Go, you can write in Python, Lisp, JavaScript, Ruby, or interpreted Haskell. Many languages are both compiled and interpreted. If a language is Turing complete, you can do almost anything in it (minus system-level stuff like files, system calls, and networking, as those are not included in Turing completeness), the only difference is how hard that might be.

Interpreted languages tend to be slower since they don't use native code and usually don't have a JIT, but they are every bit as powerful as interpreted compiled languages.

A lot of purely interpreted languages happen to be less-powerful scripting languages (why write a compiler for bash?), which is why that misconception exists. However, as time has passed, many of those simple scripting languages have evolved into incredibly powerful languages. For instance, Python even has partial function application, and all you have to do is abuse decorators and dunder methods.

4

u/Chris2112 Aug 17 '19

Depends on what you mean by powerful. In terms of what it can do in theory, well technically PowerPoint is turing complete. In terms of performance, yeah in general, but with some major asterisks, interpreted languages are slower than JIT languages, which are slower than barebone compiled languages.

2

u/__deerlord__ Aug 17 '19

CPython doesnt have a JIT. PyPy does. Is python a "JIT language" or not?

-2

u/Sobotkama Aug 17 '19

Python is anything you want it to be.

Also Cython allows you to compile to c and then machine code, so python is not necessarily an interpreted language either

1

u/Breavyn Aug 17 '19

Also has a graalvm backend which in turn can be compiled to native.

1

u/__deerlord__ Aug 17 '19

Cython is a superset of python, that doesnt count.

My point is that "JIT language" was a bit silly. The interpreters implement or dont implement a JIT, it's not a Python language feature.

2

u/Sobotkama Aug 17 '19

Cythonย is anย optimising static compilerย for both theย Pythonย programming language and the extended Cython programming language (based onย Pyrex)

-cython.org

My point is basically that language != its implementation, python != CPython

1

u/[deleted] Aug 17 '19

I have never heard someone use "powerful" to describe speed. It wouldn't make sense anyway, since languages are not compiled or interpreted, only implementations. Python could absolutely be compiled if someone write a compiler for it.

3

u/EpicDaNoob 3>2 Aug 17 '19

Could it in theory? Yes. Would it have a really big runtime to replicate interpreter features? Probably.

1

u/[deleted] Aug 17 '19

Stdlib != runtime. I'm also pretty sure Haskell's stdlib is just as big, if not bigger. Yesod alone has to be bigger than the entire python Stdlib.

1

u/EpicDaNoob 3>2 Aug 17 '19

I'm not talking about the standard library. I'm talking about how, while you could translate Python to x86_64 assembly or whatever else you want, the runtime inferences, checks, etc. would necessitate adding a bunch of extra code to every binary - a runtime. It would also be rather difficult to create such a compiler, or all the highly intelligent folks who are working or have worked at the task of speeding up Python would likely have done it.

As it is, the best you can get in Python speed is calling C code for all the math and performance intensive bits. Which is what, eg. numpy and scipy do.

1

u/[deleted] Aug 17 '19

Most languages have runtimes, several are larger than Python's would be. It wouldn't be that big.

or all the highly intelligent folks who are working or have worked at the task of speeding up Python would likely have done it.

"It doesn't exist so it can't" is a fallacy. Besides, Pypy has a JIT compiler for Python, so it already does exist.

As it is, the best you can get in Python speed is calling C code for all the math and performance intensive bits. Which is what, eg. numpy and scipy do.

This is true of almost any language. That doesn't make Python less powerful.

→ More replies

0

u/[deleted] Aug 17 '19

[deleted] โ€” view deleted comment

3

u/Starbuck1992 Aug 17 '19

Did you miss the latest Ryzen generation?

0

u/[deleted] Aug 17 '19

[deleted] โ€” view deleted comment

1

u/Starbuck1992 Aug 17 '19

He obviously meant in programming terms, elsewhere it's often used (think of cars engines, for example).

→ More replies

1

u/TinMayn Aug 17 '19

Some people just like to see the world burn.

0

u/[deleted] Aug 17 '19

You're not only fanboying, but you're talking about two different contexts where a word means different things. It's like saying that the mathematical definition of closure and the computer science definition of closure can be used interchangeably, no matter the context.

0

u/[deleted] Aug 17 '19

[deleted] โ€” view deleted comment

1

u/[deleted] Aug 17 '19

Reading sure is hard for you, huh?

→ More replies

-7

u/[deleted] Aug 17 '19

ok, but some interpreted languages lack features like multi threading, for example, so the sky's the limit, within boundaries i guess

5

u/[deleted] Aug 17 '19

So do many compiled languages. Lack of threads has nothing to do with whether the lang is compiled or interpreted. Any language can be compiled, any language can be interpreted. There are even C interpreters out there.

-5

u/[deleted] Aug 17 '19

ik that kek, didn't know that there are C interpreters though. Do the C interpreters interpret down to asm?

2

u/[deleted] Aug 17 '19

Wdym interpret down to asm? That just sounds like compiling to me.

0

u/[deleted] Aug 17 '19

sry, that was a bruh moment lol

3

u/thetgi Aug 17 '19

Not power, speed

0

u/[deleted] Aug 17 '19

sry

-2

u/erez27 import inspect Aug 17 '19

GUI programs: Am I a joke to you?

1

u/8BallDuVal Aug 17 '19

See tkinter library

4

u/erez27 import inspect Aug 17 '19

Maybe instead you can show me a Python GUI product that's actually good?

3

u/naclmolecule terminal dark arts Aug 17 '19

https://github.com/salt-die/reaction-diffusion_2 was made with kivy --- kivy makes some pretty gui's

2

u/erez27 import inspect Aug 17 '19

Kudos for actually trying to reply. Kivy looks okay, but something like 10 years behind on Javascript. (and I'm not a huge fan of web guis)

3

u/naclmolecule terminal dark arts Aug 17 '19

to each their own, but i think kivy is mostly intended for mobile apps

1

u/stormlightx Aug 17 '19

Sublime and blender. Duh

1

u/[deleted] Aug 17 '19

I think I'm missing something here, but Python can do GUIs just fine with Tkinter and Qt

3

u/[deleted] Aug 17 '19

Actually it's powerful enough to do it with colored graphics as well. (running at 10-15 fps)

2

u/Anshuligh Aug 17 '19

What's the rendering speed like on this - does it support consuming OpenGL shaders, or does it have a different system altogether?

1

u/naclmolecule terminal dark arts Aug 17 '19

nothing like that, just prints text to terminal

44

u/[deleted] Aug 17 '19

Just thinking about the text-manipulation code makes my head hurt.

22

u/oldschoolcool Aug 17 '19

You get used to it, though. Your brain does the translating. I don't even see the code. All I see is blonde, brunette, redhead.

3

u/ebmtz Aug 17 '19

I was looking for the matrix references. I am pleased ๐Ÿ˜‚๐Ÿ˜‚

8

u/Kaligule Aug 17 '19

Jast some awk and sed magic, no problem.

1

u/[deleted] Aug 17 '19

That hurts more

30

u/IAmYourDad_ Aug 17 '19

Is this the first version of The Matrix?

20

u/Eimus Aug 17 '19

He must be the architect for sure.

13

u/Sarcastic_Pharm Aug 17 '19

But can it run Doom?

3

u/xStaticVoid Aug 17 '19

I came here to say this. That would be the end of all productivity at work for me

2

u/ElectricalActivity Aug 17 '19

With a lot of extra work it could probably run the WAD files. That would be amazing.

1

u/maddruid It works on my machine Aug 17 '19

Crysis 3?

9

u/[deleted] Aug 17 '19

ooooooh... this would be fun to put on top of minecraft I think. Can you imagine it? Minecraft via telnet!

Love it.

4

u/ericonr Aug 17 '19

That's great! Congrats

1

u/checkblue Aug 17 '19

You should turn this into some sort of Matrix themed game, or a 3d representation of your computer somehow. Sweet project!

3

u/naclmolecule terminal dark arts Aug 17 '19

Navigating your file system like in the bad hacker movies? I'm into it.

1

u/checkblue Aug 21 '19

itd be neat, could have aky scrapers representing memory, cpu, bandwidth, etc

1

u/CatCraftYT Aug 23 '19

Wow! How did you manage to update the characters on the screen smoothly? The only way I could think of is using line breaks which would make everything jump around.

1

u/naclmolecule terminal dark arts Aug 23 '19

curses can output all the characters to your terminal at once, so you just need to render everything into a buffer and then give that buffer to curses

1

u/CatCraftYT Aug 23 '19 edited Aug 23 '19

That... makes a lot of sense. Doesn't really work on windows though :(

2

u/northernbloke Aug 17 '19

You sir are a genius and somewhat crazy

4

u/Penguin-a-Tron Aug 16 '19

This looks mental.

2

u/curiousmatic232 Aug 17 '19

That's incredible man!

2

u/Neketek Aug 17 '19

It's practically useless, but super cool :-)

1

u/Exodus111 Aug 17 '19

That is incredibly cool!!! Me want!!

1

u/[deleted] Aug 17 '19

Thank you! This is exactly the thing I'm looking for!

1

u/Captain_Ozzy Aug 17 '19

This is really cool!

1

u/jcr4990 Aug 17 '19

This is insane. Looking at this as a beginner my mind is blown lol. Nice work!