Interesting Visualisation – like tixy.land

Interesting Visualisation – like tixy.land

Tileflip visualisationIf you remember tixy.land that I mentioned back in November last year, here is another website that lets you control a visualisation. In this case, tileflip.xyz lets you create a single function and applies it to every square on a board. You can control how many squares the board has and how often they are updated through a pair of sliders. You can pause the animation and toggle squares by clicking on them in the mouse. One of the early examples runs Conway’s Life and has a really short function.

The functions can be quite complicated (it’s JavaScript) as this example shows which moves a ball around the screen while changing colours.

The ctx (short for context) object has various properties and functions that you can call on it. For example ctx.resolution is how many squares there are on the board and this page lists all the properties and functions.

function rule(ctx){
  
  ctx.color = ctx.floatToColor(10/ctx.t)
  
  let r = Math.min(ctx.t**2/10, ctx.resolution/4);
  let cx = ctx.t*2%(2*ctx.resolution+1);
  let cy = ctx.t*3%(2*ctx.resolution+1);

  
  if(cx >= ctx.resolution+1){
    cx = 2*ctx.resolution - cx;
  }
  
  if(cy >= ctx.resolution){
    cy = 2*ctx.resolution - cy;
  }
  
  cx = cx/(ctx.resolution)*(ctx.resolution-2*r+1)
  cx += r - 1;
  
  cy = cy/(ctx.resolution)*(ctx.resolution-2*r+1)
  cy += r - 1;
  
  let toReturn = inCircle(cx, cy, ctx.x, ctx.y, r);
  
  if(!toReturn){
    ctx.color = 'black'
    ctx.invert = true;
  }
  
  return toReturn;
}

function inCircle(cx, cy, x, y, r){
  return (cx-x)**2 + (cy-y)**2 < r**2 ? 1 : 0
}

JavaScript is like C in many ways and while not the main purpose of LearnCGames.com, websites like this are an inspiration to games creators. My other interest is C# and I’m currently learning about Blazor a technology that lets you create webpages in C# using WebAssembly so it’s quite possible that things like this could be done in Blazor.

But it’s fun to look at the different example functions and you can also download the images as gifs via a link at the bottom of the page,

New Tutorial – Implementing the Windows game Slay

New Tutorial – Implementing the Windows game Slay

Slay gameOne of my favourite casual games is Slay by Sean O’Connor. I bought the full game which sells for $10 and it’s still selling well via his site or Steam. His game is only for Windows so my reimagining (great word!) will be for Linux only including Raspberry Pi.

His game comes with several hundred islands, in four sizes from very small, small, Large and very large. You play against 6 computer players with intelligence ranging from very stupid, stupid, clever to very clever. In addition there are a bunch of predefined maps like Britain or even the world.

Troops exist in four sizes with each size three times bigger than the one below. Your territory must be big enough to support the troops there and if it isn’t they all starve.  Splitting an enemies’ territory to starve his troops is a valid tactic.

Add in with trees which can spread and waste land which spreads even faster and its a cute addictive game.

My game will be called Onslaught and use its own graphics. Islands will be computer generated and hopefully the AI will play a decent game. The first tutorial which just explains the game and the scope of what I intend to do is now available.

 

 

Atoms tutorial three- finishing the game off

Atoms tutorial three- finishing the game off

Atoms 3 screenshotI fixed the bug in the Atoms game and the third tutorial finishes it off. The source code (in this case atoms3.c) has been placed on GitHub and the final version is about 300 lines long.

As well as the bug fix, I added a line to show the number of cells owned by the player and the computer. You can see it after the player move (Player : 41 Computer 16) and after the Computer move.

This whole atoms game was an experiment to see if I can introduce C programming without having to go overboard explaining every statement etc as I’ve done in the past. I think I’ll get back to programming games!

 

Tronus Project 1945 – Coursework

Tronus Project 1945 – Coursework

Project 1945A university student by the pen name of Tronus has put a project from his 3rd year in his degree course on GitHub. The screenshot shows the game running on my PC. The build folder includes a release folder with game exe plus all dlls and game assets in a resources folder below that so you can run it immediately on Windows. ,

I vaguely remember playing a similar game (a vertical scroller) in arcades back in the 1980s. That game was 1942.

The 1945 game has been programmed in C and uses SDL2. As always I took a walk through the source just to compare how it was done say compared to my own efforts. One thing I noticed is that he uses a fixed delay of 11 milliseconds each frame in the game loop by calling SDL_delay(11) rather than setting up the video to sync to the fly back. Is this better or worse? I’m not sure.

I think I prefer my method in case something takes a bit longer. Having a fixed delay means that you could risk overrunning the 16.666 milliseconds allowed per frame. That could never happen with my asteroids game as syncing to the fly back means my game can take as long as 16.66 milliseconds per frame and still not overrun.

 

 

Answer to simple C Question

Answer to simple C Question

Answer sign
Image by Gerd Altmann from Pixabay

Hopefully you got choice 2. 62. Here’s the question again.

#include <stdio.h>
int main() {
	float* p = (float*)50;
	p = p + 3;
	printf("%u\n", p);
}

The first line in main() creates a “pointer to a float” p with the initial value 50. This 50 is the value of p i.e. the address that p is pointing to, NOT the value at that address P is pointed to.

Adding 3 to p moves the pointer by 3 x the size of a float. As it’s unspecified, this size of a float is typically 32-bits or 4 bytes. So this line adds 12 to p. It now has the value 62.

You can try this out and step through it on the Python Tutor. Despite its name, it will let you run run programs in Python, C, C++, Java, JavaScript, TypeScript and Ruby. It compiles C with GCC 4.8 and uses a Valgrind Memcheck plugin. There are a few restrictions its quite a nice way to show off execution of simple programs.

Simple C question

Simple C question

question marks
Image by Gerd Altmann from Pixabay

I saw this this morning and got the correct answer. I also tried it just to be sure in Visual Studio and apart from a warning, it compiled ok and ran and gave the correct answer.

#include <stdio.h>
int main() {
	float* p = (float*)50;
	p = p + 3;
	printf("%u\n", p);
}

If you want to avoid the warning, change the last line to

	printf("%u\n", (unsigned int)p);

So the question is what does this output?

  1. 53
  2. 62
  3. 66
  4. 68

Answer and explanation tomorrow.

Next atoms tutorial added

Next atoms tutorial added

Atom2sThe first version of the program was 90 lines long but now it has grown to 275. This includes code to let the computer play and checking code plus I’ve refactored it a bit, simplifying the code.  I haven’t extensively tested it and at least one bug has crept in. Occasionally the computer seems to claim ownership of a player cell. This stops you adding one to that cell so its not good.

You are welcome to have a look at the code and fix it. I will in a few days. The code is on GitHub and is a single file atoms2.c. As before its been compiled with Visual Studio 2019 Community edition though it should compile more or less unchanged with gcc, clang etc. It runs in a terminal/command line. You can see the computer playing in the screenshot.

I’ve also added the tutorial for this.

Where are the business games?

Where are the business games?

Entrepreneur image
Image by Gerd Altmann from Pixabay

Back in the 8-bit days there used to be a few business simulation type games about. These were mostly text games; I remember one was a multiplayer game about making and selling televisions; you had to make decisions about how many lines to run in your Factory, how much to spend on marketing, R & D etc. and then each turn the program would determine how many TVs you had sold and whether you made a profit or loss.

The only modern equivalents seems to be games like “Pizza Tycoon” and of course the web/mobile game FarmVille (which has just shut down!) . There are others e.g. Airport Tycoon etc. But the simpler games don’t seem to exist any more.

The type of game I was thinking about were more like business simulations. Back before home computers were popular in the late 1970s, I worked one summer on a farm owned by a bloke who created business games for companies for use as training aids.

One such game I played ran in a games ‘Zine back in the late 70s. The person running the game must have had a computer because each turn I’d receive a complicated printout with all sorts of accounting information on it. Things like Cost of Sales, depreciation of value of stock, cost of storing stock and so on as well as P & L and Balance Sheet. It had to have been written by an accountant!

At the very simplest there are games like Hammurabi where you have to survive multiple years by selling land, planting and harvesting wheat to feed your population. This page on Wikipedia lists nearly 200 commercial and web games. It doesn’t however mention the likes of Dope Wars or even torn.com. Possibly those are perceived more like rpg/mmorpg rather than say pure business simulations.

For the simpler type of games like the ones I was describing, C is not a bad language to implement them in.

 

 

Program a NES game in C

Program a NES game in C

BNES Marioack in the mid 80s I was busy writing games for ZX Spectrum, MSX and CBM-64 and also porting games between 6502 and Z80. That was also when the original NES appeared.  That had a 6502 CPU but a lot less RAM than the CBM-64.

In those days the NES was programmed in assembly language. C was still too new for most things but now, it’s possible to program a NES game in C. Developer Douglas Fraker has produced a series of tutorials on how to create NES games in C using cc65, a C compiler/assembler that takes C code and outputs 6502 assembly language.

His tutorials cover the whole range of things including sprites, how palettes work, music, sound effects, even how to use the Zapper (a stand alone gun-like accessory) with all source code on GitHub.  You can run games on an emulator such as the one that comes with RetroPie.

Reading the page on 6502 ASM Basics brought it all back to me even though it’s 31 years since I last wrote 6502 code.

 

.

Fascinating online WebGL

Fascinating online WebGL

WebGL RabbitsWebGL (Web Graphics Library) is a JavaScript API for rendering interactive 2D and 3D graphics in a compatible web browser without the use of plug-ins. A developer called Todd Fleming has created a webpage where C++ programs (and maybe C?) can be compiled online by Clang and then run in the browser.

The colourful rabbits (numbering approximately 30) in the screenshot are rendered in real-time and rotated and moved (transformed). Each rabbit is actually each made up of 66,848 triangles.  Just click the (WebGL- Flying bunnies)  link on the right-hand side to load the 335 lines C++ source program then hit the compile button. After it has compiled in a second or so hit the Reboot/Run button on the right-hand side to start it running.

You can select all the source code and copy/paste it into a text editor if you want to examine it. You even can save out the compiled wasm (WebAssembly) file if you really want to though as it’s binary, it probably won’t mean too much unless you have a viewer.