ヘビ食いゲーム(付源コード)
ヘビゲームを食いしん坊で、今でもBUGがたくさんあります.みんながミスを犯すのを待っています...
難易度:
1が一番難しくて、500が一番簡単です.の20个の食べ物を食べたらクリアできますよ...退屈な時に遊びましょう
難易度:
1が一番難しくて、500が一番簡単です.の20个の食べ物を食べたらクリアできますよ...退屈な時に遊びましょう
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
const int maxn = 100;
const int n = 20;
struct node
{
int x, y;
};
int map[maxn][maxn]; // 0 ,1 ,2 ,3 , 4 .
node food;
node squence[maxn]; // .
int len; // .
bool eat; // .
bool gameover; // .
int direction; // .
void Output(void);
void Move(int direction);
void Move_up(void);
void Move_down(void);
void Move_left(void);
void Move_right(void);
int main()
{
int i, j;
double start;
int gamespeed; // .
int timeover;
int game_times = 1; // .
char c = 'x';
printf(" (1 500,1 ,500 ):
");
scanf("%d", &gamespeed);
gamespeed = gamespeed;
// .
for (i = 0; i <= n + 1; i++)
{
for (j = 0; j <= n + 1; j++)
{
map[i][j] = 0;
}
}
// .
for (i = 1; i <= n; i++)
{
squence[i].x = 0;
squence[i].y = 0;
}
len = 1; // 1.
squence[len].x = 1;
squence[len].y = 1; // (1, 1).
map[1][1] = 4;
direction = 4; // .
srand(time(0));
while (game_times <= 20)
{
eat = 0; // .
while (true)
{ // , , .
food.x = rand() % 20 + 1;
food.y = rand() % 20 + 1;
if (map[food.x][food.y] == 0)
{
break;
}
}
map[food.x][food.y] = 2; // .
system("cls");
Output();
// .
while (!eat)
{
start = clock();
timeover=1;
while(!kbhit())
{ // .
if (clock() - start <= gamespeed)
{ // .
timeover = 1;
}
else
{
timeover = 0;
break;
}
}
if (timeover)
{ // .
// ,
c = getch();
c = getch();
//
if(c==72) direction = 1; // printf(" ");
if(c==80) direction = 2; // printf(" ");
if(c==75) direction = 3; // printf(" ");
if(c==77) direction = 4; // printf(" ");
}
Move(direction);
system("cls");
if (gameover)
{
Output();
printf("Game Over!!!
");
return 0;
}
Output();
}
game_times++; // .
}
printf("You win!!!
");
return 0;
}
void Move(int direction)
{
switch (direction)
{
case 1 : Move_up(); break;
case 2 : Move_down(); break;
case 3 : Move_left(); break;
default : Move_right();
}
}
void Output(void)
{
int i, j;
for (j = 0; j <= n + 1; j++)
{
printf("#");
}
printf("
");
for (i = 1; i <= n; i++)
{
for (j = 0; j <= n + 1; j++)
{
if (j == 0 || j == n + 1)
{
if (map[i][j] == 3)
{
printf("!");
}
else
{
printf("#");
}
}
else
{
if (map[i][j] == 1)
{
printf("*");
}
else if (map[i][j] == 2)
{
printf("@");
}
else if (map[i][j] == 3)
{
printf("!");
}
else if (map[i][j] == 4)
{
switch (direction)
{
case 1 : printf("%c", 30); break;
case 2 : printf("%c", 31); break;
case 3 : printf("%c", 17); break;
default : printf("%c", 16);
}
}
else
{
printf(" ");
}
}
}
printf("
");
}
for (j = 0; j <= n + 1; j++)
{
printf("#");
}
printf("
");
}
void Move_up(void)
{
int i;
int x, y;
if (len > 1 && squence[len].y == squence[len - 1].y && squence[len].x == squence[len - 1].x + 1)
{ // , .
direction = 2; // .
Move(direction);
return ;
}
// .
x = squence[len].x - 1;
y = squence[len].y;
if (x == 0 || map[x][y] == 1)
{ // .
map[x][y] = 3;
gameover = 1;
}
if (map[x][y] == 2)
{ // .
map[squence[len].x][squence[len].y] = 1;
len++;
squence[len].x = x;
squence[len].y = y;
map[x][y] = 4;
eat = 1;
}
else
{
map[squence[1].x][squence[1].y] = 0;
for (i = 1; i <= len - 1; i++)
{
squence[i].x = squence[i + 1].x;
squence[i].y = squence[i + 1].y;
map[squence[i + 1].x][squence[i + 1].y] = 1;
}
squence[len].x = squence[len].x - 1;
if (gameover)
{
map[squence[len].x][squence[len].y] = 3;
}
else
{
map[squence[len].x][squence[len].y] = 4;
}
}
}
void Move_down(void)
{
int i;
int x, y;
if (len > 1 && squence[len].y == squence[len - 1].y && squence[len].x == squence[len - 1].x - 1)
{ // , .
direction = 1; // .
Move(direction);
return ;
}
// .
x = squence[len].x + 1;
y = squence[len].y;
if (x == n + 1 || map[x][y] == 1)
{ // .
map[x][y] = 3;
gameover = 1;
}
if (map[x][y] == 2)
{ // .
map[squence[len].x][squence[len].y] = 1;
len++;
squence[len].x = x;
squence[len].y = y;
map[x][y] = 4;
eat = 1;
}
else
{
map[squence[1].x][squence[1].y] = 0;
for (i = 1; i <= len - 1; i++)
{
squence[i].x = squence[i + 1].x;
squence[i].y = squence[i + 1].y;
map[squence[i + 1].x][squence[i + 1].y] = 1;
}
squence[len].x = squence[len].x + 1;
if (gameover)
{
map[squence[len].x][squence[len].y] = 3;
}
else
{
map[squence[len].x][squence[len].y] = 4;
}
}
}
void Move_left(void)
{
int i;
int x, y;
if (len > 1 && squence[len].x == squence[len - 1].x && squence[len].y == squence[len - 1].y + 1)
{ // , .
direction = 4; // .
Move(direction);
return ;
}
// .
x = squence[len].x;
y = squence[len].y - 1;
if (y == 0 || map[x][y] == 1)
{ // .
map[x][y] = 3;
gameover = 1;
}
if (map[x][y] == 2)
{ // .
map[squence[len].x][squence[len].y] = 1;
len++;
squence[len].x = x;
squence[len].y = y;
map[x][y] = 4;
eat = 1;
}
else
{
map[squence[1].x][squence[1].y] = 0;
for (i = 1; i <= len - 1; i++)
{
squence[i].x = squence[i + 1].x;
squence[i].y = squence[i + 1].y;
map[squence[i + 1].x][squence[i + 1].y] = 1;
}
squence[len].y = squence[len].y - 1;
if (gameover)
{
map[squence[len].x][squence[len].y] = 3;
}
else
{
map[squence[len].x][squence[len].y] = 4;
}
}
}
void Move_right(void)
{
int i;
int x, y;
if (len > 1 && squence[len].x == squence[len - 1].x && squence[len].y == squence[len - 1].y - 1)
{ // , .
direction = 3; // .
Move(direction);
return ;
}
// .
x = squence[len].x;
y = squence[len].y + 1;
if (y == n + 1 || map[x][y] == 1)
{ // .
map[x][y] = 3;
gameover = 1;
}
if (map[x][y] == 2)
{ // .
map[squence[len].x][squence[len].y] = 1;
len++;
squence[len].x = x;
squence[len].y = y;
map[x][y] = 4;
eat = 1;
}
else
{
map[squence[1].x][squence[1].y] = 0;
for (i = 1; i <= len - 1; i++)
{
squence[i].x = squence[i + 1].x;
squence[i].y = squence[i + 1].y;
map[squence[i + 1].x][squence[i + 1].y] = 1;
}
squence[len].y = squence[len].y + 1;
if (gameover)
{
map[squence[len].x][squence[len].y] = 3;
}
else
{
map[squence[len].x][squence[len].y] = 4;
}
}
}