need help in game in C

This is a discussion on need help in game in C within the Game Programming forums, part of the General Programming Boards category; Hi, I'm a student in Computer Science and we have a machine project due on August 20. The problem is, ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    20

    need help in game in C

    Hi, I'm a student in Computer Science and we have a machine project due on August 20. The problem is, the professor only taught us the basics of C programming, the functions, switch statements and whatnot, and now they're giving us a project about making a game. The game is similar to Tetris in the way that there are things falling down. Because the overall project was too difficult to do, I thought of learning how to code falling texts first. Here is my code so far.

    **mp.h is a user-defined header file that was given to us.
    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <mp.h>
    #include <conio.h>
    
    void setBackground(unsigned short int color);
    int pickRandom(int min, int max);
    void delay(unsigned long milisec);
    void playSoundBeep();
    void gotoxy(int x,int y);
    void setx(int x);
    void sety(int y);
    char getKeypressed();
    void setScoreTo(int s);         // sets the score to s
    void changeScoreBy(int x);      // increments the score by x.  If x is positive the score is increased.  If x is negative the score is decreased.
    void showScore();               // shows the current score  
    int getScore();                 // returns the current score
    void clrscr(void);
    int getx(void);
    int gety(void);
    
    int main()
    {
        int lives = 3;
        int x1 = 0, y = 0;
        int x = getx();
    setBackground(BLUE);
    
    do{
    clrscr();
    gotoxy(5, y);
    printf("T");
    gotoxy(5, y++);
    delay(200);
       }
    while(y < 24);
    
    getch();
    
    }
    What I got so far is making the text fall down, only not in a straight line, but when it reaches the bottom I need it to go back up and choose another random x position and fall down in a straight line. Those two problems are what I have difficulty solving. Any help given is appreciated. Thanks

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    What I got so far is making the text fall down, only not in a straight line, but when it reaches the bottom I need it to go back up and choose another random x position and fall down in a straight line. Those two problems are what I have difficulty solving. Any help given is appreciated. Thanks
    Well, I would recommend instead of simply printing out the text, store it in a string and then and then store that string in an array or structure. You could then print the array vertically.
    Then after that you could loop through the array and check each string and evaluate its position. If it is on the ground then you can just set its position above the screen and give it a random x.

    There are many other ways to do this if you think about it, this is just a simple one.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    The object that will be falling down is actually only a letter, so I don't think I need to use an array. Also, the teacher hasn't taught us arrays and strings, but we can apply it only if we can explain how we did it, which I can't... I know this is a simple one, but I'm just a freshman college student with no experience whatsoever in programming, so this kind of "simple" programming is already a huge accomplishment for me.

  4. #4
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    The object that will be falling down is actually only a letter, so I don't think I need to use an array. Also, the teacher hasn't taught us arrays and strings, but we can apply it only if we can explain how we did it, which I can't... I know this is a simple one, but I'm just a freshman college student with no experience whatsoever in programming, so this kind of "simple" programming is already a huge accomplishment for me.
    Ah, well in that case your right, you wont need an array. My main point was to store it somewhere so you can access it later. If its just a letter you could simply use a variable, say char. I was under the impression that you are trying to code tetris, but I realized your program is a lot simpler. Well, like I said there are a number of ways to do this. If you just have one letter on screen at a time. You could simply do something like this -

    Code:
    #define SCREEN_WIDTH 10
    #define SCREEN_HEIGHT 24
    
    int lives = 3;
    int x = rand() % SCREEN_WIDTH;
    int y = 0;
    char letter = 'A';
    
    while(lives > 0)
    {
     if(y < SCREEN_HEIGHT)
     {
        clrscr();
        gotoxy(x, y);
        printf("%c",letter);
        gotoxy(x, y++);
        delay(200);
     }
     else
     {
        lives--;
        letter = rand() % 255;
        x = rand() % SCREEN_WIDTH;
        y = 0;    
     }
    
    }
    Last edited by Spidey; 07-23-2009 at 01:37 PM.

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    The thing is, it is different from tetris in the sense that all the characters that will be coming down will have to be moved to a specific position. After going to that position or to the end of the line, it will have to go back up again. My problem is maintaining that specific x position after a random generation between 0 to 79 ( my screen width should be the same as the cmd console). In my code below, I have set my x to pick a random number, but I need that number to remain constant until it goes to the y position 24. How do I do this?
    Code:
    int main()
    {
        int lives = 3;
        int x1 = 0, y = 0;
        char letter = 'a';
        
    setBackground(BLUE);
    
    while(lives > 0)
    {
       if( y < 24)
          clrscr();
          gotoxy(pickRandom(0,79), y);
          printf("%c", letter);
          switch(getKeypressed())
          {
                           case KEY_LEFT:  gotoxy(x--, y);
                           printf("%c", letter); break;
                           case KEY_RIGHT: gotoxy(x++, y);
                           printf("%c", letter); break;              
                           delay(200);
          }
    }
    
    }

  6. #6
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    look at my example above, the x is set first before you start the while loop. Then set it again after the letter reaches the ground. That scenario is reached when y > 24, hence causing the else statement to kick in.

  7. #7
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    In your new example, it will just be -

    Code:
    int main()
    {
        int lives = 3;
        int x = pickRandom(0,79);
        int  y = 0;
        char letter = 'a';
        
    setBackground(BLUE);
    
    while(lives > 0)
    {
       if( y < 24)
          clrscr();
          gotoxy(x, y);
          printf("%c", letter);
          switch(getKeypressed())
          {
                           case KEY_LEFT:  gotoxy(x--, y);
                           printf("%c", letter); break;
                           case KEY_RIGHT: gotoxy(x++, y);
                           printf("%c", letter); break;              
                           delay(200);
          }
       else
       {
          x = pickRandom(0,79);
          y = 0;
          lives--;
       }
    }
    
    }

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    Thank you so much. Now all i have to do is be able to control the character while falling down...can I make a while loop in gotoxy so that while the y is incrementing, I can change my x?

  9. #9
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    isnt that what this code is for ?
    Code:
    switch(getKeypressed())
          {
                           case KEY_LEFT:  gotoxy(x--, y);
                           printf("%c", letter); break;
                           case KEY_RIGHT: gotoxy(x++, y);
                           printf("%c", letter); break;              
                           delay(200);
          }
    also, I didn't notice this before but your if statement needs braces

    Code:
    if( y < 24)
    {
    ...
    }

  10. #10
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    Yes, but now my program waits for me to press left or right before moving right and going down. When I press the left key, it goes left and down twice.

  11. #11
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Yes, but now my program waits for me to press left or right before moving right and going down. When I press the left key, it goes left and down twice.
    Yes, that would be based on your implementation of getKeypressed();
    The reason it is going down twice is because once your program hits this function, it'll wait for your input. After that it will move down left/right and then down once. After that it will go back to the start of the loop and go down once more and wait for input. Let me see your implementation for getKeypressed() and I can tell you whats wrong.

  12. #12
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    Code:
    char getKeypressed()
    {
         char ch;
    
         ch = getch();
         if (ch==0)     
             ch = getch();
         return ch;
         
    }
    This is the function for getKeypressed() written in our defined header file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-07-2010, 11:08 PM
  2. Open Source / Semi Open source game idea. Help needed
    By CaptainPatent in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 05-16-2007, 10:44 AM
  3. game engine advice?
    By stien in forum Game Programming
    Replies: 0
    Last Post: 01-23-2007, 02:46 PM
  4. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21