Comment First Coding

Comment first coding

Comment first coding is a technique that I have used for many many years, but only today found out it has an actual name! (I had a jolly good Google before starting writing this post).

I find it especially useful when I am coding in a language which I am not overly familiar with.

The basic premise of Comment first coding, is planning out what you want your code to do using either plain English, pseudo-code or a mixture of both, in comments.

Then going through one comment at a time, actually writing the code proper.

	//db-jointeam.php - POST(team ID), find playerID from username ($_SESSIONS variable)
	//	If playerID = specified Team's ownerID
	//		Add player to teamlobbyplayers
	//		Set isActive to 1
	//	Else
	//		If isActive = 1
	//			Add player to teamlobbyplayers
	//		Else
	//			Error, lobby not active
	//		Endif
	//	Endif

Above is an example of some Comment first coding from a project I am working on which uses PHP to access a MySQL Database which holds the games player and team data.

Conclusion

And that’s pretty much all there is to Comment first coding. Like I said earlier, I find this technique to be extremely helpful generally, but more so when I’m using a language which I’m not overly familiar with.

Game Progress Update #9

Update #9

Hello World, and welcome to the ninth Dev with Dave Game Progress Update. I’ve not managed to get as much done as I’d have liked, but once again, the curse of tiny image resolutions has made creating the graphical assets take a fair bit longer than I was expecting.

The Menu

The first and most obvious change to this update is the inclusion of the menu button in the bottom left hand corner of the screen.

I’ve used a simple ham burger style icon to represent the menu, shown above with the red exclamation mark indicating that there is something to see in one or more of the menus.

Pressing the menu button opens up the main menu screen. There are currently three buttons, but depending on how things look, I may split the upgrades into two sections, which would require a 4th button.

I didn’t have time to finish the icons for these buttons so they have only simple text labels right now. Each one of the buttons opens up a new UI page.

Pressing the Upgrade button takes you to the upgrade page, where the player can unlock and upgrade various powerups,

All of the UI elements on the menus are created using the existing UI systems which I created earlier in the project.

Power Up Icons

I’ve also created icons for the power ups which the player collects. Below are the icons for the Magnet, Shield, Multiplier and Smart-bomb powerups.

I’ve not however managed to implement them properly into the game yet though.

Refactoring the UI Systems

I spent several hours refactoring the UI systems code after I noticed a much better way of organising the variables. It’s not really changed the way the code works, and there are no visible changes to the player.

Next time

In the next update I hope to have the UI looking a lot more complete, and be fairly functional as well. I’d also like to get the power-up collection a little closer to completion as well.

I also have a lot of updating to do on the Trello board and the GDD, I will try and get these done soon and I’ll update you via social media.

See you next time 🙂

Game Progress Update #8

Update #8

Hello world, welcome to the eighth Dev with Dave Game Progress Update. This time I’ve added the pickup system which will enable the player to use the powerups.

Pickups

The pickups allow the use of powerups, like the shield, which are activated for a limited time after collecting a specific pickup.

Currently the pickup spawns 15 seconds after the last one is collected or drifts off the screen. This is not how it will be in the finished game, but it’s perfect for testing purposes 🙂

The vertical movement is currently a simple sin wave, which is controlled by an angle# variable which wraps round once it reaches 360°.

And, like the coins, collecting the pickup was a simple case of modifying the existing collision function.

The Store UI

I’ve spent quite a long time this week away from the keyboard planning the UI/UX of the store.

Unfortunately the store hasn’t made it into the game yet, but here are some artists impressions of what it will look like.

Below is the Main screen from the store. The title bar area at the top contains a back button to return to the main page, the current page title and the players available balance of coins.

The remaining eight buttons represent a different section of the store. I may consider putting the player stats and achievements here, as well as a profile button and perhaps even the settings.

The next image shows the upgrades section. The title bar remains unchanged, except for the page name. The panels below this represent the upgrades you can purchase.

Each panel contains an icon which represents the upgrade, the name of the upgrade, the price and coin icon, and 5 bars which light up to indicate the level of upgrade.

