#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Help with program


    Need to fix control:
    - need solve the problem with opposite directions;
    - need to do control by cursor keys
    Code:
     
    #include <stdlib.h>
    #include <dos.h>
    #include <stdio.h>
    #include <conio.h>
    void draw_line(int x, int y);
    void show_score();
    void move();
    void setup_level();
    const int maxy=15, maxx=77;
    const int snake_start_x=33,snake_start_y=7;
    const char up_key='w', down_key='s', left_key='a', right_key='d';
    int score, sl, speed, food, i, level, firstpress, high_score=0;
    char food_field[maxy][maxx];
    char direction = right_key;
    struct snake_segment {
    int y,x;
    char h=1;
    } snake[100];
    main()
    {
    int y1,x1;
    char keypress;
    do 
    {
    food=3; level=1; score=0; speed=150;
    rand(); 
    setup_level();
    do
    {
    delay(speed); 
    if (kbhit())
    {
    keypress=getch();
    direction=keypress;
    }
    move();
    gotoxy(snake[0].x,snake[0].y);
    cprintf(" ");
    for (i=1;i<=sl;i++)
    snake[i-1]=snake[i];
    textcolor(LIGHTGREEN);
    for (i=0;i<=sl;i++)
    {
    gotoxy(snake[i].x,snake[i].y);
    cprintf("0");
    }
    textcolor(DARKGRAY);
    y1=wherey();
    x1=wherex();
    gotoxy(x1-1,y1); cprintf("%c",h);
    for (i=0;i<=sl;i++)
    if(keypress==up_key){gotoxy(x1-1,y1); cprintf("%c",h);}else
    if(keypress==down_key){gotoxy(x1-1,y1); cprintf("%c",h);}else
    if(keypress==left_key){gotoxy(x1-1,y1); cprintf("%c",h);}else
    if(keypress==right_key){gotoxy(x1-1,y1); cprintf("%c",h);}
    _setcursortype(_NOCURSOR);
    if (firstpress) { while(!kbhit()); firstpress = 0; }
    if ((snake[sl-1].y>maxy+2)||(snake[sl-1].y<=2)||
    (snake[sl-1].x>maxx+1)||(snake[sl-1].x<=1)||
     (food_field[snake[sl-1].y-3][snake[sl-1].x-2]=='X'))
    keypress='x'; 
    for (i=0;i<sl-1;i++)
    if ( (snake[sl-1].y)==(snake[i].y) &&
    (snake[sl-1].x)==(snake[i].x))
    {
    keypress='x'; 
    }
    if (food_field[snake[sl-1].y-3][snake[sl-1].x-2]=='*')
    {
    score+=sl+food; show_score(); sl++;
    if (sl==(level+3)*2)
    {
    score+=level*1; food+=1; level++;  
    if ((level%2==0)&&(speed>1)) speed-10; 
    setup_level(); 
    }
    }
    } while (keypress!='x');
    if (score > high_score) high_score = score;
    show_score();
    gotoxy(30,7); textcolor(LIGHTRED); cprintf("G A M E   O V E R");
    gotoxy(30,10); textcolor(YELLOW); cprintf("Another Game (y/n)? ");
    do keypress=getch(); while((keypress!='y')&&(keypress!='n'));
    } while (keypress=='y');
    return 0;
    }
    void setup_level()
    {
    int y,x,i;
    sl=level+4; direction=right_key;
    firstpress=1;
    for(y=0;y<maxy;y++)
    for(x=0;x<maxx;x++)
    food_field[y][x]= ' ';
    for(i=0;i<food*2;i++)
    {
    y= rand()%maxy;
    x= rand()%maxx;
    if(i<food)
    food_field[y][x]='X';
    else
    food_field[y][x]='*';
    }
    for(i=0;i<sl;i++)
    {
    snake[i].y=snake_start_y;
    snake[i].x=snake_start_x+i;
    }
    draw_line(1,2);
    for(y=0;y<maxy;y++)
    {
    gotoxy(1,y+3);
    textcolor(LIGHTBLUE); cprintf("|");
    textcolor(BROWN);
    for(x=0;x<maxx;x++)
    cprintf("%c",food_field[y][x]);
    textcolor(LIGHTBLUE);
    cprintf("|");
    }
    draw_line(1,maxy+3);
    show_score();
    gotoxy(2,maxy+6);
    textcolor(LIGHTRED);
    cprintf("Left: %c,Right: %c, Up: %c, Down: %c, Exit: x. Any key to start.",
     left_key,right_key,up_key,down_key);
     textcolor(GREEN);
    gotoxy(32,1);
    cprintf("~~~SNAKE GAME~~~");
     return;
    }
    void draw_line(int x, int y)
    {
     gotoxy(x,y); textcolor(LIGHTBLUE);
    for (x=0;x<maxx+2;x++) cprintf("=");
    return;
    }
    void show_score()
    {
    textcolor(LIGHTCYAN);
    gotoxy(2,maxy+4);
    cprintf("Level: %05d",level);
    gotoxy(40,maxy+4);
    textcolor(LIGHTGREEN);
    cprintf("Score: %05d",score);
    gotoxy(60,maxy+4);
    textcolor(LIGHTMAGENTA);
    cprintf("High Score: %05d",high_score);
    return;
    }
    void move()
    {
    switch(direction)
    {
    case(right_key): snake[sl].y=snake[sl-1].y;
                     snake[sl].x=snake[sl-1].x+1;
                     break;
    case(left_key) : snake[sl].y=snake[sl-1].y;
                     snake[sl].x=snake[sl-1].x-1;
                     break;
    case(up_key)   : snake[sl].y=snake[sl-1].y-1;
    					  snake[sl].x=snake[sl-1].x;
    					  break;
    case(down_key) : snake[sl].y=snake[sl-1].y+1;
    					  snake[sl].x=snake[sl-1].x;
                     break;
      }
    return ;
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Nice that you used code tags. Very nice, in fact. Truly commendable.

    But you do still need to indent your code. As it is, your code is an unreadable mess. We cannot bother to read such a mess.

    Here's a basic question about your use of cursor keys: Do you realize that they are extended keys? That means that instead of reading them once, you need to read them twice. The first time, you will read a zero, which tells you that this is an extended key and that you need to read it a second time. It's the second reading that contains the key code you want.

    Of course, because your code is such an unreadable mess, I can't tell whether you had realized that or not.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by dwise1_aol
    Here's a basic question about your use of cursor keys: Do you realize that they are extended keys? That means that instead of reading them once, you need to read them twice. The first time, you will read a zero, which tells you that this is an extended key and that you need to read it a second time. It's the second reading that contains the key code you want.
    Thanks, i didnt know about that.

IMN logo majestic logo threadwatch logo seochat tools logo