How to make an Easy C++ Snake Game Code::Blocks Source in description (Time-lapse) - playproduction.de

How to make an Easy C++ Snake Game Code::Blocks Source in description (Time-lapse)

Easy Cpp Projects
Views: 34485
Like: 247
The link to the source code:

if you have any question contact me on : [email protected]

Record:OBS
Edit:AVS

Enjoy!

24 Comments

  1. Hey! could you please explain your code to us per line?
    your code is awesome!
    and it would be more lovely for me, a total beginer, to understand it per line since I really want to learn about it 😀

    Thank you very much 🙂

  2. copyng #include <iostream>
    #include <conio.h>

    void run();
    void printMap();
    void initMap();
    void move(int dx, int dy);
    void update();
    void changeDirection(char key);
    void clearScreen();
    void generateFood();

    char getMapValue(int value);

    // Map dimensions
    const int mapwidth = 20;
    const int mapheight = 20;

    const int size = mapwidth * mapheight;

    // The tile values for the map
    int map[size];

    // Snake head details
    int headxpos;
    int headypos;
    int direction;

    // Amount of food the snake has (How long the body is)
    int food = 3;

    // Determine if game is running
    bool running;

    int main()
    {
    run();
    return 0;
    }

    // Main game function
    void run()
    {
    // Initialize the map
    initMap();
    running = true;
    while (running) {
    // If a key is pressed
    if (kbhit()) {
    // Change to direction determined by key pressed
    changeDirection(getch());
    }
    // Upate the map
    update();

    // Clear the screen
    clearScreen();

    // Print the map
    printMap();

    // wait 0.5 seconds
    _sleep(500);
    }

    // Print out game over text
    std::cout << "tt!!!Game over!" << std::endl << "ttYour score is: " << food;

    // Stop console from closing instantly
    std::cin.ignore();
    }

    // Changes snake direction from input
    void changeDirection(char key) {
    /*
    W
    A + D
    S

    1
    4 + 2
    3
    */
    switch (key) {
    case 'w':
    if (direction != 2) direction = 0;
    break;
    case 'd':
    if (direction != 3) direction = 1;
    break;
    case 's':
    if (direction != 4) direction = 2;
    break;
    case 'a':
    if (direction != 5) direction = 3;
    break;
    }
    }

    // Moves snake head to new location
    void move(int dx, int dy) {
    // determine new head position
    int newx = headxpos + dx;
    int newy = headypos + dy;

    // Check if there is food at location
    if (map[newx + newy * mapwidth] == -2) {
    // Increase food value (body length)
    food++;

    // Generate new food on map
    generateFood();
    }

    // Check location is free
    else if (map[newx + newy * mapwidth] != 0) {
    running = false;
    }

    // Move head to new location
    headxpos = newx;
    headypos = newy;
    map[headxpos + headypos * mapwidth] = food + 1;

    }

    // Clears screen
    void clearScreen() {
    // Clear the screen
    system("cls");
    }

    // Generates new food on map
    void generateFood() {
    int x = 0;
    int y = 0;
    do {
    // Generate random x and y values within the map
    x = rand() % (mapwidth – 2) + 1;
    y = rand() % (mapheight – 2) + 1;

    // If location is not free try again
    } while (map[x + y * mapwidth] != 0);

    // Place new food
    map[x + y * mapwidth] = -2;
    }

    // Updates the map
    void update() {
    // Move in direction indicated
    switch (direction) {
    case 0: move(-1, 0);
    break;
    case 1: move(0, 1);
    break;
    case 2: move(1, 0);
    break;
    case 3: move(0, -1);
    break;
    }

    // Reduce snake values on map by 1
    for (int i = 0; i < size; i++) {
    if (map[i] > 0) map[i]–;
    }
    }

    // Initializes map
    void initMap()
    {
    // Places the initual head location in middle of map
    headxpos = mapwidth / 2;
    headypos = mapheight / 2;
    map[headxpos + headypos * mapwidth] = 1;

    // Places top and bottom walls
    for (int x = 0; x < mapwidth; ++x) {
    map[x] = -1;
    map[x + (mapheight – 1) * mapwidth] = -1;
    }

    // Places left and right walls
    for (int y = 0; y < mapheight; y++) {
    map[0 + y * mapwidth] = -1;
    map[(mapwidth – 1) + y * mapwidth] = -1;
    }

    // Generates first food
    generateFood();
    }

    // Prints the map to console
    void printMap()
    {
    for (int x = 0; x < mapwidth; ++x) {
    for (int y = 0; y < mapheight; ++y) {
    // Prints the value at current x,y location
    std::cout << getMapValue(map[x + y * mapwidth]);
    }
    // Ends the line for next x value
    std::cout << std::endl;
    }
    }

    // Returns graphical character for display from map value
    char getMapValue(int value)
    {
    // Returns a part of snake body
    if (value > 0) return 'o';

    switch (value) {
    // Return wall
    case -1: return 'X';
    // Return food
    case -2: return 'O';
    }
    }

  3. First found a code without errors. Thanks man 🙂

  4. how can I make it turn to the left , right , up and down?

  5. Good, but if you want to put apples in REALLY random cells, you should to write :"srand(time(NULL))" in function: "main" (to write it, you should to replace "#include <iostream>" to" #include <bits/stdc++.h>"). If you don't write it, apples will appear in the same cells when you restart the code. Sorry for my English.

  6. I got everything coded right but one issue. The snake itself is just one block and doesn't leave a trial where you can run into yourself. I've double checked the code for the snake and I haven't noticed any typos. Please help.

  7. Very good, but make the speed a bit shorter.

  8. I tried IT 6 Times and IT didn't work

Leave a Reply

Your email address will not be published.