C++

Well I’ve started college and we’re learning C++. So recently I’ve been playing around creating many toy applications to get to grip with the language. What I particularly enjoy about the language is that it’s so easy to compile code for on the go. Compared to when I made simple games in C# using XNA, I needed to have Visual Studio installed on the machine along with XNA framework and if I wasn’t making games, I needed the .NET framework to make C# applications. However with C++ I can carry code::blocks around on a memory stick and be able to compile a program on any computer.

I’ve only being toying with the standard library, and like a good boy I try to avoid limiting my programs to one platform (windows) by not including the windows.h header. I still don’t completely understand how the standard library files are laid out, and don’t know a fraction of the available members I can use, but I guess that only takes time.

For a bit of content in this post I give you a simple pyramid ascii maker program. Input a number and it outputs a pyramid of “*”.

image

The source code is very simple, just getting the number of lines as an input, then using string.append() to format the pyramid.

Selec All Code:
#include <iostream>
 
using namespace std;
 
int main()
{
    int numberOfLines;
    int numberOfSpacesMade;
 
	string stringInject;
 
	cout << "How many lines to create: ";
    cin >> numberOfLines;
 
    numberOfSpacesMade = numberOfLines;
 
	for ( int i = 0; i < numberOfLines; i++ )
	{
		stringInject = "";
 
		stringInject.append( numberOfSpacesMade, ' ' );
		stringInject.append( ( numberOfLines - numberOfSpacesMade + 1 ) * 2, '*' );
 
		cout << stringInject << endl;
 
		numberOfSpacesMade--;
	}
 
    return 0;
}

C# Dev Blog

Well I haven’t updated in a while, but that doesn’t mean I haven’t been unproductive. Instead of jumping back into the game with a new design, I went back to reading C# 4.0 Pocket Reference, and I’m learning more syntax tricks and functionality of C# I didn’t think was possible before. So I plan on finishing the book, then re-reading it.

I don’t have any new content relating to my game, however I do have a slight treat, since I started programming again I’ve compiled my own little (big) list of bookmarked pages on a whole range of subjects. To save time I just opened it up in bookmarks viewer and saved it as a html file. You can view it here.

XNA Game Dev Blog #7

As I expected, I didn’t get round to do ANY coding today. However it has been a rather educational day for me. From yesterday I talked about how my code is getting a complete mess, and that I should refactor it, so I’ve been looking at a few sources today. 1 Website I was reading through was the MSDN library on XNA Framework Class Library. When I first saw this just shy of 2 weeks ago, I had nearly no idea what was going on. Looking back on my moving car, I was just guessing what methods to use based on looking at other peoples code and reading a few tutorials. The MSDN was just loads of jibberish, and that all I ever used it for was to see if I’m using a method right. So today I started looking through the XNA framework, and lots of it made sense since I’ve played around with most of the methods in SpaceShooter. I also came across this blog post, where they have created class diagram images of the XNA framework, very handy to flick through and see how everything is interlinked.

The second main source I was looking at was the source code of Net Rumble from the App Hub catalog. I chose Net Rumble to look at because it has distinct features that are similar to my game. Both have ships, move around, lasers/projectiles with upgrades, and collision. What I learned from Net Rumble, after looking at how the game works (which I’m still looking into), is that parts of the code have their own Draw calls, and LoadContent, something I thought that could only be done from the main game file, and how XNA games progressively loaded content such as the next level was some form of black magic. After cross referencing how Net Rumble is written, and looking back at the MSDN library, it’s all starting to make sense how they did it.

So today, I’m starting to learn how the XNA framework works, and how to take advantage of it all, (if not all then some of it). Also that class diagrams are brilliant to see how code is structured.

As for content for today, is my class diagram of my SpaceShooter:

As you can see, not very clear to understand what’s going on. It also shows just how bad the Game1 class is getting. It may be a while before my next blog post, but hopefully it will be about finishing the code refactoring.

XNA Game Dev Blog #6

Today I got round to adding explosions to the alien ships, as well as adding a semi upgrade to the ship. Other things done is the background shifting slightly with the player movement. Look pretty cool and a lot better than previously just having the background scroll along the Y axis.

My code is starting to get messy again, and I’m in desperate need to get it all organised again before I work on anything new. Everything up until today my code was reasonably manageable. Then when I added explosions, more code riddled into the main Game method for the creation of the animations when an alien ship is destroyed. Then when I added upgrades more functions came into the main game file with the collision checking, and setting new values of what the current upgrade the player has.

