Duplicating an moving image?

Oh dear... sorry for the horrible grammatical mistake in the title. How dare I make such a mistake :/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
void playReaper(){
	int olddirection = 0, newdirection = 0;
	int reaperDirection;
	int moveLoop;
	int health = 500;
	int activate = 0;
	struct collisionBox charHit;
	struct collisionBox reaperHit;
	initSectors4();
	srand(time(NULL));
	enemyx = rand() % 40 + 700;
	enemyy = rand() % 65 + 700;
	do {
		moveLoop = 20;
		reaperDirection = rand() % 4;
		for(int k = 0; k < moveLoop; k++){
		if(reaperDirection == 0)
			newdirection = 0;
		if(reaperDirection == 1)
			newdirection = 1;
		if(reaperDirection == 2)
			newdirection = 2;
		if(reaperDirection == 3)
			newdirection = 3;
		
						
	if(olddirection != newdirection){
		reaper[newdirection].xCoord = reaper[olddirection].xCoord;
		reaper[newdirection].yCoord = reaper[olddirection].yCoord;
		
		reaper[olddirection].drawable = 0;
		reaper[newdirection].drawable = 1;
		reaper[newdirection].currentFrame = 0;
		if(newdirection == 0 || newdirection == 1 || newdirection == 2 || newdirection == 3){
		olddirection = newdirection;}
	}
	checkReaperSectors();
reaper_animation();
	Sleep(20);
	check_reaper();
		}
	}
	while(health > 0);
	
}


So this is the code that handles my enemy. The reaper_animation() is what decides which picture of the reaper to put on screen, and that is called from a struct containing info on the reaper sprite.
check_reaper() keeps it from leaving the room it is in as well as keeps it from going through the statues I put in.

checkReaperSectors() refreshes only the background right around the reaper to get rid of a trail behind him, but keeps speed up by only refreshing a small portion of screen.

I am wondering if there is a way to duplicate this reaper. For my game I want the levels to be filled with reapers. More and more each level and the level is complete when all reapers are dead and you can then advance.

So far one reaper appears and he flies around the screen randomly, which I want. I want many flying around the screen, and if they run into the character, he gets damaged, so character has to try to shoot them as they fly around.

So, that is the background on my game haha... But yeah, is there something I can do to duplicate this reaper? Or do I just need many many functions for it then in my rungame() call however many functions I want of enemies? That seems easy, but toooooooooons of code lines. I was wondering if I could shorten it down a bit.

Thanks for any help!
Last edited on
Helloooo?
checkReaperSectors() refreshes only the background right around the reaper to get rid of a trail behind him,


This technique is referred to as "dirty rects". It is an extremely outdated method of drawing. It hasn't been in widespread use since like 1998. Whatever lib you're using for graphics is either not designed for what you're doing, or is very, very old.

You should abandon it and get a more modern lib. It will make your life much easier.

I personally like SFML. Here's a guide to getting SFML set up on VS2012:
http://cplusplus.com/forum/beginner/95295/#msg511542

I am wondering if there is a way to duplicate this reaper.


Yes. Just draw the reaper again. Whatever code you're doing to draw it the first time can be repeated to draw it as many times as you need. Just draw it in a different place each time.

Helloooo?


Give us more time to respond. You posted your original question at 11 PM my time, and posted the followup at 8 AM. I was asleep for most of that.
Last edited on
Haha but not everyone was asleep... and it was getting bumped off of first page so I just wanted to bring it to the top.

Anyway, yeah we are in graphics.h :/. If I added the new library will I have to change everything? Because this is something that is due, and due very soon, and probably wouldn't get done if everything had to be reformatted. Unless I can keep this graphics.h stuff and use the other library to just handle certain things.
yeah we are in graphics.h :/


Yes that is ancient. It's so old that most modern compilers don't even supply it. Ew.

this is something that is due, and due very soon,


Okay... if this is for an assignment or something then just keep using what you are. Don't switch over to a new lib.

I'm unfamiliar with graphics.h so I can't give you any exact code... but my previous advice should still apply:

