2d game programming-Implementation of Particle System (Snow)

Source: Internet
Author: User

After reading the article, I also started my own work. Hey hey, I didn't expect it to be done. Unfortunately, it's not very good, but it's still worth reading ......

Snow, we may be very familiar with our friends in the north. Friends in the South must have watched TV and so on. Hey hey, it's pretty big when it's snowing, I haven't carefully looked at the snow yet, =. =, Please do not laugh! Now, let's get started ......

(-) Feature description

(1) snowflake has different shapes or sizes.
(2) Snow does not fall in a straight line, and it may float everywhere due to air resistance.
(3) snow falls at different speeds due to different air resistance
(4) The farther away from us, the smaller the snow feels due to its visual relationship.
(5) Sometimes the snow is big or sometimes small
(6) Sometimes the snow is windy.
To make the snowflake have the above features, it will be a real point. Sometimes it needs to be changed based on different map factors and other factors (due to random strain of ^_^)

(2) handwritten code

First write the header file cgame_participant lesnow.h

# Ifndef _ cgame_participant lesnow_h
# DEFINE _ cgame_participant snow_h

# Pragma message ("this file have load bitmap! ")

# I nclude <stdlib. h>
# I nclude <math. h>
# I nclude <time. h>
# I nclude "cgame_engine.h"

Typedef struct {
Int X, Y; // particle Coordinate
Int Z; // snow, Particle Level

Int VX, Vy; // speed of the particle on the X and Y axes
Rect; // particle image Lattice
Bool bdeath; // has the particle died?
} Particle _ snow; // particle body

Typedef struct maid {
Particle _ snow * snow; // snow in one layer
Int snow_num; // change the number of levels of snow

Participant _snow_node * next;
} Particle _snow_node; // particle node (in fact, the linked list is not used here, for example, the array can solve the problem)

Class cgame_participant lesnow
{
PRIVATE:
Particle _snow_node * first_node; // the header node.
Participant _snow_node * cur_node; // the current node.
Participant _snow_node * arragement_node [5]; // used to save nodes at each level for faster speed
Lpdirectdrawsu *** ce7 lpddspic;

Bool bshowsnow; // snow?
Int s_width; // The default width of the snowy area is 800x600.
Int s_height; // The height of the snowy area.
DWORD beg_time; // used for latency Calculation
DWORD cur_time;
Char wind_dir; // The current snow position
Public:
Cgame_participant snow (void); // construct
~ Cgame_participant snow (void); // Structure

Bool Init (void); // Initialization
Bool draw (lpdirectdrawsu *** ce7 lpddstemp); // draw snowflake
Bool restore (void); // restore
Void release (void); // release

Void enshow (bool val = true); // visible?
Void setsize (Int & width, Int & height); // sets the screen size.

PRIVATE:
Void createnode (void); // create a node
Participant _snow createparticle (void); // create a snowflake
Void getpoint (maid & TMP); // obtain the next coordinate of the snowflake
Void getspeed (Int & VX, Int & Vy, Int & Z); // snowflake speed
Void getarrangement (Int & AR); // snowflake level
Int random (INT min, int max); // Random Function
};

# Endif

Haha, the particle system really makes full use of the random function. I thought it was useless to use rand (). I wrote this about the random function:

Int random (INT min, int max)

{

Int back =-1;

While (Back = rand () * max/max_rand) <min );

Return back;

} We can get a random number between Min-(max-1), better we can execute srand (unsigned INT) Time (null) before executing this function; pass each random number is not the same ......

The following is the source file:

# I nclude "cgame_participant lesnow.h"

# DEFINE _ max_snow_particle 100 // maximum number of snowflakes per Layer
# DEFINE _ max_h_restistance 10 // horizontal resistance
# DEFINE _ max_v_restistance 10 // vertical resistance
# DEFINE _ max_arrangement 5 // number of layers
# DEFINE _ max_speed 100 // maximum speed
# DEFINE _ delay_time 40 // latency
# DEFINE _ max_snow_pic 4 // Number of snowflake types, depending on the source Image File

Cgame_participant snow: cgame_participant snow (void)
{
Lpddspic = NULL;
First_node = NULL;
Cur_node = NULL;
}

Cgame_participant snow ::~ Cgame_participant snow (void)
{
Release ();
}

Bool cgame_participant lesnow: Init (void)
{
S_width = 800;
S_height = 600;

If (! Createsu *** Ce (& lpddspic, 5, 20 )){
Return-1;
}

If (! Loadbitmap (lpddspic, "pic // gsnowpic.bmp", true, 0 )){
Return-2;
}

Srand (unsigned) Time (null ));

Createnode ();

Bshowsnow = true;
Beg_time = 0;
Cur_time = 0;

Return true;
}