Once a powerup is fully upgraded, the price disappears as no more purchases are available.

Next time

I hope that I’ll have the store UI in place in the game, even if not fully functional. I still have a lot of graphics to design and create to do this though.

See you next time 🙂

Game Progress Update #7

Update #7

Hello world, welcome to the seventh Dev with Dave Game Progress Update. I’m happy to say that I have at least managed to get the button handling system working this time round, even if there is only one button so far!

Buttons

The button handling system is a simple set of functions which can add a button to a UI page in a single command, check for button presses every frame, and also handle the response to each button press.

The system uses variables to track whether the mouse has been pressed, is being held down, or has been released. This will allow the buttons to animate eventually.

The button properties are all stored in an array the same as the other two UI element types I’ve created.

Problems I’ve faced

The biggest issue I have had whilst creating the button handling system, was the fact that the scanlines overlay was interfering with the button sprite detection. Any checks were simply returning the overlay sprite ID instead of the button below it which was the intended target.

I fixed this by simply moving the sprite offscreen which I check for button presses, then move it back before the screen updates. It’s not an elegant fix, but It will do for now.

The second problem is that I have found it very difficult to make recognisable icons using the low resolution that the game is based on.

In the next update

Over the next two weeks I’m going to spend some time creating graphics for the various UI screens, so hopefully lots more buttons and stuff for you to see (even if it’s not entirely working by the time the next update is here)

I also need to add a system to allow pickups to the gameplay, to allow the player to use powerups. Although I doubt this will make it in until the update after the next one.

See you next time 🙂

Game Progress Update #6

Update #6

Hello world, welcome to the sixth Dev with Dave Game Progress Update. This time round I have a whole new zone to show off, as well as having fixed a couple of niggling issues with restarting the game and some work on the UI handling system with the buttons.

Asteroid Zone

The first addition in this update is the new Asteroid Zone (Section 3.3.4. in the GDD). I’m currently using an asteroid asset which I found on Open Game Art, I like the style, but it’s not using the correct colour palette.

Because of the Zone systems I’ve already set up, spawning the asteroids was fairly straight forward. I did however have to make a small modification to the UpdateZones() function. It now checks to see if the current sprite is an asteroid or not, and if it is it adds a y velocity to the movement as well.

I expanded on the zone description a little on the Trello as well, which I need to update on the GDD, but that will probably wait till Monday now.

Game Restart

The changes here are mostly just bug fixes. The particles now no longer remain on screen when the game restarts, most (if not all) game related variables now reset when you restart the game.

The last thing I changed here was adding a function to remove any of the existing zone sprites ready for a new game.

UI Handling System

The one thing currently missing from the UI Handling System is buttons. I’ve added some variables and some empty functions ready for this, and created some place holder graphics which you can see below.

I had a real issue trying to create the settings icon in such a low resolution and sticking to the design principles.

In the next update

Next time I hope to have the buttons working, and have made a start on creating some of the other UI screens.

See you again in two weeks 🙂

Rubber Duck Debugging

Introduction

Rubber Duck Debugging (or simply Rubber Ducking) is one of the most crazy sounding debugging techniques available to programmers.

The premise is simple, when you get stuck with a problem with your code that the usual debugging techniques have failed to solve, then simply grab an inanimate object and explain to it slowly and step by step what your code does.

How does it work?

There are a few reasons why Rubber Ducking works.

Firstly, when you’re explaining your code to your duck, your are forcing your brain to think about the code much more slowly than when you are simply skimming over the code and thinking about it in your head. This in turn allows your brain more time to consider exactly what is happening in the code.

Secondly, explaining to your Duck switches your focus from an observer of the code, to that of a Teacher. This change of mindset causes you to evaluate your code from a different perspective, which can in turn help with finding and fixing bugs.

It might sound silly (and you’ll probably feel quite silly when you first start talking to a rubber duck), but you would be surprised how well this technique actually works. Especially for logic problems which aren’t easy to spot when simply checking variables in the debug console.

Why a rubber duck?

