The experience of airplane game in the basic statement of C language program can complete function

Source: Internet
Author: User

On the realization of the function of airplane game in the basic sentence of C language program


A Title: Shooting Airplane Games

Two Objective: Through the C language to write a shooting class of flying games, you can play through the keyboard, the operation method is "a" "D" "w" or "←" "↑" → "To control, hit the enemy can earn points, the enemy was killed once, each game has 3 chances of survival, After the game is over, you can choose whether to start the game again.

Three Improvement: This game is my upgrade of a small game, add a color function, termination function, select Class function, so that the game can be terminated, no longer the score, the number of deaths has been increased but not the end of the situation, adding the color function, making the game look more comfortable; The game after the death three times automatically end the game, and can choose whether to restart the game, in addition to add a set of functions, so that the game can be set, change the size of the game, adjust the speed of the aircraft and so on, is the game more humane.


Four Content: Write the C language code, run the program, and debug the program, the final realization of the function of the game. This program mainly includes game control function, set whether to continue game function, output function, bullet move function, enemy movement function, set function, Menu function, etc. 7 main functions, including different functions, the original program made a great improvement, the main statement used to have Getche statements, for statements, While statement, printf statement, switch statement, and so on, added a color function, implemented a lot of functions. The main information of the producer can be displayed on the page, etc.


Five. Source code:

#include .h>

#include .h>

#include .h>

#include .h>

#define N 35

void print (int [][n]);//Output function

void Movebul (int [][n]);//bullet Move function

void Movepla (int [][n]);//enemy Movement function

void setting (void);//Set function

void menu (void);//Menu function

int scr[22][n]={0},pl=9,width=24,speed=3,density=30,score=0,death=0;//Global Variables: interface, my machine initial position, interface width, enemy speed, enemy plane density, score, death

Main (void)

{

menu ();

int i=0,j=0;

Scr[21][pl]=1;

scr[0][5]=3;

while (1)

{

if (Kbhit ())

Switch (Getch ())//control move around and enter menu

{

Case ' A ': Case ' a ':

if (pl>0)

Scr[21][pl]=0,scr[21][--pl]=1;

Break

Case ' d ': Case ' d ':

if (PL) <>

Scr[21][pl]=0,scr[21][++pl]=1;

Break

Case ' W ': Case ' W ':

scr[20][pl]=2;

Break

Case 27:

Setting ();

Break

}

if (++j%density==0)//control the speed of production of enemy aircraft

{

J=0;srand (Time (NULL));

Scr[0][rand ()%width]=3;

}

if (++i%speed==0)//control enemy movement speed, relative to the speed of the bullet movement

Movepla (SCR);

Movebul (SCR);

Print (SCR);

if (i==30000)

i=0;//lest I cross the border

}

}

void print (int a[][n])

{

System ("CLS");

int i,j;

for (i=0;i<22;i++) < p= "" >

{

a[i][width-1]=4;

for (j=0;j;j++) <>

{

if (a[i][j]==0)

printf ("");

if (a[i][j]==1)

printf ("\\5");//output the symbol of my machine

if (a[i][j]==2)

printf ("."); /Bullets

if (a[i][j]==3)

printf ("\\3"); Output enemy Symbol

if (a[i][j]==4)

printf ("|");

if (i==0&&j==width-1)

printf ("Score:%d", score);//The top right corner shows the score

if (i==1&&j==width-1)

printf ("Death:%d", death);

if (i==2&&j==width-1)

printf ("Settings: Esc");

if (i==3&&j==width-1)

printf ("Copyright:berlin");

}

printf ("\\n");

}

}

void Movebul (int a[][n])

{

int i,j;

for (i=0;i<22;i++) < p= "" >

for (j=0;j;j++) <>

{

if (i==0&&a[i][j]==2)

a[i][j]=0;

if (a[i][j]==2)

{

if (a[i-1][j]==3)

score+=10,printf ("\\7");

a[i][j]=0,a[i-1][j]=2;

}

}

}

void Movepla (int a[][n])

{

int i,j;

for (i=21;i>=0;i--)//from the last line up is to avoid the enemy aircraft directly rushed to the group.

for (j=0;j;j++) <>

{

if (i==21&&a[i][j]==3)

a[i][j]=0;//the bottom row is assigned a value of 0 to avoid crossing.

if (a[i][j]==3)

a[i][j]=0,a[i+1][j]=3;

}

if (a[20][pl]==3&&a[21][pl]==1)

death++;

}

void setting (void)

{

int sw=0,i,j;

System ("CLS");

do{sw=0;printf ("Size of \\n Game interface: 1.2. >>");

Switch (Getche ())

{

Case ' 1 ':

width=34;

Break

Case ' 2 ':

width=24;

Break

Default

printf ("\\n Error, please re-select ... \\n");

Sw=1;

}

}

while (SW);

Do

{

sw=0;

printf ("\\n Please select enemy aircraft density: 1.2. Medium 3. Small >>");

Switch (Getche ())

{

Case ' 0 ':

density=10;

Break

Case ' 1 ':

density=20;

Break

Case ' 2 ':

density=30;

Break

Case ' 3 ':

density=40;

Break

Default

printf ("\\n Error, please re-select ... \\n");

Sw=1;

}

}while (SW);

Do

{

sw=0;

printf ("\\n Aircraft flying speed: 1. Fast 2. Medium 3. Slow >>");

Switch (Getche ())

{

Case ' 1 ':

speed=2;

Break

Case ' 2 ':

speed=3;

Break

Case ' 3 ':

speed=4;

Break

Default

printf ("\\n Error, please re-select ... \\n");

Sw=1;

}

}while (SW);

for (i=0;i<22;i++) < p= "" >

for (j=0;j<45;j++) < p= "" >

scr[i][j]=0;

Scr[21][pl=9]=1;

printf ("\\n Press any key to save ...");

Getch ();

}

void menu (void)

{

printf ("Description: Press a D to control my machine left and right flight, W fired bullets \\n Settings: Please press esc\\n start game: any key \\n");

if (Getch () ==27)

Setting ();

}


Six. Summary: Through this experiment, I have a deeper understanding of C language, Getche statements, for statements, while statements, printf statements, switch statements, color functions and so on the understanding and use of more detailed, understand the usage of these statements, Clear the use of C language environment, function. As a basic programming language, C language is an indispensable language in future programming, and the usage rate will definitely increase. Mastering the C language is equivalent to mastering a technology, while the C language is relatively simple and easier to understand. Although I have a part of the game of the transformation, but I also feel that this is only a very small part of it, there will certainly be more powerful features waiting for us to improve. At the same time, I also found myself a lot of shortcomings, need to further strengthen this aspect of learning, understand their own in which aspect of the lack, hope can be more thorough in the future study to make up for their shortcomings, improve the situation, but also hope that they can easily write some small game code, I believe that I can also in the later learning process gradually mastered it, thoroughly mastered it, so that it become a sword in their hands.


The experience of airplane game in the basic statement of C language program can complete function

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.