logo资料库

纯C语言小游戏.docx

第1页 / 共14页
第2页 / 共14页
第3页 / 共14页
第4页 / 共14页
第5页 / 共14页
第6页 / 共14页
第7页 / 共14页
第8页 / 共14页
资料共14页,剩余部分请下载后查看
/* Program copyright to Software House: Green C */ #include #include #include #include #include #include struct object_fix { char name[20]; char id[5]; char desc[500]; char action[30]; char im[5]; }; struct object_move { char name[20]; char id[5]; char desc[500]; int loc; int pwr; int strg; char im[5]; }; struct rover { char name[20]; char id[5]; char desc[500]; int pwr; int strg; int location[2]; char im[5]; }; struct map /* this is the map structure*/ { char data[20]; char add_data[20]; int amount; int x; /* this were the successor keeps it's x & y values*/ int y; }; struct location /*this structure is for the successor lister*/ { float height; char obj; }; void stats_update(int selected, struct rover *p_rover)
{ switch (selected) { case 1: if(p_rover->pwr < 7) printf("\n\nYou do not have enough power to perform this action!\n\n"); else { (p_rover->pwr) -= 7; printf("You have destroyed the object!\n\n"); } break; case 2: if(p_rover->pwr < 3) printf("\n\nYou do not have enough power to perform this action!\n\n"); else if(p_rover->strg > 90) printf("\n\nYou do not have enough storage space for this object!\n\n"); else { (p_rover->pwr) -= 3; (p_rover->strg) += 10; printf("You have collected a sample of the object!\n\n"); } break; case 3: p_rover->pwr -= 10; /*Distance around object- value gained from mapper module. 1 square = -1 power*/ printf("You have avoided the object!\n\n"); break; case 4: p_rover->pwr -= 2; printf("You have driven through the obstacle!\n\n"); break; case 5: if(p_rover->pwr == 100) printf("\n\nYou do not need to charge up!\n\n"); else { p_rover->pwr = 100; printf("You have charged up your rover!\n\n"); } break; default: printf("\n\n*****ERROR*****\nInvalid Selection\n\n"); break; } } void action(char object, struct rover *p_rover) { int selection;
switch(object) { case 1: printf("\nYou have encountered: A Sandy Rock\n\n"); printf("This object can be:\n1.\tDestroyed\n2.\tCollected\nPlease choose action 1 or 2:\t"); scanf("%d", &selection); stats_update(selection, p_rover); break; case 2: printf("\nYou have encountered: A Solid Rock\n\n"); printf("This object can be:\n1.\tAvoided\n2.\tCollected\nPlease choose action 1 or 2:\t"); scanf("%d", &selection); if (selection == 1) selection = 3; stats_update(selection, p_rover); break; case 3: printf("\nYou have encountered: A Mountain\n\n"); printf("This object can be:\n1.\tAvoided\nPlease enter 1:\t"); scanf("%d", &selection); selection = 3; stats_update(selection, p_rover); break; case 4: printf("\nYou have encountered: Dust\n\n"); printf("This object can be:\n1.\tDriven through\n2.\tCollected\nPlease choose action 1 or 2:\t"); scanf("%d", &selection); if (selection == 1) selection = 4; stats_update(selection, p_rover); break; case 5: printf("\nYou have encountered: A Sheer Valley\n\n"); printf("This object can be:\n1.\tAvoided\nPlease enter 1:\t"); scanf("%d", &selection); selection = 3; stats_update(selection, p_rover); break; case 6: printf("\nYou have encountered: A Gentle Valley\n\n"); printf("This object can be:\n1.\tDriven through\n2.\tAvoided\nPlease choose action 1 or 2:\t"); scanf("%d", &selection); if (selection == 1) selection = 4; if (selection == 2) selection = 3; stats_update(selection, p_rover);
break; case 7: printf("\nYou have encountered: A 'Martian' Tree\n\n"); printf("This object can be:\n1.\tDestroyed\n2.\tCollected\n3.\tAvoided\nPlease choose action 1, 2 or 3:\t"); scanf("%d", &selection); stats_update(selection, p_rover); break; case 8: printf("\nYou have encountered: Shallow Water\n\n"); printf("This object can be:\n1.\tDriven through\n2.\tCollected\n3.\tAvoided\nPlease choose action 1, 2 or 3:\t"); scanf("%d", &selection); if (selection == 1) selection = 4; stats_update(selection, p_rover); break; case 9: printf("\nYou have encountered: Deep Water\n\n"); printf("This object can be:\n1.\tAvoided\n2.\tCollected\nPlease choose action 1 or 2:\t"); scanf("%d", &selection); if (selection == 1) selection = 3; stats_update(selection, p_rover); break; case 10: printf("\nYou have encountered: An Aggressive Alien\n\n"); printf("This object can be:\n1.\tDestroyed\nPlease enter 1:\t"); scanf("%d", &selection); selection = 1; stats_update(selection, p_rover); break; case 11: printf("\nYou have encountered: A Non-Aggressive Alien\n\n"); printf("This object can be:\n1.\tAvoided\nPlease enter 1:\t"); scanf("%d", &selection); selection = 3; stats_update(selection, p_rover); break; case 12: printf("\nYou have encountered: Another Rover\n\n"); printf("This object can be:\n1.\tAvoided\nPlease enter 1:\t"); scanf("%d", &selection); selection = 3; stats_update(selection, p_rover); break; case 13: printf("\nYou have encountered: A Power Station\n\n"); printf("You can:\n1.\tCharge up\n2.\tAvoid\nPlease choose action 1 or 2:\t"); scanf("%d", &selection);
if (selection == 1) selection = 5; if (selection == 2) selection = 3; stats_update(selection, p_rover); break; default: printf("\n\n*****ERROR*****\n\n"); break; } } void show_map(struct map *number, struct map *number_2, struct object_fix *rsny, struct object_fix *rsld, struct object_fix *mnt, struct object_fix *dst, struct object_fix *vshr, struct object_fix *vgnt, struct object_fix *mtre, struct object_fix *wshl, struct object_fix *wdp, struct object_move *aagr, struct object_move *anon, struct object_move *rvr, struct object_move *pstn, struct rover *p_rover) /*the show map function calling number and number_2 from the map structure to see if moveable objects are needed*/ { struct map f_map[8][8]; /*8 by 8 map*/ int i,j, rx, ry, object; /*this is your x and y value in your map (f_map)*/ system("cls"); /* this is your x and y value in your map (f_map)*/ //srand(time(NULL)); /*calling the time from include to gather random variables*/ for(i=0;i<8;i++) /*for loop to copy all your fixed object lists into f_map so they can be displayed*/ { for(j=0;j<8;j++) strcpy(f_map[i][j].data,""); } if (number->amount>0) /*this is looking at the add function to see weather or not there are aliens in f_map*/ { for(i=0;iamount;i++) /*allocating the number of aliens in f_map*/ strcpy(f_map[rand()%8][rand()%8].data, anon->im); /*randomizing their position*/ } if(number_2->amount>0) /*repeat of above just for rovers instead*/ { for(i=0;iamount;i++) strcpy(f_map[rand()%8][rand()%8].data, rvr->im); } rx= p_rover->location[0]; ry= p_rover->location[1]; strcpy(f_map[1][0].data, wdp->im); strcpy(f_map[4][0].data, mnt->im); strcpy(f_map[5][0].data, mnt->im); strcpy(f_map[3][1].data, dst->im); strcpy(f_map[4][1].data, dst->im); strcpy(f_map[3][2].data, dst->im);
strcpy(f_map[4][2].data, dst->im); strcpy(f_map[0][3].data, mnt->im); strcpy(f_map[1][3].data, mnt->im); strcpy(f_map[3][3].data, vshr->im); strcpy(f_map[4][3].data, dst->im); strcpy(f_map[5][3].data, vgnt->im); strcpy(f_map[3][4].data, vshr->im); strcpy(f_map[4][4].data, dst->im); strcpy(f_map[5][4].data, vgnt->im); strcpy(f_map[2][5].data, wshl->im); strcpy(f_map[3][5].data, wshl->im); strcpy(f_map[4][5].data, wshl->im); strcpy(f_map[1][6].data, pstn->im); strcpy(f_map[2][6].data, wdp->im); strcpy(f_map[3][6].data, wdp->im); strcpy(f_map[4][6].data, wshl->im); strcpy(f_map[7][6].data, mnt->im); strcpy(f_map[0][7].data, mnt->im); strcpy(f_map[1][7].data, wdp->im); strcpy(f_map[2][7].data, wshl->im); strcpy(f_map[3][7].data, wshl->im); strcpy(f_map[6][7].data, mnt->im); strcpy(f_map[rx][ry].data, p_rover->im); if((rx == 1 && ry == 0) || (rx == 2 && ry == 6) ||(rx == 3 && ry == 6) ||(rx == 1 && ry == 7)) { object = 9; action(object, p_rover); } else if((rx == 4 && ry == 0) || (rx == 5 && ry == 0) || (rx == 0 && ry == 3) || (rx == 1 && ry == 3) || (rx == 7 && ry == 6) || (rx == 0 && ry == 7) || (rx == 6 && ry == 7)) { object = 3; action(object, p_rover); } else if((rx == 3 && ry== 1) || (rx == 4 && ry == 1) || (rx == 3 && ry== 2) || (rx == 4 && ry == 2) || (rx == 4 && ry == 3) || (rx == 4 && ry == 4)) { object = 4; action(object, p_rover); } else if((rx == 3 && ry == 3) || (rx == 3 && ry == 4)) { object = 5; action(object, p_rover); } else if((rx == 5 && ry == 3) || (rx == 5 && ry == 4)) {
object = 6; action(object, p_rover); } else if((rx == 2 && ry == 5) || (rx == 3 && ry == 5 ) || (rx == 4 && ry == 5) || (rx == 4 && ry == 6) ||(rx == 2 && ry == 7) || (rx == 3 && ry == 7)) { object = 8; action(object, p_rover); } else if(rx == 1 && ry == 6) { object = 13; action(object, p_rover); } i=0; /*re-allocate i to 0 so map is printed from start*/ for(i=0;i<8;i++) /*8 by 8 map*/ { printf("+----+----+----+----+----+----+----+----+\n"); for(j=0;j<8;j++) /*8 by 8 map*/ { if(strlen(f_map[i][j].data)!=0) /*if function to print nothing but 4 spaces if there in nothing allocated in [i][j]*/ printf("|%4s",f_map[i][j].data); else printf("| "); /*end of coloumn visible map*/ } printf("|\n"); /*end of last coloumn visible map*/ } printf("+----+----+----+----+----+----+----+----+\n"); /*bottom of map*/ printf("\n"); printf("\270"); printf("Group B\n"); } void add_obj(struct map *number, struct map *number_2, struct object_fix *rsny, struct object_fix *rsld, struct object_fix *mnt, struct object_fix *dst, struct object_fix *vshr, struct object_fix *vgnt, struct object_fix *mtre, struct object_fix *wshl, struct object_fix *wdp, struct object_move *aagr, struct object_move *anon, struct object_move *rvr, struct object_move *pstn, struct rover *p_rover) /* add movable object function, *number=aliens, *number_2=rovers*/ { int object, t, f; char alien; char rover; printf("This is the add movable object function\n");
printf("how many aliens would you like?\n"); scanf("%d",&t); /*user input of amount of aliens*/ number->amount=t; /*saving the number in structure map-amount*/ printf("how many rovers would you like?\n"); scanf("%d",&f); /*user input of amount of rovers*/ number_2->amount=f; /*saving the number in structure map-amount*/ show_map(number, number_2, rsny, rsld, mnt, dst, vshr, vgnt, mtre, wshl, wdp, aagr, anon, rvr, pstn, p_rover); /*go to show map function with the number of aliens and number of rovers*/ } void successor() /*this functions askes the user for the location and then were they want to go from there, printing out that location*/ { struct map location; int menu; int ncol,nrow; /*we can change the n value depends how large the map you need.*/ ncol=8; nrow=8; printf("This is the successor function\n"); printf("Enter the \nx=\ty=\n"); scanf("%d%d",&location.x,&location.y); /*saving location in map location x and y*/ printf("Enter operator choice\n"); printf("1=left\n"); printf("2=right\n"); printf("3=upwards\n"); printf("4=down\n"); printf("5=quit\n"); scanf("%d",&menu); switch(menu) /*menu switch to show the position above, underneith, and next to the rover*/ { case 1:location.x=location.x-1; printf("The position is [%d,%d]\n",location.x,location.y); break; case 2:location.x=location.x+1; printf("The position is [%d,%d]\n",location.x,location.y); break; case 3:location.y=location.y-1; printf("The position is [%d,%d]\n",location.x,location.y); break; case 4:location.y=location.y+1; printf("The position is [%d,%d]\n",location.x,location.y); break; case 5: printf("*****back to program*****\n"); /* Quits the program and prints out the
分享到:
收藏