,,,大神,求份visual c++ 游戏代码,,,(替朋友上 课程设计,,,看到人家 啪啪啪地敲个不停,,,连一个妹子都用鄙视的眼光看我,,,真心尴尬,,,)
2条回答 默认 最新
- Amy安 2015-07-14 01:37关注
#include<stdio.h> #include<bios.h> #include<conio.h> #include<dos.h> #include<graphics.h> #include<alloc.h> #include<stdlib.h> #include<time.h> #define LEFT 0x4B00 #define RIGHT 0x4D00 #define UP 0x4800 #define DOWN 0x5000 #define ESC 0x011B #define ENTER 0x1C0D char s1_title[]="Snake game"; char s1_choose[3][11]={"start game","author","exit"}; char s2_title[]="Snake game,made by HungryAnt!"; char s2_fail[]="Game over!"; char s2_win[]="You win!!!"; int s1_x=320-45; int s1_y=240-33; int s2_x=320-150; int s2_y=240-150; int s3_x=320-120; int s3_y=240-50; char map[30][30];/*空地0,蛇身1,食物2*/ int length; int direction=2;/*上,下,左,右:0,1,2,3*/ int delay_time=20;/*延时,单位10毫秒*/ int difficult=0;/*游戏难度*/ int game_out=0; struct snake{/*************蛇结构体**************/ char x; char y; struct snake *previous;/*前*/ struct snake *next; }*head,*tail; void initsnake(){/*************蛇初始化**************/ struct snake *sn; head=sn=(struct snake *)malloc(sizeof(struct snake));/*蛇头*/ sn->x=14; sn->y=15; map[15][14]=1; sn->previous=NULL; sn->next=(struct snake *)malloc(sizeof(struct snake)); sn->next->previous=sn; sn=sn->next; sn->x=15; sn->y=15; map[15][15]=1; sn->next=(struct snake *)malloc(sizeof(struct snake)); sn->next->previous=sn; sn=sn->next; sn->x=16; sn->y=15; map[15][16]=1; sn->next=NULL; tail=sn;/*蛇尾*/ } void barbox(int x,int y,int color,int width,int height){/*填充一定范围的函数*/ setfillstyle(SOLID_FILL,color); bar(x,y,x+width-1,y+height-1); } void box(int x,int y,int color){/*填充地图小方格函数*/ barbox(x*10+1,y*10+1,color,9,9); } int choose(){/*在s1窗口里的选择框里进行选择*/ int key,i=0,j; do{ j=i; barbox(1,i*(240-s1_y)*2/3+1,LIGHTGRAY,(320-s1_x)*2-2,(240-s1_y)*2/3-1);/*设置浅灰色的选择条*/ setcolor(BLUE); outtextxy(4,i*(240-s1_y)*2/3+9,s1_choose[i]); setcolor(WHITE); outtextxy(3,i*(240-s1_y)*2/3+8,s1_choose[i]); while(bioskey(1)==0); key=bioskey(0); switch(key){ case UP: if(i>0)j=i--; break; case DOWN: if(i<2)j=i++; break; case ESC: exit(0); break; case ENTER: return i; } if(j!=i){ barbox(1,j*(240-s1_y)*2/3+1,DARKGRAY,(320-s1_x)*2-2,(240-s1_y)*2/3-1);/*设置浅灰色的选择条*/ setcolor(WHITE); outtextxy(3,j*(240-s1_y)*2/3+8,s1_choose[j]); } }while(1); } void s1_window(){/*进入程序的第一个界面--中间的窗口*/ int i=0; setviewport(s1_x,s1_y,640-s1_x-1,480-s1_y-1,0); setcolor(LIGHTBLUE);/*画两个连在一起的框架*/ rectangle(-1,-26,(320-s1_x)*2,-1); /*标题部分*/ rectangle(-1,-1,(320-s1_x)*2,(240-s1_y)*2+1);/*选择部分*/ barbox(0,-25,BLUE,(320-s1_x)*2,24);/*设置标题框架填充*/ settextstyle(0,0,1); setcolor(WHITE); outtextxy(5,-17,s1_title);/*输出标题*/ while(i<3){ barbox(1,i*(240-s1_y)*2/3+1,DARKGRAY,(320-s1_x)*2-2,(240-s1_y)*2/3-1);/*设置深灰色的条*/ setcolor(WHITE); outtextxy(3,i*(240-s1_y)*2/3+8,s1_choose[i]); i++; } } void s2_window(){/*第二个界面--游戏界面*/ int x,y; setviewport(s2_x,s2_y,640-s2_x-1,480-s2_y-1,0); clearviewport(); setcolor(LIGHTBLUE);/*画两个连在一起的框架*/ rectangle(-1,-30,(320-s2_x)*2+1,-1); /*分值框架*/ barbox(0,-29,BLUE,(320-s2_x)*2+1,28);/*设置分值框架填充*/ settextstyle(0,0,1); setcolor(WHITE); outtextxy(5,-17,s2_title);/*输出标题*/ setcolor(LIGHTGRAY); rectangle(-1,0,(320-s2_x)*2+1,(240-s2_y)*2+1);/*地图框架*/ rectangle(0,0,(320-s2_x)*2,(240-s2_y)*2); for(y=0;y<300;y+=10)/*地图绘制*/ for(x=0;x<300;x+=10) barbox(x+1,y+1,DARKGRAY,9,9); } void initmap(){/*初始化地图*/ int x,y; for(y=0;y<30;y++) for(x=0;x<30;x++) map[y][x]=0; } void setfood(){/*随机产生一个食物*/ int x,y; do{ x=rand()%30; y=rand()%30; }while(map[y][x]==1); map[y][x]=2; box(x,y,YELLOW); } void printsnake(){/*显示出蛇*/ struct snake *sn=head; box(sn->x,sn->y,LIGHTBLUE); sn=head->next; while(sn!=NULL){ box(sn->x,sn->y,LIGHTGREEN); sn=sn->next; } } void cleartime(){/*时间设置为0*/ struct time time_0; time_0.ti_min=0; time_0.ti_hour=0; time_0.ti_hund=0; time_0.ti_sec=0; settime(&time_0); } void freesnake(){/***************释放蛇所占的内存**************/ struct snake *sn,*sn1; sn=head; head=NULL; while(sn!=NULL){/*free蛇所占的内存*/ sn1=sn->next; free(sn); sn=sn1; } } void snakemove(){/***蛇移动***/ struct snake *sn; sn=head; head=(struct snake *)malloc(sizeof(struct snake));/*蛇移动一格*/ head->x=sn->x; head->y=sn->y; switch(direction){ case 0: head->y--; break; case 1: head->y++; break; case 2: head->x--; break; case 3: head->x++; break; } if(head->x<0 || head->x>29 || head->y<0 || head->y>29 || map[head->y][head->x]==1){/*如果蛇超出地图或者撞到自己,游戏失败*/ freesnake(); setviewport(0,0,639,479,0); settextstyle(0,0,3); setcolor(CYAN); outtextxy(205,210,s2_fail);/****************显示game over****************/ getch(); clearviewport();/*清除屏幕内容*/ game_out=1; return; } else{ head->next=sn; sn->previous=head; head->previous=NULL; if(map[head->y][head->x]==2){/*如果遇到食物*/ length++;/*长度增加*/ if(length==30){ freesnake(); setviewport(0,0,639,479,0); settextstyle(0,0,3); setcolor(WHITE); outtextxy(206,211,s2_win);/****************显示you win****************/ setcolor(LIGHTRED); outtextxy(205,210,s2_win);/****************显示you win****************/ getch(); clearviewport();/*清除屏幕内容*/ game_out=1; return; } difficult=(length-3)/3; delay_time=20-difficult; map[head->y][head->x]=1; setfood();/*再添加一个食物*/ } else{/*如果没有遇到食物则清除蛇尾*/ map[tail->y][tail->x]=0; sn=tail; tail=tail->previous; tail->next=NULL; map[sn->y][sn->x]=0; box(sn->x,sn->y,DARKGRAY); free(sn); map[head->y][head->x]=1; } } } void gamestart(){/*****游戏进行中*****/ int key; char l[4]; struct time t_last; setfood(); while(1){ barbox((320-s2_x)*2-28,-29,BLUE,27,28);/*覆盖上次蛇长*/ sprintf(l,"%d",length); setcolor(YELLOW); outtextxy((320-s2_x)*2-28,-17,l);/********************输出蛇长*/ printsnake(); cleartime(); key=0; while(1){ gettime(&t_last); if(t_last.ti_hund>delay_time)break;/*如果超过延时时间就退出*/ if(bioskey(1)){ key=bioskey(0); switch(key){ case UP: if(direction!=1)direction=0; break; case DOWN: if(direction!=0)direction=1; break; case LEFT: if(direction!=3)direction=2; break; case RIGHT: if(direction!=2)direction=3; break; } break; } } snakemove(); if(key==ESC){ freesnake(); game_out=1; setviewport(0,0,639,479,0); clearviewport(); return; } if(game_out==1)return; } } void s2(){/*游戏*/ length=3; direction=2; game_out=0; s2_window();/*界面*/ initmap();/*地图初始化*/ initsnake();/*蛇初始化*/ gamestart();/*游戏开始*/ if(game_out==1)return; } void s3_window(){ char author[]="Author: HungryAnt"; char email[]="E-mail: ljsunlin@126.com"; char blog[]="http://hi.baidu.com/zhongji"; char QQ[]="QQ:517377100"; setviewport(0,0,639,479,0);/*首先清屏*/ clearviewport(); setviewport(s3_x,s3_y,640-s3_x-1,480-s3_y-1,0); setcolor(LIGHTBLUE); rectangle(-1,-1,(320-s3_x)*2,(240-s3_y)*2);/*个人信息*/ barbox(0,0,BLUE,(320-s3_x)*2,(240-s3_y)*2);/*设置标题框架填充*/ settextstyle(0,0,1);/*输出作者信息*/ setcolor(BLACK); outtextxy(6,10,author); outtextxy(6,35,email); outtextxy(6,60,blog); outtextxy(6,85,QQ); setcolor(WHITE); outtextxy(5,9,author); outtextxy(5,34,email); outtextxy(5,59,blog); outtextxy(5,84,QQ); getch(); setviewport(0,0,639,479,0);/*清屏*/ clearviewport(); } void s3(){/*显示作者信息*/ s3_window(); } void s1(){ int i; while(1){ s1_window(); i=choose(); switch(i){ case 0: s2();/*游戏*/ break; case 1: s3();/*作者*/ break; case 2: exit(0); } } } void s_detectgraph(){/*自定义图形检测*/ int gdriver,gmode,errorcode; gdriver=VGA; gmode=VGAHI; registerbgidriver(EGAVGA_driver); initgraph(&gdriver,&gmode,""); errorcode = graphresult(); if (errorcode !=0){ printf("\n\t\t\tGame : Snake Game\n"); printf("\t\t\tAuthor : HungryAnt\n"); printf("\n\terror:\t%s\n", grapherrormsg(errorcode)); getch(); exit(1); } } int main(){ srand((unsigned) time(NULL));/*设置随机数不同*/ s_detectgraph(); /*图形检测*/ s1(); getch(); closegraph(); }
贪吃蛇 照着敲吧_(:зゝ∠)_
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 支付宝h5参数如何实现跳转
- ¥15 MATLAB代码补全插值
- ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
- ¥15 前后端分离的学习疑问?
- ¥15 stata实证代码答疑
- ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
- ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
- ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
- ¥15 husky+kinova jaco2 仿真
- ¥15 zigbee终端设备入网失败