| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1980 人关注过本帖, 2 人收藏
标题:开心小程序,重在参与
只看楼主 加入收藏
hack战
Rank: 1
等 级:新手上路
帖 子:1
专家分:8
注 册:2013-12-9
收藏
得分:8 
一个数组,一个函数,配合强大的思维,竟能编写出如此叼的代码,佩服!
2013-12-09 17:00
so_love
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:7
帖 子:812
专家分:4151
注 册:2013-11-25
收藏
得分:8 
研究研究。。。都是大神的成果。

一花一世界、一叶一追寻、片片花叶落、情系何人身。
2013-12-09 17:38
怪叔叔
Rank: 4
来 自:陕西
等 级:业余侠客
威 望:1
帖 子:113
专家分:234
注 册:2013-9-22
收藏
得分:8 
2013-12-09 18:06
怪叔叔
Rank: 4
来 自:陕西
等 级:业余侠客
威 望:1
帖 子:113
专家分:234
注 册:2013-9-22
收藏
得分:0 
回复 20楼 azzbcc
都不错!都不错!
但是就是想问问你运行通过了没有?
五个程序都有小小的错误,都是 i 或都 j 重复定义了
不过稍稍修改都运行通过了!
2013-12-09 18:30
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
收藏
得分:0 
回复 24楼 怪叔叔
。。。

c99


[fly]存在即是合理[/fly]
2013-12-09 20:30
wht750109
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:49
专家分:151
注 册:2013-10-21
收藏
得分:8 
自己编了一个贪吃蛇有效,效果类似,以供参考,我这个贪吃蛇有些机器能显示得分,有效不能显示,是乱码,不知为啥,有知道的给我也讲解一下。
#include<io.h>
#include<bios.h>
#include<dos.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#include<graphics.h>
#define get_char; ch1=getch();if(ch1=='n'||ch1==27) return(0);
#define delay_; for(delay_x=0;delay_x<1000;delay_x++){delay(delay_y);}
int gdriver = DETECT,gmode,errorcode;

char scr[25][40],disp_char[2];
char ch1;

