C语言游戏源码
纯C语言写地一个小型游戏源代码
/* A simple game*//*CopyRight: Guanlin*/#include<stdio.h>#include<stdlib.h>#include<string.h>#include<time.h>#include<conio.h>#include<process.h>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 or2:\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;i<number->amount;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;i<number_2->amount;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 =={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 innothing allocated in [i][j]*/printf("|%4s",f_map[i][j].data);elseprintf("| "); /*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 message */ break;default:printf("*****Please enter an integer from 1-5*****\n"); /* A default option if the user enters an incorrect value */break;}printf("\n");}void help(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) /*help function*/{int i;char mov_obj;printf("++This is the help function++\n");printf("| 1-successor |\n");printf("| 2-add movable obj |\n");printf("| 3-remove moveabla obj |\n\n");printf("\t* * *\n");printf("\t Index: \n\n");printf("\t Sandy Rock: *\n");printf("\t Solid Rock: o\n");printf("\t Water:\262\262\262\262 \n");printf("\t Shallow water: \260\260\260\260\n");printf("\t Mountain: ^^^^\n");printf("\t Dust: ....\n");printf("\t* sher valley: VVVV *\n");printf("\t shallow valey: vvvv\n");printf("\t power station: \025\n");printf("\t alien: *_*\n");printf("\t other rovers: #\n");printf("\t* * *\n");scanf("%d",&i);switch(i) /*switch menu to go to the successor function, or the add/remove function*/ {case 1:successor();break;case 2:add_obj(number, number_2, rsny, rsld, mnt, dst, vshr,vgnt, mtre, wshl, wdp, aagr, anon, rvr, pstn, p_rover);break;case 3:add_obj(number, number_2, rsny, rsld, mnt, dst, vshr,vgnt, mtre, wshl, wdp, aagr, anon, rvr, pstn, p_rover);break;default:printf("choose a number from the list\n");}}void direction(int input, 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){switch(input){case 119: /*w = up*/if(p_rover->location[0] > 0){p_rover->location[0]--;p_rover->pwr--;}else p_rover->location[0]=7;break;case 115: /*s = down*/if(p_rover->location[0] < 7){p_rover->location[0]++;p_rover->pwr--;}else p_rover->location[0]=0;break;case 97: /*a = left*/if(p_rover->location[1] > 0){p_rover->location[1]--;p_rover->pwr--;}else p_rover->location[1]=7;break;case 100: /*d = right*/if(p_rover->location[1] < 7){p_rover->location[1]++;p_rover->pwr--;}else p_rover->location[1]=0;break;default:printf("Invalid operator!\n\n");break;}}int control(int input){input = _getch();return input;}void main(void){int menu;int quit = 0;int input = 0;struct object_fix rsny, rsld, mnt, dst, vshr, vgnt, mtre, wshl, wdp;struct object_move aagr, anon, rvr, pstn;struct rover p_rover;struct map number, number_2;number.amount = 0; /* setting the initial amount of aliens as 0*/number_2.amount = 0;strcpy(,"Sandy Rock");strcpy(rsny.id,"RSNY");strcpy(rsny.desc,"A rock made of softer material that can be destroyed or collected"); strcpy(rsny.im,"*");strcpy(,"Solid Rock");strcpy(rsld.id,"RSLD");strcpy(rsld.desc,"A rock made of hard material that cannot be destroyed, but can be collected or avoided");strcpy(rsld.im,"O");strcpy(,"Mountain");strcpy(mnt.id,"MNT");strcpy(mnt.desc,"A large obstacle made of different materials that can only be avoided"); strcpy(mnt.im,"^^^^");strcpy(,"Dust");strcpy(dst.id,"DST");strcpy(dst.desc,"Small particles of sand, rock, etc. that may reduce visibility or slow down the vehicle, can be driven through but has extra power consumption");strcpy(dst.im,"....");strcpy(,"Sheer Valley");strcpy(vshr.id,"VSHR");strcpy(vshr.desc,"A depression in the land with sheer sides, it cannot be driven through and must be avoided");strcpy(vshr.im,"VVVV");strcpy(,"Gentle Valley");strcpy(vgnt.id,"VGNT");strcpy(vgnt.desc,"A depression in the land with gentle sides, it can be driven through with extra power consumption or can be avoided");strcpy(vgnt.im,"vvvv");strcpy(,"'Martian' Tree");strcpy(mtre.id,"MTRE");strcpy(mtre.desc,"A woody plant native to Mars, it may have the same characteristics as an 'Earth' tree");strcpy(mtre.im,"\330");strcpy(,"Shallow Water");strcpy(wshl.id,"WSHL");strcpy(wshl.desc,"A shallow body of water that can be driven through, can be in the formof a small puddle or a large lake");strcpy(wshl.im,"\260\260\260\260");strcpy(,"Deeper Water");strcpy(wdp.id,"WDP");strcpy(wdp.desc,"A deeper body of water that cannot be driven through, commonly in the form of a large lake or river");strcpy(wdp.im,"\262\262\262\262");strcpy(,"Aggressive Alien");strcpy(aagr.id,"AAGR");strcpy(aagr.desc,"An alien creature that will move upon its own accord, may become aggressive on contact");strcpy(aagr.im,"*_*");strcpy(,"Non-Aggressive Alien");strcpy(anon.id,"ANON");strcpy(anon.desc,"An alien creature that will move upon its own accord, will not become aggressive on contact");strcpy(anon.im,"*_*");strcpy(,"Rover");strcpy(rvr.id,"RVR");strcpy(rvr.desc,"A Mars Rover vehicle to transport around the map, it can destroy, collect or avoid objects. It has a power and object storage");strcpy(rvr.im,"#");strcpy(,"Power Station");strcpy(pstn.id,"PSTN");strcpy(pstn.desc,"A movable power station that will restore power to the Mars Rovers"); strcpy(pstn.im,"\025");strcpy(p_,"User Mars Rover");strcpy(p_rover.id,"URVR");strcpy(p_rover.desc,"A Mars vehicle that is controlled by the user. Explores the map collecting, destroying and avoiding objects");strcpy(p_rover.im,"<^^>");p_rover.pwr = 100;p_rover.strg = 0;p_rover.location[0] = 7;p_rover.location[1] = 0; /****************************************************/printf("*******************************START********************************\n\n");do{ /* Object will be gained from mapper */printf("+++++++++++++++++++++++++++++++MENU+++++++++++++++++++++++++++++++++\n");printf("Please choose one of the following option:\n");printf("1 - Start\n");printf("2 - Controls\n");printf("3 - Show Map\n");printf("4 - Help Menu\n");printf("5 - Quit\n\n");printf("--------------->");scanf("%d", &menu);switch (menu){case 1:show_map(&number, &number_2, &rsny, &rsld, &mnt, &dst, &vshr,&vgnt, &mtre, &wshl, &wdp, &aagr, &anon, &rvr, &pstn, &p_rover);while (quit != 1 ){input = control(input);if (input == 27)quit = 1;else{if(p_rover.pwr>0){direction(input, &number, &number_2, &rsny, &rsld, &mnt, &dst, &vshr,&vgnt, &mtre, &wshl, &wdp, &aagr, &anon, &rvr, &pstn, &p_rover);show_map(&number, &number_2, &rsny, &rsld, &mnt, &dst, &vshr,&vgnt, &mtre, &wshl, &wdp, &aagr, &anon, &rvr, &pstn, &p_rover);printf("Rover stats: \nPower: %d%% \nStorage: %d%%\n", p_rover.pwr, p_rover.strg);}else{printf("*******************END GAME********************\n\n"); // ends game when charge = 0.break;}}}break;case 2:quit = 0;printf("\n");printf("* * *\n");printf(" Controls \n"); // menu for the keys used on the keyboard to move the rover in different directions.printf(" Up : W \n");printf(" Down : S \n");printf("* Left : A * \n");printf("* Right : D * \n");printf(" choose : Enter \n");printf("* * *\n");break;case 3:quit = 0;show_map(&number, &number_2, &rsny, &rsld, &mnt, &dst, &vshr, &vgnt, &mtre, &wshl, &wdp, &aagr, &anon, &rvr, &pstn, &p_rover); break;case 4:quit = 0;help(&number, &number_2, &rsny, &rsld, &mnt, &dst, &vshr,&vgnt, &mtre, &wshl, &wdp, &aagr, &anon, &rvr, &pstn, &p_rover); break;case 5:printf("*********************END***********************\n"); break;}}while(menu!=5);printf("\n -----Please press any key to exit game-----\n"); getch();}。
好玩的c语言代码
好玩的c语言代码以下是一些好玩的 C 语言代码:1. Hello World#include <stdio.h>int main() {printf("Hello World!\n");return 0;}2. 乘法表#include <stdio.h>int main() {int i, j;for(i = 1; i <= 9; i++) {for(j = 1; j <= i; j++) {printf("%d x %d = %d ",j,i,i*j);}printf("\n");}return 0;}3. 猜数字游戏#include <stdio.h>#include <stdlib.h>#include <time.h>int main() {int number, guess, count = 0;srand(time(NULL));number = rand() % 100 + 1; // 随机一个 1~100 的数printf("猜一个 1~100 的数字:\n");do {scanf("%d",&guess);count++;if(guess > number) {printf("太大了,请继续猜:\n");} else if(guess < number) {printf("太小了,请继续猜:\n");} else {printf("恭喜你,猜对了!你一共猜了 %d 次。
\n",count); }} while(guess != number);return 0;}4. 计算圆的周长和面积#include <stdio.h>#define PI 3.1415926535int main() {float r, circumference, area;printf("请输入圆的半径:\n");scanf("%f",&r);circumference = 2 * PI * r; // 周长公式area = PI * r * r; // 面积公式printf("圆的周长:%f\n",circumference);printf("圆的面积:%f\n",area);return 0;}5. 温度转换#include <stdio.h>int main() {float fahrenheit, celsius;printf("请输入华氏度:\n");scanf("%f",&fahrenheit);celsius = (fahrenheit - 32) * 5 / 9; // 华氏度转摄氏度公式 printf("摄氏度:%.2f\n",celsius);return 0;}。
俄罗斯方块c语言源代码
俄罗斯方块c语言源代码俄罗斯方块游戏是一款非常受欢迎的游戏,使用C语言编写源代码实现其功能。
下面是俄罗斯方块游戏的C语言源代码:1. 创建窗口函数: // 创建窗口函数 void CreateWindow(int width, int height) { // 使用SDL库创建窗口 SDL_Init(SDL_INIT_EVERYTHING); SDL_Window *window = SDL_CreateWindow("Tetris",SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,width, height, 0); // 设置刷新时间SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1"); }2. 创建游戏函数: // 创建游戏函数 void CreateGame() { // 设置随机数种子srand((unsigned int)time(NULL)); // 加载游戏资源 LoadResources(); // 初始化游戏数据InitGameData(); // 初始化游戏界面InitGameUI(); // 开始游戏循环 GameLoop(); // 清理游戏资源 CleanupGame(); }3. 绘图函数: // 绘图函数 void Draw(int x, inty, Color color) { // 使用SDL库在指定位置绘制指定颜色的矩形 SDL_Rect rect; rect.x = x;rect.y = y; rect.w = BLOCK_SIZE; rect.h = BLOCK_SIZE; SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);SDL_RenderFillRect(renderer, &rect); }。
纯C语言写的一个小型游戏 源代码
/* A simple game*//*CopyRight: Guanlin*/ #include<stdio.h>#include<stdlib.h>#include<string.h>#include<time.h>#include<conio.h>#include<process.h> ?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;i<number->amount;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;i<number_2->amount;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);elseprintf("| "); /*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 message */ break;default:printf("*****Please enter an integer from 1-5*****\n"); /* A default option if the user enters an incorrect value */break;}printf("\n");}?void help(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) /*help function*/{int i;char mov_obj;printf("++This is the help function++\n");printf("| 1-successor |\n");printf("| 2-add movable obj |\n");printf("| 3-remove moveabla obj |\n\n");printf("\t* * *\n");printf("\t Index: \n\n");printf("\t Sandy Rock: *\n");printf("\t Solid Rock: o\n");printf("\t Water:\262\262\262\262 \n");printf("\t Shallow water: \260\260\260\260\n");printf("\t Mountain: ^^^^\n");printf("\t Dust: ....\n");printf("\t* sher valley: VVVV *\n");printf("\t shallow valey: vvvv\n");printf("\t power station: \025\n");printf("\t alien: *_*\n");printf("\t other rovers: #\n");printf("\t* * *\n");scanf("%d",&i);switch(i) /*switch menu to go to the successor function, or the add/remove function*/{case 1:successor();break;case 2:add_obj(number, number_2, rsny, rsld, mnt, dst, vshr,vgnt, mtre, wshl, wdp, aagr, anon, rvr, pstn, p_rover);break;case 3:add_obj(number, number_2, rsny, rsld, mnt, dst, vshr,vgnt, mtre, wshl, wdp, aagr, anon, rvr, pstn, p_rover);break;default:printf("choose a number from the list\n");}}void direction(int input, 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){switch(input){case 119: /*w = up*/if(p_rover->location[0] > 0)p_rover->location[0]--;p_rover->pwr--;}else p_rover->location[0]=7; break;case 115: /*s = down*/if(p_rover->location[0] < 7) {p_rover->location[0]++;p_rover->pwr--;}else p_rover->location[0]=0; break;case 97: /*a = left*/if(p_rover->location[1] > 0) {p_rover->location[1]--;p_rover->pwr--;}else p_rover->location[1]=7; break;case 100: /*d = right*/if(p_rover->location[1] < 7) {p_rover->location[1]++;p_rover->pwr--;}else p_rover->location[1]=0; break;default:printf("Invalid operator!\n\n"); break;}int control(int input){input = _getch();return input;}?void main(void){int menu;int quit = 0;int input = 0;struct object_fix rsny, rsld, mnt, dst, vshr, vgnt, mtre, wshl, wdp;struct object_move aagr, anon, rvr, pstn;struct rover p_rover;struct map number, number_2;number.amount = 0; /* setting the initial amount of aliens as 0*/number_2.amount = 0;strcpy(,"Sandy Rock");strcpy(rsny.id,"RSNY");strcpy(rsny.desc,"A rock made of softer material that can be destroyed or collected");strcpy(rsny.im,"*");strcpy(,"Solid Rock");strcpy(rsld.id,"RSLD");strcpy(rsld.desc,"A rock made of hard material that cannot be destroyed, but can be collected or avoided");strcpy(rsld.im,"O");strcpy(,"Mountain");strcpy(mnt.id,"MNT");strcpy(mnt.desc,"A large obstacle made of different materials that can only be avoided");strcpy(mnt.im,"^^^^");strcpy(,"Dust");strcpy(dst.id,"DST");strcpy(dst.desc,"Small particles of sand, rock, etc. that may reduce visibility or slow down the vehicle, can be driven through but has extra power consumption");strcpy(dst.im,"....");strcpy(,"Sheer Valley");strcpy(vshr.id,"VSHR");strcpy(vshr.desc,"A depression in the land with sheer sides, it cannot be driven through and must be avoided");strcpy(vshr.im,"VVVV");strcpy(,"Gentle Valley");strcpy(vgnt.id,"VGNT");strcpy(vgnt.desc,"A depression in the land with gentle sides, it can be driven through with extra power consumption or can be avoided");strcpy(vgnt.im,"vvvv");strcpy(,"'Martian' Tree");strcpy(mtre.id,"MTRE");strcpy(mtre.desc,"A woody plant native to Mars, it may have the same characteristics as an 'Earth' tree");strcpy(mtre.im,"\330");strcpy(,"Shallow Water");strcpy(wshl.id,"WSHL");strcpy(wshl.desc,"A shallow body of water that can be driven through, can be in the form of a small puddle or a large lake");strcpy(wshl.im,"\260\260\260\260");strcpy(,"Deeper Water");strcpy(wdp.id,"WDP");strcpy(wdp.desc,"A deeper body of water that cannot be driven through, commonly in the form of a large lake or river");strcpy(wdp.im,"\262\262\262\262");strcpy(,"Aggressive Alien");strcpy(aagr.id,"AAGR");strcpy(aagr.desc,"An alien creature that will move upon its own accord, may become aggressive on contact");strcpy(aagr.im,"*_*");strcpy(,"Non-Aggressive Alien");strcpy(anon.id,"ANON");strcpy(anon.desc,"An alien creature that will move upon its own accord, will not become aggressive on contact");strcpy(anon.im,"*_*");strcpy(,"Rover");strcpy(rvr.id,"RVR");strcpy(rvr.desc,"A Mars Rover vehicle to transport around the map, it can destroy, collect or avoid objects. It has a power and object storage");strcpy(rvr.im,"#");strcpy(,"Power Station");strcpy(pstn.id,"PSTN");strcpy(pstn.desc,"A movable power station that will restore power to the Mars Rovers");strcpy(pstn.im,"\025");strcpy(p_,"User Mars Rover");strcpy(p_rover.id,"URVR");strcpy(p_rover.desc,"A Mars vehicle that is controlled by the user. Explores the map collecting, destroying and avoiding objects");strcpy(p_rover.im,"<^^>");p_rover.pwr = 100;p_rover.strg = 0;p_rover.location[0] = 7;p_rover.location[1] = 0; /****************************************************/printf("*******************************START********************************\n\n"); do{ /* Object will be gained from mapper */printf("+++++++++++++++++++++++++++++++MENU+++++++++++++++++++++++ ++++++++++\n");printf("Please choose one of the following option:\n");printf("1 - Start\n");printf("2 - Controls\n");printf("3 - Show Map\n");printf("4 - Help Menu\n");printf("5 - Quit\n\n");printf("--------------->");scanf("%d", &menu);switch (menu){case 1:show_map(&number, &number_2, &rsny, &rsld, &mnt, &dst, &vshr,&vgnt, &mtre, &wshl, &wdp, &aagr, &anon, &rvr, &pstn, &p_rover);while (quit != 1 ){input = control(input);if (input == 27)quit = 1;else{if(p_rover.pwr>0){direction(input, &number, &number_2, &rsny, &rsld, &mnt, &dst, &vshr,&vgnt, &mtre, &wshl, &wdp, &aagr, &anon, &rvr, &pstn, &p_rover);show_map(&number, &number_2, &rsny, &rsld, &mnt, &dst, &vshr,&vgnt, &mtre, &wshl, &wdp, &aagr, &anon, &rvr, &pstn, &p_rover);printf("Rover stats: \nPower: %d%% \nStorage: %d%%\n", p_rover.pwr, p_rover.strg);}else{printf("*******************END GAME********************\n\n"); // ends game when charge = 0.}}}break;case 2:quit = 0;printf("\n");printf("* * *\n");printf(" Controls \n"); // menu for the keys used on the keyboard to move the rover in different directions.printf(" Up : W \n");printf(" Down : S \n");printf("* Left : A * \n");printf("* Right : D * \n");printf(" choose : Enter \n");printf("* * *\n");break;case 3:quit = 0;show_map(&number, &number_2, &rsny, &rsld, &mnt, &dst, &vshr,&vgnt, &mtre, &wshl, &wdp, &aagr, &anon, &rvr, &pstn, &p_rover);break;case 4:quit = 0;help(&number, &number_2, &rsny, &rsld, &mnt, &dst, &vshr,&vgnt, &mtre, &wshl, &wdp, &aagr, &anon, &rvr, &pstn, &p_rover);break;printf("*********************END***********************\n"); break;}}while(menu!=5);printf("\n -----Please press any key to exit game-----\n");getch();}。
C语言小游戏源代码《打砖块》
C语言小游戏源代码《打砖块》#include “graphics.h“#include "stdio.h"#include "conio.h" /*所需的头文件*/int on; /*声明具有开关作用的全局变量*/static int score; /*声明静态的记分器变量*//* 定义开始界面函数*/int open(){setviewport(100,100,500,380,1); /*设置图形窗口区域*/ setcolor(4); /*设置作图色*/rectangle(0,0,399,279); /*以矩形填充所设的图形窗口区域*/ setfillstyle(SOLID_FILL,7); /*设置填充方式*/floodfill(50,50,4); /*设置填充范围*/setcolor(8);settextstyle(0,0,9); /*文本字体设置*/outtextxy(90,80,"BALL"); /*输出文本内容*/settextstyle(0,0,1);outtextxy(110,180,"version 1.0");outtextxy(110,190,"made by ddt");setcolor(128);settextstyle(0,0,1);outtextxy(120,240,"Press any key to continue......");}/*定义退出界面函数*/int quitwindow(){char s; /*声明用于存放字符串的数组*/setviewport(100,150,540,420,1);setcolor(YELLOW);rectangle(0,0,439,279);setfillstyle(SOLID_FILL,7);floodfill(50,50,14);setcolor(12);settextstyle(0,0,8);outtextxy(120,80,"End");settextstyle(0,0,2);outtextxy(120,200,"quit? Y/N");sprintf(s,"Your score is:%d",score);/*格式化输出记分器的值*/ outtextxy(120,180,s);on=1; /*初始化开关变量*/}/*主函数*/main(){int gdriver,gmode;gdriver=DETECT; /*设置图形适配器*/gmode=VGA; /*设置图形模式*/registerbgidriver(EGAVGA_driver); /*建立独立图形运行程序*/initgraph(gdriver,gmode,""); /*图形系统初试化*/setbkcolor(14);open(); /*调用开始界面函数*/getch(); /*暂停*/while(1) /*此大循环体控制游戏的反复重新进行*/{intdriver,mode,l=320,t=400,r,a,b,dl=5,n,x=200,y=400,r1=10,dx=-2,dy=-2;/*初始化小球相关参数*/int left,top,right,bottom,i,j,k,off=1,m,num;/*方砖阵列相关参数*/static int pp;static int phrase; /*一系列起开关作用的变量*/int oop=15;pp=1;score=0;driver=DETECT;mode=VGA;registerbgidriver(EGAVGA_driver);initgraph(driver,mode,"");setbkcolor(10);cleardevice(); /*图形状态下清屏*/clearviewport(); /*清除现行图形窗口内容*/b=t+6;r=l+60;setcolor(1);rectangle(0,0,639,479);setcolor(4);rectangle(l,t,r,b);setfillstyle(SOLID_FILL,1);floodfill(l+2,t+2,4);for(i=0,k=0;ii++) /*此循环绘制方砖阵列*/{top[i]=k;bottom[i]=top[i]+20;k=k+21;oop--;for(j=0,m=0;jj++){left[j]=m;right[j]=left[j]+80;m=m+81;setcolor(4);rectangle(left[j],top[i],right[j],bottom[i]); setfillstyle(SOLID_FILL,j+oop);floodfill(left[j]+1,top[i]+1,4);num[i][j]=pp++;}}while(1) /*此循环控制整个动画*/{while(!kbhit()){x=x+dx; /*小球运动的圆心变量控制*/ y=y+dy;if(x+r1r||x+r1r){ phrase=0;}if((x-r1=r||x+r1=r)x+r1=l){if(yt)phrase=1;if(y+r1=tphrase==1){dy=-dy;y=t-1-r1;}}if(off==0)continue;for(i=0;ii++) /*此循环用于判断、控制方砖阵列的撞击、擦除*/for(j=0;jj++){if((x+r1=right[j]x+r1=left[j])||(x-r1=right[j]x-r1=left[j])){if(( y-r1top[i]y-r1=bottom[i])||(y+r1=top[i]y+r1=bottom[i] )) {if(num[i][j]==0){continue; }setcolor(10);rectangle(left[j],top[i],right[j],bottom[i]);setfillstyle(SOLID_FILL,10);floodfill(left[j]+1,top[i]+1,10);dy=-dy;num[i][j]=0;score=score+10;printf(“%d\b\b\b",score);}}if((y+r1=top[i]y+r1=bottom[i])||(y-r1=top[i]y-r1=bottom[i])) {if((x+r1=left[j]x+r1right[j])||(x-r1=right[j]x-r1left[j])){if(num[i][j]==0){ continue;}setcolor(10);rectangle(left[j],top[i],right[j],bottom[i]); setfillstyle(SOLID_FILL,10);floodfill(left[j]+1,top[i]+1,10);dx=-dx;num[i][j]=0;score=score+10;printf("%d\b\b\b",score);}}}if(x+r1639) /*控制小球的弹射范围*/ {dx=-dx;x=638-r1;}if(x=r1){dx=-dx;x=r1+1;}if(y+r1=479){off=0;quitwindow();break;}if(y=r1){dy=-dy;y=r1+1;}if(score==560){off=0;quitwindow();break;}setcolor(6);circle(x,y,r1);setfillstyle(SOLID_FILL,14);floodfill(x,y,6);delay(1000);setcolor(10);circle(x,y,r1);setfillstyle(SOLID_FILL,10);floodfill(x,y,10);}a=getch();setcolor(10);rectangle(l,t,r,b);setfillstyle(SOLID_FILL,10);floodfill(l+2,t+2,10);if(a==77l=565)/*键盘控制设定*/{dl=20;l=l+dl;}if(a==75l=15){dl=-20;l=l+dl;}if(a=='y'on==1)break;if(a=='n'on==1)break;if(a==27){quitwindow();off=0;}r=l+60;setcolor(4);rectangle(l,t,r,b);setfillstyle(SOLID_FILL,1);floodfill(l+5,t+5,4);delay(100);}if(a=='y'on==1) /*是否退出游戏*/ {break;}if(a=='n'on==1){ continue;} } closegraph(); }。
C语言小游戏源代码
不管怎么样,九格游戏最后的结果只有两个:
123 | 123
456 | 456
78 | 87 (证明从略)
而要两两交换而始终有解的话,(从原序列开始)必须:相邻交换的次数为偶
但我们用一种更有效的方法:
每一个总与其下第二个交换.
第7,8个与0,1交换,只要交换次数多,仍可获得相同的效果.
}
}
int isSuccess(){ /*判断是否游戏*/
int i,ret=1;
for(i=0;i<8;i++)
ret=ret&&(num[i]==(i+1));
return ret;
}
int GetTheNull(){ /*获得空格的位置*/
if (num[j+i*3]!=0)
printf("\324\315\315\315\315\274");
else
printf(" ");
}
printf("\n");
把光标移动到屏幕的x(1~80),y(1~25/50)处*/
/*和clrscr():清屏*/
int num[]={1,2,3,4,5,6,7,8,0}; /*方块的数字*/
main(){
char key=0; /*键盘码*/
int pos; /*九格中,空格的位置*/
a=random(8); /*产生随机数*/
b=(a+2)%8; /*得到下第二个的数组下标*/
change(a,b); /*交换*/
}
简单的迷宫小游戏C语言程序源代码
简单的迷宫小游戏C语言程序源代码#include <stdio.h>#include <conio.h>#include <windows.h>#include <time.h>#define Height 31 //迷宫的高度,必须为奇数#define Width 25 //迷宫的宽度,必须为奇数#define Wall 1#define Road 0#define Start 2#define End 3#define Esc 5#define Up 1#define Down 2#define Left 3#define Right 4int map[Height+2][Width+2];void gotoxy(int x,int y) //移动坐标{COORD coord;coord.X=x;coord.Y=y;SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HA NDLE ), coord );}void hidden()//隐藏光标{HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);CONSOLE_CURSOR_INFO cci;GetConsoleCursorInfo(hOut,&cci); cci.bVisible=0;//赋1为显示,赋0为隐藏SetConsoleCursorInfo(hOut,&cci);}void create(int x,int y) //随机生成迷宫{int c[4][2]={0,1,1,0,0,-1,-1,0}; //四个方向int i,j,t;//将方向打乱for(i=0;i<4;i++){j=rand()%4;t=c[i][0];c[i][0]=c[j][0];c[j][0]=t;t=c[i][1];c[i][1]=c[j][1];c[j][1]=t;}map[x][y]=Road;for(i=0;i<4;i++)if(map[x+2*c[i][0]][y+2*c[i][1]]==Wall) {map[x+c[i][0]][y+c[i][1]]=Road; create(x+2*c[i][0],y+2*c[i][1]);}}int get_key() //接收按键{char c;while(c=getch()){if(c==27) return Esc; //Escif(c!=-32)continue;c=getch();if(c==72) return Up; //上if(c==80) return Down; //下if(c==75) return Left; //左if(c==77) return Right; //右}return 0;}void paint(int x,int y) //画迷宫{gotoxy(2*y-2,x-1);switch(map[x][y]){case Start:printf("入");break; //画入口case End:printf("出");break; //画出口case Wall:printf("※");break; //画墙case Road:printf(" ");break; //画路}}void game(){int x=2,y=1; //玩家当前位置,刚开始在入口处int c; //用来接收按键while(1){gotoxy(2*y-2,x-1);printf("☆"); //画出玩家当前位置if(map[x][y]==End) //判断是否到达出口{gotoxy(30,24);printf("到达终点,按任意键结束"); getch();break;}c=get_key();if(c==Esc){gotoxy(0,24);break;}switch(c){case Up: //向上走if(map[x-1][y]!=Wall){paint(x,y);x--;}break;case Down: //向下走if(map[x+1][y]!=Wall){paint(x,y);x++;}break;case Left: //向左走if(map[x][y-1]!=Wall){paint(x,y);y--;}break;case Right: //向右走if(map[x][y+1]!=Wall){paint(x,y);y++;}break;}}}int main(){int i,j;srand((unsigned)time(NULL)); //初始化随即种子hidden(); //隐藏光标for(i=0;i<=Height+1;i++)for(j=0;j<=Width+1;j++)if(i==0||i==Height+1||j==0||j==Width+1) //初始化迷宫map[i][j]=Road;else map[i][j]=Wall;create(2*(rand()%(Height/2)+1),2*(rand()%(Width/2)+1)); //从随机一个点开始生成迷宫,该点行列都为偶数for(i=0;i<=Height+1;i++) //边界处理{map[i][0]=Wall;map[i][Width+1]=Wall;}for(j=0;j<=Width+1;j++) //边界处理{map[0][j]=Wall;map[Height+1][j]=Wall;}map[2][1]=Start; //给定入口map[Height-1][Width]=End; //给定出口for(i=1;i<=Height;i++)for(j=1;j<=Width;j++) //画出迷宫paint(i,j);game(); //开始游戏getch();return 0;}。
C语言游戏代码(里面揽括扫雷_俄罗斯方块_推箱子_五子棋_贪吃蛇)
五子棋#include <stdio.h>#include <bios.h>#include <ctype.h>#include <conio.h>#include <dos.h>#define CROSSRU 0xbf /*右上角点*/#define CROSSLU 0xda /*左上角点*/#define CROSSLD 0xc0 /*左下角点*/#define CROSSRD 0xd9 /*右下角点*/#define CROSSL 0xc3 /*左边*/#define CROSSR 0xb4 /*右边*/#define CROSSU 0xc2 /*上边*/#define CROSSD 0xc1 /*下边*/#define CROSS 0xc5 /*十字交叉点*//*定义棋盘左上角点在屏幕上的位置*/#define MAPXOFT 5#define MAPYOFT 2/*定义1号玩家的操作键键码*/#define PLAY1UP 0x1157/*上移--'W'*/#define PLAY1DOWN 0x1f53/*下移--'S'*/#define PLAY1LEFT 0x1e41/*左移--'A'*/#define PLAY1RIGHT 0x2044/*右移--'D'*/#define PLAY1DO 0x3920/*落子--空格键*//*定义2号玩家的操作键键码*/#define PLAY2UP 0x4800/*上移--方向键up*/#define PLAY2DOWN 0x5000/*下移--方向键down*/ #define PLAY2LEFT 0x4b00/*左移--方向键left*/#define PLAY2RIGHT 0x4d00/*右移--方向键right*/ #define PLAY2DO 0x1c0d/*落子--回车键Enter*//*若想在游戏中途退出, 可按Esc 键*/#define ESCAPE 0x011b/*定义棋盘上交叉点的状态, 即该点有无棋子*//*若有棋子, 还应能指出是哪个玩家的棋子*/#define CHESSNULL 0 /*没有棋子*/#define CHESS1 'O'/*一号玩家的棋子*/#define CHESS2 'X'/*二号玩家的棋子*//*定义按键类别*/#define KEYEXIT 0/*退出键*/#define KEYFALLCHESS 1/*落子键*/#define KEYMOVECURSOR 2/*光标移动键*/#define KEYINV ALID 3/*无效键*//*定义符号常量: 真, 假--- 真为1, 假为0 */#define TRUE 1#define FALSE 0/**********************************************************/ /* 定义数据结构*//*棋盘交叉点坐标的数据结构*/struct point{int x,y;};/**********************************************************/ /*自定义函数原型说明*/void Init(void);int GetKey(void);int CheckKey(int press);int ChangeOrder(void);int ChessGo(int Order,struct point Cursor);void DoError(void);void DoOK(void);void DoWin(int Order);void MoveCursor(int Order,int press);void DrawCross(int x,int y);void DrawMap(void);int JudgeWin(int Order,struct point Cursor);int JudgeWinLine(int Order,struct point Cursor,int direction);void ShowOrderMsg(int Order);void EndGame(void);/**********************************************************//**********************************************************/ /* 定义全局变量*/int gPlayOrder; /*指示当前行棋方*/struct point gCursor; /*光标在棋盘上的位置*/char gChessBoard[19][19];/*用于记录棋盘上各点的状态*//**********************************************************//**********************************************************/ /*主函数*/void main(){int press;int bOutWhile=FALSE;/*退出循环标志*/printf("Welcome ");Init();/*初始化图象,数据*/while(1){press=GetKey();/*获取用户的按键值*/switch(CheckKey(press))/*判断按键类别*/{/*是退出键*/case KEYEXIT:clrscr();/*清屏*/bOutWhile = TRUE;break;/*是落子键*/case KEYFALLCHESS:if(ChessGo(gPlayOrder,gCursor)==FALSE)/*走棋*/DoError();/*落子错误*/else{DoOK();/*落子正确*//*如果当前行棋方赢棋*/if(JudgeWin(gPlayOrder,gCursor)==TRUE){DoWin(gPlayOrder);bOutWhile = TRUE;/*退出循环标志置为真*/}/*否则*/else/*交换行棋方*/ChangeOrder();ShowOrderMsg(gPlayOrder);}break;/*是光标移动键*/case KEYMOVECURSOR:MoveCursor(gPlayOrder,press);break;/*是无效键*/case KEYINV ALID:break;}if(bOutWhile==TRUE)break;}/*游戏结束*/EndGame();}/**********************************************************//*界面初始化,数据初始化*/void Init(void){int i,j;char *Msg[]={"Player1 key:"," UP----w"," DOWN--s"," LEFT--a"," RIGHT-d"," DO----space","","Player2 key:"," UP----up"," DOWN--down"," LEFT--left"," RIGHT-right"," DO----ENTER","","exit game:"," ESC",NULL,/* 先手方为1号玩家*/gPlayOrder = CHESS1;/* 棋盘数据清零, 即棋盘上各点开始的时候都没有棋子*/ for(i=0;i<19;i++)for(j=0;j<19;j++)gChessBoard[i][j]=CHESSNULL;/*光标初始位置*/gCursor.x=gCursor.y=0;/*画棋盘*/textmode(C40);DrawMap();/*显示操作键说明*/i=0;textcolor(BROWN);while(Msg[i]!=NULL){gotoxy(25,3+i);cputs(Msg[i]);i++;}/*显示当前行棋方*/ShowOrderMsg(gPlayOrder);/*光标移至棋盘的左上角点处*/gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT);}/*画棋盘*/void DrawMap(void){int i,j;clrscr();for(i=0;i<19;i++)for(j=0;j<19;j++)DrawCross(i,j);}/*画棋盘上的交叉点*/void DrawCross(int x,int y){gotoxy(x+MAPXOFT,y+MAPYOFT); /*交叉点上是一号玩家的棋子*/if(gChessBoard[x][y]==CHESS1) {textcolor(LIGHTBLUE);putch(CHESS1);return;}/*交叉点上是二号玩家的棋子*/if(gChessBoard[x][y]==CHESS2) {textcolor(LIGHTBLUE);putch(CHESS2);return;}textcolor(GREEN);/*左上角交叉点*/if(x==0&&y==0){putch(CROSSLU);return;}/*左下角交叉点*/if(x==0&&y==18){putch(CROSSLD);return;}/*右上角交叉点*/if(x==18&&y==0){putch(CROSSRU);return;}/*右下角交叉点*/if(x==18&&y==18){putch(CROSSRD); return;}/*左边界交叉点*/if(x==0){putch(CROSSL); return;}/*右边界交叉点*/if(x==18){putch(CROSSR); return;}/*上边界交叉点*/if(y==0){putch(CROSSU); return;}/*下边界交叉点*/if(y==18){putch(CROSSD); return;}/*棋盘中间的交叉点*/ putch(CROSS);}/*交换行棋方*/int ChangeOrder(void) {if(gPlayOrder==CHESS1) gPlayOrder=CHESS2; elsegPlayOrder=CHESS1;return(gPlayOrder);}/*获取按键值*/int GetKey(void){char lowbyte;int press;while (bioskey(1) == 0);/*如果用户没有按键,空循环*/press=bioskey(0);lowbyte=press&0xff;press=press&0xff00 + toupper(lowbyte); return(press);}/*落子错误处理*/void DoError(void){sound(1200);delay(50);nosound();}/*赢棋处理*/void DoWin(int Order){sound(1500);delay(100);sound(0); delay(50);sound(800); delay(100);sound(0); delay(50);sound(1500);delay(100);sound(0); delay(50);sound(800); delay(100);sound(0); delay(50);nosound();textcolor(RED+BLINK);gotoxy(25,20);if(Order==CHESS1)cputs("PLAYER1 WIN!");elsecputs("PLAYER2 WIN!");gotoxy(25,21);cputs(" \\<^+^>/");getch();}/*走棋*/int ChessGo(int Order,struct point Cursor){/*判断交叉点上有无棋子*/if(gChessBoard[Cursor.x][Cursor.y]==CHESSNULL){/*若没有棋子, 则可以落子*/gotoxy(Cursor.x+MAPXOFT,Cursor.y+MAPYOFT); textcolor(LIGHTBLUE);putch(Order);gotoxy(Cursor.x+MAPXOFT,Cursor.y+MAPYOFT); gChessBoard[Cursor.x][Cursor.y]=Order;return TRUE;}elsereturn FALSE;}/*判断当前行棋方落子后是否赢棋*/int JudgeWin(int Order,struct point Cursor){int i;for(i=0;i<4;i++)/*判断在指定方向上是否有连续5个行棋方的棋子*/if(JudgeWinLine(Order,Cursor,i))return TRUE;return FALSE;}/*判断在指定方向上是否有连续5个行棋方的棋子*/int JudgeWinLine(int Order,struct point Cursor,int direction) {int i;struct point pos,dpos;const int testnum = 5;int count;switch(direction){case 0:/*在水平方向*/pos.x=Cursor.x-(testnum-1);pos.y=Cursor.y;dpos.x=1;dpos.y=0;break;case 1:/*在垂直方向*/pos.x=Cursor.x;pos.y=Cursor.y-(testnum-1);dpos.x=0;dpos.y=1;break;case 2:/*在左下至右上的斜方向*/pos.x=Cursor.x-(testnum-1);pos.y=Cursor.y+(testnum-1);dpos.x=1;dpos.y=-1;break;case 3:/*在左上至右下的斜方向*/pos.x=Cursor.x-(testnum-1);pos.y=Cursor.y-(testnum-1);dpos.x=1;dpos.y=1;break;}count=0;for(i=0;i<testnum*2+1;i++)/*????????i<testnum*2-1*/ {if(pos.x>=0&&pos.x<=18&&pos.y>=0&&pos.y<=18) {if(gChessBoard[pos.x][pos.y]==Order){count++;if(count>=testnum)return TRUE;}elsecount=0;}pos.x+=dpos.x;pos.y+=dpos.y;}return FALSE;}/*移动光标*/void MoveCursor(int Order,int press) {switch(press){case PLAY1UP:if(Order==CHESS1&&gCursor.y>0) gCursor.y--;break;case PLAY1DOWN:if(Order==CHESS1&&gCursor.y<18) gCursor.y++;break;case PLAY1LEFT:if(Order==CHESS1&&gCursor.x>0) gCursor.x--;break;case PLAY1RIGHT:if(Order==CHESS1&&gCursor.x<18) gCursor.x++;break;case PLAY2UP:if(Order==CHESS2&&gCursor.y>0) gCursor.y--;break;case PLAY2DOWN:if(Order==CHESS2&&gCursor.y<18) gCursor.y++;break;case PLAY2LEFT:if(Order==CHESS2&&gCursor.x>0) gCursor.x--;break;case PLAY2RIGHT:if(Order==CHESS2&&gCursor.x<18) gCursor.x++;break;}gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT); }/*游戏结束处理*/void EndGame(void){textmode(C80);}/*显示当前行棋方*/void ShowOrderMsg(int Order){gotoxy(6,MAPYOFT+20);textcolor(LIGHTRED);if(Order==CHESS1)cputs("Player1 go!");elsecputs("Player2 go!");gotoxy(gCursor.x+MAPXOFT,gCursor.y+MAPYOFT); }/*落子正确处理*/void DoOK(void){sound(500);delay(70);sound(600);delay(50);sound(1000);delay(100);nosound();}/*检查用户的按键类别*/int CheckKey(int press){if(press==ESCAPE)return KEYEXIT;/*是退出键*/elseif( ( press==PLAY1DO && gPlayOrder==CHESS1) || ( press==PLAY2DO && gPlayOrder==CHESS2))return KEYFALLCHESS;/*是落子键*/elseif( press==PLAY1UP || press==PLAY1DOWN || press==PLAY1LEFT || press==PLAY1RIGHT || press==PLAY2UP || press==PLAY2DOWN ||press==PLAY2LEFT || press==PLAY2RIGHT)return KEYMOVECURSOR;/*是光标移动键*/elsereturn KEYINV ALID;/*按键无效*/}贪吃蛇#define N 200#include <graphics.h>#include <stdlib.h>#include <dos.h>#define LEFT 0x4b00#define RIGHT 0x4d00#define DOWN 0x5000#define UP 0x4800#define ESC 0x011bint i,key;int score=0;/*得分*/int gamespeed=50000;/*游戏速度自己调整*/ struct Food{int x;/*食物的横坐标*/int y;/*食物的纵坐标*/int yes;/*判断是否要出现食物的变量*/ }food;/*食物的结构体*/struct Snake{int x[N];int y[N];int node;/*蛇的节数*/int direction;/*蛇移动方向*/int life;/* 蛇的生命,0活着,1死亡*/}snake;void Init(void);/*图形驱动*/void Close(void);/*图形结束*/void DrawK(void);/*开始画面*/void GameOver(void);/*结束游戏*/void GamePlay(void);/*玩游戏具体过程*/void PrScore(void);/*输出成绩*//*主函数*/void main(void){Init();/*图形驱动*/DrawK();/*开始画面*/GamePlay();/*玩游戏具体过程*/Close();/*图形结束*/}/*图形驱动*/void Init(void){int gd=DETECT,gm;initgraph(&gd,&gm,"c:\\tc");cleardevice();}/*开始画面,左上角坐标为(50,40),右下角坐标为(610,460)的围墙*/ void DrawK(void){/*setbkcolor(LIGHTGREEN);*/setcolor(11);setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*设置线型*/for(i=50;i<=600;i+=10)/*画围墙*/{rectangle(i,40,i+10,49); /*上边*/rectangle(i,451,i+10,460);/*下边*/}for(i=40;i<=450;i+=10){rectangle(50,i,59,i+10); /*左边*/rectangle(601,i,610,i+10);/*右边*/}}/*玩游戏具体过程*/void GamePlay(void){randomize();/*随机数发生器*/food.yes=1;/*1表示需要出现新食物,0表示已经存在食物*/snake.life=0;/*活着*/snake.direction=1;/*方向往右*/snake.x[0]=100;snake.y[0]=100;/*蛇头*/snake.x[1]=110;snake.y[1]=100;snake.node=2;/*节数*/PrScore();/*输出得分*/while(1)/*可以重复玩游戏,压ESC键结束*/{while(!kbhit())/*在没有按键的情况下,蛇自己移动身体*/{if(food.yes==1)/*需要出现新食物*/{food.x=rand()%400+60;food.y=rand()%350+60;while(food.x%10!=0)/*食物随机出现后必须让食物能够在整格内,这样才可以让蛇吃到*/ food.x++;while(food.y%10!=0)food.y++;food.yes=0;/*画面上有食物了*/}if(food.yes==0)/*画面上有食物了就要显示*/{setcolor(GREEN);rectangle(food.x,food.y,food.x+10,food.y-10);}for(i=snake.node-1;i>0;i--)/*蛇的每个环节往前移动,也就是贪吃蛇的关键算法*/{snake.x[i]=snake.x[i-1];snake.y[i]=snake.y[i-1];}/*1,2,3,4表示右,左,上,下四个方向,通过这个判断来移动蛇头*/switch(snake.direction){case 1:snake.x[0]+=10;break;case 2: snake.x[0]-=10;break;case 3: snake.y[0]-=10;break;case 4: snake.y[0]+=10;break;}for(i=3;i<snake.node;i++)/*从蛇的第四节开始判断是否撞到自己了,因为蛇头为两节,第三节不可能拐过来*/{if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0]){GameOver();/*显示失败*/snake.life=1;break;}}if(snake.x[0]<55||snake.x[0]>595||snake.y[0]<55||snake.y[0]>455)/*蛇是否撞到墙壁*/{GameOver();/*本次游戏结束*/snake.life=1; /*蛇死*/}if(snake.life==1)/*以上两种判断以后,如果蛇死就跳出内循环,重新开始*/ break;if(snake.x[0]==food.x&&snake.y[0]==food.y)/*吃到食物以后*/{setcolor(0);/*把画面上的食物东西去掉*/rectangle(food.x,food.y,food.x+10,food.y-10);snake.x[snake.node]=-20;snake.y[snake.node]=-20;/*新的一节先放在看不见的位置,下次循环就取前一节的位置*/snake.node++;/*蛇的身体长一节*/food.yes=1;/*画面上需要出现新的食物*/score+=10;PrScore();/*输出新得分*/}setcolor(4);/*画出蛇*/for(i=0;i<snake.node;i++)rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);delay(gamespeed);setcolor(0);/*用黑色去除蛇的的最后一节*/rectangle(snake.x[snake.node-1],snake.y[snake.node-1],snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);} /*endwhile(!kbhit)*/if(snake.life==1)/*如果蛇死就跳出循环*/break;key=bioskey(0);/*接收按键*/if(key==ESC)/*按ESC键退出*/break;elseif(key==UP&&snake.direction!=4)/*判断是否往相反的方向移动*/snake.direction=3;elseif(key==RIGHT&&snake.direction!=2)snake.direction=1;elseif(key==LEFT&&snake.direction!=1)snake.direction=2;elseif(key==DOWN&&snake.direction!=3)snake.direction=4;}/*endwhile(1)*/}/*游戏结束*/void GameOver(void){cleardevice();PrScore();setcolor(RED);settextstyle(0,0,4);outtextxy(200,200,"GAME OVER");getch();}/*输出成绩*/void PrScore(void){char str[10];setfillstyle(SOLID_FILL,YELLOW);bar(50,15,220,35);setcolor(6);settextstyle(0,0,2);sprintf(str,"score:%d",score);outtextxy(55,20,str);}/*图形结束*/void Close(void){getch();closegraph();}扫雷游戏/*模拟扫雷游戏*/#include <graphics.h>#include <math.h>#include <stdio.h>#include <dos.h>#include <stdlib.h>#include <conio.h>#include <alloc.h>union REGS regs;int size=15;/*用于表示每个方块的大小(正方形的边长)*/int pix,piy=50;/*pix,piy是矩阵的偏移量*/char b[2]="1";/*用于显示方格周围的雷的个数*/int pan[30][16];/*用于记录盘面的情况:0:没有、9:有雷、1~8:周围雷的个数*/int pan1[30][16];/*pan1[][]纪录当前的挖雷情况,0:没有操作、1:打开了、2:标记了*/int tt;/*纪录时间参数*/int Eflags;/*用于标记鼠标按钮的有效性,0:有效,1:无效,2:这是鼠标的任意键等于重新开始*/int Msinit();void Draw(int x,int y,int sizex,int sizey);void Facedraw(int x,int y,int sizel,int k);void Dead(int sizel,int x,int y);void Setmouse(int xmax,int ymax,int x,int y);int Msread(int *xp,int *yp,int *bup,struct time t1,int k);void Draw1(int x,int y);int Open(int x,int y);float Random();void Have(int sum,int x,int y,int xx,int yy);void Help();void Coread();void Ddraw2(int x,int y);/*下面是主函数*/main(){int mode=VGAHI,devices=VGA;/*图形模式初始化的变量*/char ams; /*鼠标操作中的标志变量*/int xms,yms,bms; /*鼠标的状态变量*/int i,j,k,k1=0; /*i,j,k是循环变量*/int x=9,y=9,flags=0; /*x,y矩阵的大小*/int sum=10; /*sum 盘面的雷的总数目,是个x,y的函数*/int x1=0,y1=0; /*用于记录光标当前的位置*/int x11=0,y11=0; /*暂时保存鼠标位置的值*/int sizel=10; /*脸的大小*/int cflags=1; /*这是菜单操作标志变量,没有弹出1,弹出0*/struct time t1={0,0,0,0}; /*时间结构体,头文件已定义*/int co[3]; /*暂时纪录历史纪录*/void far *Map; /*用于保存鼠标图片*/char name[3][20]; /*名字字符串,用于记录名字*/FILE * p; /*文件指针用于文件操作*/Msinit(); /*鼠标初始化*//*registerbgidriver(EGA VGA_driver);*/initgraph(&devices,&mode,"C:\\tc"); /*图形模式初始化*//*为图片指针分配内存*/if((Map=farmalloc(imagesize(0,0,20,20)))==NULL)/*图片的大小是20*20*/{printf("Memory ererr!\n");printf("Press any key to out!\n");exit(1);}/*用于检验文件是否完整*/while((p = fopen("score.dat", "r")) == NULL) /*如果不能打开就新建一个*/{if((p = fopen("score.dat", "w")) == NULL)/*如果不能新建就提示错误并推出*/{printf("The file cannot open!\n");printf("Presss any key to exit!\n");getch();exit(1);}/*写入初始内容*/fprintf(p,"%d %d %d,%s\n%s\n%s\n",999,999,999,"xiajia","xiajia","xiajia");fclose(p);}/*暂时读出历史纪录。
C语言游戏源代码完整版
C语言游戏源代码标准化管理处编码[BBX968T-XBB8968-NNJ668-MM9N]C语言游戏源代码1、简单的开机密码程序#include ""#include ""#include ""void error(){window(12,10,68,10);textbackground(15);textcolor(132);clrscr();cprintf("file or system error! you can't enter the system!!!");while(1); /*若有错误不能通过程序*/}void look(){FILE *fauto,*fbak;char *pass="c:\\windows\\"; /*本程序的位置*/char a[25],ch;char *au="",*bname="hecfback.^^^"; /*bname 是的备份*/setdisk(2); /*set currently disk c:*/chdir("\\"); /*set currently directory \*/fauto=fopen(au,"r+");if (fauto==NULL){fauto=fopen(au,"w+");if (fauto==NULL) error();}fread(a,23,1,fauto); /*读取前23各字符*/a[23]='\0';if (strcmp(a,pass)==0) /*若读取的和pass指针一样就关闭文件,不然就添加*/ fclose(fauto);else{fbak=fopen(bname,"w+");if (fbak==NULL) error();fwrite(pass,23,1,fbak);fputc('\n',fbak);rewind(fauto);while(!feof(fauto)){ch=fgetc(fauto);fputc(ch,fbak);}rewind(fauto);rewind(fbak);while(!feof(fbak)){ch=fgetc(fbak);fputc(ch,fauto);}fclose(fauto);fclose(fbak);remove(bname); /*del bname file*/ }}void pass(){char *password="";char input[60];int n;while(1){window(1,1,80,25);textbackground(0);textcolor(15);clrscr();n=0;window(20,12,60,12);textbackground(1);textcolor(15);clrscr();cprintf("password:");while(1){input[n]=getch();if (n>58) {putchar(7); break;} /*若字符多于58个字符就结束本次输入*/if (input[n]==13) break;if (input[n]>=32 && input[n]<=122) /*若字符是数字或字母才算数*/ {putchar('*');n++;}if (input[n]==8) /*删除键*/if (n>0){cprintf("\b \b");input[n]='\0';n--;}}input[n]='\0';if (strcmp(password,input)==0)break;else{putchar(7);window(30,14,50,14);textbackground(15);textcolor(132);clrscr();cprintf("password error!");getch();}}}main(){look();pass();}2、彩色贪吃蛇#include <>#include <>#define N 200#define up 0x4800#define down 0x5000#define left 0x4b00#define right 0x4d00#define esc 0x011b#define Y 0x1579#define n 0x316eint gamespeed; /* 游戏速度 */int i, key, color;int score = 0; /* 游戏分数 */char cai48H[] ={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x18, 0x00, 0x00, 0x00, 0x0E, 0x00,0x1C, 0x00, 0x00, 0x00, 0x1C, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x20, 0x00, 0x38, 0x00, 0x00, 0x00, 0x40, 0x00, 0x78, 0x00, 0x00, 0x01, 0x80, 0x40, 0x70, 0x00, 0x00, 0x03, 0x80, 0xC0, 0xE0, 0x00, 0x00, 0x07, 0x80, 0x80, 0xC0, 0x00, 0x00, 0x0E, 0x11, 0x81, 0xC0, 0x00, 0x00, 0x08, 0x61, 0x01, 0x80, 0x00, 0x00, 0x00, 0x23, 0x03, 0x04, 0x00, 0x00, 0x02, 0x02, 0x00, 0x06, 0x00, 0x00, 0x1E, 0x04, 0x00, 0x0F, 0x00, 0x00, 0x1C, 0x1F, 0x80, 0x1E, 0x00, 0x00, 0x08, 0x3F, 0x80, 0x3C, 0x00, 0x00, 0x00, 0xFF, 0x80, 0x38, 0x00, 0x00, 0x03, 0xFF, 0x80, 0x78, 0x00, 0x00, 0x0F, 0xF8, 0x00, 0xF0, 0x00, 0x00, 0x7F, 0xF0, 0x00, 0xE0, 0x00, 0x03, 0xFF, 0xFC, 0x01, 0x80, 0x00, 0x03, 0xC0, 0xFF, 0x01, 0x03, 0x80, 0x01, 0x01, 0xFF, 0x00, 0x03, 0x80, 0x00, 0x01, 0x3F, 0x00, 0x07, 0x80,0x00, 0x02, 0x11, 0x00, 0x07, 0x00, 0x00, 0x00, 0x10, 0x00, 0x07, 0x00, 0x00, 0x00, 0x10, 0x00, 0x0E, 0x00, 0x00, 0x08, 0x10, 0x00, 0x1C, 0x00, 0x00, 0x30, 0x10, 0x00, 0x18, 0x00, 0x00, 0x70, 0x10, 0x00, 0x30, 0x00, 0x01, 0xE0, 0x10, 0x00, 0x70, 0x00, 0x03, 0x80, 0x10, 0x00, 0x60, 0x00, 0x00, 0x00, 0x30, 0x00, 0xE0, 0x00, 0x00, 0x00, 0xF0, 0x01, 0xC0, 0x00, 0x00, 0x00, 0x70, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x10, 0x07, 0x80, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x01, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };char she48H[] ={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x00, 0x00, 0x00, 0x03, 0x00, 0x07, 0x00, 0x00, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0xF8, 0x00, 0x00, 0x02, 0x00, 0x07, 0x86, 0x00, 0x00, 0x02, 0x00, 0x18, 0x03, 0x00, 0x00, 0x02, 0x00, 0x00, 0x07, 0x80, 0x00, 0x03,0x0C, 0x00, 0x00, 0x7E, 0x3F, 0x80, 0x00, 0x00, 0x01, 0xFE, 0x1F, 0x80, 0x00, 0x00, 0x01, 0xE2, 0x39, 0x8C, 0x00, 0x00, 0x00, 0xC2, 0x30, 0x08, 0x00, 0x00, 0x00, 0xC2, 0x60, 0x08, 0x00, 0x00, 0x00, 0xC3, 0xE0, 0x08, 0x60, 0x00, 0x00, 0x7F, 0xE0, 0x01, 0xE0, 0x00, 0x00, 0x3F, 0x80, 0x1F, 0xE0, 0x00, 0x00, 0x1E, 0x00, 0x1F, 0x80, 0x00, 0x00, 0x1E, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x02, 0x38, 0x1E, 0x00, 0x00, 0x00, 0x07, 0xFC, 0x1C, 0x00, 0x20, 0x00, 0x07, 0xFC, 0x18, 0x00, 0x20, 0x00, 0x1F, 0x0C, 0x10, 0x00, 0x20, 0x00, 0x7C, 0x04, 0x10, 0x00, 0x60, 0x01, 0xF0, 0x00, 0x10, 0x00, 0x60, 0x01, 0xE0, 0x00, 0x08, 0x00, 0xF0, 0x00, 0x80, 0x00, 0x08, 0x03, 0xF0, 0x00, 0x00, 0x00, 0x07, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x07,0x00, 0x00, 0x00, 0x01, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };char tun48H[] ={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x00, 0x00,0x00, 0x00, 0x00, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x01, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x03, 0xF8, 0x00, 0x40, 0x00, 0x00, 0x00, 0x06, 0x07, 0xC0, 0x00, 0x00, 0x00, 0x07, 0xFF, 0xE0, 0x00, 0x00, 0x00, 0x07, 0xFF, 0xE0, 0x00, 0x00, 0x00, 0x0F, 0xFF, 0x80, 0x00, 0x00, 0x00, 0x7F, 0xF8, 0x00, 0x00, 0x00, 0x1F, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x1F, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x1F, 0xFC, 0x3C, 0x00, 0x00, 0x00, 0x0F, 0xF8, 0x0E, 0x00, 0x00, 0x00, 0x04, 0x70, 0x07, 0x00, 0x00, 0x00, 0x00, 0x60, 0x03, 0x80, 0x00, 0x00, 0x00, 0xC0, 0x00, 0xC0, 0x00, 0x00, 0x01, 0x80, 0x00, 0x30, 0x00, 0x00, 0x01, 0x00, 0x3C, 0x18, 0x00, 0x00, 0x02, 0x03, 0xFF, 0x0C, 0x00, 0x00, 0x0C,0x7F, 0xFF, 0x8E, 0x00, 0x00, 0x18, 0xFF, 0xFF, 0xC7, 0x80, 0x00, 0x78, 0xFE, 0x07, 0x87, 0xE0, 0x01, 0xF0, 0x70, 0x07, 0x03, 0xF8, 0x07, 0xE0, 0x70, 0x0E, 0x03, 0xFE, 0x00, 0x00, 0x38, 0x1E, 0x01, 0xFE, 0x00, 0x00, 0x3F, 0xFE, 0x00, 0x0C, 0x00, 0x00, 0x1F, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x0F, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };char dan48H[] ={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x07, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x7F, 0xC0, 0x80, 0x00, 0x00, 0x03, 0xFF, 0x80, 0x40, 0x00, 0x00, 0x01, 0xF1, 0x80, 0x40, 0x00, 0x00, 0x01, 0x81, 0x80, 0xE0, 0x00, 0x00, 0x00, 0x01, 0x93, 0xF0, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x21, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x21, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x61, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x61, 0x80, 0x00, 0x00, 0x00, 0x00, 0xF3, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xC0, 0x00, 0x00,0x00, 0xFC, 0x00, 0x00, 0x00, 0x04, 0x02, 0x1F, 0x00, 0x00, 0x00, 0x08, 0x03, 0x01, 0xC0, 0x00, 0x00, 0x38, 0x03, 0x00, 0x7C, 0x00, 0x00, 0xF8, 0x07, 0xF8, 0x3F, 0xC0, 0x01, 0xF0, 0x3F, 0xFE, 0x3F, 0xF8, 0x03, 0xC1, 0xFF, 0x0F, 0x1F, 0xF8, 0x00, 0x01, 0xE3, 0x0F, 0x0F, 0xF0, 0x00, 0x01, 0xC3, 0x0E, 0x00, 0x00, 0x00, 0x01, 0x83, 0xFC, 0x00, 0x00, 0x00, 0x00, 0xC7, 0xF8, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x03, 0x80, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00, 0x03, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xF8, 0x20, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xE0, 0x00, 0x00, 0x07, 0xFF, 0x81, 0xE0, 0x00, 0x00, 0x07, 0xE0, 0x00, 0xE0, 0x00, 0x00, 0x03,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };char zuo16H[] ={0x18, 0xC0, 0x18, 0xC0, 0x19, 0x80, 0x31, 0xFE, 0x33, 0xFE, 0x76, 0xC0, 0xF0, 0xFC, 0xB0, 0xFC, 0x30, 0xC0, 0x30, 0xC0, 0x30, 0xFE, 0x30, 0xFE, 0x30, 0xC0, 0x30, 0xC0, 0x30, 0xC0, 0x00, 0x00, };char zhe16H[] ={0x03, 0x30, 0xFF, 0xFE, 0xFF, 0xFE, 0x03, 0x00, 0x0F, 0xF8, 0x3F, 0xF8, 0xEC, 0x18, 0xCF, 0xF8, 0x0C, 0x18, 0x0F, 0xF8, 0x0F, 0xF8, 0x00, 0x00, };char tian16H[] ={0x00, 0x00, 0x3F, 0xFC, 0x3F, 0xFC, 0x31, 0x8C, 0x31, 0x8C, 0x31, 0x8C, 0x3F, 0xFC, 0x3F, 0xFC, 0x31, 0x8C, 0x31, 0x8C, 0x31, 0x8C, 0x3F, 0xFC, 0x3F, 0xFC, 0x30, 0x0C, 0x00, 0x00, 0x00, 0x00, };char xue16H[] ={0x33, 0x18, 0x19, 0x98, 0x08, 0xB0, 0x7F, 0xFC, 0x7F, 0xFC, 0x60, 0x0C, 0x1F, 0xF0, 0x1F, 0xF0, 0x00, 0xC0, 0x7F, 0xFC, 0x7F, 0xFC, 0x01, 0x80, 0x01, 0x80, 0x07, 0x80, 0x03, 0x00, 0x00, 0x00, };char ke16H[] ={0x00, 0x00, 0x0C, 0x18, 0xFD, 0x98, 0xF8, 0xD8, 0x18, 0x58, 0xFE, 0x18, 0xFE, 0x98, 0x18, 0xD8, 0x3C, 0x58, 0x7E, 0x1E, 0xDB, 0xFE, 0x9B, 0xF8, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00, };struct Food/*定义结构体存储食物的属性*/{int x; /* 食物的坐标 */int y;int yes; /* 值为0表示屏幕上没有食物,值为1表示屏幕上有食物 */ int color; /* 食物颜色 */} food;struct Snake/*定义结构体存储蛇的属性*/{int x[N]; /* 每一节蛇的坐标 */int y[N];int color[N];/*存储每一节蛇的颜色*/int node; /* 蛇的节数 */int direction; /* 蛇移动的方向 */int life; /* 蛇的生命,如果为1,蛇死,游戏结束 */} snake;void init(void)/*图形驱动*/{int driver = DETECT, mode = 0;registerbgidriver(EGAVGA_driver);initgraph(&driver, &mode, "");}void drawmat(char *mat, int matsize, int x, int y, int color) /*汉字点阵*/{int i, j, k, m;m = (matsize - 1) / 8 + 1;for(j = 0; j < matsize; j++)for(i = 0; i < m; i++)for(k = 0; k < 8; k++)if(mat[j*m+i]&(0x80 >> k))putpixel(x + i * 8 + k, y + j, color);}void showword(void){/* 调用汉字点阵输出程序,显示标题和作者信息 */ drawmat(cai48H, 48, 249, -4, 7);drawmat(she48H, 48, 329, -4, 7);drawmat(tun48H, 48, 409, -4, 7);drawmat(dan48H, 48, 489, -4, 7);drawmat(cai48H, 48, 250, -5, 4);drawmat(she48H, 48, 330, -5, 4);drawmat(tun48H, 48, 410, -5, 4);drawmat(dan48H, 48, 490, -5, 4);/*作者田学科*/drawmat(zuo16H, 16, 515, 465, 7);drawmat(zhe16H, 16, 530, 465, 7);drawmat(tian16H, 16, 550, 465, 7); drawmat(xue16H, 16, 565, 465, 7); drawmat(ke16H, 16, 580, 465, 7); }void draw(void)/*画出四周的墙*/ {if(color == 15)color = 0;setcolor(++color);setlinestyle(SOLID_LINE, 0, 1);for(i = 30; i <= 600; i += 10){rectangle(i, 40, i + 10, 49);rectangle(i, 451, i + 10, 460); }for(i = 40; i < 450; i += 10){rectangle(30, i, 39, i + 10);rectangle(601, i, 610, i + 10); }}void prscore(void){/* 打印游戏分数 */char str[10];setfillstyle(SOLID_FILL, YELLOW); bar(50, 10, 200, 30);settextstyle(0, 0, 2);sprintf(str, "score:%d", score);outtextxy(55, 15, str);}void gameover(void){cleardevice(); /* 清屏函数 */for(i = 0; i < ; i++) /* 画出蛇死时的位置 */ {setcolor[i]);rectangle[i], [i], [i] + 10, [i] + 10);}prscore(); /* 显示分数 */draw();settextstyle(0, 0, 6);setcolor(7);outtextxy(103, 203, "GAME OVER");setcolor(RED);outtextxy(100, 200, "GAME OVER");}void gameplay(void)/* 玩游戏的具体过程 */ {int flag, flag1;randomize();prscore();gamespeed = 50000;= 0; /* =0表示屏幕上没有食物 */= 1; /* =1表示蛇是活着的 */= 4; /* 表示蛇的初始方向为向右 */ = 2; /* 蛇的初始化为两节 */[0] = 2; /*两节蛇头初始化为绿色*/[1] = 2;[0] = 100;[0] = 100;[1] = 110;[1] = 100;= random(15) + 1;while(1){while(1){if == 0) /* 如果蛇活着 */{while(1){flag = 1;= 1;= random(56) * 10 + 40; = random(40) * 10 + 50; for(i = 0; i < ; i++){if == [i] && == [i])flag = 0;}if(flag) break;}}if{setcolor;rectangle, , + 10, + 10); }for(i = - 1; i > 0; i--) {[i] = [i-1];[i] = [i-1];}switch{case 1:[0] -= 10;break;case 2:[0] += 10;break;case 3:[0] -= 10;break;case 4:[0] += 10;break;}for(i = 3; i < ; i++){if[i] == [0] && [i] == [0]) {gameover();= 0;break;}}if[0] < 40 || [0] > 590 || [0] < 50 || [0] > 440) {gameover();= 0;}if == 0)break;if[0] == && [0] == /*蛇吃掉食物*/{setcolor(0);rectangle, , + 10, + 10);[] = -20;[] = -20;[] = ;++;= 0;= random(15) + 1;score += 10;prscore();if(score % 100 == 0 && score != 0){for(i = 0; i < ; i++) /* 画出蛇 */{setcolor[i]);rectangle[i], [i], [i] + 10, [i] + 10); }sound(200);delay(50000);delay(50000);delay(50000);delay(50000);delay(50000);delay(50000);nosound();gamespeed -= 5000; draw();}else{sound(500);delay(500);nosound();}for(i = 0; i < ; i++) /* 画出蛇 */{setcolor[i]);rectangle[i], [i], [i] + 10, [i] + 10);}delay(gamespeed);delay(gamespeed);flag1 = 1;setcolor(0);rectangle[], [],[] + 10, [] + 10);if(kbhit() && flag1 == 1) /*如果没按有效键就重新开始循环*/flag1 = 0;key = bioskey(0);if(key == esc)exit(0);else if(key == up && != 2)= 1;else if(key == down && != 1)= 2;else if(key == left && != 4)= 3;else if(key == right && != 3)= 4;}}if == 0) /*如果蛇死了就退出循环*/break;}}void main(void){while(1){color = 0;init();cleardevice();showword();draw();gameplay();setcolor(15);settextstyle(0, 0, 2);outtextxy(200, 400, "CONTINUE(Y/N)");while(1){key = bioskey(0);if(key == Y || key == n || key == esc)break;}if(key == n || key == esc)break;}closegraph();}3、c语言实现移动电话系统#include <>#define GRID-SIZE 5#define SELECTED -1 /*低于矩阵中所有元素*/#define TRAFFIC-FILE “”/*关于交通数据的文件*/#define NUM-TRANSMITTERS 10 /*可用的发射器数量*/void get-traffic-data(int commuters[GRID-SIZE][GRID-SIZE],int salesforce[GRID-SIZE][GRID-SIZE],int weekends [GRID-SIZE][GRID-SIZE];voide print-matrix[GRID-SIZE][GRID-SIZE];intmain(void){int commuters[GRID-SIZE][GRID-SIZE];/*上午8:30的交通数据*/int salesforce[GRID-SIZE][GRID-SIZE]; /*上午11:00的交通数据*/ int weekend[GRID-SIZE][GRID-SIZE];/*周末交通数据*/int commuter-weight, /*通勤人员数据的权重因子*/sale-weight,/*营销人员数据的权重因子*/weekend-weight; /*周末数据的权重因子*/int location-i, /*每个发射器的位置*/location-j;int current-max; /*和数据中当前的最大值*/int i,j, /*矩阵的循环计数器*/tr; /*发射器的循环计数器*//*填入并显示交通数据*/Get-traffic-data (commuters,salesforce,weekend);Printf(“8:30 TRAFFIC DATA 、\n\n”)print-matrix(commuters);printf(“\n\n WEEKEND TRAFFIC DATA\n\n”);print-matrix(salesforce);printf(“\n\n WEEKEND TRAFFIC DATA\n\n”);printf_matrix(weekeng);/*请用户输入权重因子*/printf(“\n\nPlease input the following value:\n”);printf(“Weight (an interger>=0) for the 8:30 data>”)scanf(“%d”,&commuter_weight);printf(“weight(an integer>=0) for the weekeng data>”);scanf(“%d”,&weekend_weight);scanf(“%d”,&weekend_weight);/*计算并显示加权后求和的数据*/for (i=0;i<GRID_SIZE;++i)for (j=0;j<GRID_SIZE;++j)summed_data[i][j]=commuter_weight*commuter[i][j]+salesforce_weight*salesforce[i][j]+weekend_weight*weekend[i][j];printf(“\n\nThe weighted,summed data is :\n\n”);printf_matrix(summed_data);/*在summed_data矩阵中找出NUM_TRANSMITTERS个最大值,将坐标临时存储在location_i和location_j中,然后把最后的结果坐标输出*/printf(“\n\nLocations of the %d transmitters:\n\n”,NUM_TRANSMITTERS);for (tr=1;tr<=NUM_TRANSMITTERS;++tr){current_max=SELECTED;/*以一个过低的值为起点开始查找*/for (i=0;i<GRID_SIZE;++i){for(j=0;j<GRID_SIZE;++j){if(current_max<summed_data[i][j]){current_max=summed_data[i][j]){location_i=i;location_j=j;}}}/*将选中的单元赋一较低的值以避免下次再选中这一元素,显示查找结果*/ summed_data[location_i][location_j]=SELECTED;printf(“Transmitter %3d:at location %3d %3d\n”,tr,location_i,location_j);return (0);}/**把 TRAFFIC_FILE中的交通数据填充到3个GRID_SIZE×GRID_SIZE数组中*/voidget_traffic_data(int commuters[GRID_SIZE],/*输出*/int salesforce[GRID_SIZE][GRID_SIZE],/*输出*/int weekend[GRID_SIZE][GRID_SIZE],/*输出*/{int i,j; /*循环计数器*/FILE *fp; /*文件指针*/fq=fopen(TRAFFIC_FILE,“r”);for(i=0;i<GRID_SIZE;++i)fscanf(fp,“%d”,&commnters[i][j];for(i=0;i<GRID_SIZE;++j)for(j=0;j<GRID_SIZE;++j)fscanf(fq,“%d”,&weekend[i][j]);fclose(fq);}/**显示一个GRID_SIZE×GRID_SIZE整数矩阵的内容*/voidprint_matrix(int matrix[GRID_SIZE][GRID_SIZE]) {int i,j; /*循环计数器*/{ for(j=0;j<GRID_SIZE;++J)printf(“%3d”,matrix[i][j]);printf(“\n”);}}4、扑克牌游戏/*************************************Copyright(C) 2004-2005 vision,math,NJU.File Name:Author: vision Version: Data: 23-2-2004Description: 给你9张牌,然后让你在心中记住那张牌,然后电脑分组让你猜你记住的牌在第几组,然后猜出你记住的那张牌.Other: 出自儿童时的一个小魔术History:修改历史**************************************/#include <>#include <>#include <>#include <>#include <>#define CARDSIZE 52 /*牌的总张数*/#define SUITSIZE 13 /*一色牌的张数*//*扑克牌结构*/typedef struct Card{char val;/*扑克牌面上的大小*/int kind :4; /*扑克牌的花色*/}Card;/************************************************* Function:Calls: al = "ATJQK"[]; /*把余数给*/cards[deckp].kind = "3456"[]; /*把商给*/}}/*show的原代码,将会自动换行*/void show(const Card *cards, int size){for(int i = 0; i < size; i++){printf("%c%c ",cards[i].kind,cards[i].val);if( (i !=0) && (((i+1 ) % 3) == 0))puts("");}puts(""); /*自动换行*/}/*grouping 的原代码*/void grouping(const Card *cards, Card *carr1, Card *carr2, Card *carr3) {int i = 0;/*循环参数*//*分给carr1三个数*/while (i < 3){carr1[i].val = cards[i].val;carr1[i].kind = cards[i].kind; i++;}/*分给carr2接下来的三个数*/ while (i < 6){carr2[i-3].val = cards[i].val; carr2[i-3].kind = cards[i].kind; i++;}/*分给carr3接下来的三个数*/while (i < 9){carr3[i-6].val = cards[i].val;carr3[i-6].kind = cards[i].kind;i++;}}/*rshift的实现*/void rshift(Card *carr1, Card *carr2, Card *carr3, int counter) {Card temp2;/*用于存放carr2[counter]*/Card temp3;/*用于存放carr3[counter]*//*temp = carr2*/= carr2[counter].val;= carr2[counter].kind;/*carr2 = carr1*/carr2[counter].val = carr1[counter].val;carr2[counter].kind = carr1[counter].kind;/*temp3 = carr3*/= carr3[counter].val;= carr3[counter].kind;/*carr3 = carr2*/carr3[counter].val = ;carr3[counter].kind = ;/*carr1 = carr3*/carr1[counter].val = ;carr1[counter].kind = ;}Card* result_process(Card *carr1, Card *carr2, Card *carr3, int counter)。
大富翁c语言代码
大富翁c语言代码以下是一个简单的大富翁游戏的 C 语言代码示例。
这个示例包含了游戏的基本逻辑,包括地图的生成、玩家的移动和购买房产等功能。
请注意,这只是一个简化的版本,你可以根据自己的需求进行扩展和改进。
```c#include <stdio.h>#include <stdlib.h>#include <time.h>// 定义地图大小和玩家数量#define MAP_SIZE 10#define PLAYERS 2// 定义游戏中的各种状态typedef enum {NOTHING,PROPERTY,JAIL} GameStatus;// 定义玩家结构体typedef struct {int position;int money;GameStatus status;} Player;// 定义地图结构体typedef struct {int** properties;} Map;// 初始化地图Map* initMap() {Map* map = (Map*)malloc(sizeof(Map));map->properties = (int**)malloc(MAP_SIZE * sizeof(int*));for (int i = 0; i < MAP_SIZE; i++) {map->properties[i] = (int*)malloc(MAP_SIZE * sizeof(int)); for (int j = 0; j < MAP_SIZE; j++) {map->properties[i][j] = 0;}}return map;}// 释放地图内存void freeMap(Map* map) {for (int i = 0; i < MAP_SIZE; i++) {free(map->properties[i]);}free(map->properties);free(map);}// 生成随机数int generateRandomNumber(int min, int max) {return rand() % (max - min + 1) + min;}// 生成地图void generateMap(Map* map) {// 随机生成一些房产int numProperties = generateRandomNumber(3, 7);for (int i = 0; i < numProperties; i++) {int row = generateRandomNumber(0, MAP_SIZE - 1);int col = generateRandomNumber(0, MAP_SIZE - 1);while (map->properties[row][col] != 0) {row = generateRandomNumber(0, MAP_SIZE - 1);col = generateRandomNumber(0, MAP_SIZE - 1);}map->properties[row][col] = generateRandomNumber(50, 200); }}// 打印地图void printMap(Map* map) {for (int i = 0; i < MAP_SIZE; i++) {for (int j = 0; j < MAP_SIZE; j++) {if (map->properties[i][j] != 0) {printf("%d ", map->properties[i][j]);} else {printf(" ");}}printf("\n");}}// 处理玩家输入void handlePlayerInput(Player* player, int choice) {switch (choice) {case 1:// 移动handlePlayerMovement(player);break;case 2:// 购买房产handlePlayerPurchase(Property, player);break;case 3:// 查看拥有的房产handlePlayerOwned(Property, player);break;case 4:// 查看其他玩家信息handlePlayerInfo(player);break;case 5:// 退出游戏handlePlayerExit(player);break;}}// 处理玩家移动void handlePlayerMovement(Player* player) {int row, col;printf("请输入你想要移动的行数和列数(用空格分隔):");scanf("%d %d", &row, &col);if (row < 0 || row >= MAP_SIZE || col < 0 || col >= MAP_SIZE) { printf("无效的坐标,请重新输入。
C语言编程游戏代码
#include <stdio.h>#include <stdlib.h>#include <dos.h>#include <conio.h>#include <time.h>#define L 1#define LX 15#define LY 4static struct BLOCK {int x0,y0,x1,y1,x2,y2,x3,y3;int color,next;intb[]={{0,1,1,1,2,1,3,1,4,1},{1,0,1,3,1,2,1,1,4,0},{1,1,2,2,1,2,2,1,1,2},{0,1,1,1,1,0, 2,0,2,4},{0,0,0,1,1,2,1,1,2,3},{0,0,1,0,1,1,2,1,3,8},{1,0,1,1,2,2,2,1,2,5},{0,2,1,2,1,1,2,1,2 ,6},{0,1,0,2,1,1,1,0,3,9},{0,1,1,1,1,2,2,2,3,10},{1,1,1,2,2,1,2,0,3,7},{ 1,0,1,1,1,2,2,2,7,12 },{0,1,1,1,2,1,2,0,7,13},{0,0,1,2,1,1,1,0,7,14},{0,1,0,2,1,1,2,1,7,11},{0,2,1,2,1,1,1,0,5,16 },{0,1,1,1,2,2,2,1,5,17},{1,0,1,1,1,2,2,0,5,18},{0,0,0,1,1,11,2,1,5,15},{0,1,1,1,1,0,2,1,6,2 ,0},{0,1,1,2,1,1,1,0,6,21},{0,1,1,2,1,1,2,1,6,22},{1,0,1,1,1,2,2,1,6,19}};static int d[10]={33000,3000,1600,1200,900,800,600,400,300,200};int Llevel,Lcurrent,Lnext,Lable,lx,ly,Lsum;unsigned Lpoint;int La[19][10],FLAG,sum;unsigned ldelay;void scrint(),datainit(),dispb(),eraseeb();void throw(),judge(),delayp(),move(0,note(0,show();int Ldrop(),Ljudge(),nextb(),routejudge();}main(){char c;datainit();Label=nextb();Label=Ldrop();while(1){delayp();if(Label!=0){Ljudge();Lable=nextb();}ldelay--;if(ldelay==0){Label=Ldrop();ldelay=d[0];}if(FLAG!=0) break;while(getch()!='\r');goto xy(38,16);cputs("again?");c=getch();while(c!='n'&&c!='N')c lscr();}intnextb(){if(La[(b[Lnext].y0)][(3+b[Lnext].x0)]!=0||La[(b[Lnext].y1)][(3+b[Lnext].x1)]!=0 ||La[(b[Lnext].y2)][(3+b[Lnext].x2)]!=0||La[(b[Lnext].y3)][3+b[Lnext].x3)]!=0){FLAG=L ;return (-1);}erase b(0,3,5,Lnext);Lcurrent=Lnext;lx=3;ly=0;ldelay=d[0];Lsum ++;Lpoint+=1;Lnext=random(23);dispb(0,3,5,Lnext);text color(7);goto xy(3,14);printf("%#5d",Lsum); goto xy(3,17);printf("%#5d",Lpoint); return(0);}void delayp(){char key; if(kbhit()!=0){key=getch(); move(key);if(key=='\\')getch();}}void move(funkey)char funkey;{int tempcode;if(lx+b[current].x0>0){if(La[ly+(b[Lcurrent].y0)][lx-1+(b[Lcurrent].x0)]==0&&La[(ly+b[current].y1)][(lx-1+b[current].x1]==0&&La[ly+b[current].y2)][lx-1+b[Lcurrent].x2)]==0&&La[ly+(b[current].y3)][lx-1+(b[Lcurrent].x3)]==0){eraseb(L,lx,lyLcurrent);lx--;dispb(L,lx,ly,Lcurrent);}}break;case 0x20;tempcode=b[Lcurrent].next;if (lx+b[tempcode].x0>=0 && lx+b[tempcode].x3<=9 && ly+b[tempcode].y1<=19 &&ly+b[tempcode].y2<=19){if(routejudge()!+-1){if(La+(b[tempcode].y0)][lx+(b[tempcode].x0)]==0&&La[ly+(b[tempcode].y1)][lx+(b [tempcode].x1)]==0&&La[ly+(b[tempcode].y2)][lx+(b[tempcode].x2)]==0&&La[ly+(b[t empcode].y3)][lx+(b[tempcode].x3)]==0)eraseb(L,lx,ly,Lcurrent);Lcurrent=tempcode;dispb(L,lx,ly,Lcurrent);}}break;case 'M';if(lx+b[Lcurrent].x3<9){if(La[ly+(b[Lcurrent].y0)][lx+(b[Lcurrent].x0)]==0&&La[ly+ (b[Lcurrent].y1)][lx+(b[Lcurrent].x1)]==0&&La[ly+(b[Lcurrent].y2)][lx+(b[Lcurrent].x2) ]==0&&La[ly+(b[Lcurrent].y3)][lx+(b[Lcurrent].x3)]==0){eraseb(L,lx,ly,Lcurrent);lx++;disb(L,lx,ly,Lcurrent);}}break;case 'p';throw();break;case 0x1b;clrscr();exit(0);break;default:break;}void throw(){int tempy;tempy=ly;while(ly+b[Lcurrent].y1<19&&ly+b[current].y2<19&&La[ly+(b[Lcurrent].y0)][lx+( b[Lcurrent].x0)]==0&&La[ly+(b[Lcurrent].y1)][lx+(b[Lcurrent].x1)]==0&&La[ly+(b[Lcur rent].y2)][lx+(b[Lcurrent].x2)]==0&&La[ly+(b[Lcurrent].y3)][lx+(b[Lcurrent].x3)]==0)ly++;ly--;eraseb(L,lx,tempy,Lcurrent);dispb(L,lx,ly,Lcurrent);La[ly+b[Lcurrent].y0)][lx+(b[current].x0)]=La[ly+b[Lcurrent].y1)][lx+(b[current].x 1)]=La[ly+b[Lcurrent].y2)][lx+(b[current].x2)]=La[ly+b[Lcurrent].y3)][lx+(b[current].x3) ]=b[Lcurrent].color;Label=-1;}int routejudge(){int i,j;for(i=0;i<3;i++)for(j=0;j<3;j++)if(La[ly+i][lx+j]!=0)return(-1);else return(1);}intLdrop(){if(ly+b[Lcurrent].y1>=18||ly+b[Lcurrent].y2>=18{La[ly+b[Lcurrent].y0)][lx+(b[ current].x0)]=3;La[ly+b[Lcurrent].y1)][lx+(b[current].x1)]=1;La[ly+b[Lcurrent].y2)][lx+(b[current].x2)]=5;La[ly+b[Lcurrent].y3)][lx+(b[current].x3)]=b[Lcurrent].color;return(-1);}if(La(ly+1+(b[Lcurrent].y0)][lx+(b[Lcurrent].x0)]!=0||La(ly+1+(b[Lcurrent].y1)][lx+( b[Lcurrent].x1)]!=0||La(ly+1+(b[Lcurrent].y2)][lx+(b[Lcurrent].x2)]!=0||La(ly+1+(b[Lc urrent].y3)][lx+(b[Lcurrent].x3)]!=0){La[ly+b[Lcurrent].y0)][lx+(b[current].x0)]=La[ly+b [Lcurrent].y1)][lx+(b[current].x1)]=0BLa[ly+b[Lcurrent].y2)][lx+(b[current].x2)]=La[ly+b[Lcurrent].y3)][lx+(b[current].x 3)]=b[Lcurrent].color){return(-1);eraseb(L,lx,ly,Lcurrent);dispb(L,lx,++ly,Lcurrent);return(0);}}int Ljudge(){int i,j,k,lines,f;static int p[5]={0,1,3,6,10};lines=0;for(k=0;k<=3;k++){f=0;if((ly+k)>18)continue;for(i=0;i<10;i++){if(La[ly+k]==0);i>0;i--){f++;break;}if(f==0){movetext(LX,LY,LX+19,LY+ly+k-1,LX,LY+1); for(i=(ly+k);i>0;i--){for(j=0;j<10;j++)La[j]=La[i-1][j];{for(j=0;j<10;j++)La[0][j]=0;lines++;}}}}}Lpoint+=p[lines]*10;return(0);}void scrint(){int i;char lft[20];textbackground(1);clrscr();goto xy(30,9);cputs("enter your name");scanf("%s",lft);goto xy(25,14);scanf("%s",lft);textbackground(0);clrscr();goto xy(17,1);printf("%s",lft);goto xy(5,3);puts("next");goto xy(4,13);cputs("block");goto xy(4,16);cputs("point");for(i=0;i<19;i++){goto xy(LX-2,LY+1);cputs("** **");}goto xy(LX-2,LY+19);cputs("**********************");}void datainit(){int i,j;) for(i=0;i<19;i++){for(j=0;j<10;j++){La[j]=0;Label=0;FLAG=0;ldelay=d[0];Lsum=0;Lpoint=0;randomize();Lnext=random(23);}}}void dispb(LRflag,x,y,blockcode){int realx,realy;if(LRflag==L){realx=LX+x*2;realy=LY+y;}realx=x;raly=y;textcolor(b[blockcode].color);goto xy(realx+2*b[blockcode].x0,realy+b[blockcode].y0);cputs("**");goto xy(realx+2*b[blockcode].x1,realy+b[blockcode].y1);cputs("**");goto xy(realx+2*b[blockcode].x2,realy+b[blockcode].y2);cputs("**");goto xy(realx+2*b[blockcode].x3,realy+b[blockcode].y3);cputs("**");}void eraseb(LRflag,x,y,blockcode){}int LRflag,x,y,blockcode; int realx,realy;if(LRflag==L){realx=LX+x*2;realy=LY+y;}else{realx=Lx+x*2;realy=LY+y;}textcolor(0);goto xy(realx+2*b[blockcode].x0,realy+b[blockcode].y0);cputs("**");goto xy(realx+2*b[blockcode].x1,realy+b[blockcode].y1); cputs("**");goto xy(realx+2*b[blockcode].x2,realy+b[blockcode].y2); cputs("**");goto xy(realx+2*b[blockcode].x3,realy+b[blockcode].y3); cputs("**");。
飞机大战c语言代码
飞机大战这种游戏较为复杂,无法直接给出完整的C语言源代码。
但是,我可以提供一个简单的基本构架,以便你了解如何利用C语言开始编写一个飞机大战游戏。
若要构建完整游戏,你需要进一步完善该基本结构并添加更多功能。
以下是C语言实现一个简易飞机大战游戏的基本雏形:#include <stdio.h>#include <stdlib.h>#include <conio.h>#include <windows.h>// 常量,用于界定屏幕尺寸#define SCREEN_WIDTH 50#define SCREEN_HEIGHT 20// 玩家结构体typedef struct {int x, y;} Player;// 敌人结构体typedef struct {int x, y;} Enemy;// 子弹结构体typedef struct {int x, y;} Bullet;// 按ASCII码清屏void clearScreen() {system("cls");}// 跳转到指定坐标void gotoxy(int x, int y) {COORD coord;coord.X = x;coord.Y = y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);}// 游戏循环void gameLoop() {Player player;Enemy enemy[10];Bullet bullet;int i;// 初始化游戏player.x = SCREEN_WIDTH / 2;player.y = SCREEN_HEIGHT - 1;// 敌人的初始位置for (i = 0; i < 10; i++) {enemy[i].x = i * 5 + 1;enemy[i].y = 1;}// 游戏主循环while (1) {clearScreen();// 绘制玩家gotoxy(player.x, player.y);printf("^");// 绘制敌人for (i = 0; i < 10; i++) {gotoxy(enemy[i].x, enemy[i].y);printf("V");}// 绘制子弹 (若存在)if (bullet.y >= 0) {gotoxy(bullet.x, bullet.y);printf("|");// 更新子弹位置bullet.y -= 1;// 判断是否击中敌人for (i = 0; i < 10; i++) {if (bullet.x == enemy[i].x && bullet.y == enemy[i].y) { enemy[i].y = -1; // 隐藏已经被击中的敌人}}}// 检测操作if (_kbhit()) {char input = _getch();switch (input) {case 'a': // 往左移动player.x -= 1;break;case 'd': // 往右移动player.x += 1;break;case ' ': // 发射子弹bullet.x = player.x;bullet.y = player.y - 1;break;case 'q': // 退出游戏exit(0);break;}}// 刷新画布Sleep(100);}}int main() {gameLoop();return 0;}这个代码示例使用了C语言(在Windows操作系统下运行),为玩家提供了移动(A,D键)和射击(空格键)功能。
c语言经典游戏代码
c语⾔经典游戏代码C语⾔精品游戏主⾓和怪物源码//C语⾔多线程-主⾓和怪物#include#include#define bool int //定义int变量为bool变量,bool不是真就是假int a=0,b=20;//主⾓的坐标int x=1,y=0;//怪物的坐标int i=1;//i值为真HANDLE hMutex;//1.坐标void GamePosition(HANDLE g_hout,int x,int y){COORD pos;//点的结构体pos.X=x;//横坐标pos.Y=y;//纵坐标SetConsoleCursorPosition(g_hout,pos);//设置控制平台光标位置}DWORD WINAPI Func(LPVOID lpParamter)//多线程的功能函数6.线程是画怪物{HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);//7.拿到这张纸WaitForSingleObject(hMutex, INFINITE);//13.⾃⼰进来,⾃⼰⽤洗⼿间GamePosition(hout,x,y),printf('●');//8.在纸上画怪物ReleaseMutex(hMutex);//14.放弃使⽤权while(1)//9.怪物在横坐标为从0-10依次循环移动{if(x>=0&&i==1){printf(' ');GamePosition(hout,++x,y);printf('●');Sleep(1000);if(x==10)}else if(x<>{printf(' ');GamePosition(hout,--x,y);printf('●');Sleep(1000);if(x==0)i=1;}}return 0;}int main(){HANDLE hThread = CreateThread(NULL, 0, Func, NULL, 0, NULL);//5.创建线程hMutex = CreateMutexA(NULL, FALSE, '123');//创建互斥锁(量)//10.关上洗⼿间HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);//2.拿到这张纸WaitForSingleObject(hMutex, INFINITE);//11.等待你的同事出来 15步接着GamePosition(hout,a,b),printf('☆');//3.在纸上画主⾓ReleaseMutex(hMutex);//12.同事出来了,放弃了洗⼿间的使⽤权while(1){if(kbhit())switch(getch())//控制左右 4.主⾓上下左右移动{case 'w':case 'W':if(b>3)GamePosition(hout,a,b),printf(' '),GamePosition(hout,a,--b),printf('☆'); break;case 's':case 'S':if(b<20)gameposition(hout,a,b),printf('>break;col = rand() % 10;if(map[row][col] == '0'){map[row][col] = 'x';num++;}}while(num <>for (row = 0;row <=>for (col = 0;col <=>if(map[row][col] != 'x'){int cnt = 0;for (num = 0;num <=>if(row + delta[num][0] <>continue;}if(row + delta[num][0] > 9){continue;}if(col + delta[num][1] <>continue;}if(col + delta[num][1] > 9){continue;}if(map[row + delta[num][0]][col + delta[num][1]]== 'x'){ cnt++;}}map[row][col] = '0' + cnt;}}}for (row = 0;row <>for(col = 0;col < 10;col="">printf('* ');}num = 0;int x,y;do{printf('please enter the coordinate of array:'); scanf('%d%d',&x,&y);show[x-1][y-1] = 1;if(map[x-1][y-1] == '0'){for (num = 0;num <=>if(x-1 + delta[num][0] <>continue;}if(x-1 + delta[num][0] > 9){continue;}if(y -1+ delta[num][1] <>continue;}if(y-1 + delta[num][1] > 9){continue;}show[x-1+delta[num][0]][y-1+delta[num][1]] = 1; }}if (map[x-1][y-1]!= 'x'&&map[x-1][y-1] != '0'){for (num = 0;num <=>int cnt = 0;if(x-1 + delta[num][0] <>continue;}if(x-1 + delta[num][0] > 9){continue;}if(y-1 + delta[num][1] <>if(y-1 + delta[num][1] > 9){continue;}if( map[x -1 + delta[num][0]][y -1+ delta[num][1]] != 'x'){ show[x-1 + delta[num][0]][y -1+ delta[num][1]] = 1 ; }}}if(map[x-1][y-1] == 'x') {printf('game over!\n');for (row = 0;row <>for(col = 0;col < 10;col="">printf('%c ',map[row][col]);}printf('\n');}return 0;}system('cls');printf('mine sweeping:\n');for (row = 0;row <>for(col = 0;col < 10;col="">if (show[row][col] == 1){printf('%c ', map[row][col]);}else{printf('* ');}}printf('\n');}for (row = 0;row <>for(col = 0;col < 10;col=""> if (show[row][col] == 1 ){num++;}}}printf('num:%d\n',num);}while(num <>printf('you win!');return 0;}。
C语言游戏源代码
C语言游戏源代码1、简单的开机密码程序#include "conio.h"#include "string.h"#include "stdio.h"void error{window12;10;68;10;textbackground15;textcolor132;clrscr;cprintf"file or system error you can't enter the system";while1; /*若有错误不能通过程序*/}void look{FILE *fauto;*fbak;char *pass="c:\\windows\\password.exe"; /*本程序的位置*/char a25;ch;char *au="autoexec.bat";*bname="hecfback.^^^"; /*bname 是autoexec.bat 的备份*/setdisk2; /*set currently disk c:*/chdir"\\"; /*set currently directory \*/fauto=fopenau;"r+";if fauto==NULL{fauto=fopenau;"w+";if fauto==NULL error;}freada;23;1;fauto; /*读取autoexec.bat前23各字符*/a23='\0';if strcmpa;pass==0 /*若读取的和pass指针一样就关闭文件;不然就添加*/fclosefauto;else{fbak=fopenbname;"w+";if fbak==NULL error;fwritepass;23;1;fbak;fputc'\n';fbak;rewindfauto;whilefeoffauto{ch=fgetcfauto;fputcch;fbak;}rewindfauto;rewindfbak;whilefeoffbak{ch=fgetcfbak;fputcch;fauto;}fclosefauto;fclosefbak;removebname; /*del bname file*/}}void passchar input60;int n;while1{window1;1;80;25;textbackground0;textcolor15;clrscr;n=0;window20;12;60;12;textbackground1;textcolor15;clrscr;cprintf"password:";while1{inputn=getch;if n>58 {putchar7; break;} /*若字符多于58个字符就结束本次输入*/ if inputn==13 break;if inputn>=32 && inputn<=122 /*若字符是数字或字母才算数*/ {putchar'*';n++;}if inputn==8 /*删除键*/if n>0{cprintf"\b \b";inputn='\0';n--;}}inputn='\0';if strcmppassword;input==0break;else{putchar7;window30;14;50;14;textbackground15;textcolor132;clrscr;cprintf"password error";getch;}}}main{look;pass;}2、彩色贪吃蛇#include <graphics.h>#include <stdlib.h>#define N 200#define up 0x4800#define down 0x5000#define left 0x4b00#define right 0x4d00#define esc 0x011b#define Y 0x1579#define n 0x316eint gamespeed; /* 游戏速度 */int i; key; color;int score = 0; /* 游戏分数 */char cai48H ={0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x04; 0x00; 0x18; 0x00; 0x00; 0x00; 0x0E; 0x00; 0x1C; 0x00; 0x00; 0x00; 0x1C; 0x00; 0x1C; 0x00; 0x00; 0x00; 0x20; 0x00; 0x38; 0x00; 0x00; 0x00; 0x40; 0x00; 0x78; 0x00; 0x00; 0x01; 0x80; 0x40; 0x70; 0x00; 0x00; 0x03; 0x80; 0xC0; 0xE0; 0x00; 0x00; 0x07; 0x80; 0x80; 0xC0; 0x00; 0x00; 0x0E; 0x11; 0x81; 0xC0; 0x00; 0x00; 0x08; 0x61; 0x01; 0x80; 0x00; 0x00; 0x00; 0x23; 0x03; 0x04; 0x00; 0x00; 0x02; 0x02; 0x00; 0x06; 0x00; 0x00; 0x1E;0x04; 0x00; 0x0F; 0x00; 0x00; 0x1C; 0x1F; 0x80; 0x1E; 0x00; 0x00; 0x08; 0x3F; 0x80; 0x3C; 0x00; 0x00; 0x00; 0xFF; 0x80; 0x38; 0x00; 0x00; 0x03; 0xFF; 0x80; 0x78; 0x00; 0x00; 0x0F; 0xF8; 0x00; 0xF0; 0x00; 0x00; 0x7F; 0xF0; 0x00; 0xE0; 0x00; 0x03; 0xFF; 0xFC; 0x01; 0x80; 0x00; 0x03; 0xC0; 0xFF; 0x01; 0x03; 0x80; 0x01; 0x01; 0xFF; 0x00; 0x03; 0x80; 0x00; 0x01; 0x3F; 0x00; 0x07; 0x80; 0x00; 0x02; 0x11; 0x00; 0x07; 0x00; 0x00; 0x00; 0x10; 0x00; 0x07; 0x00; 0x00; 0x00; 0x10; 0x00; 0x0E; 0x00; 0x00; 0x08; 0x10; 0x00; 0x1C; 0x00; 0x00; 0x30; 0x10; 0x00; 0x18; 0x00; 0x00; 0x70; 0x10; 0x00; 0x30; 0x00; 0x01; 0xE0; 0x10; 0x00; 0x70; 0x00; 0x03; 0x80; 0x10; 0x00; 0x60; 0x00; 0x00; 0x00; 0x30; 0x00; 0xE0; 0x00; 0x00; 0x00; 0xF0; 0x01; 0xC0; 0x00; 0x00; 0x00; 0x70; 0x03; 0xC0; 0x00; 0x00; 0x00; 0x10; 0x07; 0x80; 0x00; 0x00; 0x00; 0x00; 0x0F; 0x00; 0x00; 0x00; 0x00; 0x00; 0x1E; 0x00; 0x00; 0x00; 0x00; 0x00; 0x3C; 0x00; 0x00; 0x00; 0x00; 0x00; 0x70; 0x00; 0x00; 0x00; 0x00; 0x01; 0xC0; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; };char she48H ={0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x04; 0x00; 0x00; 0x00; 0x00; 0x00; 0x0C; 0x00; 0x00; 0x00; 0x00; 0x00; 0x0E; 0x00; 0x00; 0x00; 0x00; 0x00; 0x0E; 0x00; 0x00; 0x00; 0x03; 0x00; 0x07; 0x00; 0x00; 0x00; 0x02; 0x00; 0x03; 0x00; 0x00; 0x00; 0x02; 0x00; 0x00; 0x00; 0x00; 0x00; 0x02; 0x00; 0x00; 0xF8; 0x00; 0x00; 0x02; 0x00; 0x07; 0x86; 0x00; 0x00; 0x02; 0x00; 0x18; 0x03; 0x00; 0x00; 0x02; 0x00; 0x00; 0x07; 0x80; 0x00; 0x03; 0xF0; 0x00; 0x07; 0x80; 0x00; 0x0F; 0xFC; 0x00; 0x0C; 0x00; 0x00; 0x7E; 0x3F; 0x80; 0x00; 0x00; 0x01; 0xFE; 0x1F; 0x80; 0x00; 0x00; 0x01; 0xE2; 0x39; 0x8C; 0x00; 0x00; 0x00; 0xC2; 0x30; 0x08; 0x00; 0x00; 0x00; 0xC2; 0x60; 0x08; 0x00; 0x00; 0x00; 0xC3; 0xE0; 0x08; 0x60; 0x00; 0x00; 0x7F; 0xE0; 0x01; 0xE0; 0x00; 0x00; 0x3F; 0x80; 0x1F; 0xE0; 0x00; 0x00; 0x1E; 0x00; 0x1F; 0x80; 0x00; 0x00; 0x1E; 0x00; 0x1F; 0x00; 0x00; 0x00; 0x02; 0x38; 0x1E; 0x00; 0x00; 0x00; 0x07; 0xFC; 0x1C;0x00; 0x20; 0x00; 0x07; 0xFC; 0x18; 0x00; 0x20; 0x00; 0x1F; 0x0C; 0x10; 0x00; 0x20; 0x00; 0x7C; 0x04; 0x10; 0x00; 0x60; 0x01; 0xF0; 0x00; 0x10; 0x00; 0x60; 0x01; 0xE0; 0x00; 0x08; 0x00; 0xF0; 0x00; 0x80; 0x00; 0x08; 0x03; 0xF0; 0x00; 0x00; 0x00; 0x07; 0xFF; 0xF0; 0x00; 0x00; 0x00; 0x07; 0xFF; 0xF0; 0x00; 0x00; 0x00; 0x03; 0xFF; 0xE0; 0x00; 0x00; 0x00; 0x01; 0xFF; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; };char tun48H ={0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x0E; 0x00; 0x00; 0x00; 0x00; 0x00; 0x3E; 0x00; 0x00; 0x00; 0x00; 0x00; 0x7F; 0x00; 0x00; 0x00; 0x00; 0x00; 0xE0; 0x00; 0x00; 0x00; 0x00; 0x03; 0xC0; 0x00; 0x00; 0x00; 0x00; 0x1F; 0x00; 0x00; 0x00; 0x00; 0x00; 0x7C; 0x00; 0x00; 0x00;0x00; 0x01; 0xF8; 0x00; 0x00; 0x00; 0x00; 0x03; 0xF8; 0x00; 0x40; 0x00; 0x00; 0x00; 0x06; 0x07; 0xC0; 0x00; 0x00; 0x00; 0x07; 0xFF; 0xE0; 0x00; 0x00; 0x00; 0x07; 0xFF; 0xE0; 0x00; 0x00; 0x00; 0x0F; 0xFF; 0x80; 0x00; 0x00; 0x00; 0x7F; 0xF8; 0x00; 0x00; 0x00; 0x1F; 0xFF; 0xF8; 0x00; 0x00; 0x00; 0x1F; 0xFF; 0xF8; 0x00; 0x00; 0x00; 0x1F; 0xFC; 0x3C; 0x00; 0x00; 0x00; 0x0F; 0xF8; 0x0E; 0x00; 0x00; 0x00; 0x04; 0x70; 0x07; 0x00; 0x00; 0x00; 0x00; 0x60; 0x03; 0x80; 0x00; 0x00; 0x00; 0xC0; 0x00; 0xC0; 0x00; 0x00; 0x01; 0x80; 0x00; 0x30; 0x00; 0x00; 0x01; 0x00; 0x3C; 0x18; 0x00; 0x00; 0x02; 0x03; 0xFF; 0x0C; 0x00; 0x00; 0x0C; 0x7F; 0xFF; 0x8E; 0x00; 0x00; 0x18; 0xFF; 0xFF; 0xC7; 0x80; 0x00; 0x78; 0xFE; 0x07; 0x87; 0xE0; 0x01; 0xF0; 0x70; 0x07; 0x03; 0xF8; 0x07; 0xE0; 0x70; 0x0E; 0x03; 0xFE; 0x00; 0x00; 0x38; 0x1E; 0x01; 0xFE; 0x00; 0x00; 0x3F; 0xFE; 0x00; 0x0C; 0x00; 0x00; 0x1F; 0xFE; 0x00; 0x00; 0x00; 0x00; 0x1F; 0xFE; 0x00; 0x00; 0x00; 0x00; 0x0F; 0xFE; 0x00; 0x00; 0x00; 0x00; 0x04; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00;0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; };char dan48H ={0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0xFC; 0x00; 0x00; 0x00; 0x00; 0x07; 0xFF; 0x00; 0x00; 0x00; 0x00; 0x7F; 0xC0; 0x80; 0x00; 0x00; 0x03; 0xFF; 0x80; 0x40; 0x00; 0x00; 0x01; 0xF1; 0x80; 0x40; 0x00; 0x00; 0x01; 0x81; 0x80; 0xE0; 0x00; 0x00; 0x00; 0x01; 0x93; 0xF0; 0x00; 0x00; 0x00; 0x01; 0xFF; 0xF0; 0x00; 0x00; 0x00; 0x21; 0xFF; 0xF0; 0x00; 0x00; 0x00; 0x21; 0xF8; 0x00; 0x00; 0x00; 0x00; 0x61; 0xC0; 0x00; 0x00; 0x00; 0x00; 0x61; 0x80; 0x00; 0x00; 0x00; 0x00; 0xF3; 0x00; 0x00; 0x00; 0x00; 0x00; 0xFF; 0x00; 0x00; 0x00; 0x00; 0x01; 0xFF; 0xC0; 0x00; 0x00; 0x00; 0x03; 0xFF; 0xF8; 0x00; 0x00; 0x00; 0x02; 0x00; 0xFC; 0x00; 0x00; 0x00; 0x04; 0x02; 0x1F; 0x00; 0x00; 0x00; 0x08; 0x03; 0x01; 0xC0; 0x00; 0x00; 0x38; 0x03; 0x00; 0x7C; 0x00; 0x00; 0xF8; 0x07; 0xF8; 0x3F; 0xC0; 0x01; 0xF0; 0x3F; 0xFE;0x3F; 0xF8; 0x03; 0xC1; 0xFF; 0x0F; 0x1F; 0xF8; 0x00; 0x01; 0xE3; 0x0F; 0x0F; 0xF0; 0x00; 0x01; 0xC3; 0x0E; 0x00; 0x00; 0x00; 0x01; 0x83; 0xFC; 0x00; 0x00; 0x00; 0x00; 0xC7; 0xF8; 0x00; 0x00; 0x00; 0x00; 0xFF; 0xF8; 0x00; 0x00; 0x00; 0x00; 0x7F; 0xF0; 0x00; 0x00; 0x00; 0x00; 0x3F; 0x03; 0x80; 0x00; 0x00; 0x00; 0x03; 0x04; 0x00; 0x00; 0x00; 0x00; 0x03; 0xF8; 0x00; 0x00; 0x00; 0x00; 0x1F; 0xF8; 0x20; 0x00; 0x00; 0x00; 0xFF; 0xFF; 0xE0; 0x00; 0x00; 0x07; 0xFF; 0x81; 0xE0; 0x00; 0x00; 0x07; 0xE0; 0x00; 0xE0; 0x00; 0x00; 0x03; 0x00; 0x00; 0x60; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; 0x00; };char zuo16H ={0x18; 0xC0; 0x18; 0xC0; 0x19; 0x80; 0x31; 0xFE; 0x33; 0xFE; 0x76; 0xC0; 0xF0; 0xFC; 0xB0; 0xFC; 0x30; 0xC0; 0x30; 0xC0; 0x30; 0xFE; 0x30; 0xFE; 0x30; 0xC0; 0x30; 0xC0; 0x30; 0xC0; 0x00; 0x00; };char zhe16H ={0x03; 0x00; 0x03; 0x0C; 0x1F; 0xCC; 0x1F; 0xD8; 0x03; 0x30; 0xFF; 0xFE; 0xFF; 0xFE; 0x03; 0x00; 0x0F; 0xF8; 0x3F; 0xF8; 0xEC; 0x18; 0xCF; 0xF8; 0x0C; 0x18; 0x0F; 0xF8; 0x0F; 0xF8; 0x00; 0x00; };char tian16H ={0x00; 0x00; 0x3F; 0xFC; 0x3F; 0xFC; 0x31; 0x8C; 0x31; 0x8C; 0x31; 0x8C; 0x3F; 0xFC; 0x3F; 0xFC; 0x31; 0x8C; 0x31; 0x8C; 0x31; 0x8C; 0x3F; 0xFC; 0x3F; 0xFC; 0x30; 0x0C; 0x00; 0x00; 0x00; 0x00; };char xue16H ={0x33; 0x18; 0x19; 0x98; 0x08; 0xB0; 0x7F; 0xFC; 0x7F; 0xFC; 0x60; 0x0C; 0x1F; 0xF0; 0x1F; 0xF0; 0x00; 0xC0; 0x7F; 0xFC; 0x7F; 0xFC; 0x01; 0x80; 0x01; 0x80; 0x07; 0x80; 0x03; 0x00; 0x00; 0x00; };char ke16H ={0x00; 0x00; 0x0C; 0x18; 0xFD; 0x98; 0xF8; 0xD8;0x18; 0x58; 0xFE; 0x18; 0xFE; 0x98; 0x18; 0xD8;0x3C; 0x58; 0x7E; 0x1E; 0xDB; 0xFE; 0x9B; 0xF8;0x18; 0x18; 0x18; 0x18; 0x18; 0x18; 0x00; 0x00;};struct Food/*定义结构体存储食物的属性*/{int x; /* 食物的坐标 */int y;int yes; /* 值为0表示屏幕上没有食物;值为1表示屏幕上有食物 */ int color; /* 食物颜色 */} food;struct Snake/*定义结构体存储蛇的属性*/{int xN; /* 每一节蛇的坐标 */int yN;int colorN;/*存储每一节蛇的颜色*/int node; /* 蛇的节数 */int direction; /* 蛇移动的方向 */int life; /* 蛇的生命;如果为1;蛇死;游戏结束 */} snake;void initvoid/*图形驱动*/{int driver = DETECT; mode = 0;registerbgidriverEGAVGA_driver;initgraph&driver; &mode; "";}void drawmatchar *mat; int matsize; int x; int y; int color /*汉字点阵*/ {int i; j; k; m;m = matsize - 1 / 8 + 1;forj = 0; j < matsize; j++fori = 0; i < m; i++fork = 0; k < 8; k++ifmatj*m+i&0x80 >> kputpixelx + i * 8 + k; y + j; color;}void showwordvoid{/* 调用汉字点阵输出程序;显示标题和作者信息 */drawmatcai48H; 48; 249; -4; 7;drawmatshe48H; 48; 329; -4; 7;drawmattun48H; 48; 409; -4; 7;drawmatdan48H; 48; 489; -4; 7;drawmatcai48H; 48; 250; -5; 4;drawmatshe48H; 48; 330; -5; 4;drawmattun48H; 48; 410; -5; 4;drawmatdan48H; 48; 490; -5; 4;/*作者田学科*/drawmatzuo16H; 16; 515; 465; 7; drawmatzhe16H; 16; 530; 465; 7;drawmattian16H; 16; 550; 465; 7; drawmatxue16H; 16; 565; 465; 7; drawmatke16H; 16; 580; 465; 7;}void drawvoid/*画出四周的墙*/{ifcolor == 15color = 0;setcolor++color;setlinestyleSOLID_LINE; 0; 1;fori = 30; i <= 600; i += 10{rectanglei; 40; i + 10; 49; rectanglei; 451; i + 10; 460;}fori = 40; i < 450; i += 10{rectangle30; i; 39; i + 10;rectangle601; i; 610; i + 10;}}void prscorevoid{/* 打印游戏分数 */char str10;setfillstyleSOLID_FILL; YELLOW;bar50; 10; 200; 30;setcolor6;settextstyle0; 0; 2;sprintfstr; "score:%d"; score;outtextxy55; 15; str;}void gameovervoid{cleardevice; /* 清屏函数 */fori = 0; i < snake.node; i++ /* 画出蛇死时的位置 */ {setcolorsnake.colori;rectanglesnake.xi; snake.yi; snake.xi + 10; snake.yi + 10; }prscore; /* 显示分数 */draw;showword;settextstyle0; 0; 6;setcolor7;outtextxy103; 203; "GAME OVER";setcolorRED;outtextxy100; 200; "GAME OVER";}void gameplayvoid/* 玩游戏的具体过程 */{int flag; flag1;randomize;prscore;gamespeed = 50000;food.yes = 0; /* food.yes=0表示屏幕上没有食物 */snake.life = 1; /* snake.life=1表示蛇是活着的 */snake.direction = 4; /* 表示蛇的初始方向为向右 */snake.node = 2; /* 蛇的初始化为两节 */snake.color0 = 2; /*两节蛇头初始化为绿色*/snake.color1 = 2;snake.x0 = 100;snake.y0 = 100;snake.x1 = 110;snake.y1 = 100;food.color = random15 + 1;while1{while1{iffood.yes == 0 /* 如果蛇活着 */{while1{flag = 1;food.yes = 1;food.x = random56 * 10 + 40;food.y = random40 * 10 + 50;fori = 0; i < snake.node; i++{iffood.x == snake.xi && food.y == snake.yi flag = 0;}ifflag break;}}{setcolorfood.color;rectanglefood.x; food.y; food.x + 10; food.y + 10; }fori = snake.node - 1; i > 0; i--{snake.xi = snake.xi-1;snake.yi = snake.yi-1;}switchsnake.direction{case 1:snake.y0 -= 10;break;case 2:snake.y0 += 10;break;case 3:snake.x0 -= 10;break;case 4:snake.x0 += 10;}fori = 3; i < snake.node; i++{ifsnake.xi == snake.x0 && snake.yi == snake.y0{gameover;snake.life = 0;break;}}ifsnake.x0 < 40 || snake.x0 > 590 || snake.y0 < 50 || snake.y0 > 440{gameover;snake.life = 0;}ifsnake.life == 0break;ifsnake.x0 == food.x && snake.y0 == food.y /*蛇吃掉食物*/{rectanglefood.x; food.y; food.x + 10; food.y + 10;snake.xsnake.node = -20;snake.ysnake.node = -20;snake.colorsnake.node = food.color;snake.node++;food.yes = 0;food.color = random15 + 1;score += 10;prscore;ifscore % 100 == 0 && score = 0{fori = 0; i < snake.node; i++ /* 画出蛇 */{setcolorsnake.colori;rectanglesnake.xi; snake.yi; snake.xi + 10; snake.yi + 10;}sound200;delay50000;delay50000;delay50000;delay50000;delay50000;delay50000;gamespeed -= 5000;draw;}else{sound500;delay500;nosound;}}fori = 0; i < snake.node; i++ /* 画出蛇 */{setcolorsnake.colori;rectanglesnake.xi; snake.yi; snake.xi + 10; snake.yi + 10; }delaygamespeed;delaygamespeed;flag1 = 1;setcolor0;rectanglesnake.xsnake.node-1; snake.ysnake.node-1;snake.xsnake.node-1 + 10; snake.ysnake.node-1 + 10;ifkbhit && flag1 == 1 /*如果没按有效键就重新开始循环*/ {flag1 = 0;key = bioskey0;ifkey == escexit0;else ifkey == up && snake.direction = 2snake.direction = 1;else ifkey == down && snake.direction = 1snake.direction = 2;else ifkey == left && snake.direction = 4snake.direction = 3;else ifkey == right && snake.direction = 3snake.direction = 4;}}ifsnake.life == 0 /*如果蛇死了就退出循环*/break;}}void mainvoid{while1{color = 0;init;cleardevice;showword;draw;gameplay;setcolor15;settextstyle0; 0; 2;outtextxy200; 400; "CONTINUEY/N";while1{key = bioskey0;ifkey == Y || key == n || key == escbreak;}ifkey == n || key == escbreak;}closegraph;}3、c语言实现移动电话系统#include <stdio.h>#define GRID-SIZE 5#define SELECTED -1 /*低于矩阵中所有元素*/#define TRAFFIC-FILE “traffic.dat”/*关于交通数据的文件*/ #define NUM-TRANSMITTERS 10 /*可用的发射器数量*/void get-traffic-dataint commutersGRID-SIZEGRID-SIZE;int salesforceGRID-SIZEGRID-SIZE;int weekends GRID-SIZEGRID-SIZE;voide print-matrixGRID-SIZEGRID-SIZE;intmainvoid{int commutersGRID-SIZEGRID-SIZE;/*上午8:30的交通数据*/int salesforceGRID-SIZEGRID-SIZE; /*上午11:00的交通数据*/ int weekendGRID-SIZEGRID-SIZE;/*周末交通数据*/int commuter-weight; /*通勤人员数据的权重因子*/ sale-weight;/*营销人员数据的权重因子*/weekend-weight; /*周末数据的权重因子*/int location-i; /*每个发射器的位置*/location-j;int current-max; /*和数据中当前的最大值*/int i;j; /*矩阵的循环计数器*/tr; /*发射器的循环计数器*//*填入并显示交通数据*/Get-traffic-data commuters;salesforce;weekend;print-matrixcommuters;printf“\n\n WEEKEND TRAFFIC DATA\n\n”;print-matrixsalesforce;printf“\n\n WEEKEND TRAFFIC DATA\n\n”;printf_matrixweekeng;/*请用户输入权重因子*/printf“\n\nPlease input the following value:\n”;scanf“%d”;&commuter_weight;printf“weightan integer>=0 for the weekeng data>”;scanf“%d”;&weekend_weight;scanf“%d”;&weekend_weight;/*计算并显示加权后求和的数据*/for i=0;i<GRID_SIZE;++ifor j=0;j<GRID_SIZE;++jsummed_dataij=commuter_weight*commuterij+salesforce_weight*salesforceij+weekend_weight*weekendij;printf“\n\nThe weighted;summed data is :\n\n”;printf_matrixsummed_data;/*在summed_data矩阵中找出NUM_TRANSMITTERS个最大值;将坐标临时存储在location_i和location_j中;然后把最后的结果坐标输出*/printf“\n\nLocations of the %d transmitters:\n\n”;NUM_TRANSMITTERS;for tr=1;tr<=NUM_TRANSMITTERS;++tr{current_max=SELECTED;/*以一个过低的值为起点开始查找*/for i=0;i<GRID_SIZE;++i{forj=0;j<GRID_SIZE;++j{ifcurrent_max<summed_dataij{current_max=summed_dataij{location_i=i;location_j=j;}}}/*将选中的单元赋一较低的值以避免下次再选中这一元素;显示查找结果*/ summed_datalocation_ilocation_j=SELECTED;printf“Transmitter %3d:at location %3d %3d\n”;tr;location_i;location_j;}return 0;}/**把 TRAFFIC_FILE中的交通数据填充到3个GRID_SIZE×GRID_SIZE数组中*/voidget_traffic_dataint commutersGRID_SIZE;/*输出*/int salesforceGRID_SIZEGRID_SIZE;/*输出*/int weekendGRID_SIZEGRID_SIZE;/*输出*/{int i;j; /*循环计数器*/FILE *fp; /*文件指针*/fq=fopenTRAFFIC_FILE;“r”;fori=0;i<GRID_SIZE;++iforj=0;j<GRID_SIZE;++jfscanffp;“%d”;&commntersij;fori=0;i<GRID_SIZE;++jforj=0;j<GRID_SIZE;++jfscanffq;“%d”;&weekendij;fclosefq;}/**显示一个GRID_SIZE×GRID_SIZE整数矩阵的内容*/voidprint_matrixint matrixGRID_SIZEGRID_SIZE{int i;j; /*循环计数器*/ fori=0;i<GRID_SIZE;++j{ forj=0;j<GRID_SIZE;++Jprintf“%3d”;matrixij;printf“\n”;}}4、扑克牌游戏/*************************************CopyrightC 2004-2005 vision;math;NJU.File Name: guess_card.cppAuthor: vision Version: 1.0 Data: 23-2-2004Description: 给你9张牌;然后让你在心中记住那张牌;然后电脑分组让你猜你记住的牌在第几组;然后猜出你记住的那张牌.Other: 出自儿童时的一个小魔术History:修改历史**************************************/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <assert.h>#define CARDSIZE 52 /*牌的总张数*/#define SUITSIZE 13 /*一色牌的张数*//*扑克牌结构*/typedef struct Card{char val;/*扑克牌面上的大小*/int kind :4; /*扑克牌的花色*/}Card;/*************************************************Function: // riffleDescription: // 洗牌;然后随机的得到9张牌;要求九张牌不能有重复. Calls: //Called By: // mainTable Accessed: //被修改的表此项仅对于牵扯到数据库操作的程序Table Updated: // 被修改的表此项仅对于牵扯到数据库操作的程序Input: //Card card 牌结构; int size 结构数组的大小Output: //Return: // voidOthers: // 此函数修改card的值;希望得到九张随机牌Bug: //此函数有bug;有时会产生两个相同的牌;有待修订*************************************************/void riffleCard *cards; int size;/*************************************************Function: // showDescription: // 显示数组的内容Calls: //Called By: // mainTable Accessed: //被修改的表此项仅对于牵扯到数据库操作的程序Table Updated: // 被修改的表此项仅对于牵扯到数据库操作的程序Input: //Card *card 牌结构指针; int size 结构数组的大小Output: //Return: // voidOthers: //*************************************************/void showconst Card *cards; int size;/*************************************************Function: // groupingDescription: //把9张牌分别放到3个数组中;每组3张;a.e分组Calls: //Called By: // mainTable Accessed: //被修改的表此项仅对于牵扯到数据库操作的程序Table Updated: // 被修改的表此项仅对于牵扯到数据库操作的程序Input: //Card *card 牌结构指针; int size 结构数组的大小Output: //Return: // voidOthers: // 此函数修改 *carr1;*carr2;* carr3的值*************************************************/void groupingconst Card *cards; Card *carr1; Card *carr2; Card *carr3; /*************************************************Function: // result_processDescription: //用递归计算;所选的牌Calls: // rshiftCalled By: // mainTable Accessed: //被修改的表此项仅对于牵扯到数据库操作的程序Table Updated: // 被修改的表此项仅对于牵扯到数据库操作的程序Input: //Card *carr1; Card *carr2; Card *carr3Output: //Return: // voidOthers: // 此函数修改 *carr1;*carr2;* carr3的值*************************************************/Card* result_processCard *carr1; Card *carr2; Card *carr3; int counter; /*************************************************Function: // rshiftDescription: //右移操作Calls: //Called By: // result_processTable Accessed: //被修改的表此项仅对于牵扯到数据库操作的程序Table Updated: // 被修改的表此项仅对于牵扯到数据库操作的程序Input: //Card *carr1; Card *carr2; Card *carr3 ;int counter Output: //Return: // Card*Others: // 此函数修改 *carr1;*carr2;* carr3的值*************************************************/void rshiftCard *carr1; Card *carr2; Card *carr3; int counter;void main{Card cards9; /*存放九张牌*/Card carr13; /*第一组牌;cards array 1*/Card carr23; /*第二组牌;cards array 2*/Card carr33; /*第三组牌;cards array 3*/int select = 0; /*玩家的选择*/Card *selected_card;/*存放玩家所记住选的牌*/int counter = 0;rifflecards; 9; /*洗牌;得到九张牌*/puts"请记住一张牌千万别告诉我最多经过下面三次我与你的对话;我就会知道你所记的那张牌";puts"如果想继续玩;请准确的回答我问你的问题;根据提示回答";puts"请放心;我不会问你你选了哪张牌的";groupingcards; carr1; carr2; carr3; /*把9张牌分别放到3个数组中;每组3张;a.e分组*/showcarr1; 3;showcarr2; 3;showcarr3; 3;puts"请告诉我你记住的那张牌所在行数";select = getchar;switchselect/*分支猜你玩家记住的牌*/{case '1':selected_card = result_processcarr1; carr2; carr3; counter;break;case '2':selected_card = result_processcarr2; carr3; carr1; counter;break;case '3':selected_card = result_processcarr3; carr1; carr2; counter;break;default:puts"你在撒谎不和你玩了";fflushstdin;getchar;exit0;}if selected_card ==NULL{fflushstdin;getchar;exit0;}puts"你猜的牌为:";showselected_card; 1;puts"我猜的对吧;哈哈~~~~";fflushstdin;getchar;}/*riffle的原代码*/void riffleCard *cards; int size{char deckCARDSIZE;/*临时数组;用于存储牌*/ unsigned int seed;/*最为产生随机数的种的*/ int deckp = 0; /*在牌的产生中起着指示作用*/seed = unsigned inttimeNULL;srandseed;/*洗牌*/while deckp < CARDSIZE{char num = rand % CARDSIZE;if memchrdeck; num; deckp == 0{assertmemchrdeck;num;deckp;deckdeckp = num;deckp++;}}/*找9张牌给card*/for deckp = 0; deckp < size; deckp++{div_t card = divdeckdeckp; SUITSIZE;cardsdeckp.kind = "3456"card.quot; /*把商给card.kind*/ }}/*show的原代码;将会自动换行*/void showconst Card *cards; int size{forint i = 0; i < size; i++{printf"%c%c ";cardsi.kind;cardsi.val;if i =0 && i+1 % 3 == 0puts"";}puts""; /*自动换行*/}/*grouping 的原代码*/void groupingconst Card *cards; Card *carr1; Card *carr2; Card *carr3 {int i = 0;/*循环参数*//*分给carr1三个数*/while i < 3{carr1i.val = cardsi.val;carr1i.kind = cardsi.kind;i++;}/*分给carr2接下来的三个数*/while i < 6{carr2i-3.val = cardsi.val;carr2i-3.kind = cardsi.kind;i++;}/*分给carr3接下来的三个数*/while i < 9{carr3i-6.val = cardsi.val;carr3i-6.kind = cardsi.kind;i++;}}/*rshift的实现*/void rshiftCard *carr1; Card *carr2; Card *carr3; int counter {Card temp2;/*用于存放carr2counter*/Card temp3;/*用于存放carr3counter*//*temp = carr2*/temp2.val = carr2counter.val;temp2.kind = carr2counter.kind;/*carr2 = carr1*/carr2counter.val = carr1counter.val;carr2counter.kind = carr1counter.kind;/*temp3 = carr3*/temp3.val = carr3counter.val;temp3.kind = carr3counter.kind;/*carr3 = carr2*/carr3counter.val = temp2.val;carr3counter.kind = temp2.kind;/*carr1 = carr3*/carr1counter.val = temp3.val;carr1counter.kind = temp3.kind;}Card* result_processCard *carr1; Card *carr2; Card *carr3; int counter {rshiftcarr1; carr2; carr3; counter; /* 把数组的第一个元素依次右移*/ ifcounter == 2{return&carr22;}showcarr1; 3;showcarr2; 3;showcarr3; 3;puts"请给出你记住的牌所在行数:";fflushstdin;int input = 1;input = getchar; /*获取你选的组*/switchinput{case '1':returnresult_processcarr1; carr2; carr3; ++counter;break;case '2':return&carr2counter;break;default:puts"你在撒谎不和你玩了";return NULL;}}5、C语言实现打字游戏#include "stdio.h"#include "time.h"#include "stdlib.h"#include "conio.h"#include "dos.h"#define xLine 70#define yLine 20#define full 100#define true 1#define false 0/*---------------------------------------------------------------------*/void printScreenint level;int right;int sum;char pyLinexLine/* 刷新屏幕的输出图像 */{int i;j;clrscr;printf"level:%d Press 0 to exit;1 to pause score:%d/%d\n";level;right;sum;/* 输出现在的等级;击中数和现在已下落总数 */printf"----------------------------------------------------------------------\n";for i=0;i<yLine;i++{forj=0;j<xLine;j++printf "%c";pij;printf"\n";}/* for i */printf"----------------------------------------------------------------------\n";}/* printScreen *//*---------------------------------------------------------------------*/void leave/* 离开程序时;调用该函数结束程序.. */{clrscr;printf "\n\n\n\nThank you for playing.";delay 2500;exit 0;}/*----------------------------------------------------------------------*/int levelChoiceint level/* 进入游戏时选择游戏等级 */{while true/* void */{clrscr ;printf"please input 1-9 to choice level.choice 0 to return.\n";level=getch;level=level-48;if level>0&&level<10 return level;else if level==0leave ;elseprintf "Please input a correct number\n";}/* while true */}/* levelChoice *//*---------------------------------------------------------------------*/int newWordint sum;char pyLinexLine/* 随生成一个新的字符并将其加入数组的首行 */{int j;w;if sum=full{j=rand%xLine-2+1;w=rand%26+65;p0j=w;return ++sum;}/* if */return sum;}/* newWord *//*---------------------------------------------------------------------*/int movingint miss;char pyLinexLine/* 将最后一行置空;并使所有在数组中其他行的字符下降一行 */{int i;j;char w;for j=1;i=yLine-1;j<xLine-1;j++/* 遍历最后一行的所有字符;如果该字符非空则将其置空并使miss加一 */{if pij=' '{miss++;pij=' ';}}for i=yLine-2;i>=0;i--/* 从倒数第二行的最后一个字符开始开始向前遍历该数组内的元素;如果该位置非空则将该字符移动至下一行 */{for j=xLine-2;j>0;j--{if pij=' '{w=pij;pij=' ';pi+1j=w;}/* if */}/* forj */}/* fori */return miss;}/* moving *//*---------------------------------------------------------------------*/int wordHitchar pyLinexLine/*判断是否有字符从键盘键入..如果有;则从最后一行的最后一个元素开始遍历该数组;找出该字符;并把对应位置置空;且返回1..如果有输入;但屏幕上无对应项;或无输入则返回0*/{int i;j;char key;ifkbhit/* 判断用户是否从键盘键入字符..如果kbhit返回值为 */key=getch;ifkey{。
c语言小游戏代码
c语言小游戏代码#include <stdio.h>#include <stdlib.h>#include <windows.h>// 定义元素类型#define ELEMENT char// 游戏行数#define ROW 10// 游戏显示延迟#define SLEEPTIME 100int main(int argc, char *argv[]){// 定义游戏的棋盘,用数组存放ELEMENT array[ROW][ROW];// 定义获胜条件int winCondition = 5;// 初始化,把棋盘清空system("cls");int i,j;for(i = 0; i < ROW; i++){for(j = 0; j < ROW; j++){array[i][j] = ' ';}}// 循环游戏,当有一方满足胜利条件时终止int tmp;int count = 0; // 存放棋子数while(1){// 依次取出玩家记录的棋子int x, y;// 如果已经有子落下,则计算是第几步if(count > 0){printf("第%d步:\n", count);}// 显示游戏棋盘for(i = 0; i < ROW; i++){printf(" ");for(j = 0; j < ROW; j++){printf("---");}printf("\n|");for(j = 0; j < ROW; j++){printf("%c |", array[i][j]);}printf("\n");}printf(" ");for(j = 0; j < ROW; j++){printf("---");}printf("\n");// 要求玩家输入放下棋子的位置printf("请玩家输入要放弃棋子的位置(1-%d)\n", ROW); printf("横坐标:");scanf("%d", &x);printf("纵坐标:");scanf("%d", &y);// 判断棋子位置是否有效if(x < 1 || x > ROW || y < 1 || y > ROW || array[x-1][y-1] != ' '){printf("输入错误!\n");system("pause");system("cls");continue;}// 把棋子记录,并计数if(count % 2 == 0){array[x-1][y-1] = 'X';}else{array[x-1][y-1] = 'O';}count++;// 判断是否有获胜者int i, j, k;int tempx, tempy;for(i = 0; i < ROW; i++){for(j = 0; j < ROW; j++){if(array[i][j] == 'X' || array[i][j] == 'O') {// 判断横向是否有获胜者tmp = 1;for(k = 1; k < winCondition; k++){// 注意边界,必须验证范围有效if(j + k > ROW - 1) break;// 如果和前一个位置的棋子相同,则计数加1,否则跳出if(array[i][j+k] == array[i][j])tmp++;else break;}// 如果计数满足获胜条件,则显示获胜者if(tmp >= winCondition){printf("玩家 %c 获胜!\n", array[i][j]);system("pause");return 0;}// 判断纵向是否有获胜者tmp = 1;for(k。
2048游戏C语言源代码(后缀txt改成cpp可直接运行)
{
temp[j]=code[i][j];/*把一行数移到中间变量*/
}
temp[4]=0;
{
code[i][j]=2;/*随机选一个空格填上2或4*/
}
move++;/*增加次数*/
}
print();/*显示*/
input=getch();/*输入方向*/
{
for(j=0;j<=3;j++)
{
if(code[i][j]==0)
{
printf("| ");/*0显示空格*/
}
else
printf("Score:%d Move:%d\n",score,move);
printf("Made by Yanjisheng\n");
printf("|-------------------|\n");/*显示横向分隔线*/
for(i=0;i<=3;i++)
}
}
break;
case 'S':
case 's':/*下*/
for(j=0;j<=3;j++)
{
}while(code[i][j]!=0);
if(((unsigned)rand())%4==0)
{
code[i][j]=4;
}
else
{
int i,j;
// clrscr();/*清屏*/
魂斗罗c语言源代码
"魂斗罗"(Contra)是一款经典的横版射击游戏,由Konami公司开发。
编写一个完整的魂斗罗游戏是一个复杂的过程,涉及图形渲染、物理模拟、碰撞检测、音效处理等多个方面。
通常,这样的游戏会使用专业的游戏引擎或者特定的框架来开发,而不是直接用C语言从头开始。
c#include<stdio.h>#include<stdbool.h>// 定义玩家和敌人的结构体typedef struct {int x;int y;bool isAlive;} Player;typedef struct {int x;int y;bool isAlive;int speed; // 敌人的移动速度} Enemy;// 定义游戏的常量const int SCREEN_WIDTH = 800;const int SCREEN_HEIGHT = 600;const int PLAYER_SPEED = 5;const int BULLET_SPEED = 10;const int ENEMY_SPEED = 3;// 初始化玩家和敌人Player player = {SCREEN_WIDTH / 2, SCREEN_HEIGHT - 50, true};Enemy enemy = {SCREEN_WIDTH, SCREEN_HEIGHT / 2, true, ENEMY_SPEED};// 游戏主循环void gameLoop() {while (player.isAlive && enemy.isAlive) {// 处理玩家输入char input;printf("Move player (w/a/s/d): ");scanf(" %c", &input);switch (input) {case'w':if (player.y > 0) {player.y -= PLAYER_SPEED;}break;case'a':if (player.x > 0) {player.x -= PLAYER_SPEED;}break;case's':if (player.y < SCREEN_HEIGHT - 50) {player.y += PLAYER_SPEED;}break;case'd':if (player.x < SCREEN_WIDTH - 50) {player.x += PLAYER_SPEED;}break;}// 移动敌人enemy.x -= enemy.speed;// 检查碰撞if (player.x < enemy.x + 50 && player.x + 50 > enemy.x && player.y < enemy.y + 50 && player.y + 50 > enemy.y) {player.isAlive = false;printf("Game Over!\n");}// 打印游戏状态(仅用于演示)printf("Player position: (%d, %d)\n", player.x, player.y);printf("Enemy position: (%d, %d)\n", enemy.x, enemy.y);}}int main() {printf("Welcome to Contra Demo!\n");gameLoop();return0;}这。