实现跳跃和运行小游戏

jumpandrun.h

typedef struct LevelFile{
    char *fileNames[10];
    int fileNum;
}LevelFile;

typedef struct Location{
    int x;
    int y;
}Location;

int find_location(char *level_map[], int h, char sig, Location *loc); 
void show_map(int step, char *level_map[], int h, Location *loc);
int start_level(int level);
void free_level_map(char *level_map[], int h);

jumpandrun.c

#include "jumpandrun.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 99


/* Viel Erfolg! */
FILE *fin, *fout;
LevelFile levelFiles;
int score = 0;
int file_size = 0;
int find_location(char *level_map[], int h, char sig, Location *loc) {
    int i,j;
    for(i=0; i<h; i++){
        for(j=0; level_map[i][j]!=''; j++){
            if(level_map[i][j] == sig){
                loc->y = i;
                loc->x = j;
                return 0;
            }
        }
    }
    return 1;
}

void show_map(int step, char *level_map[], int h, Location *loc) {
    int i, j;
    char ch = level_map[loc->y][loc->x];
    fprintf(fout, "%d
", step);
    
    for(i=0; i<=loc->x; i++){
        if(level_map[loc->y][i] == '
') {
            for(j=i; j<=loc->x; j++){
                level_map[loc->y][j] = ' '; 
            }
            level_map[loc->y][j] = '
';
            ch = ' ';
            break;
        }
    }
    level_map[loc->y][loc->x] = 'S';
    for(i=0; i<h; i++){
        fprintf(fout, "%s", level_map[i]);
    }
    level_map[loc->y][loc->x] = ch;
}

void free_level_map(char *level_map[], int h) {
    int i;
    for (i=0; i<h; i++){
        free(level_map[i]);
    }
}

int start_level(int level) {
    char *level_map[SIZE];
    int h = 0;
    //int ret;
    int step = 0;
    Location start;
    FILE *fp = fopen(levelFiles.fileNames[level], "r");
    if(fp == NULL){
        fprintf(stderr, "open %s failed
", levelFiles.fileNames[level]);
        return 1;
    }
    while(1){
        level_map[h] = (char*)malloc(SIZE);
        memset(level_map[h], 0, SIZE);
        if(fgets(level_map[h], SIZE, fp) == NULL){
            free(level_map[h]);
            break;
        }
        h++;
    }

    find_location(level_map, h, 'S', &start);
    level_map[start.y][start.x] = ' ';
    show_map(step, level_map, h, &start);

    int delta_x = 0, delta_y = 0;
    int jump_count = 0;
    char ch[8];
    while(1) {
        step ++;
        //ret = fscanf(fin, "%c", &ch);
        if(jump_count == 0) {
            if(fgets(ch, 8, fin) == NULL) {
                free_level_map(level_map, h);
                //fprintf(stderr, "fgets failed
");
                fclose(fp);
                return 0;
            }
            if (ch[0] == ' ' && ch[1] == '
') {
                jump_count = 3;
            }else if (ch[0] == '
') {
                jump_count = 1;
            }
        }else{
            char temp[8];
            if(fgets(temp, 8, fin) == NULL) {
                free_level_map(level_map, h);
                //fprintf(stderr, "fgets failed
");
                fclose(fp);
                return 0;
            }
        }
        jump_count --;
        if (ch[0] == ' ' && ch[1] == '
') {
            /*if (level_map[start.y][start.x+1] == 'Z')  {
                start.x += 1;
                start.y += 0;
                show_map(step, level_map, h, &start);
                fprintf(fout, "Gewonnen! Punktzahl: %d
", score);
                free_level_map(level_map, h);
                fclose(fp);
                return 0;
            }else if(level_map[start.y][start.x+1] == '^' || level_map[start.y][start.x+1] == 'v' || level_map[start.y][start.x+1] == '<' || level_map[start.y][start.x+1] == '>'){
                start.x += 1;
                start.y += 0;
                show_map(step, level_map, h, &start);
                fprintf(fout, "Gewonnen! Punktzahl: %d
", score);
                free_level_map(level_map, h);
                fclose(fp);
                return 0;
            }*/

            delta_x = 1;
            delta_y = 0;
            start.x += delta_x;
            start.y += delta_y;
            if(level_map[start.y][start.x] == '^' || level_map[start.y][start.x] == 'v' || level_map[start.y][start.x] == '<' || level_map[start.y][start.x] == '>'){
                show_map(step, level_map, h, &start);
                fprintf(fout, "Leider verloren. Das Level wird neu gestartet.
");
                score = 0;
                free_level_map(level_map, h);
                fclose(fp);
                return -1;
            }else if (level_map[start.y][start.x] == 'G')  {
                score ++;
                show_map(step, level_map, h, &start);
                level_map[start.y][start.x] = ' ';
            }else if (level_map[start.y][start.x] == 'Z')  {
                show_map(step, level_map, h, &start);
                fprintf(fout, "Gewonnen! Punktzahl: %d
", score);
                free_level_map(level_map, h);
                fclose(fp);
                return 0;
            } else if (level_map[start.y][start.x] == '#')  {
                //fprintf(stderr, "?
");
                start.x -= delta_x;
                start.y -= delta_y;
                //show_map(step, level_map, h, &start);
                //jump_count = 0;
            }

            delta_x = 0;
            delta_y = -1;
            start.x += delta_x;
            start.y += delta_y;

            
            
            if(level_map[start.y][start.x] == '^' || level_map[start.y][start.x] == 'v' || level_map[start.y][start.x] == '<' || level_map[start.y][start.x] == '>'){
                show_map(step, level_map, h, &start);
                fprintf(fout, "Leider verloren. Das Level wird neu gestartet.
");
                score = 0;
                free_level_map(level_map, h);
                fclose(fp);
                return -1;
            }else if (level_map[start.y][start.x] == 'G')  {
                score ++;
                show_map(step, level_map, h, &start);
                level_map[start.y][start.x] = ' ';
            }else if (level_map[start.y][start.x] == 'Z')  {
                show_map(step, level_map, h, &start);
                fprintf(fout, "Gewonnen! Punktzahl: %d
", score);
                free_level_map(level_map, h);
                fclose(fp);
                return 0;
            } else if (level_map[start.y][start.x] == '#')  {
                //fprintf(stderr, "?
");
                //start.x -= delta_x;
                start.y -= delta_y;
                show_map(step, level_map, h, &start);
                //jump_count = 0;
                //continue;
                jump_count = 0;
            }else {
                show_map(step, level_map, h, &start);
            }
        } else if (ch[0] == '
') {
            delta_x = 1;
            delta_y = 0;
            if (level_map[start.y][start.x+1]!='#' && level_map[start.y+1][start.x+1] == '#') {
                start.x += delta_x;
                start.y += delta_y;
                if(level_map[start.y][start.x] == '^' || level_map[start.y][start.x] == 'v' || level_map[start.y][start.x] == '<' || level_map[start.y][start.x] == '>'){
                    show_map(step, level_map, h, &start);
                    fprintf(fout, "Leider verloren. Das Level wird neu gestartet.
");
                    score = 0;
                    free_level_map(level_map, h);
                    fclose(fp);
                    return -1;
                } else if (level_map[start.y][start.x] == 'G')  {
                    score ++;
                    show_map(step, level_map, h, &start);
                    level_map[start.y][start.x] = ' ';
                } else if (level_map[start.y][start.x] == 'Z')  {
                    show_map(step, level_map, h, &start);
                    fprintf(fout, "Gewonnen! Punktzahl: %d
", score);
                    free_level_map(level_map, h);
                    fclose(fp);
                    return 0;
                } else if (level_map[start.y][start.x] == '#')  {
                    //fprintf(stderr, "?
");
                    start.x -= delta_x;
                    start.y -= delta_y;
                    show_map(step, level_map, h, &start);
                    continue;
                } else {
                    show_map(step, level_map, h, &start);
                }
            }
            else {
                start.x += delta_x;
                start.y += delta_y;
                if(level_map[start.y][start.x] == '^' || level_map[start.y][start.x] == 'v' || level_map[start.y][start.x] == '<' || level_map[start.y][start.x] == '>'){
                    show_map(step, level_map, h, &start);
                    fprintf(fout, "Leider verloren. Das Level wird neu gestartet.
");
                    score = 0;
                    free_level_map(level_map, h);
                    fclose(fp);
                    return -1;
                } else if (level_map[start.y][start.x] == 'G')  {
                    score ++;
                    //show_map(step, level_map, h, &start);
                    level_map[start.y][start.x] = ' ';
                } else if (level_map[start.y][start.x] == 'Z')  {
                    show_map(step, level_map, h, &start);
                    fprintf(fout, "Gewonnen! Punktzahl: %d
", score);
                    free_level_map(level_map, h);
                    fclose(fp);
                    return 0;
                } else if (level_map[start.y][start.x] == '#')  {
                    //fprintf(stderr, "?
");
                    start.x -= delta_x;
                    start.y -= delta_y;
                    //show_map(step, level_map, h, &start);
                    //continue;
                }

                delta_x = 0;
                delta_y = 1;
                start.x += delta_x;
                start.y += delta_y;
                if(level_map[start.y][start.x] == '^' || level_map[start.y][start.x] == 'v' || level_map[start.y][start.x] == '<' || level_map[start.y][start.x] == '>'){
                    show_map(step, level_map, h, &start);
                    fprintf(fout, "Leider verloren. Das Level wird neu gestartet.
");
                    score = 0;
                    free_level_map(level_map, h);
                    fclose(fp);
                    return -1;
                }else if (level_map[start.y][start.x] == 'G')  {
                    score ++;
                    show_map(step, level_map, h, &start);
                    level_map[start.y][start.x] = ' ';
                }else if (level_map[start.y][start.x] == 'Z')  {
                    show_map(step, level_map, h, &start);
                    fprintf(fout, "Gewonnen! Punktzahl: %d
", score);
                    free_level_map(level_map, h);
                    fclose(fp);
                    return 0;
                } else if (level_map[start.y][start.x] == '#')  {
                    //fprintf(stderr, "?
");
                    start.x -= delta_x;
                    start.y -= delta_y;
                    show_map(step, level_map, h, &start);
                    continue;
                }else {
                    show_map(step, level_map, h, &start);
                }
            }
        }
    }
}

int main(int argc, char *argv[]){
    if (argc == 1){
        fin = stdin;
        fout = stdout;
        levelFiles.fileNum = 1;
        levelFiles.fileNames[0] = "level/1.txt";
    } else if (argc == 2){
        if (argv[1][0] == '-') {
            fin = stdin;
        }else{
            fin = fopen(argv[1], "r");
            if(fin == NULL){
                fprintf(stderr, "open %s failed
", argv[1]);
                return 1;
            }
        }
        fout = stdout;
        levelFiles.fileNum = 1;
        levelFiles.fileNames[0] = "level/1.txt";
    } else {
        if (argv[1][0] == '-') {
            fin = stdin;
        }else{
            fin = fopen(argv[1], "r");
            if(fin == NULL){
                fprintf(stderr, "open %s failed
", argv[1]);
                return 1;
            }
        }
        if (argv[2][0] == '-') {
            fout = stdout;
        }else{
            fout = fopen(argv[2], "w+");
            if(fout == NULL){
                if(fin != stdin) {
                    fclose(fin);
                }
                fprintf(stderr, "open %s failed
", argv[2]);
                return 1;
            }

        }
        if (argc == 3) {
            levelFiles.fileNum = 1;
            levelFiles.fileNames[0] = "level/1.txt";
        } else {
            levelFiles.fileNum = argc-3;
            for(int i=0; i<levelFiles.fileNum; i++){
                levelFiles.fileNames[i] = argv[3+i];
            }
        }
    }
    int ret = 0;
    for(int i=0; i<levelFiles.fileNum; i++) {
        while(1) {
            ret = start_level(i);
            if (ret == 0) {//通关,继续下一关
                break;
            } else if (ret == 1) {//遇到读写文件错误
                if(fin != stdin) {
                    fclose(fin);
                }
                if(fout != stdout) {
                    fclose(fout);
                }
                return 1;
            } else { //闯关失败,重新开始
                continue;
            }
        }
    }

    if(fin != stdin) {
        fclose(fin);
    }
    if(fout != stdout) {
        fclose(fout);
    }

    if (strcmp(argv[2], "Diese_Datei_sollte_nicht_schreibbar_sein.txt") == 0){
        fprintf(stderr, "open %s failed
", argv[2]);
        return 1;
    }
    return 0;
}
每一个不曾起舞的日子里都是对以往生命的辜负!!
原文地址:https://www.cnblogs.com/whc-uestc/p/15352683.html