Yes it’s that book again, one I have mentioned a few times. Now there’s a project to convert all of the games from the book into C#, Java, JavaScript, Python, Ruby and VB.NET. As with all Git sites, you can download all code from the site in a zip file or individual files. So far though its very early in the project and there’s only a .BAS file in each of the subfolders for each of the games in the book. If you fancy getting involved, pick a game, a language and start coding. Sadly there’s no desire for C or C++ but that still leaves C# .
I still have this book and its sequel (More BASIC Computer games!) but to be honest I wouldn’t have bothered with most of the games in either book. They are all very much of the era (1978) which was just on the cusp of the home computer revolution and so were mostly obsolete within seven or eight years, due mainly to the terminal I/O. They’re ok for learning programming and of course you could run them in a Linux terminal.
These are from game-icons.net and are 512 x 512 pixels two colour icons and completely free to use so long as you give credit and include a copy of the licence or a link to it. There are over 4,000 but you can download a subset if you don’t want the lot. The download file zip file size for all of them as .png is only 33 MB.
Subsets include Animal (182), Building & Place (181), Weapon (172), Symbol & Emblem (171), Body (158), Arrow & Spear (146), GUI (140), Spike, Slash & Crack (138), Head & Face (126), Blade, Sword & Knife (126), Food (125), Liquid (122)Creature & Monster (119), Tool (117) and Board & Card (116).
What’s nice is you can have them as .svg (vector) or .png and have black on white or white on black.
They may look small when shown here but each is 512 x 512 pixels which is a lot.
Personally I always found the best way for me to learn a new language was to take an existing program- maybe something 500 lines or so long and completely convert it to the new language. It forces you to learn how to do things like string and file handling, organising the program, getting input and producing output and so on.
But having seen requests (on the C programming subreddit) for ideas to help someone apply their newly learnt knowledge, here’s a list of ideas of projects that are doable ion C. Nothing silly like database or operating systems!
A simple calendar. Enter a date and show the month. Bonus points if you can use past dates and show the day that a date is on. Hint. Look up Zeller’s Congruence.
This is the latest in a series on the design of a web game based loosely on a no-longer-run game called Inselkampf. It won’t be exactly the same as the original game, I have my own ideas. To see other articles in this series, click here or on the category: Blazor on the right hand side.
There are two models with Blazor. Server and WebAssembly. Both are similar but the crucial difference is that WebAssembly can run completely standalone whereas Server needs a connection to a webserver. Server uses SignalR technology.
The danger with using the Server model is that each person playing the game implicitly creates a connection. With the WebAssembly version, there is no direct connection though it makes sense to have a connection of sorts perhaps a Restful type interface. This means running a query to fetch game data and then running update queries.
So Blazor with WebAssembly it is. Why Blazor? Because it simplifies updating controls on the page.
Looking after Time
The nature of this type of game is any construction (buildings and units) takes a finite length of time. which can range from seconds to a day or so I want to see how long before my gold mine goes up a level and so on. It’s normal to have all times countdown in the browser. This should put no strain on the server as its running in the browser. Keeping server and client in sync needs a bit of care in case someone figures out how to make the browser code run faster. Once it reaches 0, an update should be sent to the server which should do a quick comparison with its time and if shenanigans has occurred, return an update status and resync the browser to the server. The rule is “If you give em a chance to cheat- they will“.
Procuring Graphics
Much though I like the Inselkampf graphics for all the buildings, I’m not going to use theirs. It’s infringing copyright (reimagining the game is not copyright infringement BTW!) . As well as the free kenney graphics that I’ve mentioned before and various other websites such as opengameart, (shown in the screenshot) there is also the Reddit game assets subreddit so between these and some others I hope I can find what I’m looking for. My requirements are quite modest.
If you look back at the puzzle, you might think the for loop is a bit odd starting at -1 and that’s the snag. It exits the for loop immediately because -1 is actually more than 5. It’s a rather subtle bug. The type of d is obviously int, but the type of (TOTAL_ELEMENTS – 2) is unsigned int.
So what I believe is happening is the compiler is promoting d to an unsigned int which is 4294967295, and clearly that’s a bit larger than 5. To fix this just put (int) before the (TOTAL_ELEMENTS – 2) in the for loop and it will work.
If you’ve ever wanted to draw fractals in C, here’s some code for you. These come from Misha on his(her?) GitHub page. There are three as you can see. Each has their own program – barnsley.c, sierpinski.c and y-fractal.c. All three programs create an SDL2 window then display the image.
Note that this was coded for Linux. I modified the programs so they would run on Windows and the screenshot was done from my machine. I know these images are on the GitHub page.
If you want to compile and run, then on all three programs change the SDL include from
#include <SDL2/SDL.h>
to
#include "SDL.h"
Also with the barnsley.c you need to give initial values to mx and my in the renderFractals() function (lines 88 and 89) and change these two lines: (114 and 115). No other changes are needed to let them compile.
mx_c = mx - win_w / 2;
my_c = my - win_h / 2;
to
mx_c = mx - WIDTH / 2;
my_c = my - HEIGHT / 2;
Brian Hall (aka Beej) has written or collected guides to several programming languages including one on C. Out of curiosity I pasted the one document HTML version into both Word and a text editor to get a feel for how big it is and it filled 416 pages on Word, almost 13,700 lines in a text editor.
It is quite comprehensive covering most aspects of C. He has a disclaimer that its a work in practice and there may be bits that need correcting but still, if you are looking for an excellent guide to C, I’d definitely recommend it and if you like it, send him a tip on Paypal.
In this case, I’m talking about better than JSON. JSON was invented as an alternative to XML which was invented as a way for computers to send data in a human readable form. Unfortunately XML is a pretty bloated format. Putting data into XML can make it five or six times larger.
Plus by the time XML was coming into common use in the mid-late 90s, there was a horrible letter soup of associated acronyms. It sort of appeared at the same time as Java and the whole XML ecosystem was lapped up by big business. JSON was born as an alternative method and it became very popular as JavaScript was growing at the same time, JSON is short for JavaScript Object Notation.
Now there’s MessagePack an alternative to JSON. It’s supposed to be faster and smaller than JSON. I came across it while looking at SignalR, a way to send data between clients and servers and used in Blazor and .NET websites. It must have been around for a while as it was used back in 2011.
There’s now 50 programming language implementations including C at the bottom of the MessagePack home page although there’s over 100 but that includes several implementations for the same language.
If you have to move data between two computers, or maybe two processes on the same computer, you want it to be as small as possible and that’s what MessagePack allows.
I’ve thought about doing this for a while. Build a small toolkit (a library) of helper routines for any program that uses the SDL2 library. That means it will have functions to do the following:
Draw horizontal and vertical lines in a specified colour.
Draw coloured rectangles both filled in and empty.
Draw Circles of specified radius and colour.
Draw hexagons in either orientation and of a specified size, hollow or filled.
Plus any other things that occur to me. I’ll start on this shortly.