So I am in a C++ class and I need help with my game development. I have been working on this problem for a while and I have decided to ask if anyone out there knew how to make a sprite move on it's own using classes. I will attach the source code for my project and my entire goal is to make the "orc" move on it's own without the need for a key to be pressed. I have been working on this problem and have found no solution since everyone is moving sprites without using classes. I can move one sprite with a a class and it works fine, I just can't seem to make one sprite move on it's own. I have created an update class and I am not sure what to do next. I usually like to figure out problems such as this on my own, but I can't seem to so I would be very happy if someone could help me out. The class I am working with is the enemy class. I included everything so whoever knows what needs to be done can help me out in a easier way. I thank the person or people who are willing to help me in advance.

Doing it with a class is the same as doing it without a class. There are really two goals here: automatic movement of a sprite and object-oriented programming.

The first is pretty simple, but requires you to think in steps. In order for it to look like something is moving you need to incrementally change its position, orientation, or shape. Each iteration of the program loop you need to move the sprite a little bit in the direction that it is travelling. Exactly how you do this is up to you. You can hard-code it, you can develop something dynamic that figures it out at run-time, or you can develop something dynamic that uses pseudo-random numbers to "invent" the movement at run-time.

The second is pretty simple too, but it takes years to figure out how to do properly. In my opinion, it is best to ignore static members of classes entirely. They are global and sort of defeat the purpose of using a class. There are rare occasions where it is useful, but almost never necessary. Instead, think of your class as the blueprint for an object. An object is a thing. A noun. It can have attributes/properties/data and behaviors/functions.

Start by imagining what objects might be involved in automating the movement of a sprite. For starters, there is a "sprite". Whether that means a bitmap, a game object, or a character is up to you. You may want to create a class for that so that you can instantiate sprite objects. You may want a separate object to figure out the actual movement. Perhaps an object to represent points in the "world", like the current position of the sprite, where it is ultimately trying to go, and the different points along the route that it will need to go to get there. You may want to include algorithms for path finding, for example, if you want a dynamic solution. There are many possibilities for objects.

One of the best strategies that I can give you for writing OO is to use dependency injection. Pass all of your data in and out of an object. Don't just let an object magically have values. Make sure those are passed in through a constructor. When you want to do something, make sure that you're passing that data in, and returning the result. In other words, avoid void functions with void argument lists.

Also try to visualize how you would like to use the object. In the context of simulating the movement of a sprite, you are going to need to be able to move one step at a time. You might do something like this:

OO is entirely subjective. There is no defined right way. You can do it however you want. It is a tool that you can use to organize your program. It doesn't change the underlying program at all. It doesn't magically make it better or easier.

This is a fairly simple example. Depending on how big the game is, it might be worth finding path finding algorithms and using those to figure out the route dynamically. And you can use pseudo-random number generators to make up starting and ending points at run-time to simulate being "alive" and having original ideas. These things are not trivial though and will require quite a lot of effort to accomplish.

Try to focus on a tiny piece of every problem at a time. Nobody can fit an entire, non-trivial program in their head at once. You need to break the problem down into smaller pieces and focus on one piece at a time.

What you're looking for is an AI, artificial intelligence, basically something that guides the enemies action. It could be as simple as rolling a random number with rand()%N will get you 0 - N-1 and then you base the direction of movement off of that. Or you can make your enemy always face towards or away from your player using atan2(ydiff , xdiff) and then use sin and cos to get you the relative direction of movement in each dimension.

Thank you all for helping me, but I still am having a hard time figuring all this out especially I am semi-new to C++ and Allegro 5. I understand what you are saying that I need to do, but I am still not sure at how I would put it into my code. I know I need to set direction X and Y or (DX and DY), but I am still not sure how to add them together in the update method. I know this may sound stupid and it may sound like I have no experience, but I am really not very experienced and know very little. Most of what I accomplished was through the guidance of my teacher. Yet I still cannot solve this problem, if there is anything anyone can guide me to or make it easier for me to understand than that would be very very appreciated since I prefer to write my own game code and not have someone else do it for me.

Use an update method that is called every time you receive a timer event and have it set the new dx and dy and then check for collision and move the character if there was no collision otherwise handle it.