Thread: game

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    8

    game

    Hello guyz im trying to create "game" in lang. c. I create 2d string where i randomly put '*' and i move the char '&' by arrows. Ss there some possible way how can i do something when the '&' hit '*'?
    Thanks (under is my code)
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<windows.h>
    #include<stdlib.h>
    #include<time.h>
    
    #define KeyUp 72
    #define KeyLeft 75
    #define KeyRight 77
    #define KeyEsc 27
    #define KeyDown 80
    
    void gotoxy (short x, short y)
    {
        COORD pos = {x,y};
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
    }
    
    int main()
    {
        printf("VITAJ V HRE THE METEOR!\n");
        printf("TVOJOU ULOHOU JE PREJST CEZ PADAJUCE METEORITY!\n");
        printf("SI READY?(stlac lub.klavesu)");
        getch();
        system("cls");
        printf("START!(hru ukoncis stlacenim klavesy ESC)");
        Sleep(3000);
        system("cls");
        int i,r;
        int x=8,y=7;
        int x1=1,y1=1;
        int ch,k=0;
    
        char field[7][20]={"-----------------",//pole
                           "                 ",
                           "                 ",
                           "                 ",
                           "                 ",
                           "                 ",
                           "                 ",
                           "                 "};
        for(i=0;i<7;i++)//vypis pola
        {
            printf("%s\n",field[i]);    
        }
        for(i=0;i<=15;i++)
        {
            x1=rand()%15+1;
            y1=rand()%5+2;
            gotoxy(x1,y1);
            printf("*");
            Sleep(100);
        }
        while(k!=2)
        {
            while(k!=1)
            {
            gotoxy(x,y);printf("&");
            ch=getch();
                switch(ch)
                {
                    case KeyUp:if(y>1)y--;k++;break;
                    case KeyDown:if(y<7)y++;k++;break;
                    case KeyLeft:if(x>0)x--;k++;break;
                    case KeyRight:if(x<16)x++;k++;break;
                    case KeyEsc:k++;k++;break;
                }    
            }
            if(k==2)
            {
                system("cls");
                printf("UKONCIL SI HRU!");
                Sleep(1000);
                return 0;
            }
            k=0;
        }
        
    
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You're going to have to remember where you put the *, so you'll want to store those and not just print them out. It looks like you very briefly use some sort of COORD data structure to deal with points, but then devolve back into separate x's and y's; if so you'll presumably need to put the x's in an array and the y's in an array and then check whether your current coordinate (x,y) matches a star_x[i] and a corresponding star_y[i].

  3. #3
    Registered User
    Join Date
    Oct 2018
    Posts
    8
    can you help me how exactly i store it? i think i just have to change this part but i dont how exactly do it
    Code:
    for(i=0;i<=15;i++)
        {
            x1=rand()%15+1;
            y1=rand()%5+2;
            gotoxy(x1,y1);
            printf("*");
            Sleep(100);
        }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The same way you've stored every other number you want to save: you declare a variable to hold the number you want to keep, and then later you use = to put a number in that spot. In this case, since you want a bunch of related variables, rather than try to come up with a bunch of different names, you can use an array to store them all (and use an [index] to access each particular item).

  5. #5
    Registered User
    Join Date
    Oct 2018
    Posts
    8
    i am lost
    really dont know what to do

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You're generating x1 and y1 in that loop; store them somewhere so that you can use them later.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Starting a simple game server for tile based game
    By cpu0 in forum Game Programming
    Replies: 1
    Last Post: 01-19-2017, 12:15 PM
  2. Should I use a game engine in making a RPG game?
    By m3rk in forum Game Programming
    Replies: 6
    Last Post: 01-26-2009, 04:58 AM
  3. Guessing game: how to quit the game?
    By hzr in forum C Programming
    Replies: 5
    Last Post: 12-18-2008, 10:53 AM
  4. craps game & dice game..
    By cgurl05 in forum C Programming
    Replies: 3
    Last Post: 03-25-2006, 07:58 PM

Tags for this Thread