📜 ⬆️ ⬇️

We compiled a snake in the browser

Have you seen linux on a PC emulator written in javascript?
If not - here is a small post about it .
But he himself .

This emulator is a good way to get to know the linux console.
If you have or had Ubuntu (and this is also linux), it had a user interface, as in Windows, programs and games are installed using the package manager.

Well, what can be done here?

One of the applied actions that can be performed in linux is to compile programs from source.
If you have ever written a program, you used an IDE like Visual Studio or CodeBlocks, but in this case, obviously, no IDE exists.
But you can compile the code from the console using the gcc program, which is very handy in the system image.
')
Just need to write in the console: gcc hello.c and press [ENTER]
Here hello.c is the file name, or rather the path to it, but now we are writing only the name, since hello.c is in the current folder.
Yes, in the console we have the current folder, its path is written to the left of # or $
( # - it means we work as root with unlimited rights; $ - as a regular user).
Moving to folders using cd% folder name% or cd .. to go up the folder higher in the hierarchy, and also ls , to list the files and folders in the current folder:

image

Well, we ordered gcc hello.c and wait:
(hello.c was put in advance by the author in the image of the system, for testing)
image

I waited a minute or two, but the program still compiled.
gcc created the a.out file (the default name), this is a ready-made binary, so let's run:
If you just write the name of the program, the command line interpreter will say that you have not found such a file,
because in this case it is not looking in the current folder, but in / usr / bin, for example, in other places where programs like cd, ls, gcc are located.
Therefore, we write ./a.out : ( ./ - means the current folder)
image

We see Hello, World on the screen, it means everything works. But a full minute on helloworld is long! Is it possible to quickly?
There is a simple solution: instead of the gcc compiler, use tcc - everything is the same, only instead of gcc hello.c you need to write tcc hello.c

Let's try to compile something more complicated!
True, we can only run the console application, so take the source code of the console snake nsnake .
The archive is a folder src, in it just what we need.
But the question is: how can these files be transferred to our emulator?
This question has an answer in the emulator FAQ:
You need to use the clipboard - the area for inserting text on the right and the command (program) cat <and> is redirecting the input and output streams respectively for the cat command: cat </ dev / clipboard> main.c
You also need to create a folder for the game and a src folder ( using the mkdir src command), into which we will copy the sources.
As a result, this process looks like this:

image

Using the up / down arrows, you can navigate through the history of the commands entered. So, in order not to enter a command each time, you can show the previous one using the up arrow and edit it.

Source codes are in place, now they need to be compiled, and then we will play.
But it's not so simple, an attempt to compile any of the files will result in an error:
image

The fact is that the compilation of the entire project is sometimes very complicated and time consuming, you need to add options to the compiler and linker to compile the files separately, and then collect the object files together, use external libraries, and the program also contains documentation and more ...
So that you don’t think about it when compiling a program from source, a Makefile is being written - it is just spelled out what to compile and so on.
In short, you need to copy it to the folder with the game on the emulator.
Also, you need to create folders bin, obj, and doc, as in the folder with the game that you have on your computer.
Type make - a program that will process the Makefile and compile everything according to it.
image

In general, I was tired of waiting and I pressed CTRL + C to complete the process. Why so long?
Because in the Makefile it says that we need to compile with gcc, and we don’t need this, we want faster using tcc.
So we find this line in the Makefile and replace gcc with tcc:
image
Delete the old Makefile, copy the new one and re- make :
UPD: A simpler version of ZyXI : make CC = tcc
image

Much faster. Great! It is written that you can already play!
Please note that only files that were not compiled by gcc last time were compiled.
Because the files were compiled separately, and for each C-file a .OBJ file was created in the obj folder, and then they gathered in
executable file in the bin folder.

Run ( bin / nsnake ), the screen clears and ...

image

Like this! We saw that the files were displayed in different colors!
Well, okay, let's play snake and without flowers.
In the snake source code, there is some line that displays an error message on the screen and, most importantly, interrupts the program.
If you delete it, the game may fall, since the function that controls the color of the symbols of the terminal will fall.
Or maybe it will just work somehow.
The code is in the engine.c file on line 243:



We write two slashes in front of each of the two lines - now the code is inactive, in other words, commented out.
Re-create engine.c and rebuild the program:



Now the snake starts and we see the game menu. Hooray!


There is a problem with the choice of the level of complexity and the presence of edges. Firstly, due to the fact that there are no colors, and the active item is highlighted in a different color. They are also chosen by the arrows, and the arrows do not work in the game.

You can play with WASD.



PS: to complete the installation of the game, you need to copy the documentation and perform a make install , but problems will arise here.
This possibility has not yet been considered: if there is already an executable file of the game, it can be transferred to the emulator using uuencode and uudecode (this is also written in the FAQ ).

UPD: the TERM = xterm command solves the problem of missing colors, and
TERM = linux to the heap makes the workers arrows - you can choose the level of complexity and the presence of edges.
Thanks to kekekeks for the tip.
(however, a strange glitch appears - the right border is smeared over the elements on the left - see the screenshot in the comments)

Source: https://habr.com/ru/post/207846/


All Articles