C语言控制台贪食蛇
给同学写了一个vc6.0的C语言的控制台贪食蛇,做练习用的。没什么意思,又丑又没成就感。话说,用控制台写比用OpenGL写还麻烦。想当年我没几分钟就写完了,现在竟然写了一个半小时,水平大不如从前了,要好好加油才行。
https://files.cnblogs.com/PureMilk/SnakeWithC.rar
这几天有点自信心过度膨胀的状态,主要是写车牌识别写的手感太好了。不知道做完这个项目,是不是可以找一份不错的工作。忽然觉得工作也是没什么意思的事情,无非就是有钱拿。工作真的只是为了赚钱吗?我想我还是希望自己是个有用的人。可是这样就是个有用的人了吗?应该不是的吧。那,有用是开心的吗?我不知道。现在的我,还是很想赚钱的,恩。不知道等一切我想要的都要到了,我该做什么呢。也许再不会有什么可以让自己很有活力的事情,每天只要简单的,满足的就好了吧。
把源码贴上来吧。
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
enum block{
BLANK = 0, FOOD, SNAKE, HEAD
};
int snake[800][2] = {0};
int len = 0;
int food[2] = {0};
enum block map[20][80] = {HEAD};
int gameOver = 0;
void CreatFood( void )
{
int flag = 1;
int i;
while( flag == 1 )
{
flag = 0;
food[0] = rand()%80;
food[1] = rand()%20;
printf( "%d,%d\n", food[0], food[1] );
for ( i = 0; i < len; ++i )
{
if ( snake[i][0] == food[0] && snake[i][1] == food[1] )
{
flag = 1;
}
}
}
}
void Init( void )
{
int i;
//初始化蛇
len = 3;
for ( i = 0; i < len; ++i )
{
snake[i][0] = len-i;
}
//初始化食物
CreatFood();
}
void Draw( void )
{
int i,j;
for ( i = 0; i < 20; ++i )
{
for ( j = 0; j < 80; ++j )
{
map[i][j] = BLANK;
}
}
//把蛇画到map里
map[snake[0][1]][snake[0][0]] = HEAD;
for ( i = 1; i < len; ++i )
{
map[snake[i][1]][snake[i][0]] = SNAKE;
}
//把食物画到map里
map[food[1]][food[0]] = FOOD;
system( "cls" );
// printf( "%d,%d\n", food[0], food[1] );
for ( i = 0; i < 20; ++i )
{
for ( j = 0; j < 80; ++j )
{
switch ( map[i][j] )
{
case BLANK:
putchar(' ');
break;
case FOOD:
putchar('$');
break;
case SNAKE:
putchar('*');
break;
case HEAD:
putchar('@');
break;
default:
break;
}
}
}
}
int Eat( void )
{
if ( snake[0][0] == food[0] && snake[0][1] == food[1] )
{
return 1;
}
return 0;
}
int Die( void )
{
int i;
//撞边界死
if ( snake[0][0] < 0 || snake[0][0] > 80 )
{
return 1;
}
if ( snake[0][1] < 0 || snake[0][1] > 20 )
{
return 1;
}
//撞自己死
for ( i = 1; i < len; ++i )
{
if ( snake[i][0] == snake[0][0] && snake[i][1] == snake[0][1] )
{
return 1;
}
}
return 0;
}
void Move( char key )
{
int x, y;
int pos = 0;
int i;
int d[4][2] = {
{0,-1},
{-1,0},
{0,1},
{1,0},
};
switch ( key )
{
case 'w':
pos = 0;
break;
case 'a':
pos = 1;
break;
case 's':
pos = 2;
break;
case 'd':
pos = 3;
break;
default:
break;
}
x = snake[len-1][0];
y = snake[len-1][1];
for ( i = len-1; i > 0; --i )
{
snake[i][0] = snake[i-1][0];
snake[i][1] = snake[i-1][1];
}
snake[0][0] += d[pos][0];
snake[0][1] += d[pos][1];
if ( Eat() )
{
snake[len][0] = x;
snake[len][1] = y;
++len;
CreatFood();
}
else if ( Die() )
{
gameOver = 1;
}
}
char Judge( char key, char lastKey )
{
if ( key == 'd' && lastKey == 'a' )
{
return lastKey;
}
if ( key == 'a' && lastKey == 'd' )
{
return lastKey;
}
if ( key == 'w' && lastKey == 's' )
{
return lastKey;
}
if ( key == 's' && lastKey == 'w' )
{
return lastKey;
}
return key;
}
int main( int argc, char * argv[] )
{
char lastKey = 'd';
char key = lastKey;
int lastTime = GetTickCount();
srand( (unsigned int)GetTickCount() );
Init();
Draw();
while ( 1 )
{
while ( !kbhit() )
{
while ( GetTickCount()-lastTime < 500 )
{
Sleep(1);
}
lastTime = GetTickCount();
Move( key );
if ( !gameOver )
{
Draw();
}
}
key = getch();
key = Judge( key, lastKey );
lastKey = key;
Move( key );
if ( !gameOver )
{
Draw();
}
else
{
puts( "You Lose!");
break;
}
}
return 0;
}
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
enum block{
BLANK = 0, FOOD, SNAKE, HEAD
};
int snake[800][2] = {0};
int len = 0;
int food[2] = {0};
enum block map[20][80] = {HEAD};
int gameOver = 0;
void CreatFood( void )
{
int flag = 1;
int i;
while( flag == 1 )
{
flag = 0;
food[0] = rand()%80;
food[1] = rand()%20;
printf( "%d,%d\n", food[0], food[1] );
for ( i = 0; i < len; ++i )
{
if ( snake[i][0] == food[0] && snake[i][1] == food[1] )
{
flag = 1;
}
}
}
}
void Init( void )
{
int i;
//初始化蛇
len = 3;
for ( i = 0; i < len; ++i )
{
snake[i][0] = len-i;
}
//初始化食物
CreatFood();
}
void Draw( void )
{
int i,j;
for ( i = 0; i < 20; ++i )
{
for ( j = 0; j < 80; ++j )
{
map[i][j] = BLANK;
}
}
//把蛇画到map里
map[snake[0][1]][snake[0][0]] = HEAD;
for ( i = 1; i < len; ++i )
{
map[snake[i][1]][snake[i][0]] = SNAKE;
}
//把食物画到map里
map[food[1]][food[0]] = FOOD;
system( "cls" );
// printf( "%d,%d\n", food[0], food[1] );
for ( i = 0; i < 20; ++i )
{
for ( j = 0; j < 80; ++j )
{
switch ( map[i][j] )
{
case BLANK:
putchar(' ');
break;
case FOOD:
putchar('$');
break;
case SNAKE:
putchar('*');
break;
case HEAD:
putchar('@');
break;
default:
break;
}
}
}
}
int Eat( void )
{
if ( snake[0][0] == food[0] && snake[0][1] == food[1] )
{
return 1;
}
return 0;
}
int Die( void )
{
int i;
//撞边界死
if ( snake[0][0] < 0 || snake[0][0] > 80 )
{
return 1;
}
if ( snake[0][1] < 0 || snake[0][1] > 20 )
{
return 1;
}
//撞自己死
for ( i = 1; i < len; ++i )
{
if ( snake[i][0] == snake[0][0] && snake[i][1] == snake[0][1] )
{
return 1;
}
}
return 0;
}
void Move( char key )
{
int x, y;
int pos = 0;
int i;
int d[4][2] = {
{0,-1},
{-1,0},
{0,1},
{1,0},
};
switch ( key )
{
case 'w':
pos = 0;
break;
case 'a':
pos = 1;
break;
case 's':
pos = 2;
break;
case 'd':
pos = 3;
break;
default:
break;
}
x = snake[len-1][0];
y = snake[len-1][1];
for ( i = len-1; i > 0; --i )
{
snake[i][0] = snake[i-1][0];
snake[i][1] = snake[i-1][1];
}
snake[0][0] += d[pos][0];
snake[0][1] += d[pos][1];
if ( Eat() )
{
snake[len][0] = x;
snake[len][1] = y;
++len;
CreatFood();
}
else if ( Die() )
{
gameOver = 1;
}
}
char Judge( char key, char lastKey )
{
if ( key == 'd' && lastKey == 'a' )
{
return lastKey;
}
if ( key == 'a' && lastKey == 'd' )
{
return lastKey;
}
if ( key == 'w' && lastKey == 's' )
{
return lastKey;
}
if ( key == 's' && lastKey == 'w' )
{
return lastKey;
}
return key;
}
int main( int argc, char * argv[] )
{
char lastKey = 'd';
char key = lastKey;
int lastTime = GetTickCount();
srand( (unsigned int)GetTickCount() );
Init();
Draw();
while ( 1 )
{
while ( !kbhit() )
{
while ( GetTickCount()-lastTime < 500 )
{
Sleep(1);
}
lastTime = GetTickCount();
Move( key );
if ( !gameOver )
{
Draw();
}
}
key = getch();
key = Judge( key, lastKey );
lastKey = key;
Move( key );
if ( !gameOver )
{
Draw();
}
else
{
puts( "You Lose!");
break;
}
}
return 0;
}