Thread: New to C Coding: Snake Game Project

  1. #1
    Registered User
    Join Date
    May 2018
    Posts
    3

    Angry New to C Coding: Snake Game Project

    Hello all! I am new to C coding and this forum... so excuse the ugly and crude code I am about to ask for help on.

    Basically I am creating a Snake game for a course I am taking, and I have worked out pretty much everything to a point. I was looking for help on how to make the body of the snake bend! At the moment it just flops around and follows the head of the snake.

    Yes I know there are plenty of snake game codes online but that's not what I am looking for. I wanted to create it by myself but realized its a bit more then I knew how to do myself.

    If someone could look at my code and enlighten me on how to make the body bend I would be very appreciative.

    Code:
    //The only thing it is missing is how to bend the snakes body #include <stdio.h> #include <time.h> #include <stdlib.h> #include <conio.h> #include<time.h> #include<ctype.h> #include <time.h> #include <windows.h> #include <process.h> #define UP 72 #define DOWN 80 #define LEFT 75 #define RIGHT 77 struct coordinate{ int X; int Y; int direction; }; typedef struct coordinate coordinate; coordinate head, apple, sBody; int body[500]; int gameOver=0; int appleEaten=0; int life=3; int main() { setXY(2,2); printf("Remaining Lives"); apple.X = 0; apple.Y = 0; boarderDraw(); spawnSnake(); move(); } void boarderDraw() { setXY(2,2); printf("Remaining Lives: "); setXY(19,2); printf("%d", life); int i; for(i=5; i<81; i++) { setXY(i,5); printf("~"); setXY(i,30); printf("~"); } for(i=5; i<31; i++) { setXY(5,i); printf("|"); setXY(80,i); printf("|"); } } void spawnSnake() { int x=81/2; int y=31/2; head.X = x; head.Y = y; head.direction = RIGHT; setXY(x,y); printf(">"); } void move(){ endGame(); boarderDraw(); food(); printBody(); char input; while(!kbhit()) { int localDir; localDir=head.direction; if(localDir==LEFT) { endGame(); boarderDraw(); system("cls"); head.X--; boarderDraw(); food(); setXY(head.X,head.Y); printf("<"); eat(); printBody(); } else if(localDir==RIGHT) { endGame(); boarderDraw(); system("cls"); head.X++; boarderDraw(); food(); setXY(head.X,head.Y); printf(">"); eat(); printBody(); } else if(localDir==UP) { endGame(); boarderDraw(); system("cls"); head.Y--; boarderDraw(); food(); setXY(head.X,head.Y); printf("^"); eat(); printBody(); } else { endGame(); boarderDraw(); system("cls"); head.Y++; boarderDraw(); food(); setXY(head.X,head.Y); printf("v"); eat(); printBody(); } } if(kbhit()) { endGame(); while(gameOver==0) { input = getch(); //Change INPUT to desired direction. if(input == UP) { up(); } if(input == DOWN) { down(); } if(input == LEFT) { left(); } if(input == RIGHT) { right(); } else if(gameOver==1) { system("cls"); } else if(input == 27) { gameOver=1; system("cls"); exit(0); } } } } void goToXY(int x, int y) { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); } void setXY(int x, int y) { HANDLE h; //undefined pointer h COORD c; fflush(stdout); c.X = x; c.Y = y; h = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(h,c); } void up() { eat(); if (head.direction!=DOWN) { head.direction=UP; head.Y--; system("cls"); boarderDraw(); setXY(head.X,head.Y); printf("^"); endGame(); move(); food(); printBody(); } else { endGame(); move(); food(); } } void down() { eat(); if (head.direction!=UP) { head.direction=DOWN; head.Y++; system("cls"); boarderDraw(); setXY(head.X,head.Y); printf("v"); endGame(); move(); food(); printBody(); } else { endGame(); move(); food(); } } void left() { eat(); if (head.direction!=RIGHT) { head.direction=LEFT; head.X--; system("cls"); boarderDraw(); setXY(head.X,head.Y); printf("<"); endGame(); move(); food(); printBody(); } else { endGame(); move(); food(); } } void right() { eat(); if (head.direction!=LEFT) { head.direction=RIGHT; head.X++; system("cls"); boarderDraw(); setXY(head.X,head.Y); printf(">"); endGame(); move(); food(); printBody(); } else { endGame(); move(); food(); } } void food() { if(apple.X==0 && apple.Y==0) { int rx = ((rand() % 74 + 5)); int ry = ((rand() % 24 + 5)); apple.X=rx; apple.Y=ry; setXY(apple.X, apple.Y); printf("A"); } else { setXY(apple.X, apple.Y); printf("A"); } } void eat() { if((head.X == apple.X) && (head.Y == apple.Y)) { appleEaten++; apple.X=0; apple.Y=0; body[appleEaten]=0; } /*for(int i=0;i<appleEaten;i++) { setXY(head.X, (head.Y)); printf("%d", body[i]); food(); move(); } */ } void printBody() { if (head.direction == RIGHT) { for(int i=1;i<=appleEaten;i++) { sBody.X=(head.X-i); sBody.Y=(head.Y); setXY(sBody.X, sBody.Y); printf("%d", body[i]); } } else if (head.direction == LEFT) { for(int i=1;i<=appleEaten;i++) { sBody.X=(head.X+i); sBody.Y=(head.Y); setXY(sBody.X, sBody.Y); printf("%d", body[i]); } } else If (head.direction == UP) { for(int i=1;i<=appleEaten;i++) { sBody.X=(head.X); sBody.Y=(head.Y+i); setXY(sBody.X, sBody.Y); printf("%d", body[i]); } } else if (head.direction == DOWN) { for(int i=1;i<=appleEaten;i++) { sBody.X=(head.X); sBody.Y=(head.Y-i); setXY(sBody.X, sBody.Y); printf("%d", body[i]); } } } void endGame() { if(head.X<=5||head.X>=81||head.Y<=5||head.Y>=30) { life--; system("cls"); appleEaten=0; main(); } else { gameOver=0; } if(life==0) { gameOver=1; system("cls"); exit(0); } }

    Thanks for looking, and side note although its not a huge deal if anyone can help make the screen less flashy that would be great.

    -Spec

  2. #2
    Registered User
    Join Date
    May 2018
    Posts
    3
    Copy and paste didn't work so great there from codeblocks... sorry no indents I swear I have them!

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    952
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <ctype.h>
    
    #include <windows.h>
    #include <conio.h>
    #include <process.h>
    
    #define UP    72
    #define DOWN  80
    #define LEFT  75
    #define RIGHT 77
    
    struct coordinate {
        int X;
        int Y;
        int direction;
    };
    
    typedef struct coordinate coordinate;
    
    coordinate head, apple, sBody;
    
    int body[500];
    int gameOver = 0;
    int appleEaten = 0;
    int life = 3;
    
    int main() {
        setXY(2, 2);
        printf("Remaining Lives");
        apple.X = 0;
        apple.Y = 0;
        boarderDraw();
        spawnSnake();
        move();
    }
    
    void boarderDraw() {
        setXY(2, 2);
        printf("Remaining Lives: ");
        setXY(19, 2);
        printf("%d", life);
        int i;
        for (i = 5; i < 81; i++) {
            setXY(i, 5);
            printf("~");
            setXY(i, 30);
            printf("~");
        }
        for (i = 5; i < 31; i++) {
            setXY(5, i);
            printf("|");
            setXY(80, i);
            printf("|");
        }
    }
    
    void spawnSnake() {
        int x = 81 / 2;
        int y = 31 / 2;
        head.X = x;
        head.Y = y;
        head.direction = RIGHT;
        setXY(x, y);
        printf(">");
    }
    
    void move() {
        endGame();
        boarderDraw();
        food();
        printBody();
        char input;
        while (!kbhit()) {
            int localDir;
            localDir = head.direction;
            if (localDir == LEFT) {
                endGame();
                boarderDraw();
                system("cls");
                head.X--;
                boarderDraw();
                food();
                setXY(head.X, head.Y);
                printf("<");
                eat();
                printBody();
            }
            else if (localDir == RIGHT) {
                endGame();
                boarderDraw();
                system("cls");
                head.X++;
                boarderDraw();
                food();
                setXY(head.X, head.Y);
                printf(">");
                eat();
                printBody();
            } else if (localDir == UP) {
                endGame();
                boarderDraw();
                system("cls");
                head.Y--;
                boarderDraw();
                food();
                setXY(head.X, head.Y);
                printf("^");
                eat();
                printBody();
            } else {
                endGame();
                boarderDraw();
                system("cls");
                head.Y++;
                boarderDraw();
                food();
                setXY(head.X, head.Y);
                printf("v");
                eat();
                printBody();
            }
        }
        if (kbhit()) {
            endGame();
            while (gameOver == 0) {
                input = getch();    //Change INPUT to desired direction.         
                if (input == UP) {
                    up();
                }
                if (input == DOWN) {
                    down();
                }
                if (input == LEFT) {
                    left();
                }
                if (input == RIGHT) {
                    right();
                } else if (gameOver == 1) {
                    system("cls");
                } else if (input == 27) {
                    gameOver = 1;
                    system("cls");
                    exit(0);
                }
            }
        }
    }
    
    void goToXY(int x, int y) {
        COORD coord;
        coord.X = x;
        coord.Y = y;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
    
    void setXY(int x, int y) {
        COORD c;
        fflush(stdout);
        c.X = x;
        c.Y = y;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
    }
    
    void up() {
        eat();
        if (head.direction != DOWN) {
            head.direction = UP;
            head.Y--;
            system("cls");
            boarderDraw();
            setXY(head.X, head.Y);
            printf("^");
            endGame();
            move();
            food();
            printBody();
        } else {
            endGame();
            move();
            food();
        }
    }
    
    void down() {
        eat();
        if (head.direction != UP) {
            head.direction = DOWN;
            head.Y++;
            system("cls");
            boarderDraw();
            setXY(head.X, head.Y);
            printf("v");
            endGame();
            move();
            food();
            printBody();
        } else {
            endGame();
            move();
            food();
        }
    }
    
    void left() {
        eat();
        if (head.direction != RIGHT) {
            head.direction = LEFT;
            head.X--;
            system("cls");
            boarderDraw();
            setXY(head.X, head.Y);
            printf("<");
            endGame();
            move();
            food();
            printBody();
        } else {
            endGame();
            move();
            food();
        }
    }
    
    void right() {
        eat();
        if (head.direction != LEFT) {
            head.direction = RIGHT;
            head.X++;
            system("cls");
            boarderDraw();
            setXY(head.X, head.Y);
            printf(">");
            endGame();
            move();
            food();
            printBody();
        } else {
            endGame();
            move();
            food();
        }
    }
    
    void food() {
        if (apple.X == 0 && apple.Y == 0) {
            int rx = ((rand() % 74 + 5));
            int ry = ((rand() % 24 + 5));
            apple.X = rx;
            apple.Y = ry;
            setXY(apple.X, apple.Y);
            printf("A");
        } else {
            setXY(apple.X, apple.Y);
            printf("A");
        }
    }
    
    void eat() {
        if (head.X == apple.X && head.Y == apple.Y) {
            appleEaten++;
            apple.X = 0;
            apple.Y = 0;
            body[appleEaten] = 0;
        }
    /*  for(int i=0;i<appleEaten;i++)            
        {                
            setXY(head.X, (head.Y));                
            printf("%d", body[i]);                
            food();                
            move();            
        }*/
    }
    
    void printBody() {
        if (head.direction == RIGHT) {
            for (int i = 1; i <= appleEaten; i++) {
                sBody.X = (head.X - i);
                sBody.Y = (head.Y);
                setXY(sBody.X, sBody.Y);
                printf("%d", body[i]);
            }
        } else if (head.direction == LEFT) {
            for (int i = 1; i <= appleEaten; i++) {
                sBody.X = (head.X + i);
                sBody.Y = (head.Y);
                setXY(sBody.X, sBody.Y);
                printf("%d", body[i]);
            }
        } else if(head.direction == UP) {
            for (int i = 1; i <= appleEaten; i++) {
                sBody.X = (head.X);
                sBody.Y = (head.Y + i);
                setXY(sBody.X, sBody.Y);
                printf("%d", body[i]);
            }
        }
        else if (head.direction == DOWN) {
            for (int i = 1; i <= appleEaten; i++) {
                sBody.X = (head.X);
                sBody.Y = (head.Y - i);
                setXY(sBody.X, sBody.Y);
                printf("%d", body[i]);
            }
        }
    }
    
    void endGame() {
        if (head.X <= 5 || head.X >= 81 || head.Y <= 5 || head.Y >= 30) {
            life--;
            system("cls");
            appleEaten = 0;
            main();
        } else {
            gameOver = 0;
        }
        if (life == 0) {
            gameOver = 1;
            system("cls");
            exit(0);
        }
    }
    I'm not sure what you mean by bending the body. And I can't run your program since I don't use windows. Are you saying that the snake is always drawn as a straight line? In that case, you need to store the coordinates of each body part and I guess "shift" all the coords when it moves (each takes the coord of the body part in front of it except for the head which adds a new coord).

    Some criticism:
    Don't reapeat header files.
    "Border" doesn't have an 'a' in it.
    Use putchar instead of printf to output a single charcter: putchar('>');
    Instead of calling main recursively, use a loop.
    You should have function prototypes before main.
    You shouldn't be using global variables. (Maybe next program!)

    As for pasting code into code tags in the forum, if you have a "Paste as plain text" option, use that; otherwise, paste it into notepad first, then recopy it to the clipboard from there, then paste it into the forum.
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  4. #4
    Registered User
    Join Date
    May 2018
    Posts
    3
    Quote Originally Posted by john.c View Post
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <ctype.h>
    
    #include <windows.h>
    #include <conio.h>
    #include <process.h>
    
    #define UP    72
    #define DOWN  80
    #define LEFT  75
    #define RIGHT 77
    
    struct coordinate {
        int X;
        int Y;
        int direction;
    };
    
    typedef struct coordinate coordinate;
    
    coordinate head, apple, sBody;
    
    int body[500];
    int gameOver = 0;
    int appleEaten = 0;
    int life = 3;
    
    int main() {
        setXY(2, 2);
        printf("Remaining Lives");
        apple.X = 0;
        apple.Y = 0;
        boarderDraw();
        spawnSnake();
        move();
    }
    
    void boarderDraw() {
        setXY(2, 2);
        printf("Remaining Lives: ");
        setXY(19, 2);
        printf("%d", life);
        int i;
        for (i = 5; i < 81; i++) {
            setXY(i, 5);
            printf("~");
            setXY(i, 30);
            printf("~");
        }
        for (i = 5; i < 31; i++) {
            setXY(5, i);
            printf("|");
            setXY(80, i);
            printf("|");
        }
    }
    
    void spawnSnake() {
        int x = 81 / 2;
        int y = 31 / 2;
        head.X = x;
        head.Y = y;
        head.direction = RIGHT;
        setXY(x, y);
        printf(">");
    }
    
    void move() {
        endGame();
        boarderDraw();
        food();
        printBody();
        char input;
        while (!kbhit()) {
            int localDir;
            localDir = head.direction;
            if (localDir == LEFT) {
                endGame();
                boarderDraw();
                system("cls");
                head.X--;
                boarderDraw();
                food();
                setXY(head.X, head.Y);
                printf("<");
                eat();
                printBody();
            }
            else if (localDir == RIGHT) {
                endGame();
                boarderDraw();
                system("cls");
                head.X++;
                boarderDraw();
                food();
                setXY(head.X, head.Y);
                printf(">");
                eat();
                printBody();
            } else if (localDir == UP) {
                endGame();
                boarderDraw();
                system("cls");
                head.Y--;
                boarderDraw();
                food();
                setXY(head.X, head.Y);
                printf("^");
                eat();
                printBody();
            } else {
                endGame();
                boarderDraw();
                system("cls");
                head.Y++;
                boarderDraw();
                food();
                setXY(head.X, head.Y);
                printf("v");
                eat();
                printBody();
            }
        }
        if (kbhit()) {
            endGame();
            while (gameOver == 0) {
                input = getch();    //Change INPUT to desired direction.         
                if (input == UP) {
                    up();
                }
                if (input == DOWN) {
                    down();
                }
                if (input == LEFT) {
                    left();
                }
                if (input == RIGHT) {
                    right();
                } else if (gameOver == 1) {
                    system("cls");
                } else if (input == 27) {
                    gameOver = 1;
                    system("cls");
                    exit(0);
                }
            }
        }
    }
    
    void goToXY(int x, int y) {
        COORD coord;
        coord.X = x;
        coord.Y = y;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
    
    void setXY(int x, int y) {
        COORD c;
        fflush(stdout);
        c.X = x;
        c.Y = y;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
    }
    
    void up() {
        eat();
        if (head.direction != DOWN) {
            head.direction = UP;
            head.Y--;
            system("cls");
            boarderDraw();
            setXY(head.X, head.Y);
            printf("^");
            endGame();
            move();
            food();
            printBody();
        } else {
            endGame();
            move();
            food();
        }
    }
    
    void down() {
        eat();
        if (head.direction != UP) {
            head.direction = DOWN;
            head.Y++;
            system("cls");
            boarderDraw();
            setXY(head.X, head.Y);
            printf("v");
            endGame();
            move();
            food();
            printBody();
        } else {
            endGame();
            move();
            food();
        }
    }
    
    void left() {
        eat();
        if (head.direction != RIGHT) {
            head.direction = LEFT;
            head.X--;
            system("cls");
            boarderDraw();
            setXY(head.X, head.Y);
            printf("<");
            endGame();
            move();
            food();
            printBody();
        } else {
            endGame();
            move();
            food();
        }
    }
    
    void right() {
        eat();
        if (head.direction != LEFT) {
            head.direction = RIGHT;
            head.X++;
            system("cls");
            boarderDraw();
            setXY(head.X, head.Y);
            printf(">");
            endGame();
            move();
            food();
            printBody();
        } else {
            endGame();
            move();
            food();
        }
    }
    
    void food() {
        if (apple.X == 0 && apple.Y == 0) {
            int rx = ((rand() % 74 + 5));
            int ry = ((rand() % 24 + 5));
            apple.X = rx;
            apple.Y = ry;
            setXY(apple.X, apple.Y);
            printf("A");
        } else {
            setXY(apple.X, apple.Y);
            printf("A");
        }
    }
    
    void eat() {
        if (head.X == apple.X && head.Y == apple.Y) {
            appleEaten++;
            apple.X = 0;
            apple.Y = 0;
            body[appleEaten] = 0;
        }
    /*  for(int i=0;i<appleEaten;i++)            
        {                
            setXY(head.X, (head.Y));                
            printf("%d", body[i]);                
            food();                
            move();            
        }*/
    }
    
    void printBody() {
        if (head.direction == RIGHT) {
            for (int i = 1; i <= appleEaten; i++) {
                sBody.X = (head.X - i);
                sBody.Y = (head.Y);
                setXY(sBody.X, sBody.Y);
                printf("%d", body[i]);
            }
        } else if (head.direction == LEFT) {
            for (int i = 1; i <= appleEaten; i++) {
                sBody.X = (head.X + i);
                sBody.Y = (head.Y);
                setXY(sBody.X, sBody.Y);
                printf("%d", body[i]);
            }
        } else if(head.direction == UP) {
            for (int i = 1; i <= appleEaten; i++) {
                sBody.X = (head.X);
                sBody.Y = (head.Y + i);
                setXY(sBody.X, sBody.Y);
                printf("%d", body[i]);
            }
        }
        else if (head.direction == DOWN) {
            for (int i = 1; i <= appleEaten; i++) {
                sBody.X = (head.X);
                sBody.Y = (head.Y - i);
                setXY(sBody.X, sBody.Y);
                printf("%d", body[i]);
            }
        }
    }
    
    void endGame() {
        if (head.X <= 5 || head.X >= 81 || head.Y <= 5 || head.Y >= 30) {
            life--;
            system("cls");
            appleEaten = 0;
            main();
        } else {
            gameOver = 0;
        }
        if (life == 0) {
            gameOver = 1;
            system("cls");
            exit(0);
        }
    }
    I'm not sure what you mean by bending the body. And I can't run your program since I don't use windows. Are you saying that the snake is always drawn as a straight line? In that case, you need to store the coordinates of each body part and I guess "shift" all the coords when it moves (each takes the coord of the body part in front of it except for the head which adds a new coord).

    Some criticism:
    Don't reapeat header files.
    "Border" doesn't have an 'a' in it.
    Use putchar instead of printf to output a single charcter: putchar('>');
    Instead of calling main recursively, use a loop.
    You should have function prototypes before main.
    You shouldn't be using global variables. (Maybe next program!)

    As for pasting code into code tags in the forum, if you have a "Paste as plain text" option, use that; otherwise, paste it into notepad first, then recopy it to the clipboard from there, then paste it into the forum.
    Is there an equivalent of system(“cls”) and setcursor as they are what I use Windows for

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,121
    Yes there is an alternative.

    You see SetConsoleCursorPosition ?

    Well head on over to MSDN and find out what other functions reside in the Win32 Console API.
    I'm sure you'll find something to clear the screen.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Snake game
    By coder222 in forum C Programming
    Replies: 2
    Last Post: 12-08-2015, 03:03 PM
  2. Dat snake game...
    By esrelmantis in forum C++ Programming
    Replies: 3
    Last Post: 05-19-2013, 04:24 AM
  3. About SnaKE game..
    By ozumsafa in forum C Programming
    Replies: 3
    Last Post: 10-19-2007, 05:46 PM
  4. My Snake Game
    By ()Q() in forum Game Programming
    Replies: 2
    Last Post: 07-23-2002, 03:03 PM
  5. game (snake)
    By nps in forum C Programming
    Replies: 1
    Last Post: 10-09-2001, 10:37 PM

Tags for this Thread