Aircraft Game (modified version) and its structure analysis

Source: Internet
Author: User
Tags define function


#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 movement function

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

void setting (void);//Set function

void menu (void);//Menu function

void oper (void);//Operation function

Global variables:

int Scr[22][n] = {0}; Interface array

int PL = 9, x = 21; Initial position of my machine

int width =, speed = 3, density = score = 0, death = 0; Enemy aircraft speed, enemy aircraft density, score, death

int main (void)

{

menu ();

Oper ();

}


void Oper ()

{

int i = 0, j = 0;

SCR[X][PL] = 1; Initialize the location of my machine and enemy aircraft

SCR[0][5] = 3;

SCR[8][4] = 3;

SCR[2][7] = 3;

SCR[5][4] = 3;

SCR[6][4] = 3;

Initialization complete

while (1)

{

if (_kbhit ())

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

{

Case ' A ': Case ' a ':

if (pl > 0)

SCR[X][PL] = 0, SCR[X][--PL] = 1;

Break

Case ' d ': Case ' d ':

if (PL < width-2)

SCR[X][PL] = 0, SCR[X][++PL] = 1;

Break

Case ' Q ': Case ' Q ':

if (x>0)

SCR[X][PL] = 0, SCR[--X][PL] = 1;

Break

Case ' E ': Case ' e ':

if (x < 21)

SCR[X][PL] = 0, SCR[++X][PL] = 1;

Break

Case ' W ': Case ' W ':

SCR[X-1][PL] = 2;

Break

Case 27:

Setting ();

Break

}

if (++j%density = = 0)//control the speed of the 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:SSD");

}

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 + =, printf ("\7");

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

}

}

}

void Movepla (int a[][n])

{

int I, J;

for (i = +; I >= 0; i--)//From the last line is to avoid the enemy aircraft directly rushed to the group.

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

{

if (i = = && A[i][j] = = 3)

A[I][J] = 0;//The bottom row is assigned a value of 0 to avoid crossing the line.

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

A[I][J] = 0;

A[i + 1][j] = 3;

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

death++;a[i][j]=0;

}


}

}

}

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, QE control my machine up and down flight, W fired bullets \ n set: Press esc\n to start the game: any key \ n by Yan_xu");

if (_getch () = = 27)

Setting ();

The modifications are as follows:

    1. Increase the number of enemy aircraft

    2. Speed up enemy aircraft generation

    3. The plane can move up and down

Code Analysis:

1. Define the function first

2. Define function type, screen width, speed, enemy plane density, score, death int scr[22][n]={0},pl=9,width=24,speed=3,density=30,score=0,death=0; This program uses the shaping int.
3. Control the aircraft's movement coordinates: Enter a: The aircraft moves one cell to the left on the x-axis. Input d: The plane moves to the right of the x-axis, enter Q: The plane moves on the y-axis one grid, enter E: The plane moves down the y-axis, W is the position of the bullet, the initial position in the aircraft position on the Y axis to reduce a lattice.
4. Bonus Rule of death: if the coordinates of the bullet are the same as the Y value of the enemy plane, a bullet hits the enemy plane, plus points. In contrast, the coordinates of the enemy plane and the coordinates of the Y-value of the same, X-value difference one of my machine died.
5. Critical Position: Limited in the coordinate area the screen size cannot be exceeded.
6. Set the interface size of the aircraft speed, enemy aircraft density and control the direction of the key.

Study No.: 150206210

This article from "11639407" blog, declined reprint!

Aircraft Game (modified version) and its structure analysis

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.