貪吃蛇(C++實現,VC6.0編譯,使用了EasyX圖形庫)

?

程序效果:

?

?

?

?

代碼:

?

//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:{movekey=4; 374 };break; 375 case ' ':{int t=0; 376 while(t==0){ 377 if(kbhit()){ 378 key=getch(); 379 if(key==' '){ 380 t=1; 381 } 382 } 383 } 384 }; 385 } 386 } 387 } 388 CloseHandle(hThread1); 389 BeginBatchDraw(); 390 showgame(snake,food); 391 IMAGE gameoverimg; 392 loadimage(&(gameoverimg), _T(GAMEOVER)); 393 putimage(WIDTH/2-(gameoverimg.getwidth()/2),HEIGHT/2-(gameoverimg.getheight()/2),&(gameoverimg)); 394 395 saverecord(); 396 FlushBatchDraw(); 397 while(1) 398 { 399 Sleep(3000); 400 } 401 saverecord(); 402 return 0; 403 }

?

?

?

?

?素材:

?

wall.jpg

tailu.jpg

tailr.jpg

taill.jpg

taild.jpg

headup.jpg

headright.jpg

headleft.jpg

headdown.jpg

food.jpg

bodylr.jpg

gameover.jpg

?

?

record.txt 內容:

330
140
110

?

yeah.mp3游戲背景音樂。

yeah.wav 蛇吃到食物時的嗶聲。

?

程序寫于大二上學期。

2016.4.12更新博客。

END

轉載于:https://www.cnblogs.com/maxuewei2/p/5273944.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/373450.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/373450.shtml
英文地址,請注明出處:http://en.pswp.cn/news/373450.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

3.0 C++遠征:is a

4-4is_a 0.派生類Soldier繼承自基類Person //Person.h class Person { public:Person(string name "Jim");~Person();void play(); protected:string m_strName; };//Soldier.h class Soldier : public Person { public:Soldier(string name "James", in…

python中sorted的用法append_python sorted()排序詳解

排序&#xff0c;在編程中經常遇到的算法&#xff0c;我也在幾篇文章中介紹了一些關于排序的算法。有的高級語言內置了一些排序函數。本文講述Python在這方面的工作。供使用內置函數sorted()/list.sort()的使用簡單應用python對list有一個內置函數&#xff1a;>>> a[5…

云上的播放框架變得簡單:Openshift模塊

僅僅幾年前&#xff0c;找到一個負擔得起的Java Web應用程序托管解決方案是一項艱巨的任務&#xff0c;而尋找免費的托管解決方案是一項不可能的任務。 更不用說甚至考慮自動縮放&#xff0c;單命令部署&#xff0c;持續集成等事情&#xff0c;這都是科幻小說。 去年見證了云計…

C#中的yield return與Unity中的Coroutine(協程)(下)

Unity中的Coroutine&#xff08;協程&#xff09; 估計熟悉Unity的人看過或者用過StartCoroutine() 假設我們在場景中有一個UGUI組件&#xff0c; Image&#xff1a; 將以下代碼綁定到Image 1 using UnityEngine;2 using System.Collections;3 using System.Threading;4 using …

字節流轉化為文件流_C#文件轉換為字節流及字節流轉換為文件

本文講解了C#實現文件轉換為字節流的方法。文件轉換為字節流的步驟如下1、通過文件流打開指定文件(FileStream fs)&#xff1b;2、定義字節流(byte[] fileBytenew byte[fs.Length])&#xff1b;3、把文件讀取到字節流(fs.Read(fileByte,0,fileByte.Length))&#xff1b;4、關閉…

Spring和JSF集成:導航

我希望這是有關我在Spring和JavaServer Faces之間提供深度集成的努力的一系列博客中的第一篇。 這里提到的所有內容都是“正在進行中的工作”&#xff0c;因此&#xff0c;如果您簽出代碼&#xff0c;請注意它是一個不斷變化的目標。 期待一些粗糙的邊緣&#xff0c;如果有時會…

【CSS3動畫】transform對文字及圖片的旋轉、縮放、傾斜和移動

前言&#xff1a;之前我有寫過CSS3的transform這一這特性&#xff0c;對于它的用法&#xff0c;還不是很透徹&#xff0c;今天補充補充&#xff0c;呵呵 你懂的&#xff0c;小司機準備開車了。 a)再提一提transform的四個屬性 ①旋轉--->rotate(參數a)&#xff0c;單位deg&a…

宏的用法與簡介

預處理指令&#xff1a;例如&#xff1a;#include<stdio.h> #include<stdlib.h> #define MAX 20 ............. 因為他們由預處理器解釋的&#xff0c;所以稱作預處理指令。預處理器讀取源代碼&#xff0c;然后對其修改&#xff0c;并把修改過的…