What I need to do is make a way for the player to call the add laser method by itself rather than the main game method, that way I can push most of the code onto the playership class. It would also help if I found a way to use the Draw method outside of the main game file too, however I’m not sure that’s possible. I’ll have to look into some examples on the App Hub Catalog and see how some of those projects do it. I wont be adding anything else until I sort my code out though, other wise I will seriously regret not doing it later on.

Here’s a video though of the current build:

 

Once I do refactor my code however, I plan on doing a list of things:

  • Add all upgrades (including the sprites that go on the playership visually)
  • Add more aliens (different size, different health, perhaps ones that fire lasers back)
  • Add a HUD of current health, scoring and general updates on the game (like what upgrade you picked up)

Later down the line, and probably the last thing I will do is create the menu screen, options, highscores etc.

XNA Game Dev Blog #5

Well I got animations working, both looping and non looping. It was surprisingly easy. All I needed to do was create a rectangle bound of the current section of the texture I want to draw, then every iteration where current game time – past game time > next frame, add on the sprite width to the bounds X axis. Here’s another animated gif of it working: Left is a non looped animation, right is looped.
This is the animation sprite sheet used: I put a red border on the edge of each sprite. 64×64 for each section.
You’ll also notice that the alien space ships move slightly different to previous posts too. A lot less predictable than before, for the better. Implementing the current objects to have animations is rather simple too. The animation class derives from the baseObject class, and if needs be I can just make my current object classes inherit from the animation class, add in the extra arguments and it’s done; everything will still work as before.

 

The next task is to add explosions for when a ship blows up. Then perhaps upgrades. After that probably move onto extra enemies, then scoring.

XNA Game Dev blog #4

To follow on from yesterday’s post, I got a bit done today. I got round to making the lasers and collisions collide with each other after playing around a bit. I also changed the playership slightly, to give it less of an advantage. I removed the side large cannons, of the ship and plan on making them an upgrade option. So now the ship looks like this to start with:Had a slight epiphany while I was doing the collision detection. Originally I was doing:

Selec All Code:
1
2
3
4
5
6
7
if  ( laser.pos.x >= alien.pos.x & laser.pos.x + width <= alien.pos.x + width )
{
	if  ( laser.pos.Y >= alien.pos.Y & laser.pos.Y + height <= alien.pos.x + height )
	{
		//Collision detected
	}
}

This code looks very messy and is so easy to make an error in the logic, and even harder to find it. So instead what I did was create 2 Rectangle objects that represent the bounds of the alien and projectile, then checked if they intersect, like so:

Selec All Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
for ( int i = 0; i < projectiles.Count(); i++ )
{
	for ( int j = 0; j < aliens.Count(); j++ )
	{
 
		if ( projectiles[ i ].Active && aliens[ j ].Active )
		{
			Rectangle rectangle1 = new Rectangle( ( int ) projectiles[ i ].Position.X, ( int ) projectiles[ i ].Position.Y, projectiles[ i ].Width, projectiles[ i ].Height );
			Rectangle rectangle2 = new Rectangle( ( int ) aliens[ j ].Position.X, ( int ) aliens[ j ].Position.Y, aliens[ j ].Width, aliens[ j ].Height );
 
			if ( rectangle1.Intersects( rectangle2 ) )
			{
 
				//aliens[ i ].Active = false;
				aliens[ j ].health--;
 
				projectiles[ i ].Active = false;
			}
		}
	}
}

If I can find a way to post code to a blog that doesn’t mess up the white space formatting and maybe has syntax highlighting for C# I’d paste the actual code. But until then here’s the current solution file for you to view the code yourself. Some classes aren’t used and I’m sure some things could be done better.

Small “error” that started bugging me for a good 30 minutes is the movement. While developing this game I noticed that for some reason, I could move in an angle by pressing the up and left arrow together, and same with down and right, and every variation in between. However when I implemented the shooting with the space bar, I was able to move up and right while shooting, but not up and left. I spent ages looking over the code for some syntax error, until it dawned on me that I’m developing on a laptop. This might not sound like an issue at first, but my laptop’s keyboard is recognised as a PS/2 connection, which if you’re familiar with one for gaming, does not support multiple key press detection as a USB one would. To prove that it was indeed the laptop that is causing the error, I plugged in my old logitech G11 USB keyboard and played the game, and as expected, movement was perfectly fine!

