| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 635 人关注过本帖
标题:贪吃蛇:为什么没吃到食物有时也增加一个星号
只看楼主 加入收藏
fdgfdfdh
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2013-5-7
结帖率:71.43%
收藏
已结贴  问题点数:20 回复次数:3 
贪吃蛇:为什么没吃到食物有时也增加一个星号
#include<stdio.h>
#include<windows.h>
//#include<stdlib.h>
#include<conio.h>
#include<string.h>
#define  UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3
#define TURN_NUM 1000
#define INIT_LENGTH 8
#define UP_EDGE 0
#define DOWN_EDGE 24
#define LEFT_EDGE 0
#define RIGHT_EDGE 79
#define X 0
#define Y 0
char buf[1000]="@*******";
char *snake=buf;
char FOOD='$';
int food_num=0;
int score=0;
int snake_length=INIT_LENGTH;         
int cursor[2]={LEFT_EDGE+20+INIT_LENGTH+1,UP_EDGE+10};  
int head[2]={LEFT_EDGE+20+INIT_LENGTH,UP_EDGE+10};  
int tail[2]={0,0};
int old_tail[2];
int food[2]={0,0};
int init_position[2]={LEFT_EDGE+X,UP_EDGE+Y};
int direction=RIGHT;
int turn_point[TURN_NUM][2];
int head_turn_num=0;
int tail_turn_num=0;
int game_over=0;


void main()
{
    void gotoxy(int x,int y);
    void generate_food();
    void cur_state();
    void print_introduction();
    void init();
    void control();
    void move();
    void move_up();
    void move_down();
    void move_left();
    void move_right();
    void update_tail_position();
   
    void eat_food();


    char ch;
    while(!game_over)
    {
        print_introduction();
        printf("\t\t按任意键开始游戏\n");
        getch();
        system("cls");
        //cur_state();
        init();
        control();
        system("cls");
        gotoxy(0,10);
        printf("\t\t当前得分是%d\n",score);
        printf("\t\t这条蛇共转了%d次,他的深长%d\n",head_turn_num,strlen(snake));
        printf("\t\t游戏结束还玩么?\n");
        scanf("%c",&ch);
        getchar();
        if(ch=='y'||ch=='Y')
            continue;
        else
        {
            system("cls");
            printf("\n\n\t\t\t谢谢,再见\n");
            game_over=1;

        }
    }
}

void print_introduction()
{
    int i=0;
    int rule_num;
    char *rule[4];        
    rule[i++]="\n\n\t\t\t欢迎来玩贪吃蛇游戏\n";
    rule[i++]="\t\t游戏规则如下:\n";
    rule[i++]="\t\t1.按上下左右键控制蛇的运动方向\n";
    rule[i++]="\t\t2.按CTRL+C结束当前游戏\n";
    rule_num=i;
    system("cls");
    for(i=0;i<rule_num;i++)
    {
        puts(rule[i]);
    }
}

void cur_state()
{
    printf("游戏当前状态的参数如下:\n");
    printf("snake=%s\n",snake);
    printf("snake_body_length=%d\n",strlen(snake));
    printf("head=(%d,%d)\n",head[0],head[1]);
    printf("tail=(%d,%d)\n",tail[0],tail[1]);
    printf("direction=%d\n",direction);
    printf("game_over=%d\n",game_over);
}





void gotoxy(int x,int y)
{
    COORD coord={x,y};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}


void generate_food()
{
    int i=0,j=0;
    do
    {
        i=rand()%DOWN_EDGE;
    }while(i<UP_EDGE||i>DOWN_EDGE);
    do
    {
        j=rand()%RIGHT_EDGE;                              
    }while(j<LEFT_EDGE||j>RIGHT_EDGE);
    food[0]=j;
    food[1]=i;
    gotoxy(food[0],food[1]);
    putchar(FOOD);
    gotoxy(cursor[0],cursor[1]);
}


void init()
{
    int i;
    food_num=0;
    score=0;
    snake_length=INIT_LENGTH;                        
    cursor[0]=init_position[0]+INIT_LENGTH;         
    cursor[1]=init_position[1];                     
    head[0]=init_position[0]+INIT_LENGTH-1;         
    head[1]=init_position[1];                       
    tail[0]=init_position[0];                        
    tail[1]=init_position[1];                        
    direction=RIGHT;
    head_turn_num=0;
    tail_turn_num=0;
    game_over=0;
    turn_point[0][0]=RIGHT_EDGE;                     
    turn_point[0][1]=0;
    generate_food();
    gotoxy(init_position[0],init_position[1]);
    for(i=snake_length;i>0;i--)
        putchar(snake[i-1]);
}


void update_tail_position()
{
    old_tail[0]=tail[0];
    old_tail[1]=tail[1];
   
    if(tail_turn_num<head_turn_num)
    {
        if(tail[0]<turn_point[tail_turn_num%TURN_NUM][0])
            tail[0]+=1;
        else if(tail[0]>turn_point[tail_turn_num%TURN_NUM][0])
            tail[0]-=1;
        else if(tail[1]<turn_point[tail_turn_num%TURN_NUM][1])
            tail[1]+=1;
        else if(tail[1]>turn_point[tail_turn_num%TURN_NUM][1])
            tail[1]-=1;

        else if(tail[0]==turn_point[(tail_turn_num)%TURN_NUM][0]&&tail[1]==turn_point[(tail_turn_num)%TURN_NUM][1])
        {tail_turn_num+=1;}
    }
    else if(tail_turn_num==head_turn_num)
    {
        if(tail[0]<head[0])
            tail[0]+=1;
        else if(tail[0]>head[0])
            tail[0]-=1;
        else if(tail[1]<head[1])
            tail[1]+=1;
        else if(tail[1]>head[1])
            tail[1]-=1;

    }
}

            



