我使用我的Arduino和ETS-7000设备设计了一个反应游戏,我遇到了一些问题。
游戏的想法是会有两个玩家互相对战,我使用了三个LED,它们已经在ETS-7000上了。
两个7段将在9和9开始,当第三个LED闪烁时,两个玩家应按下开关。
较慢的玩家将减少1点,为此他的7段将为8并且游戏将继续,直到某些达到0并且输掉。
我的问题是我做的每一件事都是正确的,但是较慢的玩家在7段上减少了1分,而玩家的速度就越快。
我需要帮助,所以我将代码调整为反对的方式
P.S。我为7段使用了BCD编码
int SW2 = 3; //try
int SW1 = 4;
int LED0 = A0;
int LED1 = 12;
int LED2 = 13;
// int fault = 0;
//long wait = 0;
//long now = 0;
void setup(){
pinMode(A5, OUTPUT);
pinMode(A4, OUTPUT);
pinMode(A3, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(LED0, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(SW1, INPUT);
pinMode(SW2, INPUT);
//Initializing components
randomSeed(analogRead(1));
}
void loop(){
reset:
int n=9, x=9;
digitalWrite(A5, HIGH);
digitalWrite(A4, LOW);
digitalWrite(A3, LOW);
digitalWrite(A2, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
do{
digitalWrite(LED0, HIGH);
delay(2000);
digitalWrite(LED1, HIGH);
//Wait for a random period of time, between 3 seconds
//And 6 seconds. Get ready!
delay(random(500,8000));
digitalWrite(LED2, HIGH);
//Swing your swords as fast as you can, the faster one
//will be returned by getWinner()
int winner=getWinner();
//The yellow led by side of the winner will light up
if(winner==1){
if (n==1)
{
digitalWrite(A5, LOW);
digitalWrite(A4, LOW);
digitalWrite(A3, LOW);
digitalWrite(A2, LOW);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED0, HIGH);
delay(100);
digitalWrite(LED0, LOW);
delay(100);
digitalWrite(LED0, HIGH);
delay(100);
digitalWrite(LED0, LOW);
delay(100);
digitalWrite(LED0, HIGH);
delay(100);
digitalWrite(LED0, LOW);
delay(100);
digitalWrite(LED0, HIGH);
delay(100);
digitalWrite(LED0, LOW);
delay(100);
digitalWrite(LED0, HIGH);
delay(100);
digitalWrite(LED0, LOW);
delay(5000);
break;
}
else
{n=n-1;}
}else{ if (x==1)
{
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(LED0, LOW);
digitalWrite(LED1, LOW);
digitalWrite(LED2, HIGH);
delay(100);
digitalWrite(LED2, LOW);
delay(100);
digitalWrite(LED2, HIGH);
delay(100);
digitalWrite(LED2, LOW);
delay(100);
digitalWrite(LED2, HIGH);
delay(100);
digitalWrite(LED2, LOW);
delay(100);
digitalWrite(LED2, HIGH);
delay(100);
digitalWrite(LED2, LOW);
delay(100);
digitalWrite(LED2, HIGH);
delay(100);
digitalWrite(LED2, LOW);
delay(5000);
break;
}
else { x=x-1;}
}
if (n==0){
digitalWrite(A5, LOW);
digitalWrite(A4, LOW);
digitalWrite(A3, LOW);
digitalWrite(A2, LOW);
} else if (n==1){
digitalWrite(A5, HIGH);
digitalWrite(A4, LOW);
digitalWrite(A3, LOW);
digitalWrite(A2, LOW);
} else if (n==2){
digitalWrite(A5, LOW );
digitalWrite(A4, HIGH);
digitalWrite(A3, LOW);
digitalWrite(A2, LOW);
} else if (n==3){
digitalWrite(A5, HIGH);
digitalWrite(A4, HIGH);
digitalWrite(A3, LOW);
digitalWrite(A2, LOW);
} else if (n==4){
digitalWrite(A5, LOW);
digitalWrite(A4, LOW);
digitalWrite(A3, HIGH);
digitalWrite(A2, LOW);
} else if (n==5){
digitalWrite(A5, HIGH);
digitalWrite(A4, LOW);
digitalWrite(A3, HIGH);
digitalWrite(A2, LOW);
} else if (n==6) {
digitalWrite(A5, LOW);
digitalWrite(A4, HIGH);
digitalWrite(A3, HIGH);
digitalWrite(A2, LOW);
} else if (n==7){
digitalWrite(A5, HIGH);
digitalWrite(A4, HIGH);
digitalWrite(A3, HIGH);
digitalWrite(A2, LOW);
} else if (n==8){
digitalWrite(A5, LOW);
digitalWrite(A4, LOW);
digitalWrite(A3, LOW);
digitalWrite(A2, HIGH);
} else if (n==9){
digitalWrite(A5, HIGH);
digitalWrite(A4, LOW);
digitalWrite(A3, LOW);
digitalWrite(A2, HIGH);
}
if (x==0){
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
} else if (x==1){
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
} else if (x==2){
digitalWrite(8, LOW );
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
} else if (x==3){
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
} else if (x==4){
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
} else if (x==5){
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
} else if (x==6) {
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
} else if (x==7){
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
} else if (x==8){
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
} else if (x==9) {
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
}
digitalWrite(LED0, LOW);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
delay(2000);
} while(true);
goto reset;
}
//The function below waits for either of the tilter
//switch to be swang. The first one to swing
//will be returned by its number
int getWinner(){
do{
if(digitalRead(SW1)==HIGH){
return 1;
}else if(digitalRead(SW2)==HIGH){
return 2;
}
}while(true);
}
答案 0 :(得分:2)
我将以连续的片段为您提供更正版本。
首先,应在顶部定义引脚编号,并给出有意义的名称。如果所有内容都按玩家编号索引,它也会使代码变得更加简单。
#define PLAYER1 0
#define PLAYER2 1
const int ALERT_LED = 12;
// Pins of the BCD display for both players, most significant bit first
const int PLAYER_DISPLAY[2][4] = {
{ A2, A3, A4, A5 },
{ 11, 10, 9, 8 }
};
const int PLAYER_LED[2] = { A0, 13 };
const int PLAYER_SWITCH[2] = {4, 3 };
功能是你的朋友。我们将在此处重新组合输出功能。
void set_display (int player, int value)
{
for (int i = 0; i < 4; i++) {
digitalWrite (PLAYER_DISPLAY[player][3 - i], value & (1 << i));
}
}
void blink_led (int pin)
{
for (int i = 0; i < 5; i++) {
digitalWrite (pin, HIGH);
delay(100);
digitalWrite (pin, LOW);
delay(100);
}
}
我们对输入功能也这样做。
// return the number of the first player to press its switch
int get_winner ()
{
while (true) {
for (int i = 0; i < 2; i++) {
if (digitalRead(PLAYER_SWITCH[i]) == HIGH) {
return i;
}
}
}
}
我们设置了引脚方向。
void setup ()
{
// set pins direction and ensure the leds are off
pinMode (ALERT_LED, OUTPUT);
digitalWrite (ALERT_LED, LOW);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
pinMode (PLAYER_DISPLAY[i][j], OUTPUT);
}
pinMode (PLAYER_LED[i], OUTPUT);
digitalWrite (PLAYER_LED[i], LOW);
pinMode (PLAYER_SWITCH[i], INPUT);
}
// initialize random number generator
randomSeed(analogRead(1));
}
现在,由于我们的准备工作,主循环更简单,逻辑清晰,更容易更改,更容易发现错误。
void loop()
{
int score[2] = { 9, 9 };
for (int i = 0; i < 2; i++) {
set_display (i, score[i]);
}
while (true) {
// turn on the leds in sequence
digitalWrite(PLAYER_LED[PLAYER1], HIGH);
delay(2000);
digitalWrite(PLAYER_LED[PLAYER2], HIGH);
delay(random(500,8000));
digitalWrite(ALERT_LED, HIGH);
// get winner
int winner = get_winner();
int loser = (winner == PLAYER1) ? PLAYER2 : PLAYER1;
// update score
score[loser]--;
set_display (loser, score[loser]);
// turn the leds off
digitalWrite(PLAYER_LED[PLAYER1], LOW);
digitalWrite(PLAYER_LED[PLAYER2], LOW);
digitalWrite(ALERT_LED, LOW);
// if the game is over, blink the winner's led and restart
if (score[loser] == 0) {
blink_led (PLAYER_LED[winner]);
delay(5000);
break;
}
delay(2000);
}
}
我希望你能从这些原则中学习。如果您不了解所有内容(特别是set_display()的工作方式),这没关系。
答案 1 :(得分:1)
很难准确地阅读这里发生的事情,但听起来你正在遭受逻辑反转。
基本上,你的程序应该这样做:
实际所执行的程序是:
有一种简单的方法可以使您的程序正常工作 - 如果这是唯一的问题 - 那就改变了
的逻辑if( winner == 1 )
到
if( winner != 1 )
这将颠倒逻辑并解决问题。然而。请注意,这可能只是“两个错误使权利”的情况。如果你的代码错误地计算了胜利者,那就完全是这样,你最好修复它而不是稍后扭转错误。同样值得注意的是,如果您想将此扩展到3个或更多玩家,现在使用!=逻辑将意味着您将不得不在以后重写它。
如果胜利者的计算是正确的,那么你纠正它的另一个选择是改变被改变的数字,所以在获胜者的第一个if / else部分== 1,用x交换每个x的实例反之亦然。您可能还需要交换LED 9-11和A2-A5的实例,具体取决于设置方式。由于缺乏任何评论,这很难说清楚:这些LED实际代表的是什么:)