C语言2D游戏-程序

时间:2024-07-17 07:21:05
#define _CRT_SECURE_NO_WARNINGS 1
#define HIGH 10
#define WIDTH 10
#define PLAYER 4][1
#define GOAL 4][9
#include <stdio.h>
#include <Windows.h>
void print_table(int arr[HIGH][WIDTH]) {
	system("cls");
	int i = 0;
	int ia = 0;
	for (i = 0; i < HIGH; i++) {
		for (ia = 0; ia < WIDTH; ia++) {
			printf("%d%c", arr[i][ia], WIDTH - 1 == ia ? '\n' : ' ');
		}
	}
}
int main() {
	printf("欢迎你来玩这个游戏,在这个游戏里“0”是空地,“1”是你,而“8”是终点,在这之中,你只需要到达终点,就可以赢了\n");
	Sleep(2800);
	int arr[HIGH][WIDTH] = { 0 };
	if (&arr[PLAYER] == &arr[GOAL]) {
		goto a;
	}
	arr[PLAYER] = 1;
	arr[GOAL] = 8;
	char ch = 0;
	int* p = &arr[PLAYER];
	int i = 0;
	do {
		print_table(arr);
		ch = getchar();
		while ('\n' != getchar()) {
			;
		}
		*p = 0;
		switch (ch) {
		case 'w':
			for (i = 0; i < WIDTH; i++) {
				if (p == &arr[0][i]) {
					break;
				}
			}
			if (WIDTH == i) {
				p -= WIDTH;
			}
			break;
		case 'a':
			for (i = 0; i < HIGH; i++) {
				if (p == &arr[i][0]) {
					break;
				}
			}
			if (HIGH == i) {
				p--;
			}
			break;
		case 's':
			for (i = 0; i < WIDTH; i++) {
				if (p == &arr[HIGH - 1][i]) {
					break;
				}
			}
			if (WIDTH == i) {
				p += WIDTH;
			}
			break;
		case 'd':
			for (i = 0; i < HIGH; i++) {
				if (p == &arr[i][WIDTH - 1]) {
					break;
				}
			}
			if (HIGH == i) {
				p++;
			}
			break;
		default:
			break;
		}
		*p = 1;
	} while (8==arr[GOAL]);
	a:
	system("cls");
	printf("恭喜你,你赢了");
	return 0;
}