Tag: C#

Game in development – at last!

I’m not sure exactly why. Confidence?  Time?  Simply being in awe of daring to do something that I hold in such high regard?  I simply don’t know but I’ve tried and tried to motivate myself to write a modern version of Laser Squad for a long, long time.  (Same goes with Dark Side, Hydrofool, Tau Ceti and Auf Wiedersehen Monty)  I got as far as a buggy version of Monty before real life took over.  Now I’ve got some motivation and I’ve pulled my finger out…

Rebel Forces is now in development!!!

I feel it would be a heresy to simply remake Laser Squad since, although it’s a classic, it’s also quite limited by the machine it was originally written on.  My intention is to remake an enhanced Laser Squad.  Cleverer, more flexible and with many more scenarios – even a campaign!

The current state of things is that I have defined the core C# objects and am building using MonoGame which should allow me to (eventually) publish for all the major platforms.  I’m going to charge a nominal fee for the game.  It would be nice to actually make some cash to pay for licences, etc. but I’m not planning on charging more than a pound for each version.

Currently in scope are:

Windows 8 Store, Windows, Windows Phone

in scope but lacking a licence:

Linux, Android, Ouya, PS Mobile, iOS, Mac OSX

Anyway, I digress.  I currently have the core objects in place albeit slightly threadbare.  I’m in the process of building an editor to allow me to build scenarios.  Naturally, once I have some scenarios ready, I can build an engine to process the moves, etc.

I don’t like to make detailed timescales since real life has a nasty habit of interfering in your plans but if all goes well, I’m hoping to have the editor up and running properly by mid-March.  Then I’d hope the engine would take another month to 6 weeks to build.  Finally, I’d hope that while testing, I can get my sprite sheets cleaned up with nicer graphics and ping in some music and sound effects.  Then a final round of beta testing (any volunteers?) and I can start putting together the releasable campaigns – with free future updates/campaigns subject to time and demand.

I also plan to contact Julian Gollop for permission to include the original missions as unlockable rewards in-game.  I’ve heard he’s made the game public domain but to have his blessing would make it even better.

One final thing, I’ve made the editor with a rule builder which should (hopefully) give stronger conditions and scoring for gameplay.  For example, I’ve included some sabotage/subterfuge rules to allow for missions where you hack a computer for data or even plant a virus.  Also, you could plant bombs or transponders for mother ships to target a particular location :)

If anyone has any other suggestions or ideas, please leave a comment.

VN:F [1.9.22_1171]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.22_1171]
Rating: +1 (from 1 vote)

C# – goto, the spawn of satan

Oops, it’s been a week since I last posted but I guess I earned a break from working hard and I was struggling for an issue I came across recently. Then a quick google and I noticed some code that used something I haven’t seen since I was a teenager programming in locomotive basic on my Amstrad CPC 6128… a goto statement. I knew that C# does have this command and there are people out there who argue that it is useful. It probably is, but for the sake of your (and anyone who would be maintaining your code in future) sanity, DON’T do it!

I had another skim through the internet and found the following source code that uses goto:

for(int i = 0; i < size.Width; i++) { for(int j = 0; j < size.Height; j++) { Color c = Color.FromArgb((int)pixels[i,j]); if(c.A < 255) { hasAlpha = true; goto breakSpot; } } } breakSpot:

as you can see, the user is trying to iterate through a 2D array looking for a pixel with a value larger than 255 (meaning it has an alpha value) and is then exiting the loop. Despite this being a (relatively) safe usage of the goto statement, it makes for lazy coding.

How would I do it? well, here is what I'd do...

for(int i = 0; (i < size.Width && !hasAlpha); i++) { for(int j = 0; (j < size.Height && !hasAlpha); j++) { Color c = Color.FromArgb((int)pixels[i,j]); if(c.A < 255) { hasAlpha = true; } } }

Oooh, you didn't know you could do that did you? It's a for loop! You have a start value, an adjuster and a condition. The condition doesn't even have to have anything to do with the value used to count the loop. It generally does but it isn't required.

Anyway, I hope someone found that useful and that I've advanced the cause of the 'anti goto' movement. Enjoy!

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

C# – Interfaces… why?

Yep, you read right. Why on earth do we use interfaces? I’ve seen a plethora of crappy examples involving shapes, fruits, vegetables, etc. attempting to explain why we use inheritance and interfaces. Bloody useless! Why? They have no proper real-world application that we can associate with. Unless you’re a mathematician or a greengrocer, why on earth would you care about fruit or shapes?

Ok, lets start with a definition of an interface.

An interface is a contract of service within a programming language.

Ok, what on earth does THAT mean? It means when you implement an interface, you are agreeing to provide functionality that satisfies the interface contract.

So… what use is that exactly?

An interface would be a structure with no implemented methods. A (terrible) example from MSDN is:

interface IMyInterface: IBase1, IBase2
void MethodA();
void MethodB();

This defines an interface with two methods. MethodA and MethodB. It has no implementation code. It also can inherit other interfaces (IBase1, IBase2) so you can structure them. That’s more a FYI than anything because that complicates things far too much for the simple explanation I want to give out.

