Animation In Dev C%2b%2b

Posted on
Animation In Dev C%2b%2b 7,9/10 3998 votes

On my last post (How To Draw 2D Primitive Objects on Windows Forms Application using C++ .NET), we’ve learned about how to draw some 2D objects on Form and Panel. That’s not difficult right? But now, in this post, we’ll learn about how to move that objects. Of course it’ll be very boring if that objects didn’t move. So, let’s get started! Don’t forget that I’m using Microsoft Visual Studio 2008 Professional Edition in this tutorial.

Step 1

Make a new Visual C++ CLR Windows Forms Application Project. Click FileNewProject…. Choose Visual C++CLRWindows Forms Application and name your Solution Simple2DAnimationUsingTimer.

Step 2

Then, make a Timer Object inside your Form. Go to the property, change the Interval to 50 (It means that it’ll be active every 50 milliseconds) and change Enabled to true.

Step 3

Now, let’s go to the code! first, make a Point object that represents the position (Top Left) of the 2D object. In this case, let’s try to make a rectangle. Let’s call it rectanglePos. Then, we make rectangleSpeed to represents the speed of that rectangle when it’s moving.

Step 4

  1. A2A Not an expert but, I did a few such things during my college days. It was really fun. I made use of two things: 1. ASCII Escape codes We can do amazing things using them.
  2. C program for bouncing ball graphics animation In this program, we first draw a red color ball on screen having center at (x, y) and then erases it using cleardevice function. We again draw this ball at center (x, y + 5), or (x, y - 5) depending upon whether ball is moving down or up. This will look like a bouncing ball.

Next step, let’s initialize this rectanglePos with (0, 0) value and rectangleSpeed with (4, 4) value inside Form1(void) constructor.

To do that we need to: Create the concept of a facing direction. Track which direction the character is currently facing and flip the sprites if necessary. /games-for-usb-loader-gx.html. Add a new walking animation. Have some way of switching between idle and walking depending on the characters movement speed.

Step 5

Next, let’s make the code inside Form1Paint event to draw the rectangle based on rectanglePos onto your Form window.

Copy the following code inside Form1_Paint().

Step 6

Now, let’s put some code inside timer1_Tick() to animate (or, actually, it’s only to move) the rectangle. The speed of rectangle move is based on rectangleSpeed. And of course, I’ve put a simple collision detection between rectangle and the wall of Form.

Step 7

Animation In Dev C 2b 2b Programming

Finally, try to run your program, and it should be like this:

Okay, I think that’s all I got to tell. If you want to make more rectangles or you want to add some circles, just add some Pos and Speed. You can make it by using array or List (try googling if you don’t get it). Okay, hope this simple tutorial gives you some inspirations. See you on my next Post.

DevC%2b%2b

Downloads

Animation In Dev C 2b 2b Tutorial

Cover Photo by Icons8 team on Unsplash.