"Whatever code you're doing to draw it the first time can be repeated to draw it as many times as you need. Just draw it in a different place each time."

If you can post the code you're using to draw the reaper, I might be able to show you how to draw it a second time.

If I added the new library will I have to change everything?


Yes. It probably wouldn't be worth it for this project, but keep it in mind for the future.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void reaper_animation()
{
	
	for (int i = 0; i < 4; i++){
		if( ! reaper[i].drawable)
			continue;
	
		//Puts image of reaper on screen based on inputs from struct
	readimagefile(reaper[i].frames[reaper[i].currentFrame],
		reaper[i].xCoord,
		reaper[i].yCoord,
		reaper[i].xCoord + reaper[i].charWidth,
		reaper[i].yCoord + reaper[i].charHeight );
	
	reaper[i].currentFrame++; //increments the "frame" by one, thus only animating one image at a time
	if(reaper[i].currentFrame == reaper[i].animationFrames){
		reaper[i].currentFrame = 0;} //When frame 4 is reached, it resorts back to first frame and restarts
		//character[i].drawable = 0;}
	
	reaper[i].xCoord += reaper[i].xMove;
	reaper[i].yCoord += reaper[i].yMove;					
	}
}


playReaper() that I posted in first post determines a random direction for reaper to go in. Then playReaper() calls reaper_animation(), which is this code above. reaper_animation() readimagefile's the correct reaper image based on what direction it is going in. So it is this reaper_animation that "draws" the reaper, based on input from playReaper. playReaper is what is called from my playGame() function.

And yeah.... I am hoping to start messing around with XNA this summer. That seems to be a pretty solid game making system :)
bump
I'm a little confused. That code already looks like it's drawing 4 reapers. So just change that 4 to be however many reapers you have and it should draw as many as you need.
Well what it is supposed to be doing, is that in the code in my first post, reaper is given a random 'newdirection' value. That value is between 0 and 3. When the first code reaches reaper_animation(), the 'i' value becomes the same as the 'newdirection' value. So say newdirection = 3, then i = 3.

So the for loop in reaper_animation() will not execute any of the code until i = 3 due to the continue command.

Once i = 3, it draws the proper image of the reaper using readimagefile(), then there are 4 frames for a whole animation loop, so currentFrame gets incremented each movement to properly animate it.

At least that is what should be going on. I might have made a mistake somehwere because right now it is actually producing multiple reapers even though it is only supposed to be one... But even still, only the "true" reaper moves. The others just sit at the wall lol...
So it sounds to me like you understand how to draw multiple things, then. You're just asking about the organizational aspect of it. Is that right?

This is where OOP is nice. Make a Reaper class and give it member functions to update its animations, draw it, do its AI logic, etc:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Reaper
{
public:
  void draw( drawingstuff& drawstuff )  // where 'drawingstuff' is whatever stuff you need from graphics.h to draw
  {
     // .. code to draw the reaper
  }

  void update()
  {
    // .. code to walk through the animation and do AI logic and stuff
  }

private:
  // .. data members to keep track of this Reaper's state.  Stuff like
  //   position, any drawing info needed, animation state, etc, etc.
};


The goal is to make the class fully "self-contained" so it does everything that a single reaper needs to do.

Once you have that... all you have to do to make more Reapers is make more of them. Like an array or something:

 
Reaper myReapers[100];  // now you have 100 reapers.  It's that easy. 


From there... just loop through and update/draw all of them:

1
2
3
4
5
6
7
8
// when updating your game logic:
for(int i = 0; i < 100; ++i)
  myReapers[i].update();


// ... when drawing
for(int i = 0; i < 100; ++i)
  myReapers[i].draw( ... );
In Lines 17 - 24 of your original post why not replace that mess with reaperDirection = newdirection;

The if statement at Line 34 in your original post is redundant, your testing to make sure that 'newdirection' has a value of 0 - 3 but those are the only possible values it could be at that point in your code.

When the first code reaches reaper_animation(), the 'i' value becomes the same as the 'newdirection' value.

