Kingherc's .NET (and you're caught in it)

Bits and bytes about anything regarding technology or other things worth pointing out.

Ιανουάριος 2010 - Posts

HitEmUp - Source code of my MIX 10K Challenge Entry

What is it?

As I hope you’ve seen in my previous post, I participated in the MIX 10K Challenge (see here for the entry). HitEmUp features a classic idea, hitting enemies with your mouse as fast as you can! Hit 'Em hard and quickly to gain more points and more ranks!

HitEmUp Instructions: Click on the purple targets before their timer expires and get +1 Score or you lose a life. Don't click an empty nest, or you lose a life. Lose all lives and it's game over! Click on the yellow targets before their timer expires to get +1 Life. The more points you get, the more ranks you achieve and the more harder it gets!

HitEmUp_Screenshot HitEmUpCode

At the end it shows you a Game Over image taken randomly from the Bing services. It also features short sounds dynamically downloaded from the internet. The game gets really interesting and harder when you reach 175 score, about to get the Lieutenant rank.

I developed HitEmUp in Silverlight 3.0. It was a bit challenging trying to get the source code smaller and smaller in order to pack it into 10 kilobytes. That’s the main reason this contest is interesting. Not only for the pieces of software, but for techniques on making the code smaller. Even if it really doesn’t matter much, because C# gets compiled in IL :)

Play it!

You must have Silverlight 3.0 installed on your machine. Download this file, and extract it to your desktop. Open view.html with your favorite browser to play the game. Note: Internet resources may not play if run locally.

Source code

The source code is made up of two solutions. Download this file. The big one is the main project, right before getting minified and is about ~20K (only the .cs and .xaml files are counted against the size limit). The smaller one is the minified one and is about ~8K. [Note: The minified one is more updated than the big one, because I made some changes after minifying my entry.]

To understand what’s being done, start by examining the big project before going to the minified one. The first thing to check out is the Nest usercontrol which represents each of the 16 nests of the game. It has methods on how to activate a nest, either with a purple or yellow target, how much time it will be activated and an event when the timer expires etc. Next thing to check out is the main page. First examine the XAML code for the layout and the objects. Then, check out how the timer works.

The main variables that control the gameplay are: The maximum score which is set to 800 (the scores and the ranks are defined in the main page’s constructor). The spidersSpawnInterval which is the milliseconds between the random activation of nests. The badProbability which determines if the nest will be populated (randomly) with a purple or yellow target. The spidersTimeToSmash which sets the duration of a nest’s activation (yellow targets get half time).

Also, when the score increases, depending on the score value, the game progressively changes the above variables to create a harder gameplay. When Lives deplete, the game ends and shows a random “congratulations” image from the Bing web search services.

Next read on, on how the code got minified into more than the half of its size.

Minifying the C# code

Let’s tell many of the tricks used (or could be used) to minify the code:

  1. Main idea: Short names and code everywhere! Start with a one-letter named solution and project. Next thing to do is minify the code, before minifying the names.
  2. Remove unnecessary code from App.xaml and App.cs.
  3. var is your friend for declaring variables. It detects type automatically.
  4. Use lambdas where possible. The implicitly typed arguments are great for shaving off type names (just like var). Also, short-hand notation for usual procedures (searching, looping, replacing etc.) is possible. See this handy post for more on LINQ tricks.
  5. LINQ extensions to IEnumerable and IList are great. ForEach and Cast will save you a lot of code especially when combined with lambdas.
  6. Use the short-hand if-statement notation () ? : when you want to assign a value.
  7. Use the short-hand C# 3.0 notation for constructing objects and setting properties immediately. Example: var f = new DoubleAnimation(){To=1,Duration=A.t(500)}; where A.t is a shared function that returns TimeSpan.FromMilliseconds(500).
  8. Remember that you can use multiple assignments in a single statement. They are evaluated from right to left.
  9. Use fields instead of properties. Do not use access modifiers if not required.
  10. Code should be preferred to XAML. Many expressions require much less code than xaml code.
    1. Consider subclassing commonly-used non-sealed framework classes, to avoid long names.
    2. Use many “usings” in one big code file, if possible.
    3. Create small functions for typical procedures, like adding a shadow to an object, hiding or showing it, placing it (with Canvas.SetLeft for example), animating it etc. Store them inside a static class to be used everywhere in the project.
  11. Where XAML is required, consider:
    1. Think if Webdings font has a shape that you may need. If yes, use that instead of drawing paths with Blend. Remember you can specify a unicode character in a string like “\u0085”. Webdings are included by-default in Silverlight and will not add up to the size.
    2. Do not use unecessary color gradients or effects.
  12. C# is a very managed object-oriented language. It features a very strongly-typed system and allows Visual Studio to do dynamic refactoring on-the-fly. So next step, select each variable and function, select refactor, select rename. Choose a one-letter name to rename the variable. If small letters deplete, use capital ones. After those, start with two-letters names.
  13. Cleanup all of the whitespace. In C#, only semicolons and some spaces are important. Remember special spacing rules like int[]a and int?a are valid.
