vc6.0贪吃蛇代码
时间: 2023-07-02 11:16:01 浏览: 182
VC6.0贪吃蛇代码
以下是一个简单的VC6.0贪吃蛇代码,供参考:
```c++
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define MAXLEN 200
int main()
{
int x=20,y=10,score=0,foodx,foody,len=3,dir=1,i,j;
int snake[MAXLEN][2]={0};
char key=72;
HANDLE hOutput;
COORD loc;
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(hOutput, &cursor_info);
srand(GetTickCount());
foodx=rand()%70+5;
foody=rand()%20+5;
while(1)
{
loc.X=0;
loc.Y=0;
SetConsoleCursorPosition(hOutput, loc);
for(i=0;i<25;i++)
{
for(j=0;j<75;j++)
{
if(i==0 || i==24 || j==0 || j==74)
printf("#");
else if(i==y && j==x)
printf("@");
else if(i==foody && j==foodx)
printf("*");
else
{
int flag=0;
for(int k=0;k<len;k++)
{
if(snake[k][0]==j && snake[k][1]==i)
{
printf("@");
flag=1;
break;
}
}
if(flag==0)
printf(" ");
}
}
printf("\n");
}
if(x==0 || x==74 || y==0 || y==24)
{
printf("Game Over!\n");
break;
}
if(x==foodx && y==foody)
{
score++;
len++;
foodx=rand()%70+5;
foody=rand()%20+5;
}
for(i=len-1;i>0;i--)
{
snake[i][0]=snake[i-1][0];
snake[i][1]=snake[i-1][1];
}
snake[0][0]=x;
snake[0][1]=y;
if(kbhit())
key=getch();
if(key==72 || key=='w') //上
y--;
else if(key==80 || key=='s') //下
y++;
else if(key==75 || key=='a') //左
x--;
else if(key==77 || key=='d') //右
x++;
Sleep(100);
}
return 0;
}
```
注意:此代码未进行优化和封装,仅供参考。
阅读全文