对于编程课程,我必须用fubarino和10 led显示屏制作一个小游戏。在游戏中的某一点上,我需要显示玩家的健康栏或积分。健康状况可以在10到0之间。我使用了这段代码。
value = -1;
//set 1 bit off per point lost
for(int i = 10; i>points[playerNr]; i--){
value &= !(1<<(points[playerNr]-1));
}
showValue(value);
方法showValue(int value)不是问题,已经可以用于其他用途。它显示了当时led栏上整数为1的位。
我的问题是,这段代码翻转了所有的LED。所以我认为它将值设置为0。
我将在下面添加其余方法。但我不认为问题就在那里。
// 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;
}