Airplane game Software: The initial experience of C language application

Source: Internet
Author: User

C Language course with a class, the teacher provides a C language plane game let us feel, last semester C language course, mainly is the practice of various sentences, this time is to use the knowledge to feel a real system.

Install C-free First, then paste the code in to run,


650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7F/E9/wKioL1cxiMjAMVQ0AAAc43jQkcw639.png "title=" P-2.png "alt=" Wkiol1cximjamvq0aaac43jqkcw639.png "/>

Although the interface is a bit simple, but the real game, C language can do so interesting things, really did not think.

Analysis of the program, the feeling is not too complicated, is the last semester to learn the combination of simple statements, but the use of very good. First look at the use of several statements:

1, the first is on the screen display of the statement printf, a little forgotten, Baidu a bit,

Http://baike.baidu.com/link?url=o519-jfU1dAGZofRVZFwEaF4qrbznSFPmLTlsZn6hYaQRGTA3vruOKe6DbiQY4rjs0lTiS5EA_m5QOR6AMyjN


2, another is the multi-conditional judgment switch--case

Application method

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<width-2)

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

Break

Case ' W ': Case ' W ':

scr[20][pl]=2;

Break

Case 27:

Setting ();

Break

}

To control the left and right movement and access to the menu,

3. Functions of aircraft and bullets

Use the A and D controls to move around and use W to control the firing of bullets. Enemy aircraft constantly flying from the opposite, bullets hit enemy aircraft score, my machine was the enemy plane collision my machine died.

4. Use of two-dimensional array scr[][]

The core of the game is to keep drawing the interface on the screen, showing the enemy aircraft, our planes, bullets, how to achieve it?

A two-dimensional array is defined in the program, corresponding to the game interface. Read the keyboard key to manipulate the data in the array, output the entire array, refresh, repeat.

A bit like an X/y coordinate, with different values to control the printf display different characters, the following procedure is very well understood, is to use a if multi-conditional judgment,

for (j=0;j<width;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: Wang Yu");

}

printf ("\ n");

If it is 0, show blank, 1 show my plane, 2 show bullets, 3 show enemy aircraft, others are displayed.

Why not switch-case it, as if the last 3 conditions are not unique, inconvenient to use case.

5, to achieve the movement of enemy aircraft

The most attractive place to feel the program is to do the movement of the enemy aircraft, and the movement of my plane, as well as the movement of bullets, and hit the plane, the plane disappears, feel very magical, analysis code, suddenly, the original so realized, really simple statement can also achieve complex functions,

Enemy movement: Starting from the first element of the array line, if it is the number of enemy aircraft, the value of moving down one row,

The original position is assigned a blank value.

Bullet movement: Judging from the first element of the last line of the array, if it is a bullet value, the value moves up one line,

The original position is assigned a blank value.

Output array: Starting from the first element of the array, judging the array inside, and outputting the corresponding symbol

The whole process is to keep emptying the screen and re-painting the screen,


The bullet goes up, and every time you clear the screen, you add 1,

This look at the function

void Movebul (int a[][n])

It's clear.

The other is a way to understand not difficult.

The other is the parameter settings, is not the focus of the software, but in which, learning a common C language implementation of the menu,

sw=0;

printf ("\ nthe enemy aircraft speed: 1. Fast 2.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;

}


You can implement the prompt, let the user enter the same, and then modify the parameters according to the user's input.


Experience:

1, C language Game program can see, C language is our first programming language, from the computer does not have any programming ability rookie, to now can read C language program, feel oneself in slowly progress.

2, the computer application system, is continuously receives the user input, then according to the corresponding logic, displays the corresponding content the process.

3, according to this idea, I can change the program into a snake game, control snakes around certain, that is, my plane can not only run below, can also run around, this as long as the limit of my aircraft in 21 lines of the restrictions cancel on it.

4, the plane and the bullet collided, is two coordinates consistent,

For example

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

death++;

Snake if the upper and lower of an array is equal to a different value, the two values represent elements such as snakes and fruit that are eaten.

5, can also be modified into a bumper car game and so on, feel this idea can expand a lot.


The back is the source of the plane.

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <time.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<width-2)

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++)

{

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

for (j=0;j<width;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: Wang Yu");

}

printf ("\ n");

}

}

void Movebul (int a[][n])

{

int i,j;

for (i=0;i<22;i++)

for (j=0;j<width;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<width;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 ("\ n the size of the game interface: 1. Big 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 ("\ nthe enemy aircraft speed: 1. Fast 2.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++)

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

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 flight, W fired bullets \ n set: Please press esc\n to start the game: any key \ n by Yan_xu");

if (Getch () ==27)

Setting ();

}






This article is from the "Bgd-wang blog" blog, make sure to keep this source http://10676614.blog.51cto.com/10666614/1771811

Airplane game Software: The initial experience of C language application

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.