0

I'm currently doing my A-Level Computing project for which I am making my own version of the classic game Space Invaders.

To create the wave of space invaders I want to use a 2D array of images, where the images are loaded from a disk and then displayed on the form but I am unsure of how load the images into the array and then display the array on a form.

The current arrays are:

ImagePaths:array [1..3] of string =('SpaceInvader1.jpg', 'SpaceInvader2.jpg', 'SpaceInvader3.jpg'); 

Wave:array[1..11, 1..5] of TImage; x,y:integer; 

What I would like to know is: how would I load an image into an array element? eg how would I load 'SpaceInvader1.jpg' to array element [1,1]?

Any help would be greatly appreciated.

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
user2180030
  • 17
  • 1
  • 5
  • 11 times 5 does not equal 3. What am I missing? – David Heffernan Mar 17 '13 at 19:05
  • This is what I am trying to recreate by using a 2D array of images http://1.bp.blogspot.com/_2TCYQxSUqGQ/TIetTqkVf5I/AAAAAAAAKYw/buxwoNO2UCU/s1600/space%2520invaders.jpg Specifically the wave of space invaders – user2180030 Mar 17 '13 at 19:12
  • Why do you need a 2D array of images? What are the two dimensions? – David Heffernan Mar 17 '13 at 19:19
  • my teacher told me to use a 2D array of images to create the wave of space invaders, eg [1..11,1..5] and each element in the array is an image so when you draw it onto the form it creates the wave of space invaders – user2180030 Mar 17 '13 at 19:22
  • That's not how I would do it! I'd have an array of those dimensions which contained the state of the invader. Then the render code would draw the appropriate representation of that state. – David Heffernan Mar 17 '13 at 19:23
  • so would this idea not work then?: create a 2D array of images with dimensions [1..11, 1..5] then for each element load an image into it and then draw the array on the form. That is the way that I want to do it really because my teacher can help me with it if necessary and that is the way he advised. – user2180030 Mar 17 '13 at 19:28
  • What happens when an invader is killed? Why do you want all those identical copies of the image? Or are you going to create a small library of images and have the array contain references to those images? Anyway, if you want to do it your way, you don't our advice, why don't you just do it your way? And what's more, you didn't actually ask a question. What is your specific question? – David Heffernan Mar 17 '13 at 19:30
  • How can you not have identical copies of the same image in space invaders? I understand that this may not be the best method solve this problem and your way is probably better but I have only been programming for about a year and so I am not that good yet. I just wanna know if this way would work and how you would load the image into the array that is all. – user2180030 Mar 17 '13 at 19:34
  • This problem is similar but not quite the same as what I want to do http://stackoverflow.com/questions/7612740/loading-images-into-timage-via-array – user2180030 Mar 17 '13 at 19:35
  • "How can you not have identical copies of the same image in space invaders?" Well, I could explain how. But you want to do it your way. So, I won't bother. – David Heffernan Mar 17 '13 at 19:38
  • I want to do it my way yes, as it is the way my teacher recommended for me to approach it. I understand that it may not be the best way, and your approach will be better but it is probably too advanced for me. I would like you to explain how I would create the 2D array (what variables I would need etc) and then how to load the images into it – user2180030 Mar 17 '13 at 19:43
  • Do you know how to use arrays? Do you know how to create images? If so, then you already know what you need to know. – David Heffernan Mar 17 '13 at 19:48
  • The thing that I really don't understand is the loading of the images into the array elements – user2180030 Mar 17 '13 at 19:50
  • What don't you understand? Your question is very poor. All the details are in comments. Please improve the question. Did you declare the array yet? What does the declaration look like? – David Heffernan Mar 17 '13 at 19:52
  • ImagePaths:array [1..3] of string =('SpaceInvader1.jpg', 'SpaceInvader2.jpg', 'SpaceInvader3.jpg'); Wave:array[1..11, 1..5] of TImage; x,y:integer; – user2180030 Mar 17 '13 at 20:00
  • Now, how would I load for example 'SpaceInvader1.jpg' into element 1,1 in the array? – user2180030 Mar 17 '13 at 20:02
  • Create the image with TImage.Create. Load it with LoadFromFile. Don't use lossy jpeg. Use PNG or GIF. You really don't need multiple copies, but you are dead set on doing it your way.... – David Heffernan Mar 17 '13 at 20:10
  • Please please please edit the question to include this information. Don't leak it out in comments. – David Heffernan Mar 17 '13 at 20:12
  • Thank you very much. I only want to do it this way because it is not too complicated (it doesn't need to be massively complex, it is only an A-Level project) and if I get stuck doing it, my teacher can advise and help me in lesson. – user2180030 Mar 17 '13 at 20:16
  • Your edit does little to address my concerns. All the good details have come out in comments. My answer relies on those comments. But they should be in the question. Please keep trying. – David Heffernan Mar 17 '13 at 20:19

2 Answers2

2

If you're going to be coding graphical animations, you probably won't want to do it directly on the form. Trying to move things around can be tricky, especially if you want to animate smoothly and not get a lot of flicker and graphical artifacts.

It would be better to use a rendering library. The workflow goes like this:

  • Create a rendering context on the form. This is a control that provides a surface for graphical animations to run on.
  • Load your three images into memory.
  • Create an array of objects to represent the game data associated with your monsters. (Position, movement direction and speed, etc.) It can be flat or 2D, whichever works better for you.
  • Set up a render loop that goes like this:
    • For each monster in the array, draw it at its position.
    • Draw the player's ship and all projectiles.
    • Check for collisions and handle appropriately.
    • Check for player input and handle appropriately.

You can find plenty of information on rendering libraries for Delphi in the forums at Pascal Game Development.

Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477
2

You have the following declarations:

ImagePaths:array [1..3] of string =(
  'SpaceInvader1.jpg', 
  'SpaceInvader2.jpg', 
  'SpaceInvader3.jpg'
); 
Wave: array[1..11, 1..5] of TImage; 

You want to know how to populate the array of images. It is quite wasteful to create 55 images when 3 will suffice. So instead of that, use indirection. Store references to the images. And TImage is a visual component, and so not appropriate for a sprite.

I would hold the images in an array like this:

Sprites: array [1..3] of TBitmap;

And populate it

JPEGImage := TJPEGImage.Create;
try
  for i := 1 to 3 do 
  begin
    JPEGImage.LoadFromFile(ImagePaths[i]);
    Sprites[i] := TBitmap.Create;
    Sprites[i].Assign(JPEGImage);
  end;
finally
  JPEGImage.Free;
end;

Then populate your Wave array like this, for example:

Wave: array[1..11, 1..5] of TBitmap; 
....
for i := 1 to 11 do
  for j := 1 to 5 do
    Wave[i,j] := Sprites[1];// or whichever sprite you want

Of course your sprites may be better with a real name rather than in an array.


Some other comments:

  1. JPEG is a bad format for a game sprite. It is a lossy format. A plain Windows bitmap would be fine, as would a GIF or PNG.
  2. I'd much rather see the images as embedded resources. Then your executable can stand alone.
  3. I'd also far rather see your Waves array holding the state of each invader. And then you would create a function that would render that state onto a canvas.
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490