void move_up()
{
    if(cursor[1]>=UP_EDGE)
    {
        gotoxy(head[0],head[1]);
        putchar('*');
        gotoxy(head[0],head[1]-1);
        putchar(snake[0]);
        gotoxy(tail[0],tail[1]);
        putchar(0);
        update_tail_position();
        head[1]-=1;
        cursor[0]=head[0];
        cursor[1]=head[1]-1;
        gotoxy(cursor[0],cursor[1]);
    }
    else
        gotoxy(head[0],head[1]);
}


void move_down()
{
    if(cursor[1]<=DOWN_EDGE)
    {
        gotoxy(head[0],head[1]);
        putchar('*');
        gotoxy(head[0],head[1]+1);
        putchar(snake[0]);
        gotoxy(tail[0],tail[1]);
        putchar(0);
        update_tail_position();
        head[1]+=1;
        cursor[0]=head[0];
        cursor[1]=head[1]+1;
        gotoxy(cursor[0],cursor[1]);
    }
    else
        gotoxy(head[0],head[1]);
}


void move_left()
{
    if(cursor[0]>=LEFT_EDGE)
    {
        gotoxy(head[0],head[1]);
        putchar('*');
        gotoxy(head[0]-1,head[1]);
        putchar(snake[0]);
        gotoxy(tail[0],tail[1]);
        putchar(0);
        update_tail_position();
        head[0]-=1;
        cursor[0]=head[0]-1;
        cursor[1]=head[1];
        gotoxy(cursor[0],cursor[1]);
    }
    else
        gotoxy(head[0],head[1]);
}


void move_right()
{
    if(cursor[0]<=RIGHT_EDGE)
    {
        gotoxy(head[0],head[1]);
        putchar('*');
        gotoxy(head[0]+1,head[1]);
        putchar(snake[0]);
        gotoxy(tail[0],tail[1]);
        putchar(0);
        update_tail_position();
        head[0]+=1;
        cursor[0]=head[0]+1;
        cursor[1]=head[1];
        gotoxy(cursor[0],cursor[1]);
    }
    else
        gotoxy(head[0],head[1]);
}






void move()
{
    if(direction==UP)
        move_up();
    else if(direction==DOWN)
        move_down();
    else if(direction==LEFT)
        move_left();
    else if(direction==RIGHT)
        move_right();
}


void eat_food()
{
   
        snake[snake_length++]=snake[1];
        tail[0]=old_tail[0];
        tail[1]=old_tail[1];
        gotoxy(tail[0],tail[1]);
        putchar(snake[1]);
        food_num++;
        score=food_num;
        gotoxy(cursor[0],cursor[1]);
}
   




void control()
{
    char command;
    while(1)
    {
        command=getch();
   
        if(command=='W'&&(direction==LEFT||direction==RIGHT))
            {
                direction=UP;
                turn_point[head_turn_num%TURN_NUM][0]=head[0];                 
                turn_point[head_turn_num%TURN_NUM][1]=head[1];                  
                head_turn_num++;
        }
        else if(command=='A'&&(direction==UP||direction==DOWN))
        {
            direction=LEFT;
            turn_point[head_turn_num%TURN_NUM][0]=head[0];
            turn_point[head_turn_num%TURN_NUM][1]=head[1];
            head_turn_num++;
        }
        else if(command=='S'&&(direction==LEFT||direction==RIGHT))
        {
            direction=DOWN;
            turn_point[head_turn_num%TURN_NUM][0]=head[0];
            turn_point[head_turn_num%TURN_NUM][1]=head[1];
            head_turn_num++;
        }
        else if(command=='D'&&(direction==UP||direction==DOWN))
        {
            direction=RIGHT;
            turn_point[head_turn_num%TURN_NUM][0]=head[0];
            turn_point[head_turn_num%TURN_NUM][1]=head[1];
            head_turn_num++;
        }
        else
        {
        }
        move();
        
    }
}
搜索更多相关主题的帖子: include 贪吃蛇 cursor 
2013-07-11 17:07
丶弱水彡千
Rank: 5Rank: 5
来 自:地狱十九层
等 级:职业侠客
威 望:2
帖 子:203
专家分:369
注 册:2013-6-16
收藏
得分:7 
这真不好看

这个怎么玩
2013-07-11 18:19
C语言NEW
Rank: 2
来 自:C语言家族
等 级:论坛游民
帖 子:68
专家分:65
注 册:2013-6-26
收藏
得分:7 
学习

C语言菜鸟啦啦啦,必须的。
2013-07-11 20:03
navy_jia
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2014-4-25
收藏
得分:0 
为嘛,只能向前走
2014-05-04 10:34
快速回复:贪吃蛇:为什么没吃到食物有时也增加一个星号
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.039706 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved