验证中...
Languages: C/C++
Categories: 微信开发
Latest update 2019-11-09 18:21
源.cpp
Raw Copy
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<windows.h>
#define random(x) (rand()%x)
#define high 20
#define width 50
//全局变量
int position_x, position_y; //飞机位置坐标
int canvas[high][width] = { 0 };//二维数组定义元素位置,0输出空格,1输出飞机,2输出子弹,3敌机
int enemy_x, enemy_y; //敌机位置坐标
int score; //游戏得分
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = { 1,0 };//第二个值为0表示隐藏光标
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy(int x, int y) //光标移动到该位置
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void sartup() //数据初始化
{
enemy_x = width / 2;
enemy_y = 1;
canvas[enemy_y][enemy_x] = 3;
position_x = width / 2; //飞机位置坐标
position_y = high / 2;
canvas[position_y][position_x] = 1;
score = 0;
HideCursor();
}
void show() //显示游戏画面
{
int i, j; //i对应high j对应width
gotoxy(0, 0); //光标移动到原点,以下清屏
for (i = 0; i < high - 1; i++)
{
for (j = 0; j < width - 1; j++)
{
if (j == 0 || j == width - 2)
{
printf("|");
}
else if (i == 0 || i == high - 2)
{
printf("-");
}
else if (canvas[i][j] == 1)
{
printf("*");
}
else if (canvas[i][j] == 2)
{
printf("|");
}
else if (canvas[i][j] == 3)
{
printf("@");
}
else
printf(" "); //输出空格
}
printf("\n");
}
printf("score:%d\n", score);
}
void updatewithoutinput() //与用户输入无关的更新
{
int i, j;
for (i = 0; i < high; i++) //子弹上升
{
for (j = 0; j < width; j++)
{
if (canvas[i][j] == 2)
{
canvas[i][j] = 0;
if (i > 0)
{
canvas[i - 1][j] = 2;
}
if (i == enemy_y && j == enemy_x)
{
canvas[enemy_y][enemy_x] = 0;
enemy_y = 1;
enemy_x = rand() % width;
canvas[enemy_y][enemy_x] = 3;
score++;
}
}
}
}
if (enemy_y > high - 2) //敌机飞出下界
{
canvas[enemy_y][enemy_x] = 0;
enemy_y = 1;
enemy_x = rand() % width;
canvas[enemy_y][enemy_x] = 3;
score--;
}
static int speed = 10;
if (speed < 10)
{
speed++;
}
if (speed == 10)
{
canvas[enemy_y][enemy_x] = 0; //敌机下落
enemy_y++;
canvas[enemy_y][enemy_x] = 3;
speed = 0;
}
if (position_x <= 0) //防止飞机飞出边界
{
canvas[position_y][position_x] = 0;
position_x++;
canvas[position_y][position_x] = 1;
}
else if (position_x >= width - 1)
{
canvas[position_y][position_x] = 0;
position_x--;
canvas[position_y][position_x] = 1;
}
if (position_y <= 0)
{
canvas[position_y][position_x] = 0;
position_y++;
canvas[position_y][position_x] = 1;
}
else if (position_y >= high - 1)
{
canvas[position_y][position_x] = 0;
position_y--;
canvas[position_y][position_x] = 1;
}
}
void updatewithinput() //与用户输入有关的更新
{
char input; //输入控制字符
if (kbhit()) //当按键时执行
{
input = getch();
if (input == 'a') //使飞机移动
{
canvas[position_y][position_x] = 0;
position_x--;
canvas[position_y][position_x] = 1;
}
if (input == 'd')
{
canvas[position_y][position_x] = 0;
position_x++;
canvas[position_y][position_x] = 1;
}
if (input == 'w')
{
canvas[position_y][position_x] = 0;
position_y--;
canvas[position_y][position_x] = 1;
}
if (input == 's')
{
canvas[position_y][position_x] = 0;
position_y++;
canvas[position_y][position_x] = 1;
}
if (input == ' ')
{
canvas[position_y - 1][position_x] = 2;
}
if (input == 27) //ESC对应的值为27
{
system("pause"); //任意暂停程序
}
}
}
int main()
{
sartup(); //数据初始化
while (1) //执行游戏循环
{
show(); //显示游戏画面
updatewithoutinput(); //与用户输入无关的更新
updatewithinput(); //与用户输入有关的更新
}
return 0;
}

Comment list( 0 )

Sign in for post a comment

Help Search

191139_cd20d5fd_5186603 191143_ebef6f8d_5186603