'why this show() function produce two * and the element will stop sometimes .There is no error in the code

This is a plane game function I wrote. I use a two-dimensional array to represent the game variables but The running result is abnormal, and * will jump suddenly. And there will be two * , at the same time, and the plane also will stop There should be no two * in the process of traversing the two-dimensional array. I tried to modify the position of * but I still couldn't.It's OK to run part of the code alone, but when you use the key operation, the program makes an error, but I don't know what's wrong

  #include<stdio.h>
    #include <windows.h>
    #include<stdlib.h>
    #include<conio.h>
    #define enemynum 3 
int element [20][30];
int position_x,position_y; 
int enemy_x[enemynum],enemy_y[enemynum];
int score;


    void gotoxy(int x, int y)
    {
        HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
        COORD pos;
        pos.X = x;
        pos.Y = y;
        SetConsoleCursorPosition(handle, pos);
    }
    
    
    void HideCursor()
    {
        CONSOLE_CURSOR_INFO cursor_info = {1,0};
        SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
    }
    
    void startup()
    { element[20][30]={0};
    position_x=10;position_y=15;
     element[position_x][position_y]=1;
     for(int k=0;k<enemynum;k++)
     { 
        enemy_x[k]=rand()%3;enemy_y[k]=rand()%20;
        element[enemy_x[k]][enemy_y[k]]=3;
     }
      
     
         HideCursor();
    } 

This is an encapsulated pointer callback function. I don't think we need to consider the above functions

void show()
        { int i,j;
         gotoxy(0,0); 
         for(i=0;i<20;i++) 
           {
            for(j=0;j<30;j++)
            { if(element[i][j]==1)
              { 
              printf("*");
              }else if(element[i][j]==3)
              { 
                printf("@");
              }
             else
            printf(" ");
            }
            printf("\n"); 
            }
        }
                 void updatewhithout()
        {      
             int i,j;
            for(i=0;i<20;i++)               
           {
            for(j=0;j<30;j++)
            {
                if(element[i][j]==2)
                {
                    element[i][j]=0;
                    if(i>0)
                    element[i-1][j]=2;
                     
                } 
                
            
            }
             
           } static int flag;
           if(flag<20)
           flag++;
                if(flag==20)
                {      for(int k=0;k<enemynum;k++)
                     {
                        if(enemy_x[k]==20)
                    {
                        enemy_x[k]=rand()%3;
                        enemy_y[k]=rand()%20;
                    }
                     element[enemy_x[k]][enemy_y[k]]=0;
                    enemy_x[k]++;
                  
                  
                  
                  element[enemy_x[k]][enemy_y[k]]=3;
                  flag=0;
                     } 
                
                }
              
            
              
          
     }
                    void  updatewhith()
                    { char ch;
                if( kbhit())
                    ch=getch();
                    if(ch=='a')
                    { element[position_x][position_y]=0;
                    position_y--;
                    element[position_x][position_y]=1;
                    }
                        if(ch=='d')
                    { element[position_x][position_y]=0;
                    position_y++;
                    element[position_x][position_y]=1;
                    }
                    
                
                
                    }
            int main()
             {startup(); 
             while(1)
               {show();
                 updatewhithout();
                 updatewhith(); 
               } 
            
             } 
c


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source