C 在10个led显示屏上显示healthbar
对于编程课程,我必须制作一个带有fubarino和10个led显示屏的小游戏。在游戏中的某一点上,我需要向玩家显示“healthbar”或分数。运行状况可以介于10和0之间。我用了这段代码C 在10个led显示屏上显示healthbar,c,arduino,binary,C,Arduino,Binary,对于编程课程,我必须制作一个带有fubarino和10个led显示屏的小游戏。在游戏中的某一点上,我需要向玩家显示“healthbar”或分数。运行状况可以介于10和0之间。我用了这段代码 value = -1; //set 1 bit off per point lost for(int i = 10; i>points[playerNr]; i--){ value &= !(1<<(points[playerNr]-1)); } showVa
value = -1;
//set 1 bit off per point lost
for(int i = 10; i>points[playerNr]; i--){
value &= !(1<<(points[playerNr]-1));
}
showValue(value);
value=-1;
//每丢失一点设置1位
对于(int i=10;i>点[playerr];i--){
值&=!(1您可以使用,直接创建一个设置了“n个最低位”的整数,而不是循环清除位(1u抱歉,您能否详细说明一下,您的评论对我来说不是很清楚。您会怎么做(1uu
表示未签名的onliterals@Typhaon这不是一个变量,这是一个无符号后缀,用于使1
具有类型unsigned int
,而不是int
。您可以使用showValue((1u So value=0;value |=(1)替换整个for
循环
// playerNr is either 0 or 1 for one player or 2 for both
void showPoints(int playerNr){
bool both = false;
//subtract one point from both players if playerNr == 2
if(playerNr == 2){
both = true;
playerNr = 1;
}
points[playerNr]--;
value = -1;
//flash all lights to show that the points are going to be shown
showValue(value);
delay(50);
//set 1 bit off per point lost
for(int i = 10; i>points[playerNr]; i--){
value &= !(1<<(points[playerNr]-1));
}
showValue(value);
//game position is either 0 or 9
pos = 9*playerNr;
//ascend is true if playerNr is 0 and vice versa
ascend = !playerNr;
if(both){
pointLost = 0;
} else {
pointLost = -1;
}
//reset the game if points are 0
if(points[playerNr]==0){
points[0] = 10;
points[1] = 10;
points[playerNr]++;
pointLost = playerNr;
}
timer = 1000;
}