The name Rubber Duck Debugging comes from a story in the book “The Pragmatic Programmer” by David Thomas and Andrew Hunt, where a programmer would carry around a rubber duck and explain his code to it.

You can of course use anything you like. If you want to explain your code to your mug, then feel free. If you have a favourite cuddly toy then they would be just as happy to listen to you.

Pets can also work, although it’s a little frustrating when they walk off halfway through your explanation.

I’ve even successfully rubber ducked with my girlfriend (who has absolutely no idea about code or computers) whilst taking a relaxing evening walk away from the house and PCs.

Conclusion

I hope you have found this post interesting, and that it gives you another tool to use the next time you’re sat in front of the PC tearing your hair out because you cannot get the code to work.

Game Progress Update #5

Update #5

Hello world. Welcome to the fifth Dev with Dave Game Progress Update. The biggest change in this update is the work done to the UI Handling system.

UI Framework

This is where the bulk of the work has been done for this update. Continuing off from where I left it last update, I have added functions to load in the UI data as well as to change the current UI Page.

I’ve got the Text and Images working 100% but I still need to add the functionality for the UI Buttons.

I’ve created three UI pages so far. One for the pre-game, in game, and the Game over screens. UI pages are stored in an Array, and contain data relating to all the elements on that page. This allows a simple function to change the page when required, by simply hiding all the old elements and showing the new ones.

Unfortunately a bug in the UI handling code caused a big delay in development this update.

Restarting the game

I also made a start on being able to restart the game. I’ve not managed to completely finish this yet so it’s not demonstrated in the video.

Basically this bit simply resets whichever variables and sprites are necessary for the game.

Refactoring

Now that the code has been developed a little more, I have also been busy tidying up some of the less neat code and refactoring where necessary.

This has mostly just been moving code to the correct scripts and making sure that the comments are still accurate.

In the next update

I’m hoping to get another zone type working for the next update, and perhaps the beginning of the powerup collection system. I’ll also get restart game bit working properly.

See you next fortnight 🙂

Nine slice scaling

Introduction

One of my favourite visual effects in Flappy-vaders is the CRT TV style border around the screen, making it look very similar to what I remember my ZX Spectrum looking like back when I was little.

Creating the border graphic was fairly straight forward using Paint.net, but implementing it in game was slightly trickier.

When I initially put the border in place, I simply resized it to fit the dimensions of the screen. Which works fine when working on a screen the same aspect as the original image, but when using screens of different aspects then distortion soon becomes apparent in the corners and edges of the image.

The easiest way I could think of to remedy this was to use a technique called Nine slice scaling ( or sometimes 9 Slicing, or Scale 9).

How it works

Nine slice scaling is a way of scaling 2D images proportionally, which helps maintain rounded corners, borders and the like.

Normally Nine slice scaling is used for buttons or UI panels, where you would divide up a panel like the one below into 9 pieces, but it works just as well for the CRT TV outline shape as well.

Now, with the shape above, you can see that if you were to stretch it horizontally the corners would deform, becoming elongated horizontally, as would the borders on the left and right sides of the box.

With the Nine slice scaling, you divide the box into 9 smaller sections. And resize them all individually. The corners remain the same size no matter what happens. The top and bottom edges can resize horizontally but not vertically, and the left and right edges are resized vertically but not horizontally. The middle section is resized both horizontally and vertically as this has no details which can be distorted.

In the image above, the shape on the left has been resized using the nine slice scaling technique, the one on the right has just been resized to fit. You can see the difference in quality.

This technique means you can simply have 1 image for all the different sized buttons your UI needs, or for all your UI Panels etc.

Use in Flappy Vaders.

I decided to use 8 individual sprites, as I don’t want the big middle piece to be used for the border. This prevents the device from having to render a huge chunk of alpha transparency, which is never good for performance.

I also converted the original image into a texture atlas, so I could assign the sprites the correct section of image. Using texture atlases also speeds up render times because fewer draw calls are made.

Conclusion

And that’s all for now 🙂 I hope you found this blog informative.