Category: Game

First new C Tutorial has been added

First new C Tutorial has been added

Atomes boardAs promised, I’ve added the first C Tutorial using the Atoms (aka Chain Reaction) game. However this is just a very easy intro to the Atoms game and doesn’t have a line of C code in it. We’ll save that for the next tutorial.

The planned tutorials for this are:

  1. Intro to the Atoms game.
  2. Drawing the Atoms board.
  3. Reading the keyboard and making a move.
  4. Handling explosions and checking if the game is over.
  5. Programming the computer player AI.
  6. Drawing a high-score table.

These will each include a C listing and an explanation of what the new C code does.

 

GitHub Gameoff-500 entries

GitHub Gameoff-500 entries

Game Off 2020Between November 1st and December 1st, GitHub organised the Game Off 2020 where programmers submitted their own original games on the theme moonshot. There were 500 submitted and you can view them ordered by score here.

The top scoring entries are shown here on GitHub which also explains what programming language and toolkits were used. It’s interesting that of the 500 entries more than half were for Windows and approximately the same number are browser playable. The breakdown is Browser playable (262), Windows (252), macOS (108), Linux (96) and Android (8).

The GitHub link shows the programming language and C# + Unity seems to make up a large proportion as well as Godot +GdScript. But I haven’t been through them all and it would be nice to see if there are any that are C + SDL.

 

Some thoughts about game AI

Some thoughts about game AI

Military
Image by Dimitris Vetsikas from Pixabay

When I say AI, I mean control logic for computer played units in a game, not Ai as in Machine Learning or Artificial Intelligence. For instance in the Empire type game, that I started (On the Games source link) which has ships and units fighting on land and sea, capturing cities and building new units.

An AI for an Empire type game has a lot of logic to be implemented. I thought about this quite a bit and came up with a task based Ai system. A neutral city is spotted by an exploring ship. If its a transport with armour on infantry then it just attacks the city and hopefully captures it.

If not a task is created. This comprises a number of steps.

  1. Locate units nearest to the city that aren’t allocated to the task.
  2. If the units are on the same continent then just send them to attack the city.
  3. If not Locate the nearest transport and send it to rendezvous with the units at the nearest point to them. Load the units.
  4. If there aren’t any units free then locate the nearest friendly city that’s not tasked with anything and task it to build the units.
  5. If there’s no transport, have the city build a transport after building the armour/infantry units.

That’s just one for task but what if a friendly city is threatened and the tasked units are the nearest. Care has to be taken so that units aren’t flip flopped, getting orders one turn then being pulled away three turns later to a different task.

Each task should be given a time-to-live (say 30 turns), long enough to acquire the resources (units it needs) to do the job and reach the target. The rule then is no diverting units on a task unless a dire emergency (like threatening one of its cities) and those are the nearest.

There’s nothing worse than allocating units to a task then seeing enemy units sail right past unopposed. Writing code to deal with different situations and priorities is not an easy task

 

Pacman in C99 that runs in the browser

Pacman in C99 that runs in the browser

PacmanSuch a simple game yet still incredibly popular. Pacman is now 40 years old. I must confess, its not one that I was great at (that would be Battle Zone- the 3D wireframe tanks on the moon game- I could play that for an hour for just 10p),

This cross-platform implementation by German programmer Andre Weissflog (aka Flooh) looks spot on and plays it as well. Most of the game has been implemented in one C source file and its definitely worth reading the comments at the top of that file to see how things are done.

For instance “audio output works through an actual Namco WSG emulator which generates sound samples for 3 hardware voices from a 20-bit frequency counter,  4-bit volume and 3-bit wave type (for 8 wavetables made of 32 sample values each stored in a ROM dump)”.

The game uses the author’s Sokol libraries which are Simple STB-style cross-platform libraries for C and C++, written in C. It’s the first time I'[d come across these and they’re worthy of a separate look at. STB is a another library that I’ll also look at in the future. I’ve added a link to this Pacman to the C Code library. Happy Christmas!

PS. Also included in the GitHub download is The Pac-man dossier by Jamey Pittman, which is a 48 page PDF and tells you everything about playing Pacman. Stuff like the logic of gameplay, mechanics, easter eggs and the hardware specs of the original game boards not to mention the infamous level 255 overflow.

Interestingly the original hardware was a 3MHZ Z80A with 2 KB of RAM and 16KB of ROM on a 224 x 288 16 colour screen .

 

The L game by Edward de Bono

The L game by Edward de Bono

The L Game
Image from Wikipedia

I bought a book The Five Day course in Thinking by the author Edward de Bono when I was at University. It included a seemingly very simple two player strategy game called The L Game.  If you’ve heard of the phrase Lateral Thinking that was invented by him back in 1967.