django 日志寫入mysql_如何將django orm模型 寫入數據庫

1、指定連接pymysql(python3.x)先配置_init_.pyimport pymysqlpymysql.install_as_MySQLdb()2、配置連接mysql文件信息settings.pyDATABASES {default: {ENGINE: django.db.backends.mysql,NAME: django_orm, #你的數據庫名稱USER: root, #你的數據庫用戶名PASSWORD: , #你的數…

ORM的問題第2部分–查詢

在我以前關于對象關系映射工具&#xff08;ORM&#xff09;的帖子中&#xff0c;我討論了在處理當今常見的ORM&#xff08;包括Hibernate&#xff09;時遇到的各種問題。 其中包括與從POJO生成架構有關的問題&#xff0c;實際性能和不斷出現的維護問題。 本質上&#xff0c;結論…

【轉】如何減少接口響應時間

Premature optimization is the root of all evil. — Donald Knuth 對于程序優化&#xff0c;我一直采取保守的態度&#xff0c;除非萬不得已。但是隨著業務的不斷發展&#xff0c;程序越來越復雜&#xff0c;代碼越寫越多&#xff0c;優化似乎是終有一天會到來的事情。 那么對…

數據庫行轉列在現實需求中的用法

select t.客戶姓名,sum(case when t.收款類型首款 then t.金額 else 0 end as 首款),sum(case when t.收款類型尾款 then t.金額 else 0 end as 尾款) from table t group by t.客戶姓名 這段sql的意思 是 查詢出所有客戶收款信息 然后按客戶分組 分組后 然后將這個客戶的所…

mysql生產環境加索引_【生產篇】_MySQL環境下如何查看基于表的索引定義

【引言】今天中午項目組來一需求&#xff0c;欲在MySQL環境的某張表下創建幾個BTREE索引。要創建索引&#xff0c;首先需要了解基表的表結構&#xff0c;以及已經包含的索引。Oracle的表結構大家都很熟悉&#xff0c;但MySQL表結構和已創建索引的查看怎么操作&#xff0c;本文將…

Hadoop模式介紹-獨立,偽分布式,分布式

了解了什么是Hadoop之后&#xff0c;讓我們在單機上啟動Hadoop&#xff1a; 這篇文章包含在ubuntu上安裝Hadoop的說明。 這是Hadoop安裝的快速分步教程。 在這里&#xff0c;您將獲得以獨立模式 &#xff08;單節點集群&#xff09;安裝Hadoop所需的所有命令及其說明&#xff0…

apk反編譯方式

一、Apk反編譯得到Java源代碼 下載上述反編譯工具包&#xff0c;打開apk2java目錄下的dex2jar-0.0.9.9文件夾&#xff0c;內含apk反編譯成java源碼工具&#xff0c;以及源碼查看工具。 apk反編譯工具dex2jar&#xff0c;是將apk中的classes.dex轉化成jar文件 源碼查看工具jdgui…

優化Hibernate所鼓勵的7大措施

優化Hibernate所鼓勵的7大措施&#xff1a; 1.盡量使用many-to-one&#xff0c;避免使用單項one-to-many2.靈活使用單向one-to-many3.不用一對一&#xff0c;使用多對一代替一對一4.配置對象緩存&#xff0c;不使用集合緩存5.一對多使用Bag 多對一使用Set6.繼承使用顯示多態 HQ…

如何用c 控制mysql數據庫_用C語言操作MySQL數據庫

函數描述mysql_affected_rows()返回上次UPDATE、DELETE或INSERT查詢更改&#xff0f;刪除&#xff0f;插入的行數。mysql_autocommit()切換autocommit模式&#xff0c;ON/OFFmysql_change_user()更改打開連接上的用戶和數據庫。mysql_charset_name()返回用于連接的默認字符集的…

數據結構(RMQ):POJ 3624 Balanced Lineup

Balanced LineupDescription For the daily milking, Farmer Johns N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a conti…

Apache Thrift快速入門教程

Thrift是一種跨語言RPC框架&#xff0c;最初是在Facebook上開發的&#xff0c;現在作為Apache項目開源。 這篇文章將描述如何以不同的模式&#xff08;例如阻塞&#xff0c;非阻塞和異步&#xff09;編寫Thrift服務和客戶端。 &#xff08;我覺得后兩種模式的文檔較少&#xff…

數組拆分為新數組

package com.classes;//已知數組a&#xff0c;將奇數位置元素存到b數組中&#xff0c;偶數位置元素存到c數組中public class Shuzu1118_4 { public static void main(String[] args) { int [] a{3,6,9,1,4,7,2,5,8}; int [] b; //定義數組b int [] c; //定義數組c//先找出數組…