Interfaces can be very useful. Let’s use a real-world example that we should all be fairly familiar with. Let’s pick an ATM at a bank. (I pinched this example for a C# design book I picked up about 7 years ago. I don’t have the book to hand or recall the exact name. I may add it here at a later date)

A bank ATM has the following basic functionality:

– Enter Pin Number
– Check balance
– Pay in Money
– Withdraw Money
– Print Mini-Statement
– Sign out

If I pop over to my bank branch, I get a few more functions but that’s nice and simple for now. In the UK, we have a system called ‘LINK’. Basically, this allows account holders of a LINK member bank to use basic functionality at another LINK member bank’s ATM for free. Naturally, this would have limited functionality since it can get quite busy on banking networks and you really want minimal traffic so the banks will want to provide basic functionality on their ATMs for LINK member account holders. So, how do we guarantee the same functionality on all ATMs? Every bank functions slightly differently to the other so what are we going to do? Yep, that’s right, we use an interface!

The interface could be structured as follows:

interface ILinkFunctions:
double balance;

void CheckPin();
void ShowBalance();
void WithdrawCash();
void PayInMoney();

If every bank implemented this interface for their ATM software when dealing with LINK customers, they all get the same functionality!!

This now means that when you use a LINK machine, you will have the same basic functions wherever you go. Not only can you use this to guarantee that certain functionality exists, you can also use the interface as a type when making use of the functionality in other parts of your code. Here’s a simple example of that in use:

public void SpecificLinkFunction(ILinkFunctions source)
--Implementation here--

The above code allows you to make use of the knowledge that ILinkFunctions has certain guarantees. Think of how you could be writing a game in XNA and you want to process ALL sprites rather than have to process individual categories of sprite (baddies, hero, bosses, bullets, etc.). If all these sprite classes implemented an interface, you could process all sprites of that type in one simple function that made use of the interface they all share! Doesn’t that make life so much easier?

I’ve barely touched the surface here and I’m waffling on but a nice little example involving pencils is online here

Good luck!

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: +1 (from 1 vote)

C#, XNA – The problem with Monty

Ah! Monty Mole! That loveable character from the 1980s series of games for the Amstrad, Spectrum and Commadore. As some may already know, I’m in the process of remaking “Auf Wiedersehen Monty” with the outstanding help of Lobo and Infamous. Sadly, finding time to complete this while my work/financial situation is so bad at the moment is a bit of a problem. Despite needing to re-work the collision detection and pretty much finish the thing, it is gone quite well so far. I thought I’d share one of the interesting ideas that some people may not realise they can do in C# and XNA which is storing the state of the user controlled sprite as an enum! Here’s a snippit of code from the game:

// Deals with Stationary and moving for animation purposes
if ((((mCurrentState == MontyState.WalkingLeft) || (mCurrentState == MontyState.WalkingRight)) || ((mCurrentState == MontyState.ClimbingUp) || (mCurrentState == MontyState.ClimbingDown))) || (mCurrentState == MontyState.Stationary))

It looks a little complex doesn’t it? Simply translated, it basically checks whether the Monty character is walking left, walking right, climbing up, climbing down or stationary. I’m sure you could have worked that out but it is so much more readable like that isn’t it? Much more readable and efficient than creating loads of flags or a numerical variable with sokme kind of coded number representing the character’s status.

So… how did I do this? Well, I used an enum! The code is here:

public enum MontyState

This now means I can create a variable like this:

public MontyState mCurrentState = MontyState.Stationary;

Whenever something happens, I change Monty’s state to whatever it should be and the program can react accordingly. I’m probably teaching some people how to suck eggs here but I did have a good think before I decided to take this approach. Making it public means you can use the status in other parts of the application too. You can even make it static if you wish.


VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

C# – Accessing attributes directly in XML

Well hello and welcome to my blog.  God knows if it’ll be of any use to anyone but if it is, I’d appreciate a comment saying so.  It isn’t much but it’ll make me think that I do occasionally do something worthwhile for the world.

Right, onto the topic of the first post.  If, like me, you get pissed off with hunting the internet for an example of some code that does something similar to what you’re trying to achieve but never seem to find it, read on.  That’s pretty much the main reason I set this blog up;  to highlight minor (and major) issues in development that crop up from time to time and post a solution (even code, you never know) for others to make use of.

Anyway, the other day I was working on some code that processed some XML and I was required to produce some C# that extracted a particular value from this file.

Use LINQ I hear you cry!  Not in .NET 2.0 you can’t.  I hunted around for examples and, lo and behold, I found nothing of any real use.  Plenty of examples that use foreach loops to iterate through the many attributes and writes them out to the console but nothing exactly describing what I needed. (By the way, who the hell does that in the first place?  MSDN!  I’m looking at you here.  Producing piss-poor examples is no help to anyone!)

Anyway, onto the code.  Let’s take an example XML file like the following:

<?xml version=”1.0″?>
<TheDocument xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”>
<TheHeader Version=”3″ FileType=”1″ Size=”44″/>

Ok, it’s a bit crappy but it is an example.  Anyway, I need to access “FileType” from “TheHeader”.  How do I do it?

Here’s how:

// Create the XML Document
XmlDocument _xmlFile = new XmlDocument();

// Create an XmlTextReader to load the file
XmlTextReader reader = new XmlTextReader(@”C:\SampleFile.xml”);

// Load the file into the XML Document using the reader

// Set up an XML element and pop the document into it
XmlElement root = _xmlFile.DocumentElement;

// OK, now select that attribute
int _fileVersion = Int32.Parse(root.SelectSingleNode(“/TheDocument/TheHeader/@Version”).Value);

Although this example is fairly specific, you should be able to create a C# project, paste the code in and get it working quite quickly.  You will need to include the following using statements at the top though:

using System.Xml;
using System.IO;

You’d have to hunt high and low to find anything that useful on t’internet.

Anyway, please be gentle as it was my first post.  All feedback welcome and any suggestions for future posts would be nice too.

Toodle pip!

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)