int win_x = 39 , win_y = 24 ;
char snake_top='Q' , snake_middle = 'O' , snake_tail = 'V' ;
char snake_wall = 'X' , snake_other = 'Z' , snake_food = 'F' ;
int direction_old = 1,direction = 1 , direction_change_times=0,snake_length=3;
int snake_life=1,top_offset_x=1,top_offset_y=0;
int score=0,food_status=0;
unsigned int food_times=0,snake_step_times=0;
int snake_x[1200],snake_y[1200];
int text_color=3,text_back=0,text_mode=3;
int delay_x=1000,delay_y=150;
int check_kip=0,check_middle=0;
char far *image_food;
void main()
{
    void graph_open();
    void graph_close();
    void disp_init_scr();
    void disp_window();
    void seek_snake();
    void clear_grid();
    void game_first_delay();
    void game_delay();
    void game_direction();
    void game_over();
    void disp_snake_step();
    void disp_snake_go();
    void init_snake_food();
    void snake_eat_food();
    void outtextxy_text(char *);
    void clear_one_line();
    void get_image_food( int,  int);
    void check_create_snake_food();
    void game_if_retry();
    void game_retry_init();

        graph_open();


        system("cls");
        settextjustify(0,2);
        settextstyle(0,0,2);
        setbkcolor(9);
        setcolor(15);
        loop_1:
        disp_init_scr();          /*OK,define scr[40][25]*/
        seek_snake();           /*OK,random define snake length==3,disp snake*/
        init_snake_food();        /*OK,random look for a  space define food "F" ,disp*/
        game_direction();        /*OK,according to direction define offset x,y*/
        disp_snake_go();        /*ok,if life ,delay and step*/

        game_retry_init();
        game_if_retry();
        game_direction();
        if(snake_life==1)
        goto loop_1;
    free(image_food);
    graph_close();
    system("cls");
}

    void game_retry_init()
    {
        direction_old = 1;
        direction = 1 ;
        direction_change_times=0;
        snake_length=3;
        top_offset_x=1;top_offset_y=0;
        score=0;
        food_status=0;
        food_times=0;
        snake_step_times=0;
        check_kip=0;
        check_middle=0;

    }

    void game_if_retry()
    {
        int check_key=0;int re_circle=1;
      while(re_circle)
      {
        outtextxy(10*16,18*16,"press r restart,other quit");
          {
            check_key=0;
            check_key=bioskey(0);
                    switch( check_key )
                        {
                            case 0x1372  :
                                {
                                    snake_life=1;
                                    check_key=0;
                                    re_circle=0;
                                } break;
                            case 0x1071  :
                                {
                                    snake_life=0;
                                    check_key=0;
                                    re_circle=0;
                                } break;
                            default :
                                {
                                    snake_life=0;
                                    re_circle=1;
                                    check_key=0;
                                } break;
                        }
            }
        }
    }

    void disp_snake_go()
    {
        game_first_delay();
        while(snake_life)
        {
            {
                game_delay();
                disp_snake_step();
                snake_step_times++;
            }
        }
    }

    void game_direction()
    {
        switch( direction )
            {
                case 1  : { } break;
                case 2  : { top_offset_x=  0 ; top_offset_y=1;} break;
                case 3  : { top_offset_x= -1 ; top_offset_y=0;} break;
                case 4  : { top_offset_x=  0 ; top_offset_y=-1;} break;
                default : {top_offset_x=  1 ; top_offset_y=0;direction=1;  } break;
            }
    }

    void game_delay()
    {
        int check_i,check_k=0;
        for(delay_x=0;delay_x<1000;delay_x++)
        {
            delay(delay_y) ;
            if( bioskey(1) )
                {
                    check_k++;
                    check_kip=( bioskey(0) );
                    check_middle = check_kip;
                    switch( check_kip )
                        {
                            case 0x4d00  :
                                {
                                    if(direction_old!=3&&direction_old!=1)
                                    {
                                        direction=1 ;
                                        game_direction();
                                        check_kip=0;
                                    }
                                } break;
                            case 0x5000  :
                                {
                                     if(direction_old!=4&&direction_old!=2)
                                    {
                                        direction=2 ;
                                        game_direction();
                                        check_kip=0;
                                    }
                                } break;
                            case 0x4b00  :
                                {
                                     if(direction_old!=1&&direction_old!=3)
                                    {
                                        direction=3 ;
                                        game_direction();
                                        check_kip=0;
                                    }
                                } break;
                            case 0x4800  :
                                {
                                    if(direction_old!=2&&direction_old!=4)
                                    {
                                        direction=4 ;
                                        game_direction();
                                        check_kip=0;
                                    }
                                } break;
                            default :
                                {
                                    check_kip=0;
                                    snake_life=0;
                                } break;
                        }
                }
        }
        game_direction();
        if(direction_old!=direction)
        {
            direction_change_times++;
            direction_old=direction;
        }
    }

    void game_first_delay()
    {
        int check_i,check_k=0,check_key=0,check_circle=1;
        if(bioskey(1))
        {
            check_key=bioskey(0);
            check_key=0;
        }
        else
        check_key=bioskey(0);
                    switch( check_key )
                        {
                            case 0x4d00  :
                                {
                                        direction=1 ;
                                } break;
                            case 0x5000  :
                                {
                                        direction=2;
                                } break;
                            case 0x4b00  :
                                {
                                        direction=3 ;
                                } break;
                            case 0x4800  :
                                {
                                        direction=4 ;
                                } break;
                            default :
                                {
                                        direction=1 ;
                                } break;
                        }
        snake_life=1;
        game_direction();
        if(direction_old!=direction)
        {
            direction_change_times++;
            direction_old=direction;
        }
    }

    void game_over()
    {
        char * char_over;
        snake_life=0;
        sprintf(char_over,"12!@:'[]{}GameOver,direction=%d",direction_change_times);
        outtextxy(1*16,(win_y+1)*16,char_over);
        printf("\r123gameoverfood=%d;direction=%d;step=%d",food_times,direction_change_times,snake_step_times);
        getch();
    }

    void outtextxy_text( char *string_text )
    {
        int i,j;
            for(i=0;i<=win_x;i++)
            clear_grid(i*16,(win_y+1)*16);
            outtextxy(0,(win_y+2)*16,string_text);
            free(string_text);
    }
    void clear_one_line( )
    {
        int i,j;
            for(i=0;i<=win_x;i++)
            clear_grid(i*16,(win_y+2)*16);
    }

    void clear_grid(int x,int y)
    {
        int grid_size;
        char far *point1;
        grid_size=imagesize(0,0,15,15);
        point1=malloc(grid_size);
        if(!point1) return -1;
        getimage(x,y,x+15,y+15,point1);
        putimage(x,y,point1,XOR_PUT);
        free(point1);
    }
    void copy_grid(int x,int y,int x1,int y1)
    {
        int grid_size;
        char far *point1;
        grid_size=imagesize(0,0,15,15);
        point1=malloc(grid_size);
        if(!point1) return -1;
        getimage(x,y,x+15,y+15,point1);
        putimage(x1,y1,point1,COPY_PUT);
        free(point1);
    }
    void get_image_food( int food_x,int food_y)
    {
        int grid_size;
        grid_size=imagesize(0,0,15,15);
        image_food=malloc(grid_size);
        if(!image_food) return -1;
        getimage(food_x,food_y,food_x+15,food_y+15,image_food);
    }
    void copy_image_food(int x,int y)
    {
        putimage(x,y,image_food,COPY_PUT);
    }

    void graph_open()
    {

       initgraph(&gdriver, &gmode,"");
      errorcode = graphresult();
      system("\cls");
      if (errorcode != 0)
      {
          printf("Graphics error: %s \n", grapherrormsg(errorcode));
          printf("press any key to halt:");
          getch();
      }
      else
          {
          system("\cls");
           }
    }
    void graph_close()
    {
          closegraph();
    }


    void disp_init_scr()
    {
        int i,j;
        for(j=0;j<=win_y;j++)
            for(i=0;i<=win_x;i++)
            {
                if( (i<2) || (i>win_x-2) || (j<2) || (j>win_y-2) )
                    scr[j][i]=snake_wall;
                else
                    scr[j][i]=snake_other;
            }
        disp_char[0]='x';
        disp_char[1]='\0';

        for( i = 0 ; i < 1200 ; i++ )
        {
            snake_x[i] = -1;
            snake_y[i] = -1;
        }
        disp_window();
    }

    void disp_window()
    {
        int i,j;
        cleardevice();
        for(j=0;j<=win_y;j++)
        {
            for(i=0;i<=win_x;i++)
            {
                    disp_char[0]=scr[j][i];
                    /*if(disp_char[0]!='Z')*/
                    outtextxy(i*16,j*16,disp_char);
            }
        }
    }


    void seek_snake()
    {
        int i=win_x,j=win_y,seek_tail=0;
        int seek_result=0,seek_random=0;

        for(j=0;j<=win_y;j++)
            for(i=0;i<=(win_x-6);i++)
            {
                if(scr[j][i]==snake_other&&scr[j][i+1]==snake_other&&
                    scr[j][i+2]==snake_other&&scr[j][i+3]==snake_other&&
                    scr[j][i+4]==snake_other&&scr[j][i+5]==snake_other)
                 {
                    seek_result++;
                 }
            }
            if(seek_result<=0)
            game_over();
            srand( (unsigned)time(NULL) );
            seek_random = rand() % seek_result ;

        for( seek_result = 0 , j = 0 ; j <= win_y ; j++ )
            for( i = 0 ; i <= ( win_x - 6 ) ; i++ )
            {
                if(scr[j][i]==snake_other&&scr[j][i+1]==snake_other&&
                    scr[j][i+2]==snake_other&&scr[j][i+3]==snake_other&&
                    scr[j][i+4]==snake_other&&scr[j][i+5]==snake_other)
                {
                    if( seek_result == seek_random )
                    {
                        scr[j][i+3]=snake_top;
                        snake_x[0]=i+3;
                        snake_y[0]=j;
                        scr[j][i+2]=snake_middle;
                        snake_x[1]=i+2;
                        snake_y[1]=j;
                        scr[j][i+1]=snake_tail;
                        snake_x[2]=i+1;
                        snake_y[2]=j;

                        disp_window();
                        return(0);
                    }
                    seek_result++;
                }
            }
    }
    void snake_eat_food()
    {

    }

    void init_snake_food()
    {
        int i=win_x,j=win_y,seek_tail=0;
        int seek_result=0,seek_random=0;
        if(food_status==0)
        {
            for(j=0;j<=win_y;j++)
                for(i=0;i<=win_x;i++)
                {
                    if(scr[j][i]==snake_other)
                     {
                        seek_result++;
                     }
                }
                if(seek_result <= 0 )
                game_over();
                srand( (unsigned)time(NULL) );
                seek_random = rand() % seek_result ;

            for( seek_result = 0 , j = 0 ; j <= win_y ; j++ )
                for( i = 0 ; i <= win_x  ; i++ )
                {
                    if(scr[j][i]==snake_other)
                    {
                        if( seek_result == seek_random )
                        {
                            scr[j][i]=snake_food;
                            food_status = 1 ;
                            clear_grid(i*16,j*16);
                            outtextxy(i*16,j*16,"F");
                            get_image_food(i*16,j*16 );
                            return(0);
                        }
                        seek_result++;
                    }
                }
        }
        else
        return(0);
    }

    void check_create_snake_food()
    {
        int i=win_x,j=win_y,seek_tail=0;
        int seek_result=0,seek_random=0;
        if(food_status==0)
        {
            for(j=0;j<=win_y;j++)
                for(i=0;i<=win_x;i++)
                {
                    if(scr[j][i]==snake_other)
                     {
                        seek_result++;
                     }
                }
                if(seek_result <= 0 )
                game_over();
                srand( (unsigned)time(NULL) );
                seek_random = rand() % seek_result ;

            for( seek_result = 0 , j = 0 ; j <= win_y ; j++ )
                for( i = 0 ; i <= win_x  ; i++ )
                {
                    if(scr[j][i]==snake_other)
                    {
                        if( seek_result == seek_random )
                        {
                            scr[j][i]=snake_food;
                            food_status = 1 ;
                            clear_grid(i*16,j*16);
                            copy_image_food(i*16,j*16 );
                            return(0);
                        }
                        seek_result++;
                    }
                }
        }
        else
        return(0);
    }

    void disp_snake_step()
    {
        int i,j,k,i_top,j_top,i_tail,j_tail,i_middle,j_middle,i_next,j_next;



        i_top = snake_x[0];
        j_top = snake_y[0];
        i_tail = snake_x[snake_length-1];
        j_tail = snake_y[snake_length-1];

        j_next = (snake_y[0]) + top_offset_y ;
        i_next = (snake_x[0]) + top_offset_x ;

        if( (scr[ j_next ][ i_next ] != snake_other) &&(scr[ j_next ][ i_next ] != snake_food) )
            {
                snake_life = 0;
                game_over();
                return(0);
            }
        else
        if( scr[ (snake_y[0])+top_offset_y ][( (snake_x[0]) +
                top_offset_x )  ] == snake_other )
            {
                for( k = 0 ; k < snake_length ; k++ )
                {
                    if(k==0)
                        {
                            i = snake_x[k];
                            j = snake_y[k];
                            i_middle = snake_x[k] + top_offset_x;
                            j_middle = snake_y[k] + top_offset_y;
                            copy_grid( i * 16 , j * 16 , i_middle * 16 , j_middle * 16 ) ;
                            scr[j_middle][i_middle] = snake_top;
                        }
                    else
                        {
                                i = snake_x[k];
                                j = snake_y[k];
                                i_middle = snake_x[k-1];
                                j_middle = snake_y[k-1];

                                copy_grid( i * 16 , j * 16 , i_middle * 16 , j_middle * 16 ) ;
                                if( k < ( snake_length-1 ) )
                                {
                                    scr[j_middle][i_middle] = snake_middle;
                                }
                                if( k == ( snake_length-1 ) )
                                {
                                    scr[j_middle][i_middle] = snake_tail;
                                    scr[j][i] = snake_other;

                                    clear_grid( i * 16 , j * 16  ) ;

                                }
                        }
                }

                for( k = 0 ; k < snake_length ; k++ )
                {
                    if( k < ( snake_length - 1) )
                        {
                            snake_x[snake_length-1-k] = snake_x[snake_length-2-k] ;
                            snake_y[snake_length-1-k] = snake_y[snake_length-2-k] ;
                        }
                    if( k == snake_length - 1 )
                            {
                                    snake_x[snake_length-1-k] = i_top + top_offset_x  ;
                                    snake_y[snake_length-1-k] = j_top + top_offset_y;
                            }
                }
            }
            else
            if( scr[ (snake_y[0])+top_offset_y ][( (snake_x[0]) +
                top_offset_x )  ] == snake_food )
            {
                for( k = 0 ; k < snake_length  ; k++ )
                {
                    if(k==0)
                        {
                            i = snake_x[k];
                            j = snake_y[k];
                            i_middle = snake_x[k] + top_offset_x;
                            j_middle = snake_y[k] + top_offset_y;
                            copy_grid( i * 16 , j * 16 , i_middle * 16 , j_middle * 16 ) ;
                            scr[j_middle][i_middle] = snake_top;
                        }
                    else
                        {
                                i = snake_x[k];
                                j = snake_y[k];
                                i_middle = snake_x[k-1];
                                j_middle = snake_y[k-1];

                                if( k < ( snake_length-1 ) )
                                {
                                    copy_grid( i * 16 , j * 16 , i_middle * 16 , j_middle * 16 ) ;
                                    scr[j_middle][i_middle] = snake_middle;
                                }
                        }
                }

                for( k = 0 ; k <= snake_length ; k++ )
                {
                    if( k < ( snake_length ) )
                        {
                            snake_x[snake_length-k] = snake_x[snake_length-1-k] ;
                            snake_y[snake_length-k] = snake_y[snake_length-1-k] ;
                        }
                    if( k == snake_length  )
                        {
                            snake_x[snake_length-k] = i_top + top_offset_x;
                            snake_y[snake_length-k] = j_top + top_offset_y;
                        }
                }
                snake_length++;
                food_times++;
                food_status = 0 ;
                check_create_snake_food();
            }
    }
