运行截图:
代码:
1 #include <iostream> 2 #include <graphics.h> 3 #include <time.h> 4 #include <stdlib.h> 5 using namespace std; 6 class point 7 { 8 public: 9 int x,y; 10 point *next; 11 point *last; 12 point() 13 { 14 15 } 16 point(int x1,int y1) 17 { next=NULL; 18 last=NULL; 19 x=x1; 20 y=y1; 21 } 22 }; 23 class she 24 { 25 public: 26 bool live; 27 int length; 28 int diction; 29 point* head; 30 she() 31 { live=true; 32 length=1; 33 diction=2; 34 head=new point(20,20); 35 } 36 void move(int x) 37 { 38 point *p=head; 39 while(p->next) 40 { 41 p=p->next; 42 } 43 while(p->last) 44 { 45 p->x=p->last->x; 46 p->y=p->last->y; 47 p=p->last; 48 } 49 if(x==1) 50 { 51 head->y-=2; 52 } 53 if(x==2) 54 { 55 head->y+=2; 56 } 57 if(x==3) 58 { 59 head->x-=2; 60 } 61 if(x==4) 62 { 63 head->x+=2; 64 } 65 } 66 void eat(int x,int y) 67 { 68 point *p=new point(x+2,y+2); 69 p->next=head; 70 head->last=p; 71 head=p; 72 } 73 bool islive() 74 { 75 return live; 76 } 77 78 }; 79 class showview 80 { 81 public: 82 point* m; 83 she s; 84 point food; 85 showview() 86 { 87 m=new point(10,10); 88 m->next=new point(400,400); 89 putcolor(); 90 dian(); 91 } 92 void dian() 93 { 94 srand((unsigned)time(NULL)); 95 int n1=m->next->x-m->x; 96 int m1=m->next->y-m->y; 97 int x=rand()%n1+m->x; 98 int y=rand()%m1+m->y; 99 while(1) 100 { 101 if(getpixel(x,y)!=BLACK) 102 { 103 x=rand()%n1+m->x; 104 y=rand()%m1+m->y; 105 }else{ 106 break; 107 } 108 } 109 food.x=x; 110 food.y=y; 111 putpixel2(x,y,YELLOW); 112 } 113 void putpixel2(int i,int j,int color) 114 { 115 putpixel(i,j,color); 116 putpixel(i-1,j,color); 117 putpixel(i+1,j,color); 118 putpixel(i,j-1,color); 119 putpixel(i,j+1,color); 120 putpixel(i-1,j-1,color); 121 putpixel(i-1,j+1,color); 122 putpixel(i+1,j-1,color); 123 putpixel(i+1,j+1,color); 124 } 125 126 void putcolor() 127 { 128 //墙 129 for(int i=m->x;i<=m->next->x;i++) 130 { 131 putpixel(i,m->y,WHITE); 132 putpixel(i,m->next->y,WHITE); 133 } 134 for(int i=m->y;i<=m->next->y;i++) 135 { 136 putpixel(m->x,i,WHITE); 137 putpixel(m->next->x,i,WHITE); 138 } 139 //蛇 140 141 point *p=s.head->next; 142 if(s.head!=NULL) 143 putpixel2(s.head->x,s.head->y,RED); //头 144 else 145 cout<<"jsdfk"<<endl; 146 while(p!=NULL) 147 { 148 putpixel2(p->x,p->y,GREEN); //身 149 p=p->next; 150 } 151 152 } 153 bool meet(int x,int y,int x1,int y1) 154 { 155 if(x==x1&&y==y1) 156 return true; 157 if(x+1==x1&&y+1==y1) 158 return true; 159 if(x+1==x1&&y-1==y1) 160 return true; 161 if(x==x1&&y+1==y1) 162 return true; 163 if(x==x1&&y-1==y1) 164 return true; 165 if(x+1==x1&&y==y1) 166 return true; 167 if(x-1==x1&&y==y1) 168 return true; 169 return false; 170 } 171 void start() 172 { 173 char x; 174 while(1) 175 { api_sleep(100); 176 if(kbhit()) 177 { 178 x=getch(); 179 if(x=='w') 180 s.diction=1; 181 if(x=='s') 182 s.diction=2; 183 if(x=='a') 184 s.diction=3; 185 if(x=='d') 186 s.diction=4; 187 } 188 s.move(s.diction); 189 if(meet(s.head->x,s.head->y,food.x,food.y)) 190 { 191 s.eat(food.x,food.y); 192 cleardevice(); 193 putcolor(); 194 dian(); 195 continue; 196 } 197 if(getpixel(s.head->x,s.head->y)==WHITE||getpixel(s.head->x,s.head->y+1)==WHITE||getpixel(s.head->x,s.head->y-1)==WHITE||getpixel(s.head->x+1,s.head->y)==WHITE) 198 break; 199 cleardevice(); 200 putcolor(); 201 putpixel2(food.x,food.y,YELLOW); 202 } 203 } 204 205 }; 206 207 208 int main() 209 { initgraph(-1,-1); 210 showview ob; 211 ob.start(); 212 //ob.start(); 213 closegraph(); 214 return 0; 215 }