小游戏——飞机

 

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

#include<stdio.h>
#include<windows.h>
#include<conio.h>
#include<stdlib.h>
#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;
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值