C # GDI + game programming: Role animation, drawing, and moving in the battle of the ghost of crazy bombs V1.0

Source: Internet
Author: User

/*_______________________________________________________________

 

The principle of an animation is to draw/play images continuously. When you draw/play different expressions and a certain number of motion pictures per second, it is a visual illusion and a smooth animation effect will be displayed! With animation, an object has a soul and a life.

Game object analysis:

In the game, only a part of the objects are active, including the main character and ghost. The following describes the structure of the game:

 

We can see that the game is mainly divided into two parts: static objects and activity objects. Everyone inherits a base class. The base class has an abstract method, that is, drawing, all objects must overwrite the rendering method to render their own images.

Baseentity

Public abstract class baseentity <br/>{< br/> /// <summary> <br/> // object coordinate <br/> /// </Summary> <br/> Public point poi; </P> <p> /// <summary> <br/> // draw an object <br/> /// </Summary> <br/> public abstract void draw (Graphics G ); <br/>}

The code for creating a bomb in a game is as follows:

Bomb

/// <Summary> <br/> /// bomb <br/> /// </Summary> <br/> public class bomb: baseentity <br/>{< br/> // explosion delay time <br/> Public int explodedelay = 50; <br/> bitmap [] bmp s = new bitmap [utility. bombframes]; </P> <p> /// <summary> <br/> // data initialized from the resource by default <br/> /// </Summary> <br/> public bomb () <br/>{< br/> bmp s = initresources. instance. initstaticobjects (<br/> utility. bombvalue, <br/> utility. bombframes, <br/> New size (utility. bombsizewidth, utility. bombsizeheight), <br/> initmode. fromresource); <br/>}</P> <p> private int I = 0; <br/> Public override void draw (Graphics g) <br/> {<br/> I = I + 1 <utility. bombframes? I + 1: 0; <br/> G. drawimage (BMP s [I], poi. x, poi. y, utility. gridsize, utility. gridsize); <br/>}< br/>}

First, initialize the bomb material, and then rewrite the painting method to draw it frame by frame. This will show a simple animation effect!

Unlike static objects, the moving objects naturally have their own motion methods, such as moving and jumping. This is also very simple, just changing the X and Y coordinates of objects. Both the protagonist and the monster in the game are active entities with the same basic attributes. Therefore,ActivityentityClass to better reuse code.

Activityentity

/// <Summary> <br/> // direction enumeration <br/> /// </Summary> <br/> Public Enum direction <br/> {<br /> left, // left <br/> up, // up <br/> right, // right <br/> down, // downward <br/> static // remain unchanged <br/>}</P> <p> /// <summary> <br/> // The object is alive. object <br/> /// </Summary> <br/> public class activityentity: baseentity <br/>{< br/> // role (including the main character and enemy) <br/> Public String role; <br/> // life <br/> Public int life; <br/> // speed <br/> Public int Speed = 1; <br/> // direction <br/> Public direction; <br/> // record frame <br/> private int frame = 0; </P> <p> // stores the role walking diagram. <br/> protected bitmap [] [] bmp s = new bitmap [utility. roledirections] [] <br/>{< br/> New bitmap [utility. rolesteps], // <br/> New bitmap [utility. rolesteps], // left <br/> New bitmap [utility. rolesteps], // right <br/> New bitmap [utility. rolesteps] // <br/>}; </P> <p> // default Resource Access <br/> Public activitye Ntity (string role) <br/>{< br/> If (! String. isnullorempty (role) <br/>{< br/> This. role = role; <br/> initactivityentity (role, initmode. fromresource); <br/>}</P> <p> Public activityentity (string role, initmode Mode) <br/>{< br/> If (! String. isnullorempty (role) <br/>{< br/> This. role = role; <br/> initactivityentity (role, mode ); <br/>}</P> <p> // <summary> <br/> // initialize the game role <br/> /// </Summary> <br/> /// <Param name = "role"> role name </param> <br/> /// <Param name = "Mode"> </param> <br/> private void initactivityentity (string role, initmode mode) <br/>{< br/> switch (mode) <br/>{< br/> case initmode. fromresource: <br/> BMP = Initresources. instance. initactivityobjects (role, initmode. fromresource); <br/> break; <br/> case initmode. fromfilepath: <br/> If (system. io. file. exists (role) <br/> bmp s = initresources. instance. initactivityobjects (role, initmode. fromfilepath); <br/> break; <br/>}</P> <p> // <summary> <br/> // move a role <br/> // </Summary> <br/> Public Virtual void move () <br/>{< br/> switch (Direction) <B R/>{< br/> case direction. left: <br/> poi. x-= speed; <br/> break; <br/> case direction. up: <br/> poi. y-= speed; <br/> break; <br/> case direction. right: <br/> poi. X + = speed; <br/> break; <br/> case direction. down: <br/> poi. Y + = speed; <br/> break; <br/> case direction. static: <br/> break; <br/>}</P> <p> // <summary> <br/> // record frames <br/> // </Summary> <br/> Public void recordframe () <BR/>{< br/> switch (Direction) <br/>{< br/> case direction. left: <br/> frame = 1; <br/> break; <br/> case direction. up: <br/> frame = 3; <br/> break; <br/> case direction. right: <br/> frame = 2; <br/> break; <br/> case direction. down: <br/> frame = 0; <br/> break; <br/> case direction. static: <br/> break; <br/>}</P> <p> // <summary> <br/> // rewrite the method of drawing objects <br/> // /</Summary> <br/> private Int I = 0; <br/> Public override void draw (Graphics g) <br/>{< br/> switch (direction) <br/>{< br/> case direction. down: <br/> case direction. left: <br/> case direction. right: <br/> case direction. up: <br/> I = I + 1 <utility. rolesteps? I + 1: 0; <br/> break; <br/> case direction. static: <br/> I = 0; <br/> break; <br/>}< br/> G. drawimage (BMP s [frame] [I], poi. x, poi. y, utility. gridsize, utility. gridsize); <br/>}< br/>}

We can see that the move () method is to change the X and Y of the object in different directions, so that we can move the main character in a few simple words. Take the main character as an example:

Actor

/// <Summary> <br/> // main character of the game <br/> /// </Summary> <br/> public class actor: activityentity <br/>{< br/> // fortune <br/> Public int money; </P> <p> public actor () <br/>: base (utility. actorvalue) <br/>{< br/>}</P> <p> public actor (string role, initmode mode) <br/>: Base (role, Mode) <br/>{}</P> <p> Public void move (bool passable) <br/>{< br/> If (passable) <br/>{< br/> base. move (); <br/> base. recordframe (); <br/>}< br/> else <br/>{< br/> base. recordframe (); <br/>}< br/>}

Inherit the activity body and reload the move () method. This method is mainly used to move and implement the main character animation if there are no obstacles. Otherwise, only frames are recorded.

(Save the invisible effect to local browsing)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.