Bool cgame_participant lesnow: Draw (lpdirectdrawsu *** ce7 lpddstemp)
{
If (! Bshowsnow ){
Return true;
}

Participant _snow_node * tmp_node = first_node;
Particle _ Snow;
Wind_dir = random (0, 3 );
Int death_snow = 0;
While (tmp_node! = NULL ){
For (INT I = 0; I <tmp_node-> snow_num; I ++ ){
If (tmp_node-> snow. bdeath ){
Death_snow ++;
}
}

Tmp_node = tmp_node-> next;
}

Bool Z [4] = {false, false };
For (INT I = 0; I <death_snow; I ++ ){
Bool isfind = false;
Snow = createparticle ();

If (! Z [Snow. Z]) {
For (Int J = 0; j <arragement_node [Snow. Z]-> snow_num; j ++ ){
If (arragement_node [Snow. Z]-> snow [J]. bdeath ){
Arragement_node [Snow. Z]-> snow [J] = snow;

Isfind = true;

Break;
}
}

If (! Isfind ){
Z [Snow. Z] = true;
}
}
}

Bool isrest = false;
Cur_time = timegettime ();
If (cur_time-beg_time)> = _ delay_time ){
Isrest = true;
Beg_time = cur_time;
}

Tmp_node = first_node;
While (tmp_node! = NULL ){
For (INT I = 0; I <tmp_node-> snow_num; I ++ ){
If (isrest ){
Getpoint (tmp_node-> snow );

If (tmp_node-> snow. x> = (s_width) | (tmp_node-> snow. x <0) | (tmp_node-> snow. y> = (s_height) | (tmp_node-> snow. Y <0 )){
Tmp_node-> snow. bdeath = true;
}
}


If (! Tmp_node-> snow. bdeath ){
If (! BLT (lpddstemp, tmp_node-> snow. X, tmp_node-> snow. Y, lpddspic, tmp_node-> snow. rect, true )){
Return-1;
}
}
}

Tmp_node = tmp_node-> next;
}

Return true;
}

Bool cgame_participant lesnow: Restore (void)
{
Return true;
}

Void cgame_participant lesnow: release (void)
{
Releasesu *** Ce (lpddspic );
If (first_node! = NULL ){
Participant _snow_node * TMP = first_node-> next;

While (TMP! = NULL ){
TMP = first_node-> next;
Delete [] first_node-> snow;
Delete first_node;
First_node = TMP;
}

First_node = NULL;
}
}

Void cgame_participant lesnow: createnode (void)
{
First_node = new maid;
Cur_node = first_node;
First_node-> snow_num = random (2, _ max_snow_particle );
First_node-> snow = new maid [first_node-> snow_num];
For (INT I = 0; I <first_node-> snow_num; I ++ ){
First_node-> snow = createparticle ();
}
Arragement_node [0] = first_node;

For (I = 0; I <4; I ++ ){
Participant _snow_node * TMP = new participant _snow_node;
TMP-> next = NULL;
TMP-> snow_num = random (2, _ max_snow_particle );
TMP-> snow = new maid [TMP-> snow_num];
For (Int J = 0; j <TMP-> snow_num; j ++ ){
TMP-> snow [J] = createparticle ();
}

Arragement_node = TMP;
Cur_node-> next = TMP;
Cur_node = TMP;
}
}

MAID: createparticle (void)
{
Particle _ Snow back;

Getarrangement (back. z );
Getspeed (back. VX, back. Vy, back. z );
Int I = random (0, _ max_snow_pic );
If (I> = 4 ){
I = 3;
}
Back. rect. Left = 0;
Back. rect. Right = 5;
Back. rect. Top = I * 5;
Back. rect. Bottom = (I + 1) * 5;

Back. x = random (0, s_width );
Back. Y = random (0, 10 );
Back. bdeath = false;

Return back;
}

Void cgame_participant lesnow: getpoint (participant _snow & TMP)
{
TMP. x + = TMP. VX;
TMP. Y + = TMP. Vy;
}

Void cgame_participant lesnow: getarrangement (Int & AR)
{
AR = random (0, _ max_arrangement );
If (Ar> = 5 ){
AR = 4;
}
}

Void cgame_participates snow: getspeed (Int & VX, Int & Vy, Int & Z)
{
Int power = random (1, _ max_h_restistance );
If (wind_dir ){
VX = _ max_speed/power/(Z + 1 );
} Else if (wind_dir = 0 ){
VX =-(_ max_speed/power/(Z + 1 ));
} Else if (wind_dir = 3 ){
VX = 0;
}

Power = random (1, _ max_v_restistance );
Vy = _ max_speed/power/(Z + 1 );
}

Void cgame_participant lesnow: setsize (Int & width, Int & height)
{
S_width = width;
S_height = height;
}

Void cgame_participant lesnow: enshow (bool Val)
{
Bshowsnow = val;
}

Int cgame_participates snow: Random (INT min, int max)
{
Int back;

While (Back = (RAND () * max/rand_max) <min );

Return back;
}

Let's do it ourselves, make our own particle system, and describe the wonderful nature for the sake of the game. ^_^

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.