Sources
Share/Bookmark
Posted: Ιαν 30 2010, 12:24 μμ by kingherc | with 2 comment(s) |
Filed under: ,
Please support my MIX 10K Challenge entry - HitEmUp!

I made a little Silverlight 3.0 game for the MIX 10K Challenge. HitEmUp features a classic idea, hitting enemies with your mouse as fast as you can! Hit 'Em hard and quickly to gain more points and more ranks! Read the instructions at the start screen for more on the game logic. The game is complete in the sense that it has a beginning, a progressive speed and ends if you reach the top score. At the end it shows you a Game Over image taken randomly from the Bing services. It also features short sounds dynamically downloaded from the internet. The game gets really interesting and harder when you reach 175 score, about to get the Lieutenant rank. Try it!

As the entry can win the Community prize by gathering a lot of good ratings, I would like to ask you all to please support my entry by rating the game with 5 stars! Here’s where you can play and rate the game: http://mix10k.visitmix.com/entry/details/192. Leave a comment if you like it!

At the end of January, after the contest’s finished, I will also post on my blog the source files of the game. I will upload the full version and the minified one. It was really interesting trying to minify 20K into 8K! But it can really happen for C# source files.

Share/Bookmark
Access SkyDrive from Windows Explorer through WebDAV

Nowadays the cloud services are blooming really fast. One very useful service that many hosts are offering is online storage. A notable example is Microsoft’s SkyDrive service which offers 25GB of free online file storage. There’s also a rumor about Google offering a similar service named GDrive, following the example of GMail.

One thing most people dislike about online storage is having to go to the host’s website in order to upload, download, edit files. Fortunately, there are internet protocols to help us. And fortunately, Windows supports natively the WebDAV protocol for accessing remote file servers through Windows Explorer, plus supporting the HTTPS for solid security. All you need is have a SkyDrive account. Note: See my previous article on how to create your WebDAV folder to access them remotely.

Before starting, I recommend creating a new private folder in your SkyDrive, e.g. named “Files” and do the following in that folder. If you do not want to create a new folder, remember that sometimes a folder in SkyDrive has a different name shown and a different real name that is shown in the address bar. We need the folder’s name that appears in the address bar. Also, I must note that I haven’t been able to map the SkyDrive’s root folder, but only the folders shown in SkyDrive’s homepage separately. We will continue with one folder, the “Files” folder we created. You can follow the same process to map all of your other SkyDrive folders.

Now we have to “discover” the WebDAV address which we will use for our SkyDrive: I don’t know if this method will work for all SkyDrive users, but it will work surely for users who have signed up for the beta of Office Web Apps. If you create/upload a new Office document (e.g. Powerpoint) to SkyDrive, you can then “Edit” it with PowerPoint Web App. You will then see a button called “Open in Powerpoint” which will open Powerpoint 2007 and edit the online document. In your local powerpoint, click Save As and you will see the internet WebDAV address Powerpoint uses to upload the file. This is the address we need to connect Windows Explorer to SkyDrive’s WebDAV features!

My address has the following pattern: \\wsmdyp.docs.live.net@SSL\DavWWWRoot\{SkyDriveID}\Files
where {SkyDriveID} is an alphanumerical string that identifies your account and is shown in the address bar of your browser when you’re browsing your SkyDrive. Then we need to map this address to our Windows Explorer. Open the Command Prompt and execute the following command:

net use * \\wsmdyp.docs.live.net@SSL\DavWWWRoot\{SkyDriveID}\Files /SAVECRED /PERSISTENT:YES

It will create a new network drive in Windows Explorer and then you’ll be able to access your SkyDrive files! You can also rename the folder with a more friendly name like “Sky Files”.

The only issue left is the one involving the Live ID credentials. If you restart your PC, the network connection won't work (except if you previously login to SkyDrive). We have to make Windows remember your Live ID. This is what you must do (tested in Windows 7): Go to Window's Control Panel and click Credential Manager. In the left panel of the Control Panel, you'll see a link named "Link Online IDs" (also can be found in Control Panel's User Accounts). There you'll be able to link your Windows account with your Live ID. Additionally, in the Credential Manager, you can add a "Generic Credential" with your live ID (this is usually your hotmail) and your password, though I do not know if it's necessary to do that. After doing these things, you'll be able to access the skydrive network connection without having to previously login with the browser.

Finally, there is also a freeware application that does away with all the WebDAV hassle and does the same job: http://skydriveexplorer.com/.

As this is my post of the very first day of 2010, I wish you all a happy new year full of healthiness and creativity!

Sources:

Share/Bookmark