2013-12-09 21:34
dtxwz
Rank: 2
等 级:论坛游民
帖 子:79
专家分:45
注 册:2011-9-18
收藏
得分:8 
有意思。
2013-12-09 21:46
神软通天丶
Rank: 1
等 级:新手上路
帖 子:13
专家分:7
注 册:2013-11-4
收藏
得分:0 
吓尿了。。。。我苦逼的C语言啊。。
2013-12-10 23:20
liu229118351
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:83
专家分:101
注 册:2013-10-23
收藏
得分:0 
回复 15楼 韶志
我就是输入了w还有a,s,d输进去都这样的。。。

单曲循环,需要信心+耐心+恒心
2013-12-12 21:15
菜鸟ぃ求助
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2013-10-28
收藏
得分:0 
//我也实现了第三个  用了好长时间  真是受益匪浅  谢谢楼主的帖子!!
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
char M[30][30], flag[10], m;
int a, b;
void child();//产生小孩
void draw();//画图
int main()
{
    child();
    while (1)
    {
        int panduan = 1;
        while (panduan)
        {
            panduan = 0;
            draw();
            printf("请输入方向(wasd/WASD)\t");
            gets_s(flag);
            if (flag[0] != 'w' && flag[0] != 'a'&& flag[0] != 's' && flag[0] != 'd'&& flag[0] != 'W' && flag[0] != 'A' && flag[0] != 'S' && flag[0] != 'D')
            {
                system("cls");
                printf("输入有误!请重新输入\n"); panduan = 1;
            }
        }
        switch (flag[0])
        {
        case 's':
        case 'S':if (b == 28) b = 1; else b = b + 1; break;
        case 'A':
        case 'a':if (a == 1) a = 28; else a = a - 1; break;
        case 'W':
        case 'w':if (b == 1) b = 28; else b = b - 1; break;
        case 'D':
        case 'd':if (a == 28) a = 1; else a = a + 1; break;
        }
        system("cls");
    }
    return 0;
}
void draw()
{
    char c = 2;
    for (int i = 0; i < 30; i++)
    {
        for (int j = 0; j < 30; j++)
        {
            if (i == 0 || i == 29)
            {
                for (int n = 0; n < 30; n++)
                    M[i][j] = c;
            }
            else
            {
                if (j == 0 || j == 29)
                    M[i][j] = c;
                else
                    M[i][j] = ' ';
            }
            M[b][a] = '*';
            putchar(M[i][j]);
        }
        printf("\n");
    }

}
void child()
{
    srand(time(0));
    a = rand() % 29;
    b = rand() % 29;
}

生命不息,战斗不止!!
2013-12-13 23:35
快速回复:开心小程序,重在参与
数据加载中...
 
   



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

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