Hello, dear Habrazhiteli!
Just want to make a reservation that this article was created just for fun, and does not pretend to any teaching property.
In our school, computer science is taught from the 6th grade, but we practically haven’t taught anything, because we so harmoniously and diligently formatted texts in the Word that we finished studying it only to the 9th grade. It was a little sad, and I started programming. I remember how copy-paste “Hello, World!” From a book in C ++, then the first calculator, then another calculator, then I learned ActionScript, made a couple of creepy games, and then I became lazy.
After another boring task on Excel, my friend and I decided to implement our long-standing idea - to make a multiplayer game. On school computers, there was Macromedia Flash 8, Borland Delphi 7, and at the other end of the class, the Windows Server 2003 emblem glittered on the monitor.
First try...
... and the very first failure. We tried to use the standard server socket component from Delphi, but this was affected by our little programming experience and we could not even connect. Even the search for possible problems on the Internet did not help - it was terribly interfered by the
useless Internet censor. Desperately, we were already thinking of giving up our idea, when suddenly a friend had a strange thought: “What if we do everything with text files ?!”. At first, I looked at him as if I was crazy, and then I realized that there was something in his idea.
')
As a result, we agreed that we should try to place the text files on a shared disk, kindly provided by the server, and see what happens.
Language selection
I attached great importance to this, even though the choice was very small: the old ActionScript, Delphi or Pascal. Defending the Flash, I tried long and hard to read the files, but (again, because of my limited experience), he did not want to open a text file. For a long time I was freaking out, calming down, thinking what was wrong, but to no avail. In the end, I quit this business, and again we went to Excel with happy mugs.
After about half a quarter, I had a free lesson - there was no teacher. We sat down at the computer, opened Turbo Pascal and decided to write "something with WinApi". Having rummaged in the help, we found such things as
SetCursorPos () and
GetCursorPos () . With a cunning facial expression, we wrote programs that synchronized the mouse cursors on two computers. It happened like this:

For "falling asleep" used crutch:
while (true) do begin delay:=0; while delay<10 do delay:=delay+0.00001; end;
For some unknown reason, the sleep function did not work, and we had to load the computer with extra calculations.
It is impossible to convey our joy when the cursor on another computer moves without the help of a mouse! Sources can be found
here . Inspired, we set about creating ping-pong.
Actually, the creation
So, we, inspired by our small but life-affirming Win, have begun to create ping-pong. Klin was the issue of visualization. We decided to use a two-dimensional array of characters, in which the field is filled with spaces with several characters' | '- players and "ball". There were 3 programs - controlling the first player, controlling the second player and controlling the ball and its physics.
Control
The control is performed with the mouse, using the same
GetCursorPos () . The first trouble that we encountered was the scaling of the cursor coordinates relative to our “grid” array. If our grid has 70x24 attributes, then school monitors with a resolution of 1024x640, which is much more. We learned the ratio of the “grid” and monitor attributes, and divided the mouse coordinates by the resulting ratio:
getCursorPos(cursorPos); py := cursorPos.y div 20; if(py > h - 5) then py := h - 5; if(py < 1) then py := 1;
In our case, the ratio turned out to be equal to 20. We also block the exit of our cursor beyond the coordinates of the array (h is the height of the field, 5 is the height of the player).
Ball physics
As I noted above, the whole physics of the ball is calculated in a separate program. Physics is the simplest - when the ball reaches the field boundaries or collisions with players, its velocity vector changes to the opposite. When colliding with players to give the gameplay
at least some interest, we increase the speed of our ball. When the ball goes beyond the array, we place it in the center of our field and attach random speed. Great, finished with the ball.
Synchronization
Perhaps the funniest thing about this game is synchronization. We have three text files:
ball.txt ,
player_1.txt and
player_2.txt . The ball.txt records the coordinates of the ball, the player’s upper limit of the player’s files. Each file is simultaneously kept open at once by three programs, which significantly slows down the whole game process. Nevertheless, no significant lags were found.
The moral of this fable ...
... obvious. If you have nothing to do, then you can even create such nonsense as this. and we can say that the time we spent is not entirely useless - when you play your own, albeit clumsy, stupid, but the game ... It’s impossible to convey emotions. Of course, we are just the most ordinary shkololo and we admire everything (as we admired “Hello World”), which is somehow related to programming, but it seems to me that we have not wasted time.
PS The sources are
here . Little comments!
PPS Completely forgot. The programs we started right away in the shared folder, so that they are sharpened to lie in the same folder as the text files.