Round3:我的黑白框雷霆战机进阶2

基于Round1的游戏,在其中加入了更多的元素。

欢迎试玩以及提出意见。

#include
#include
#include
#include


#define High 15                  //游戏画面尺寸
#define Width 25
#define EnemyNum 5        //敌机个数




int position_x,position_y;                                               //飞机位置
int enemy_x[EnemyNum],enemy_y[EnemyNum];         // 敌机位置
int canvas[High][Width] = {0};                                       //定义一个二维数组存储游戏界面中的对应元素(0为空格,1为飞机                                                                                             *,2为子弹|,3为敌机@)
int score;                                                                         //得分
int BulletWidth;                                                               //子弹宽度
int EnemyMoveSpeed;                                                    //敌机移动速度






void gotoxy (int x,int y)
{
HANDLE handle = GetStdHandle (STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition (handle,pos);
}






void startup ()
{
int k;
position_x = High-1;
position_y = Width/2;
canvas[position_x][position_y] = 1;
for (k = 0;k < EnemyNum;k++)
{
enemy_x[k] = rand()%2;
enemy_y[k] = rand()%Width;
canvas[enemy_x[k]][enemy_y[k]] = 3;
}
score = 0;
BulletWidth = 0;
EnemyMoveSpeed = 20;
}






void show ()
{
int i,j;
gotoxy (0,0);
for (i = 0;i < High;i++)
{
for (j = 0;j < Width;j++)
{
if (canvas[i][j] == 0)
printf (" ");
else if (canvas[i][j] == 1)
printf ("*");
else if (canvas[i][j] == 2)
printf ("|");
else if (canvas[i][j] == 3)
printf ("@");
}
printf ("\n");
}
printf ("SCORE:%d\n",score);
Sleep (20);
}










void updateWithoutInput ()
{
int i,j,k;
static int speed = 0;
for (i = 0;i < High;i++)
{
for (j = 0;j < Width;j++)
{
if (canvas[i][j] == 2)
{
for (k = 0;k < EnemyNum;k++)
{
if ((i == enemy_x[k]) && (j == enemy_y[k]))
{
score ++;
if ((score % 5 == 0) && (EnemyMoveSpeed > 3))

EnemyMoveSpeed --;

                                                for (BulletWidth = 0;BulletWidth < 2;BulletWidth++)

{

                                                        if (score == 10)

BulletWidth ++;

                                                }

canvas[enemy_x[k]][enemy_y[k]] = 0;
enemy_x[k] = rand()%2;
enemy_y[k] = rand()%Width;
canvas[enemy_x[k]][enemy_y[k]] = 3;
canvas[i][j] = 0;
}
}
canvas[i][j] = 0;
if (i > 0)
canvas[i-1][j] = 2;
}
}
}
if (speed < EnemyMoveSpeed)
speed ++;
for (k = 0;k < EnemyNum;k++)
{
if ((position_x == enemy_x[k]) && (position_y == enemy_y[k]))
{
printf ("失败!\n");
Sleep (3000);
system ("pause");
exit (0);
}
if (enemy_x[k] > High)
{
canvas[enemy_x[k]][enemy_y[k]] = 0;
enemy_x[k] = rand()%2;
enemy_y[k] = rand()%Width;
canvas[enemy_x[k]][enemy_y[k]] = 3;
score --;
}
if (speed == EnemyMoveSpeed)
{
for (k = 0;k < EnemyNum;k++)
{
canvas[enemy_x[k]][enemy_y[k]] = 0;
enemy_x[k] ++;
speed = 0;
canvas[enemy_x[k]][enemy_y[k]] = 3;
}
}
}

}

void updateWithInput ()
{
char input;
if (kbhit ())
{
input = getch ();
if (input == 'a'&&position_y > 0)
{
canvas[position_x][position_y] = 0;
position_y --;
canvas[position_x][position_y] = 1;
}
else if (input == 'd'&&position_y < Width-1)
{
canvas[position_x][position_y] = 0;
position_y ++;
canvas[position_x][position_y] = 1;
}
else if (input == 'w')
{
canvas[position_x][position_y] = 0;
position_x --;
canvas[position_x][position_y] = 1;
}
else if (input == 's')
{
canvas[position_x][position_y] = 0;
position_x ++;
canvas[position_x][position_y] = 1;
}
else if (input == ' ')
{
int k;
int left = position_y - BulletWidth;
int right = position_y + BulletWidth;
if (left < 0)
left = 0;
if (right > Width-1)
right = Width-1;
for (k = left;k <= right;k++)
canvas[position_x-1][k] = 2;
}
}
}






int main ()
{
startup ();
while (1)
{
show ();
updateWithoutInput ();
updateWithInput ();
}
return 0;

}


Round3:我的黑白框雷霆战机进阶2_第1张图片

你可能感兴趣的:(学习近况)