生成小游戏黄金矿工的代码
时间: 2023-04-02 22:05:06 浏览: 138
你好,以下是生成小游戏黄金矿工的代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAP_WIDTH 10
#define MAP_HEIGHT 10
#define GOLD_NUM 20
int map[MAP_HEIGHT][MAP_WIDTH];
void init_map() {
int i, j;
for (i = ; i < MAP_HEIGHT; i++) {
for (j = ; j < MAP_WIDTH; j++) {
map[i][j] = ;
}
}
}
void print_map() {
int i, j;
for (i = ; i < MAP_HEIGHT; i++) {
for (j = ; j < MAP_WIDTH; j++) {
if (map[i][j] == ) {
printf("□ ");
} else if (map[i][j] == 1) {
printf("■ ");
} else if (map[i][j] == 2) {
printf("$ ");
}
}
printf("\n");
}
}
void generate_gold() {
int i, x, y;
srand(time(NULL));
for (i = ; i < GOLD_NUM; i++) {
x = rand() % MAP_WIDTH;
y = rand() % MAP_HEIGHT;
if (map[y][x] == ) {
map[y][x] = 1;
} else {
i--;
}
}
}
int main() {
int x = MAP_WIDTH / 2, y = MAP_HEIGHT - 1;
char c;
int score = ;
init_map();
generate_gold();
while (1) {
system("clear");
print_map();
printf("score: %d\n", score);
printf("use ← → to move, press space to dig\n");
c = getchar();
if (c == 'a' && x > ) {
x--;
} else if (c == 'd' && x < MAP_WIDTH - 1) {
x++;
} else if (c == ' ') {
if (map[y][x] == 1) {
map[y][x] = 2;
score++;
}
}
if (score == GOLD_NUM) {
printf("you win!\n");
break;
}
}
return ;
}
阅读全文