Where is this happening? newdirection is a local variable within playReaper and it doesn't get passed to reaper_animation. Also the only 'i' value I see exists soley in the scope of that for loop within reaper_animation.

So the for loop in reaper_animation() will not execute any of the code until i = 3 due to the continue command.

That's not at all what your wrote. That continue command only ensures that the iteration of that loop is skipped if the drawable variable for that instance of reaper is not true.

I think if you made a proper Reaper struct or class like Disch suggests that most of these issues will become easier for you to see and solve.
Last edited on
"where drawingstuff is whatever you need from graphics.h to draw the reaper"

Does this mean just my images? "PIX\\Reaper\\reaper_left_l.gif" etc...?

and update() would be basically what I have in my reaper_animation() code?

and private() would be current frame it is on, and its starting position?

I have never done a class before... All I have learned is some C.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
struct animate {
	int drawable;
	int animationFrames;
	char *frames[TOTAL_FRAMES+1];
	int charWidth;
	int charHeight;
	int xCoord;
	int yCoord;
	int xMove;
	int yMove;
	int currentFrame;
};

struct animate reaper[] =
{
	{
		1,
		3,
		{
			"PIX\\Reaper\\reaper_right_r.gif",
			"PIX\\Reaper\\reaper_right_still.gif",
			"PIX\\Reaper\\reaper_right_l.gif",
			"PIX\\Reaper\\reaper_right_still.gif",
		},
		50,
		50,
		enemyx,
		enemyy,
		10,
		0,
		0,
	},
	{
		0,
		3,
		{
			"PIX\\Reaper\\reaper_left_r.gif",
			"PIX\\Reaper\\reaper_left_still.gif",
			"PIX\\Reaper\\reaper_left_l.gif",
			"PIX\\Reaper\\reaper_left_still.gif",
		},
		50,
		50,
		enemyx,
		enemyy,
		-10,
		0,
		0,
	},
	{
		0,
		3,
		{
			"PIX\\Reaper\\reaper_up_r.gif",
			"PIX\\Reaper\\reaper_up_still.gif",
			"PIX\\Reaper\\reaper_up_l.gif",
			"PIX\\Reaper\\reaper_up_still.gif",
		},
		50,
		50,
		enemyx,
		enemyy,
		0,
		-10,
		0,
	},
	{
		0,
		3,
		{
			"PIX\\Reaper\\reaper_down_r.gif",
			"PIX\\Reaper\\reaper_down_still.gif",
			"PIX\\Reaper\\reaper_down_l.gif",
			"PIX\\Reaper\\reaper_down_still.gif",
		},
		50,
		50,
		enemyx,
		enemyy,
		0,
		10,
		0,
	},
};


I currently am using this struct to call info for reaper to animate and move him.
Drawable variable activates one of the structs... depending on what 'i' is in reaper_animation().
Last edited on
So, based on the code I have provided... this is all the code I have that controls the reaper (my enemy). I don't really get classes, and I don't quite know how to go about creating a struct of my reaper struct to create many reapers.

Like if I made a struct of 100 reaper[]'s, then called a certain amount. To randomly place them, and have them all go about their separate business.

Disch gave a skeleton for a reaper class, but I have never done anything with classes before...

Would anyone be willing to help guide me in constructing a Reaper class? Or creating an array of animated reapers?

HOWEVER! I do have a concern... If i change reapers to a class instead of struct, will character, and boss have to be converted? Or can they stay as structs? (they are very similarly set up like reaper). Because if changing one to a class means changing ALL of them, it would probably be easier at this point to just work with the structs.
The only difference between a class and a struct is that in a class member data and functions are private by default. Otherwise the terms are interchangeable. I would need some comments in that code you posted, if that's how you're creating the objects then we have a lot of work to do.
The only difference between a class and a struct is that in a class member data and functions are private by default. Otherwise the terms are interchangeable.


While that's technically true about the implementation, conceptually the two are very different.
closed account (N36fSL3A)
I think you should just use a newer graphics API. This is shameful.
Topic archived. No new replies allowed.