On a 4 x 4 board there are two L shapes and two neutral single pieces. To win you just have to move your L shape and (optionally) one of the neutral pieces so that your opponent can’t move his L shape.

I’ve had a cursory search for C source versions of this game and there are bound to be more than the one I found which was written for an Arduino WifiBoy Pro. There’s a downloadable zip file which contains a file wbpro_chess_L.ino. Despite the extension, it is a C source code text file.

If you are looking for an interesting programming problem then create a computer opponent for a game. The good thing about this is, its so simple graphics wise that you could draw the board in a console with B = Blue, R = red and 1 and 2 the two neutral pieces like this.

1RR.
.BR.
.BR.
.BB2

 

Drawing  dungeon rooms using characters

Drawing dungeon rooms using characters

Some roguelike roomsThe final game will use graphics but those graphics will be based on characters, so I’ve started off by drawing a room or two using the provided extended ASCII characters.

Here for example is an 8 x 8 cell room with four inner columns and four possible doors. I’ve used spaces in one and full stops in the other to see which looks better. I think I prefer full stops as you can count them but it’s not a big difference.

╔════╬═╗  ╔════╬═╗  /----\
║      ║  ║......║  |....|
║ ╬  ╬ ║  ║.╬..╬.║  #....|
╬      ╬  ╬......╬  |....|
║      ║  ║......║  \.||./
║ ╬  ╬ ║  ║.╬..╬.║
║      ║  ║......║
╚═╬════╝  ╚═╬════╝

The 3rd room is smaller and roughly octagonal though not as nice looking. I used the normal ASCII characters for that with the two slashes ( / and \ ) as well as # for a vertical wall door and two | for a double door. Here’s a corridor that goes round two corners and then meets another one.

═══     ╔════╦══
   ╚════╝    ║

There are plenty of ASCII character charts and I used this one which describes all the graphical chars making it easy, albeit a bit tedious to draw these in any text editor. There are single box characters as well as the double ones I’ve used. The ╬ character has proved very versatile as it provides not only columns but doors in both horizontal and vertical walls.

It’s noticeable that despite this room being square, it looks rectangular thanks to characters being taller than wide. Also in WordPress, the gaps between lines are more noticeable as gaps whereas in Notepad++ (the editor I used to create these) as shown in the image at the top, these gaps are absent.

How to make a game with QR codes

How to make a game with QR codes

Example.com QR codeThis follows on from yesterday’s post about creating QR Codes. How about creating a web game similar to the Choose Your own Adventure type games but with a difference? I did think about implementing this as a proof of concept and may yet still but ideas are worthless until executed so I’m happy to put this out there. Here are a few notes on a proposed web QR game.

  1. You can get open source QR generators for PHP, C#, probably most programming languages.
  2. The web pages show one or more QR codes with a title on them. These are actions and by opening the URL (say in a QR reading mobile app) the action applies. Eg Go West into a room, open a chest etc.
  3. If you use an 8 character parameter to the URL that gives you something like 2.8 trillion different destinations. eg example.com/r/h65tr321.  The idea is that the web application generates a page from this parameter. If the parameter has been spoofed then you fall into a bottomless cavern, get eaten by an Ice Worm etc and start again. These parameters are generated anew for each game.
  4. You can add extra information such as carrying items with extra characters. Unlike web games, there only way to pass information is via the url. So the URL might now be example.com/r/h65tr321a6. (the QR code shown is for this url) The page would still be created from h65tr321 but the a6 might mean you are carrying say a sword and key. If you add on a character to the query for each item, you can have 36 different types of item using a-z and 0-9.
  5. The whole adventure is created as a list of rooms, each with a description. A random eight character code is generated for each room. That’s where the h65tr321 comes from. At the start this list of rooms is read into the web application.
  6. Puzzles are of the blocker type. To open a door that lets you escape you need to be carrying a key but that’s in a chest protected by a Dragon. You can fight the Dragon but only if you have the sword and the Dragon will always kill you unless you are carrying a potion of extra hit points. So first you have to explore the rooms, going from room to room and picking up items. But you can only carry three items at a time. You can either use or drop an item which stays in the room where you dropped it.
  7. Every room you enter when carrying items displays each items with a use and a drop QR code.
  8. Other ideas include a time element that reduces each time you move rooms/do an action. You only get so long to escape but a potion that can be used once adds more time.

As you can use any app to scan a QR code and jump to the url, it would be a bit tedious to play but might make for an educational project for teenagers or schoolkids.

It should be possible to build an app might be able to detect the QR codes automatically and display just the link text. Again like QR generators code there are open source packages for reading QR codes.

 

 

