小游戏——飞机

 

 在终端运行的一个小游戏, 随机生成10个敌机,子弹随分数增加变强,敌机随分数增加变快。

#include
#include
#include
#include
#define _CRT_SECURE_NO_WARNINGS
#define High  25
#define Width  50
#define EnemyNum 5
int canvas[25][50];
int position_x, position_y;
int grade;
int enemy_x[EnemyNum], enemy_y[EnemyNum];
int velocity;
int score = 0;
void gotoxy(int x, int y) {
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos);
}
void HideCursor() {
	CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void startup() {
	grade = 0;
	velocity = 20;
	memset(canvas, 0, sizeof(canvas));
	position_x = High / 2;
	position_y = Width / 2;
	canvas[position_x][position_y] = 1;
	velocity = 20;
	score = 0;
	int k;
	for (k = 0; k < EnemyNum; k++) {
		enemy_x[k] = rand() % 2;
		enemy_y[k] = rand() % (High - 3 - 2 + 1) + 2;
		//canvas[enemy_x[k]][enemy_y[k]] = 2;
	}
}
void show() {
	gotoxy(0, 0);
	int i, j;
	for (i = 0; i < Width + 2; i++)printf("#");
	printf("\n");
	for (i = 0; i < High; i++) {
		printf("#");
		for (j = 0; j < Width; j++) {
			int k;
			int flag = 0;
			for (k = 0; k < EnemyNum; k++) {
				if (i == enemy_x[k] && j == enemy_y[k]) {
					printf("@");
					flag = 1;
					break;
				}
			}
			if (flag == 0) {
				if (canvas[i][j] == 0) {
					printf(" ");
				}
				else if (canvas[i][j] == 1) {
					printf("*");
				}
				else if (canvas[i][j] == 2) {
					printf("|");
				}
			}
		}
		printf("#\n");
	}
	for (i = 0; i < Width + 2; i++)printf("#");
	printf("\n");
	printf("得分:%d   按键说明:上 w, 下 s, 左 a,右 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) {
				int k;
				int flag = 0;
				for (k = 0; k < EnemyNum; k++) {
					if (i <= enemy_x[k] && j == enemy_y[k]) {
						score++;
						canvas[i][j] = 0;
						enemy_x[k] = rand() % 2;
						enemy_y[k] = rand() % (Width - 3 - 2 + 1) + 2;
						if (score / 20 <= 3) {
							grade = score / 20;
						}
						else {
							grade = 3;
						}
						if (20 - (score / 5) > 3) {
							velocity = 20 - (score / 5);
						}
						else {
							velocity = 4;
						}
						flag = 1;
						break;
					}
				}
				if (flag == 0) {
					canvas[i][j] = 0;
					if (i - 1 > 0) {
						canvas[i - 1][j] = 2;
					}
				}
			}
		}
	}
	int k;
	static int speed = 0;
	if (speed < velocity)speed++;
	for (k = 0; k < EnemyNum; k++) {
		if (enemy_x[k] == position_x && enemy_y[k] == position_y) {
			show();
			printf("Gameover!\n");
			Sleep(3000);
			exit(0);
		}
		if (enemy_x[k] >= High) {
			if(score > 0)score--;
			enemy_x[k] = rand() % 2;
			enemy_y[k] = rand() % (Width - 3 - 2 + 1) + 2;
		}
		if (speed >= velocity) {
			enemy_x[k]++;
		}
	}
	if (speed >= velocity) {
		speed = 0;
	}
}
void updateWithInput() {
	char input;

	if (_kbhit()) {
		input = _getch();
		if (input == 'a') {
			canvas[position_x][position_y] = 0;
			if (position_y > 0) {
				position_y--;
			}
			canvas[position_x][position_y] = 1;
		}
		else if (input == 'd') {
			canvas[position_x][position_y] = 0;
			if (position_y < Width - 1) {
				position_y++;
			}
			canvas[position_x][position_y] = 1;
		}
		else if (input == 'w') {
			canvas[position_x][position_y] = 0;
			if (position_x > 0) {
				position_x--;
			}
			canvas[position_x][position_y] = 1;
		}
		else if (input == 's') {
			canvas[position_x][position_y] = 0;
			if (position_x < High - 1) {
				position_x++;
			}
			canvas[position_x][position_y] = 1;
		}
		else if (input == ' ') {
			int left = position_y - grade;
			int right = position_y + grade;
			if (left < 0)left = 0;
			if (right > Width - 1) {
				right = Width - 1;
			}
			int k;
			//left = 0;
			//right = Width - 1;
			for (k = left; k <= right; k++) {
				canvas[position_x - 1][k] = 2;
			}
		}
	}

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

 

你可能感兴趣的:(xiaoxiangmu,kaifa)