├── README.md └── 贪吃蛇 ├── 贪吃蛇.cpp ├── 贪吃蛇.exe └── 贪吃蛇.o /README.md: -------------------------------------------------------------------------------- 1 | 用C++写的一个简易贪吃蛇 2 | -------------------------------------------------------------------------------- /贪吃蛇/贪吃蛇.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define H 22 7 | #define W 22 8 | using namespace std; 9 | 10 | class chessboard 11 | { 12 | public: 13 | char qp[H][W]; 14 | int i,j,x1,y1; 15 | chessboard(); 16 | void food(); 17 | void prt(int grade,int score,int gamespeed); 18 | }; 19 | chessboard::chessboard() 20 | { 21 | for(i=1;i<=H-2;i++) 22 | for(j=1;j<=W-2;j++) 23 | qp[i][j]=' '; 24 | for(i=0;i<=H-1;i++) 25 | qp[0][i] = qp[H-1][i] = '#'; 26 | for(i=1;i<=H-2;i++) 27 | qp[i][0] = qp[i][W-1] = '#'; 28 | food(); 29 | } 30 | void chessboard::food() 31 | { 32 | srand(time(0)); 33 | do 34 | { 35 | x1=rand()%W-2+1; 36 | y1=rand()%H-2+1; 37 | } 38 | while(qp[x1][y1]!=' '); 39 | qp[x1][y1]='$'; 40 | } 41 | void chessboard::prt(int grade,int score,int gamespeed) 42 | { 43 | system("cls"); 44 | cout << endl; 45 | for(i=0;i=0;i--) 71 | { 72 | start=clock(); 73 | while(clock()-start<=1000); 74 | system("cls"); 75 | if(i>0) 76 | cout << "\n\n\t\tCountdown:" << i << endl; 77 | } 78 | for(i=1;i<=3;i++) 79 | qp[1][i]='*'; 80 | qp[1][4]='@'; 81 | for(i=0; i<4; i++) 82 | { 83 | zb[0][i] = 1; 84 | zb[1][i] = i + 1; 85 | } 86 | } 87 | void snake::move() 88 | { 89 | score=0; 90 | head = 3,tail = 0; 91 | grade = 1, length = 4; 92 | gamespeed = 500; 93 | direction = 77; 94 | while(1) 95 | { 96 | timeover = 1; 97 | start = clock(); 98 | while((timeover=(clock()-start<=gamespeed))&&!kbhit()); 99 | if(timeover) 100 | { 101 | getch(); 102 | direction = getch(); 103 | } 104 | switch(direction) 105 | { 106 | case 72: x= zb[0][head]-1; y= zb[1][head];break; 107 | case 80: x= zb[0][head]+1; y= zb[1][head];break; 108 | case 75: x= zb[0][head]; y= zb[1][head]-1;break; 109 | case 77: x= zb[0][head]; y= zb[1][head]+1;break; 110 | } 111 | if(x==0 || x==21 ||y==0 || y==21) 112 | { 113 | cout << "\tGame over!" << endl;break; 114 | } 115 | if(qp[x][y]!=' '&&!(x==x1&&y==y1)) 116 | { 117 | cout << "\tGame over!" << endl;break; 118 | } 119 | if(x==x1 && y==y1) 120 | { 121 | length ++; 122 | score=score+100; 123 | if(length>=8) 124 | { 125 | length -= 8; 126 | grade ++; 127 | if(gamespeed>=200) 128 | gamespeed = 550 - grade * 50; 129 | } 130 | qp[x][y]= '@'; 131 | qp[zb[0][head]][zb[1][head]] = '*'; 132 | head = (head+1)%100; 133 | zb[0][head] = x; 134 | zb[1][head] = y; 135 | food(); 136 | prt(grade,score,gamespeed); 137 | } 138 | else 139 | { 140 | qp[zb[0][tail]][zb[1][tail]]=' '; 141 | tail=(tail+1)%100; 142 | qp[zb[0][head]][zb[1][head]]='*'; 143 | head=(head+1)%100; 144 | zb[0][head]=x; 145 | zb[1][head]=y; 146 | qp[zb[0][head]][zb[1][head]]='@'; 147 | prt(grade,score,gamespeed); 148 | } 149 | } 150 | } 151 | int main() 152 | { 153 | chessboard cb; 154 | snake s; 155 | s.move(); 156 | } 157 | -------------------------------------------------------------------------------- /贪吃蛇/贪吃蛇.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bystc/Snake-cpp/111e18dece3423d2bde5705cfd695860ab779e96/贪吃蛇/贪吃蛇.exe -------------------------------------------------------------------------------- /贪吃蛇/贪吃蛇.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bystc/Snake-cpp/111e18dece3423d2bde5705cfd695860ab779e96/贪吃蛇/贪吃蛇.o --------------------------------------------------------------------------------