A first-person-shooter in 32KB

A first-person-shooter in 32KB

Anarch FPSDon’t expect this to be Call of Duty standard but then those games typically have a 50GB or higher footprint on disk. Anarch comes with 10 levels, 6 weapons, 7 enemy types and 3 ammo types and runs in 200 KB of disk space and runs in 32KB RAM. It’s 100% C but doesn’t use any FPU, GPU or file I/O. It’s old skool 90s style.

If you’ve played original Doom you’ll have an idea what it’s like.

Interestingly, the creator has put everything, source code, graphics etc into the public domain which is a very philanthropic gesture. So if you want to understand how to write a FPS (first-person-shooter) in C, take a look. You can even play it in a browser.

I’ve added this to the C Code links page (on the top menu).

 

C++ Template Metaprogramming Game

C++ Template Metaprogramming Game

Snake
Image by OpenClipart-Vectors from Pixabay

Templates in C++ are a useful feature. Without them you’d not have template functions, or more usefully template classes like vector etc.  But there is an even more useful feature called template metaprogramming. It’s a very advanced and clever feature; one I have never done and I don’t think anyone could say they’ve mastered C++ unless they are good at it.

Here’s an example of the simplest example I could find. It comes from here and what it does is generate factorials of numbers at compile time. So when you run it it comes back with the answer immediately.

// factorial.cpp

#include <iostream>

template <int N>                                                                 // (2)
struct Factorial{
    static int const value = N * Factorial<N-1>::value;
};

template <>                                                                      // (3)
struct Factorial<1>{
    static int const value = 1;
};

int main(){
    
    std::cout << std::endl;
    
    std::cout << "Factorial<5>::value: " << Factorial<5>::value << std::endl;    // (1)
    std::cout << "Factorial<10>::value: " << Factorial<10>::value << std::endl;
    
    std::cout << std::endl;

}

But if you think that is clever how about a game where every time you compile it, it makes a move and remembers the move between turns? A developer called Matt Bierner has developed a snake game using template metaprogramming.

So I downloaded it into my Ubuntu, installed Clang and clang tool just for good measure and compiled. This is the output. Iv’e snipped a lot out after the first two. Ot’s very clever, in this case, not much use but I doubt if there is any other programming language in which you could do this. The compile plays the game, running it just outputs the results. Yes it’s not exactly practical but still…

david@davidvm:~/STT-C-Compile-Time-Snake-master/stt-snake$ clang++ -std=c++1y main.cpp -o snake ; ./snake
------------------
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺▶*╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
------------------
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺*╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺▶▶╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
....
-- You Are Dead --
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
*╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺▼╺╺╺╺╺╺╺╺
╺█▲╺╺╺╺╺╺╺
╺▼▶╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
More thoughts on the design of Rogue like

More thoughts on the design of Rogue like

Unicorn hackSometimes I walk up around 3:00 AM and my mind is abuzz with things like this. Last night was one such night. The first thought was I should stop calling it a Roguelike. There’s a certain set of conventions with those and I don’t want to be limited by that.

For instance here’s an article about things you should or shouldn’t do in designing Roguelikes.

The game (for the next Ebook) is not going to be an all singing all dancing version but will be an MVP in the parlance. That’s Minimum Viable Product,

So if I’m going to divert away from the ‘standard’ then it needs a name. Something evocative like Dungeon Trawler but shorter more catchy.  So I’m going with V.O.R. (Vaults of Ruin) or maybe just Vor,

So onto design aspects. Here’s a list of the features:

  • 10-15 different Monster types with differing melee, range weapons and magic. Can have packs of them, not just one. There is only one player. Should be fun!
  • Simple set of castable spells using a Mana value. It is used up casting spells but regenerates slowly as you move. It can be replenished quicker by consuming potions.
  • No food or water but the character has an energy rating. This is depleted by movement and fighting. Rest or sleep oe rating food replenishes it but I don’t want it to be a major thing. I’m not having deep dungeons with 25 or more levels, but 12 levels maximum.
  • Weapons are sword, dagger or bow. Can switch between them.
  • A few powerful magical treasures.
  • Turn based not real-time. Asteroids is real-time but movement will be restricted to so many squares for you and monsters. You probably won’t be able to outrun the faster moving monsters.
  • Limited visibility can make things interesting. It means the game has to calculate what squares are visible each move.

I was tempted to do away with grid movement; for instance in the Asteroids game, they can move at any angle and velocity and are not tied to a grid. But that might be a bit too complicated so movement will be grid limited and Up/Down, Left/Right and maybe diagonal.

The screenshot is from an open source (.NET Core/C#) web rogue game called Unicornhack.