• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

贪吃蛇(C++实现,VC6.0编译,使用了EasyX图形库)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

程序效果:

 

 

 

 

代码:

 

//main.cpp
1
#include <iostream> 2 #include<fstream> 3 #include <graphics.h> 4 #include <conio.h> 5 #include<ctime> 6 #include<windows.h> 7 #include<mmsystem.h> 8 #pragma comment(lib,"Winmm.lib") 9 10 #define IMGWIDTH 20 11 #define IMGHEIGHT 20 12 #define WIDTH 1100 13 #define HEIGHT 650 14 #define UP 72 15 #define DOWN 80 16 #define LEFT 75 17 #define RIGHT 77 18 #define HEADUP "headup.jpg" 19 #define HEADDOWN "headdown.jpg" 20 #define HEADLEFT "headleft.jpg" 21 #define HEADRIGHT "headright.jpg" 22 #define BODYLR "bodylr.jpg" 23 #define BODYUD "bodyud.jpg" 24 #define TAILL "taill.jpg" 25 #define TAILR "tailr.jpg" 26 #define TAILU "tailu.jpg" 27 #define TAILD "taild.jpg" 28 #define WALL "wall.jpg" 29 #define FOOD "food.jpg" 30 #define GAMEOVER "gameover.jpg" 31 #define SNAKE "snake.jpg" 32 #define L 60 // 33 #define U 80 // 34 #define R 900 // 35 #define D 600 // 36 using namespace std; 37 38 IMAGE wallimg; 39 int score=0,grade=1,rec[4]; 40 class Food; 41 class BodyNode{ 42 private: 43 IMAGE img; 44 int x; 45 int y; 46 BodyNode* next; 47 BodyNode(){} 48 friend class Body; 49 friend bool check(Body& snake,Food& food); 50 public: 51 BodyNode(char* str,int X,int Y){ 52 x=X; 53 y=Y; 54 loadimage(&img, _T(str)); 55 next=NULL; 56 } 57 BodyNode(BodyNode& bodynode){ 58 x=bodynode.x; 59 y=bodynode.y; 60 next=bodynode.next; 61 img=bodynode.img; 62 } 63 }; 64 class Body{ 65 private: 66 BodyNode* head; 67 BodyNode* tail; 68 int length; 69 public: 70 friend bool check(Body& snake,Food& food); 71 Body(){ 72 head=new BodyNode(HEADRIGHT,L+(10*(IMGWIDTH)),U+(10*(IMGHEIGHT))); 73 head->next=new BodyNode(BODYLR,head->x-head->img.getwidth(),head->y); 74 head->next->next=new BodyNode(TAILR,head->next->x-head->img.getwidth(),head->y); 75 tail=head->next->next; 76 length=2; 77 } 78 int Length(){return length;} 79 bool IsDead(){ 80 BodyNode* p=head->next; 81 while(p!=NULL){ 82 if(head->x==p->x&&head->y==p->y){ 83 return true; 84 } 85 p=p->next; 86 } 87 return false; 88 } 89 int Move(int dir){ //1 上 2下 3左 4 右 90 BodyNode* p=head->next; 91 int tempx=head->x,tempy=head->y,temp_x,temp_y; 92 while(p!=tail->next){ 93 temp_x=p->x; 94 temp_y=p->y; 95 p->x=tempx;; 96 p->y=tempy; 97 tempx=temp_x; 98 tempy=temp_y; 99 p=p->next; 100 } 101 if(dir==1){ 102 loadimage(&(head->img), _T(HEADUP)); 103 head->y=head->next->y-head->img.getheight(); 104 } 105 if(dir==2){ 106 loadimage(&(head->img), _T(HEADDOWN)); 107 head->y=head->next->y+head->img.getheight(); 108 } 109 if(dir==3){ 110 loadimage(&(head->img), _T(HEADLEFT)); 111 head->x=head->next->x-head->img.getwidth(); 112 } 113 if(dir==4){ 114 loadimage(&(head->img), _T(HEADRIGHT)); 115 head->x=head->next->x+head->img.getwidth(); 116 } 117 if((head->x+head->img.getwidth())>R){head->x=L;} 118 if(head->x<L){head->x=R-head->img.getwidth();} 119 if((head->y+head->img.getheight())>D){head->y=U;} 120 if(head->y<U){head->y=D-head->img.getheight();} 121 p=head; 122 while(p->next!=tail){ 123 p=p->next; 124 } 125 if(p->x-tail->x>0){loadimage(&(tail->img), _T(TAILR));} 126 if(p->x-tail->x<0){loadimage(&(tail->img), _T(TAILL));} 127 if(p->y-tail->y>0){loadimage(&(tail->img), _T(TAILD));} 128 if(p->y-tail->y<0){loadimage(&(tail->img), _T(TAILU));} 129 return 0; 130 } 131 int Grow(){ 132 BodyNode*p=head; 133 while(p->next!=tail){ 134 p=p->next; 135 } 136 tail->img=p->img; 137 tail->next=new BodyNode(&(TAILL[0]),tail->x-(p->x-tail->x),tail->y-(p->y-tail->y)); 138 139 tail=tail->next; 140 if(p->x-tail->x>0){loadimage(&(tail->img), _T(TAILR));} 141 if(p->x-tail->x<0){loadimage(&(tail->img), _T(TAILL));} 142 if(p->y-tail->y>0){loadimage(&(tail->img), _T(TAILD));} 143 if(p->y-tail->y<0){loadimage(&(tail->img), _T(TAILU));} 144 tail->next=NULL; 145 length++; 146 return 0; 147 } 148 int Show(){ 149 BodyNode* p=head; 150 while(p!=NULL){ 151 putimage(p->x, p->y, &(p->img)); 152 p=p->next; 153 } 154 return 0; 155 } 156 }; 157 class Food{ 158 private: 159 int x; 160 int y; 161 IMAGE rat; 162 public: 163 friend bool check(Body& snake,Food& food); 164 Food(){ 165 x=400; 166 y=300; 167 loadimage(&(rat), _T(FOOD)); 168 } 169 int givefood(Body& snake){ 170 srand((unsigned)time(0)); 171 while(check(snake,*this)==true){ 172 x=0;y=0; 173 while(!(x>L&&y>U)){ 174 x= rand()%900; 175 y= rand()%600; 176 } 177 x=x-(x%20); 178 y=y-(y%20); 179 } 180 return 0; 181 } 182 int Show(){ 183 putimage(x,y,&(rat)); 184 return 0; 185 } 186 }; 187 int showgame(Body& snake,Food food){ 188 189 cleardevice(); 190 BeginBatchDraw(); 191 setbkcolor(RGB(0,0,0)); //设置背景色 192 setcolor(YELLOW); //设置绘图色 193 outtextxy((WIDTH/2)-80, 10, "贪吃蛇"); 194 outtextxy(L+300, U-IMGHEIGHT-20, "按空格 暂停"); 195 outtextxy(R+IMGWIDTH+20, U+IMGHEIGHT+20, "最高记录:"); 196 char rec1[10];//,rec2[10],rec3[10]; 197 for(int index=0;index<=2;index++){ 198 sprintf(rec1, "%d",rec[index]); 199 outtextxy(R+IMGWIDTH+20, U+IMGHEIGHT+20+(index+1)*30, rec1); 200 } 201 int i,j; 202 for(i=L-IMGWIDTH;i<=R;i=i+IMGWIDTH){ 203 j=U-IMGHEIGHT; 204 putimage(i,j,&(wallimg)); 205 } 206 for(j=U;j<=D;j=j+IMGHEIGHT){ 207 i=L-IMGWIDTH; 208 putimage(i,j,&(wallimg)); 209 } 210 for(j=U;j<=D;j=j+IMGHEIGHT){ 211 i=R; 212 putimage(i,j,&(wallimg)); 213 } 214 for(i=L-IMGWIDTH;i<=R;i=i+IMGWIDTH){ 215 j=D; 216 putimage(i,j,&(wallimg)); 217 } 218 outtextxy(L-10, U-IMGHEIGHT-20, "得分:"); 219 char s[10]; 220 sprintf(s, "%d",score); 221 outtextxy(L+60, U-IMGHEIGHT-20, s); 222 outtextxy(L+120, U-IMGHEIGHT-20, "长度:"); 223 char l[10]; 224 sprintf(l, "%d",snake.Length()); 225 outtextxy(L+180, U-IMGHEIGHT-20, l); 226 227 snake.Show(); 228 229 food.Show(); 230 FlushBatchDraw(); 231 Sleep(50*grade); 232 233 return 0; 234 } 235 bool check(Body& snake,Food& food){ 236 BodyNode* pl=snake.head; 237 bool flag=false; 238 while(pl!=snake.tail->next){ 239 240 if((pl->x==food.x)&&(pl->y==food.y)){flag=true;break;} 241 242 pl=pl->next; 243 } 244 return flag; 245 } 246 DWORD WINAPI Fun1Proc(LPVOID IpParameter) 247 { 248 mciSendString("play yeah.mp3 repeat", NULL, 0, NULL);//播放 249 return 0; 250 } 251 int menu(){ 252 //cleardevice(); 253 outtextxy((WIDTH/2)-80, 120, "贪吃蛇"); 254 outtextxy((WIDTH/2)-180, 230, "按↑和↓选择难度"); 255 outtextxy((WIDTH/2)-180, 250, "按空格键确定选择"); 256 return 0; 257 } 258 int record(){ 259 fstream f1("record.txt"); //打开文件,若文件不存在就创建它 260 if(!f1) return -1; 261 //int a=0,b=0,c=0; 262 f1>>rec[0]>>rec[1]>>rec[2]; 263 264 f1.close(); 265 return 0; 266 } 267 int saverecord(){ 268 system("del record.txt "); 269 ofstream f1("record.txt"); //打开文件,若文件不存在就创建它 270 if(!f1) return -1; 271 int flag=1; 272 int temp; 273 rec[3]=score; 274 while(flag==1){ 275 flag=0; 276 for(int i=0;i<=2;i++){ 277 if(rec[i]<rec[i+1]){ 278 flag=1; 279 temp=rec[i]; 280 rec[i]=rec[i+1]; 281 rec[i+1]=temp; 282 } 283 } 284 } 285 286 f1<<rec[0]<<"\n"<<rec[1]<<"\n"<<rec[2]; 287 288 f1.close(); 289 if(score!=rec[3]){ 290 settextstyle(50, 0, _T("宋体")); 291 outtextxy(WIDTH/2-(50*5),U+40, "恭喜你创造了新纪录!"); 292 } 293 return 0; 294 } 295 int main(){ 296 for(int i=0;i<=3;i++){ 297 rec[i]=0; 298 } 299 record(); 300 IMAGE snakeimg; 301 302 HANDLE hThread1; 303 hThread1 = CreateThread(NULL,0,Fun1Proc,NULL,0,NULL); 304 char key; 305 int movekey=4; 306 int movekeyold=movekey; 307 initgraph(WIDTH,HEIGHT); 308 char temp=0,choose; 309 while(temp!=' '){ 310 cleardevice(); 311 if(kbhit()){ 312 choose=getch(); 313 fflush(stdin); 314 switch(choose){ 315 case UP:{ 316 grade++; 317 };break; 318 case DOWN:{ 319 grade--; 320 }; 321 } 322 } 323 if(grade==4){grade=3;} 324 if(grade==0){grade=1;} 325 outtextxy(420,350,"简单"); 326 outtextxy(420,380,"中等"); 327 outtextxy(420,410,""); 328 // cleardevice(); 329 menu(); 330 outtextxy(300,300,"请选择难度:"); 331 if(grade==3){ 332 outtextxy(390,350,""); 333 } 334 if(grade==2){ 335 outtextxy(390,380,""); 336 } 337 if(grade==1){ 338 outtextxy(390,410,""); 339 } 340 temp=getch(); 341 } 342 loadimage(&(wallimg), _T(WALL)); 343 Body snake; 344 Food food; 345 showgame(snake,food); 346 while(snake.IsDead()==false){ 347 while(snake.IsDead()==false&&(!kbhit())){ 348 349 if(movekey==1&&movekeyold==2||movekey==2&&movekeyold==1||movekey==3&&movekeyold==4||movekey==4&&movekeyold==3){ 350 movekey=movekeyold;} 351 movekeyold=movekey; 352 snake.Move(movekey); 353 showgame(snake,food); 354 if(check(snake,food)==true){ 355 mciSendString("play yeah.wav", NULL, 0, NULL);//播放 356 cout<<'\a'; 357 score+=10; 358 food.givefood(snake); 359 snake.Grow(); 360 snake.Grow(); 361 snake.Grow(); 362 } 363 } 364 if(kbhit()){ 365 key=getch(); 366 switch(key){ 367 case UP:{movekey=1; 368 };break; 369 case DOWN:{movekey=2; 370 };break; 371 case LEFT:{movekey=3; 372 };break; 373 case RIGHT:{movek

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#执行异步操作的几种方式发布时间:2022-07-18
下一篇:
c++依据输入动态声明数组(一维,二维)发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap