Lu Qitao ITCSC 1,150,809,125
1.printf is a variable parameter function , One of the most common applications, in this for everyone to wake up, function printf read from left to right, and then put first read to the bottom of the stack, the last read on the top of the stack, processing time from the top of the stack, so we see the result is, Start processing from the right. I hope you will not make similar mistakes.
2. Move: Starting from the first element of the array line, if the number of enemy aircraft, the value of moving down one line, the original position is assigned to 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 and 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.
3.AD control to move around, with W control fired bullets. Enemy aircraft constantly flying from the opposite, bullets hit enemy aircraft score, my machine was the enemy plane collision my machine died.
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);
4. Summary
One, for each knowledge point, should immediately make up the corresponding program, sometimes there may be grammatical errors, encountered a better method can also try, many times you think of the code how to write and you really write out there is a big gap.
Second, in the learning process of the function, we must understand the function and the specific format. It is worth emphasizing that in the process of writing loops, be sure to understand the conditions of the cycle.
Third, in the learning process of the function, we must understand the function and the specific format. It is worth emphasizing that in the process of writing loops, be sure to understand the conditions of the cycle.
Four, before and after the mastery, accumulate the knowledge that should grasp.
The following is the program source code
#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 ("Set: Esc");
}
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 the line.
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 ':d ensity=10;
Break
Case ' 1 ':d ensity=20;
Break;case ' 2 ':d ensity=30;
Break;case ' 3 ':d ensity=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");
if (Getch () ==27) setting ();
}
This article from "11493781" blog, declined reprint!
First time job