今天我为大家带来贪吃蛇的源代码。
想玩的可以登录copy一下
源代码如下:
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <winable.h>
#include <ctime>
#define random(a,b) (rand() % (b-a+1))+ a;
#define sr srand((unsigned)time(0))
using namespace std;
int slen, x, y;
int pot,tot;
int nowlen;
int cx, cy;
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};
int hx[2000000],hy[2000000];
int cnt;
int k;
int nx = 0, ny = 1, f = 0;
int pip[300][300];
void start();
void gotoxy(int y, int x){
COORD pos = {x,y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
}
void frame(){
cout << " ---------------------------------------------------------------------" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " | |" << endl;
cout << " ---------------------------------------------------------------------" << endl;
}
int ux,uy;
void tanch(){
sr;
ux = random(5, 18);
sr;
uy = random(9, 65);
while(pip[ux][uy] == 1){
sr,ux = random(5, 18);
sr,uy = random(9, 65);
}
gotoxy(ux, uy);
cout << "-" << endl;
}
void init(){
cx = cy = x = y = 10;
slen = 4;
nowlen = 0;
gotoxy(10, 10);
cout << "-";
tanch();
}
void end(){
system("cls");
cout<<"total "<<pot<<endl;
system("pause");
exit(0);
}
int main(){
system("color f0");
start();
system("cls");
frame();
init();
while(1){
Sleep(300 - k);
x += nx, y += ny;
if(x == ux && y == uy){
slen++;
if(k + 10 < 300)
k += 10;
tanch();
tot++;
pot += 10*tot*0.7;
}
if(x == 0 || x == 22 || y == 4 || y == 72){
end();
}
gotoxy(x,y);
pip[x][y] = 1;
cout<<"+";
gotoxy(hx[cnt],hy[cnt]);
cout<<"-";
if(nowlen == slen){
gotoxy(cx,cy);
cout<<" ";
pip[cx][cy] = 0;
cx = hx[cnt - slen + 1], cy = hy[cnt - slen + 1];
gotoxy(cx,cy);
cout<<" ";
pip[cx][cy] = 0;
cx = hx[cnt - slen + 2], cy = hy[cnt - slen + 2];
}
else{
nowlen++;
if(nowlen == slen)
gotoxy(10,10),cout<<" ";
}
if(kbhit()){
char ch;
ch = getch();
switch(ch){
case 8:end();break;
case 91:{
if(k - 50 > 0)
k -= 50;
break;
}
case 93:{
if(k + 50 < 250)
k += 50;
break;
}
case ' ':{
while(1){
if(kbhit()){
ch = getch();
if(ch == 'z')
break;
}
}
break;
}
case 'w':{
if(f == 1)break;
f = 3;
nx = dx[f], ny = dy[f];
break;
}
case 's':{
if(f == 3)break;
f = 1;
nx = dx[f], ny = dy[f];
break;
}
case 'a':{
if(f == 0)break;
f = 2;
nx = dx[f], ny = dy[f];
break;
}
case 'd':{
if(f == 2)break;
f = 0;
nx = dx[f], ny = dy[f];
break;
}
}
}
hx[++cnt] = x, hy[cnt] = y;
}
}
void start()
{
for(int i = 1; i <= 10; i++)
{
gotoxy(10,49);
cout<<"Loading";
for(int j = 1; j <= i % 4; j++)
cout<<".";
cout<<" ";
gotoxy(9,49);
for(int j = 1; j <= i; j++)
cout<<"=";
Sleep(200);
}
system("cls");
cout<<" 请将输入法调至英文状态即可开始游戏\n\n 空格键暂停,Z键继续\n\n '['减速,']'加速\n\n WSAD操控贪吃蛇\n\n 撞到墙或按删除键结束\n\n ";
system("pause");
system("cls");
}