Here’s a small animated picture of the current game in action though:

Next to do is add animations, although for this I’ll have to create my own animation class to work through a sprite sheet at a set speed. I’m worried that if I create these animations I may have to redo a bit of my current drawing code of the baseObject class to support it and keep things modular. I’ll do explosion animations first and then see what I need to do to get the animation class to work with my current objects on the screen such as the player and alien ships. I may not add animations for the lasers as they are so small anyway and fly past the screen.

There’s no set objective yet, or scoring/ending of the game, the aliens just keep respawning and you have to keep shooting. Also there’s no collision between the aliens and the player ship.

Here’s a download link for the current solution though.

XNA Game Dev blog #3.1

Talking about my previous blog post. I searched around a bit and found it I can change/disable the colour key, by just right clicking on the image, going into properties and disabling the colour key. This will save me a lot of time recolouring the images. Here’s an image of where to disable it, highlighted orange:

XNA Game Dev blog #3

Well I spent most of today re-doing all of my code. Now it correctly supports deriving from classes. Before I just had a class for each object, each one was self contained, until today I finally learned the correct way to create inheritance with classes. I created my baseObject class, which has just the 2 basic functions as of now. The constructor function which has parameters for the viewport, texture and position and then the draw method which is just a basic spritebatch.Draw(texture, position, Color.White);

Doing all of this recoding has made the game so much more easier to work with, and I’m getting very comfortable with the C# syntax. I finally got shooting of lasers done (although no collision detection yet). While playing around with the shooting, I thought it would be a good idea to be able to cycle through your weapons that you want to fire, rather than firing everything all at once. The way lasers are done is when the space key is pressed down, it then checks if the player can shoot (if x amount of seconds have passed since the last shot) then adds a new instance of the laser object to a List. The List is the only reference point of the laser object, and will also make it easier looping through all the lasers to draw/update and soon check for collisions. Not only this but it will make it easier removing the laser object as I just have to call the List.RemoveAt() method, (correct me if i’m wrong) removing the reference point, passing it onto the garbage collection.

Funny error I came across when testing out the alien ships, the pink colour I used in the original alien ship, is the colour XNA uses to mark as transparent. So upon drawing the aliens only part of the ships were showing. As seen in the below image:

So now I’ll have to recolour the alien ships. I still have to add the flame effect back onto the player ship for when it’s moving forward, hopefully try get it animated as well. I will probably do the same for the lasers, as right now you can’t really see them, so I may play with sprite effects later. But first I’m going to get the game to a playable state with an actual goal. Then to start re-organising my code and making it more efficient which I should have really done while I was coding it all.

Draw art for my game

Been asked a few times now where I got my art from, so hopefully this post will clear things up a bit.

They are done in MSPaint, and then maybe touched up in photoshop (as it supports layers). But here’s a video of how I do it time lapsed from 15 minutes to just under 2.

XNA Game Dev blog #2

Not much coding done recently, all I have changed is the scale of the planets/satellites to a random value that’s no bigger than their native texture size and 16×16. I think later I’ll have to add some form of dust/mist effect that scales with the planets. So that when a planet is very small, a slight mist/desaturation is applied to give a better illusion it is further away. I did however add another layer of stars, which made a big difference visually.

I started working on the player ship graphics, fixing some small errors while also creating some upgrades for the ship. I did this by opening the original spaceship in photoshop, then by creating a new layer I’m able to draw on top, keeping the original safe, while also making sure everything is in proportion and same colour/style. Saving the images will just be as easy as hiding layers and save as a PNG file type.

Here’s an animated gif file I created to show how it works:

For the automatic laser, I plan on having the top most layer rotate around, aiming towards enemy ships ahead while shooting. This will require some more code for the aiming process which is simple math. Here’s an article I found a while back that’s aiding me.

I’m going to start working on the enemy ships now. After a recent talk with Ben Krueger, I’ve decided that the player, alien ships and even lasers fired are very similar (they both have a texture and position) with the slight difference that the user can move the player ship, while the computer controls the aliens, and lasers move in a set direction at a set speed. Weapons, are appended onto these objects, such as the laser cannons, this will also make it easier later on to create new aliens with better weapons, as all that will need changing is the base texture, and lasers attributes it fires.

Here’s a screenshot of current progress however: