当前位置:网站首页>C language --- basic function realization of push box 01

C language --- basic function realization of push box 01

2022-06-26 16:41:00 D_ eretay

Catalog

1. Preface

2. Game effect display

3. Project analysis

4. Map implementation

4.1 Store maps

4.2 Print a map

5. Control character movement

5.1 Find controlled roles

5.2 Enabling mobility

6. Judge victory


1. Preface

The first thing to push the box is c A classic project of language , This article will show you how to implement... From scratch c How does the language implement push box

2. Game effect display

3. Project analysis

Game elements :

  • clearing 0
  • The wall 1
  • role 2
  • The box 3
  • Victory point 4

Game purpose :

The player controls the character to move in the open space of the map , Avoid obstacles by pushing the box , Push all the boxes to the victory point to succeed .

 

4. Map implementation

4.1 Store maps

First, we use an array to store the map , Why do we use char instead of int? Because int Generally 4 byte ,char by 1 byte , We can greatly reduce the memory required to store maps . If you want to write more than one map , Just expand the array to three dimensions , The author will explain it in a later article .

char map[10][10] = 
{ 
    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, 
    { 1, 0, 0, 0, 0, 1, 0, 0, 0, 1 }, 
    { 1, 0, 0, 0, 0, 1, 4, 0, 0, 1 }, 
    { 1, 0, 0, 0, 0, 1, 1, 0, 0, 1 }, 
    { 1, 0, 0, 0, 0, 1, 0, 0, 0, 1 }, 
    { 1, 0, 0, 2, 0, 0, 0, 0, 0, 1 }, 
    { 1, 1, 1, 1, 0, 0, 3, 0, 0, 1 }, 
    { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
    { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, 
    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } 
};

4.2 Print a map

Corresponding to the printing of the map, we need to traverse the map , utilize switch Statement to print the map on the console .

for (size_t i = 0; i < 10; i++) 
{ 
    for (size_t j = 0; j < 10; j++) 
    { 
        switch (map[i][j]) 
        {
            case 0: 
                printf(" "); //  Note that there are two spaces , If it is a space, there will be some problems in map printing  
                break; 
            case 1: 
                printf("█"); 
                break; 
            case 2:     
                printf("*"); 
                break; 
            case 3: 
                printf("●"); 
                break; 
            case 4: 
                printf("*"); 
                break; 
            default: 
                break; 
        } 
    }
    printf("\n"); 
}

5. Control character movement

5.1 Find controlled roles

int posX = 0, posY = 0; 

for (size_t i = 0; i < 10; i++) 
{ 
    for (size_t j = 0; j < 10; j++) 
    { 
        if (2 == map[i][j] || 2 + 4 == map[i][j]) 
        { 
            posX = i; 
            posY = j; 
            break; 
            // Find role , Record where they are x Axis and y Axis coordinates 
        } 
    } 
} 

5.2 Enabling mobility

//  control ( keyboard :WSAD( The up and down or so )) 
//  You need to get the keys from the keyboard ( character ) 
switch (getch()) // getch() This method needs to import... At the beginning #include <conio.h>
{
    case 'w': 
    case 'W': 
        printf(" Up \n"); 
        /*
         Upward logic : 
         Change the value of array elements  
         Take people as reference  
         It could be : 
         clearing   Move  
         The wall   Immobility  
         Be successful   Move  
         The box   Judge what is on the box  
         clearing   Move  
         Be successful   Move  
         The wall   Immobility  
         Another box   Immobility  
        */
        //  Above is the open space or success point  map[posX][posY]: Where the protagonist is  
        if (0 == map[posX - 1][posY] || 4 == map[posX - 1][posY]) 
        { 
            //  Current position: people leave  
            map[posX][posY] -= 2; 
            //  People from above come here  
            map[posX - 1][posY] += 2; 
        }
        //  There are boxes on it ( Push the box away from the point ) 
        else if (3 == map[posX - 1][posY] || 3 + 4 == map[posX - 1][posY]) 
        { 
            //  The top of the box is open space or success point  map[posX - 1][posY]:  Box position  
            if (0 == map[posX - 2][posY] || 4 == map[posX - 2][posY]) 
            { 
                //  Current position: people leave  
                map[posX][posY] -= 2; 
                //  People from above come here  
                map[posX - 1][posY] += 2; 
                //  The upper position box leaves  
                map[posX - 1][posY] -= 3; 
                //  Top box on top come here  
                map[posX - 2][posY] += 3; 
            } 
        }
        break; 
    case 's': 
    case 'S': 
        printf(" Down \n"); 
        break; 
    case 'a': 
    case 'A': 
        printf(" towards the left \n"); 
        break; 
    case 'd': 
    case 'D': 
        printf(" towards the right \n"); 
        break; 
    default: 
        break; 
}

6. Judge victory

Search through the map , If you find no boxes , It can be judged as a victory .(size_t yes C++ What's inside ,size_t amount to unsigned int, Here is only for understanding , It can be used int Instead of size_t)

bool isWin() 
{ 
    for (size_t i = 0; i < 10; i++) 
    { 
        for (size_t j = 0; j < 10; j++) 
        { 
            if (map[i][j] == 3) 
            { 
                return false; 
            } 
        } 
    }
    return true;
}

原网站

版权声明
本文为[D_ eretay]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170507083719.html