c - 我的分数计算变量的值异常增加

标签 c debugging cmd

用 C 语言编写了一个类似太空入侵者类型的游戏。除了有时“分数”的值(记录每次入侵者死亡时分数增加)随机增加数十万次外,一切都运行顺利。不知道为什么。完整代码以备不时之需 Cmd space invaders

void laserUpdate(){
char explosion = 'X';
//laser control
for(i = 0; i < xAxis; i++){     
    for(j = yAxis - 1; j >= 0; j--){
        if(world[j][i] == invaderLaser){                        
            if(world[j + 1][i] == shield){                          
                world[j + 1][i] = ' ';                      
                world[j][i] = ' ';                      
            }                   
            else if(world[j + 1][i] == player){                     
                world[j + 1][i] = explosion;                                            
                victory = 0;                        
                break;                          
            }                   
            else if(world[j + 1][i] == playerLaser){                    
                world[j][i] = explosion;                        
                world[j + 1][i] = ' ';                  
            }                   
            else{
                world[j + 1][i] = invaderLaser;                     
                world[j][i] = ' ';                  
            }                   
        }                   
        else if(world[j][i] == playerLaser){                        
            if(world[j - 1][i] == invaderLaser){                            
                world[j - 1][i] = explosion;                            
                world[j][i] = ' ';                                              
            }                                   
            else if(world[j - 1][i] == invader){                    
                world[j - 1][i] = explosion;                        
                world[j][i] = ' ';                      
                score += 50;                        
                totalInvaders--;                                                
                if(totalInvaders == 0){                         
                    victory = 0;                            
                    break;          
                }                                           
            }                   
            else if(world[j - 1][i] == shield){                     
                world[j][i] = ' ';                      
            }                   
            else{                       
                world[j][i] = ' ';
                world[j - 1][i] = playerLaser;
                j--;
            }
        }
        else if(world[j][i] == explosion){
            print();
            Sleep(50);
            world[j][i] = ' ';
        }   
        invaderLaserProduction();
    }
  }
}

最佳答案

解决方案取决于游戏 field 是在两个边缘都环绕,还是禁止入内。无论哪种方式,你都必须处理

world[j + 1][i]

j == yAxis - 1 时索引越界,并且

world[j - 1][i]

j == 0 时索引越界。

这些条件位于 j 循环的开始和结束处。

关于c - 我的分数计算变量的值异常增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48600850/

相关文章:

C:打印以 'a' 开头的每个可能的字符串,递归函数

c - C 中 mktime 的不一致结果

java - 使用 Java 在命令行上打印清晰的文本

linux - 如何使用嵌入式设备在内核上工作?

cmd - 如何在Windows上运行./configure

java - 一种简单的防弹技术来检查系统是否有 jre (windows)

c - PIC18F47J53 的 ADC 代码在按值传递时出现意外行为

c - 将参数作为引用

javascript - 警报框未打开

javascript - JavaScript 中的调用堆栈大小