Using an image as an object

Ok so say I'm making a 2D game, I have one picture as the background and another one as the (foreground) land. Etc for those of you who know the game worms - Like that. Well basically I was going to try to make a clone of that. I know how to code everything except for the foreground picture which would need to be solid and funny shape. So just wondering how exactly I would go about about making it solid to have a character walk on. And to be able to explode. I Understand how I think I would be able to program that. But firstly I want to be able to load in a solid background that's able to be moved around on.

http://upload.wikimedia.org/wikipedia/en/0/08/Worms_World_Party_screenshot.png

Here's a link to a picture of how I would want the background and foreground to be for example. Just not sure how to go about it.

Cheers,
Myth.
I would use a chroma key (magenta #FF00FF or perhaps black #000000) and make those pixels transparent, for example, by setting an "empty space" flag every pixel would have. Then, as the terrain gets destroyed, you just set the "empty space" flag on each of the pixels.
You could make the flag 0 for empty space (transparent and passable), 1 for ground (opaque and impassable), and 2 for burned ground (opaque and passable). You could make a pretty interesting map by having a value 3 (transparent and impassable).
So your saying to use a "chroma key" (I'll have to research it as I've never heard of it before) and set that as a certain color to overlay over the picture. Then use that as flags to be visible and invisible so it displays those sections of the image?
Ok I just read how a chroma key works on wiki and it sounds like a good idea to use something in the lines of it. Never used it before so it may be a little tricky but guess I'll see what happens. Only question now though is - am I actually able to use this chroma key as checking to see if the ground is solid or not to walk on?
The chroma is merely for the benefit of however is editing the image. It would be more efficient to load this as an array of color values and transparency values.
Suppose your image was this (#= ground, .=space):
...............#
.............###
...........#####
.........#######
.......#########
.....###########
...#############
.###############

Using data like that from code is awkward. Instead, you'd have something like this:
color[0][0]==something
space[0][0]==1
...
color[0][16]==something
space[0][16]==0
I understand what you mean. But does this mean that I'm going to have to go over my whole image and technically write for each pixel if its an object or not?
Could you rephrase your question? What do you mean by "technically"?
And while you're at it, what do you mean by "object"? Your entire terrain should be one big object.
Alright, Well lets forget about the background image I was talking about and just focus on the foreground picture. So say this image was like a big blob (something like this for example - http://www.varitek.co.uk/img/blobblue.png) Ok now say I needed to make it so everything in it was solid so a character could not go inside it. I would gather you would get the edges of the picture (the blue blob) and make them solid or something? So you could move anywhere around that blob but not inside it. Does that make my question any better for you to understand or would you like me to try to explain it another way?
Oh yeah, say in worms for example - they can walk on the top of the terrain. But it's usually bumpy and what not. So how could I make it so a character was able to walk over the top of it and follow the pathway of the outline of the image?
Ok, so we'll call the foreground, terrain.
A terrain would at least need two members: an array with pixel data, and a 2D array related to the other by location that holds wherer a pixel is passable or not. When I say "related by location", I mean that the array element [x][y] is applied to the pixel [x][y].
To fill your arrays, you need an image. Let's take this for example: http://img391.imageshack.us/my.php?image=samplecw4.png
What you would do first, is load this image into memory. Then you go through each pixel and check if it matches your chroma key. If it does, you set your passability array element accordingly.
Once this is done, you copy your terrain to the screen pixel by pixel. If the pixel should be passable, you don't copy it, otherwise you do.
Now you have the destructible terrain (it is destroyed by setting elements on your passability array and hiding pixels on the screen). You only need to write the algorithm that will allow your worms to move, or not.
Keeping the worms from falling through is easy. It harder to make them move. First you need to set a maximum climbable height:
Your borders will only look soft. From the point of view of the program, they will be full of steps.:
.............#....................
...........###....................
##################################

Your worms need a maximum climbable distance to avoid embarassing situations. They will also need to realize if there's a tunnel that's too short for them:
.....####################
.....####################
.....####################
......................... <-- The worm should not be able to walk here.
.....####################

And they should also get on top of layers:
.........................
......................... <-- The worm should step on here.
.....####################
.........................
.....####################
Awesome helios - as always perfect replies mate. Cheers for that. All of it I understand just have to see how I go with this chroma key. Doesn't look to hard. I've just never used it before.

Thanks a bunch,
Myth.
You will also want to check out "double buffering".

Even though you are using C++, it is also worth your time to peruse the PyGame site: http://www.pygame.org/news.html , particularly the tutorials section, because it covers blitting and sprites.

You should be using some form of library for this stuff. Get a book on writing 2D side-scrolling AKA parallax video games. The information on "tiles" is invaluable in building your world environment, collision detection, objects, etc.

Good luck!
Cheers mate, but I've used SDL and XNA to build tile engines and parallax scrolling games. But cheers for that site - I haven't seen it before.
Topic archived. No new replies allowed.