关于一款c++贪吃蛇小游戏
好久不资瓷了。
首先声明,这个东西为转载(窝不会写这个。)
原作者:洛谷dalaoWZK20080124。
代码如下:
#include <iostream> #include <Windows.h> #include <conio.h> #include <fstream> using namespace std; char ch='w',o; int i=10,j=10,a[100][100],b[100000],c[100000],u=0,gh=0,i1=0,j1=0,h=1,s=0,m=0,su=8; int main() { ifstream data("D:\\max.data"); int dd,d; while (data >> dd) { d=dd; } data.close(); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN); cout << "-"; for(int k=0; k<=20; k++) cout << "--"; cout << "\n"; for(int k=1; k<=20; k++) { cout << "| "; for(int u=1; u<=20; u++) cout << " "; cout << "|\n"; } cout << "-"; for(int k=0; k<=20; k++) cout << "--"; cout << "\n当前得分:0 当前速度:18\n历史最高分:"<< d; while(true) { Sleep((11-su)*50); gh++; b[gh]=i; c[gh]=j; a[i][j]=1; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {j*2,i}); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN ); printf("█"); if(kbhit()) { o = getch(); if(o=='+'&&su+1<=10) { su++; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN); SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {25,22}); cout << su << " "; } if(o=='-'&&su-1>=1) { su--; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN); SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {25,22}); cout << su << " "; } if(o=='w'&&a[i-1][j]!=1) ch=o; if(o=='s'&&a[i+1][j]!=1) ch=o; if(o=='a'&&a[i][j-1]!=1) ch=o; if(o=='d'&&a[i][j+1]!=1) ch=o; } switch (ch) { case 'w': i--; break; case 's': i++; break; case 'a': j--; break; case 'd': j++; break; default: break; } if(i>20) i=1; if(i==0) i=20; if(j>20) j=1; if(j==0) j=20; if(a[i][j]==1) { SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {30,11}); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN); cout << "游戏结束 按1退出"; char uuu; while(uuu!='1') uuu=getch(); system("cls"); if(s>d) { cout << "破记录了"; std::ofstream outFile; outFile.open("D:\\max.data"); outFile << s; outFile.close(); } return 0; } if(i==i1&&j==j1) { h=1; m=0; s=s+su; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN); SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {9,22}); cout << s; } a[i][j]=2; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {j*2,i}); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE ); printf("█"); while(m==0) { i1=(rand()%20)+1; j1=(rand()%20)+1; if(a[i1][j1]==0) { m=1; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {j1*2,i1}); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN|FOREGROUND_RED ); printf("█"); } } int k=0; if(h==0) { while(k<gh) { k++; if(b[k]!=0) { a[b[k]][c[k]]=0; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {c[k]*2,b[k]}); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE); printf(" "); b[k]=0; break; } } } h=0; } }
有空的话可以颓一下(不是)