0

I'm working on a really simplified version of space invaders. I generate some aliens then as time goes on I lower them closer to the bottom of a screen which I'm just using arrays for. I've got it generating and lowering the invaders but when I tried to hit the keypad button to delete an alien it was ignored. I believe its because I'm not constantly looking for the Keypad's input so my checkInput() function isn't doing what it should do. I wanted to poll for it but I am not sure how or where I should do it. Any help would be appreciated.

#include <msp430.h>
#include "peripherals.h"
#include <stdlib.h>

// Function Prototypes
void swDelay(char numLoops);
void countDown();
//void speedCheck();
void generateAliens();
void inputCheck();
void displayAliens();
void initLeds(void);


// Declare globals here
int game;
//int win;
//int turn;
//int levelSpeed;
char arrAliens0[5] = {' ', ' ', ' ', ' ', ' '};
char arrAliens1[5] = {' ', ' ', ' ', ' ', ' '};
char arrAliens2[5] = {' ', ' ', ' ', ' ', ' '};
char arrAliens3[5] = {' ', ' ', ' ', ' ', ' '};
char arrAliens4[5] = {' ', ' ', ' ', ' ', ' '};
char arrAliens5[5] = {' ', ' ', ' ', ' ', ' '};
char arrTempAliens1[5];
char arrTempAliens2[5];
char arrTempAliens3[5];
char arrTempAliens4[5];
char arrTempAliens5[5];

void main(void)
{
//unsigned char ret_val = 0x0F;
unsigned char currKey=0;

// Define some local variables

WDTCTL = WDTPW | WDTHOLD;       // Stop watchdog timer

// Useful code starts here
initLeds();
configDisplay();
configKeypad();

while (1)    // Forever loop
{
    GrClearDisplay(&g_sContext); // Clear the display
    while (getKey() != '*')
    {
        // *** Intro Screen ***
        GrStringDrawCentered(&g_sContext, "Space Invaders!", AUTO_STRING_LENGTH, 48, 30, TRANSPARENT_TEXT);
        GrStringDrawCentered(&g_sContext, "Press '*' to", AUTO_STRING_LENGTH, 48, 50, TRANSPARENT_TEXT);
        GrStringDrawCentered(&g_sContext, "Start the Game", AUTO_STRING_LENGTH, 48, 65, TRANSPARENT_TEXT);
        GrFlush(&g_sContext);
    }

    currKey = getKey();

    if (currKey  == '*')
    {
        game = 1;
        //turn = 0;
        countDown();

        while (game == 1)
        {
            //speedCheck();
            generateAliens();
            inputCheck();
            //displayAliens();
            //turn++;
        }

        GrClearDisplay(&g_sContext);
        GrStringDrawCentered(&g_sContext, "You Lose!", AUTO_STRING_LENGTH, 48, 45, TRANSPARENT_TEXT);
        GrFlush(&g_sContext);
        swDelay(3);
    }

}  // end while (1)

} //end main

void countDown()
{
    //3 Count
    GrClearDisplay(&g_sContext);
    GrStringDrawCentered(&g_sContext, "3", AUTO_STRING_LENGTH, 48, 45, TRANSPARENT_TEXT);
    GrFlush(&g_sContext);
    swDelay(3);
    GrClearDisplay(&g_sContext);

//2 Count
GrStringDrawCentered(&g_sContext, "2", AUTO_STRING_LENGTH, 48, 45, TRANSPARENT_TEXT);
GrFlush(&g_sContext);
swDelay(3);
GrClearDisplay(&g_sContext);

//1 Count
GrStringDrawCentered(&g_sContext, "1", AUTO_STRING_LENGTH, 48, 45, TRANSPARENT_TEXT);
GrFlush(&g_sContext);
swDelay(3);
GrClearDisplay(&g_sContext);
}

/*void speedCheck()
{
if (turn <= 3)
{
    levelSpeed = 10;
}

else if ((turn > 3) && (turn <= 6))
{
    levelSpeed = 8;
}

else if ((turn > 6) && (turn <= 9))
{
    levelSpeed = 4;
}

else if ((turn > 9) && (turn <= 12))
{
    levelSpeed = 2;
}

else
{
    levelSpeed = 1;
}
}*/

void generateAliens()
{
memcpy(arrTempAliens1, arrAliens1, 5);
memcpy(arrTempAliens2, arrAliens2, 5);
memcpy(arrTempAliens3, arrAliens3, 5);
memcpy(arrTempAliens4, arrAliens4, 5);
memcpy(arrTempAliens5, arrAliens5, 5);

memcpy(arrAliens1, arrAliens0, 5);
memcpy(arrAliens2, arrTempAliens1, 5);
memcpy(arrAliens3, arrTempAliens2, 5);
memcpy(arrAliens4, arrTempAliens3, 5);
memcpy(arrAliens5, arrTempAliens4, 5);

/*arrTempAliens1 = arrAliens1;
arrTempAliens2 = arrAliens2;
arrTempAliens3 = arrAliens3;
arrTempAliens4 = arrAliens4;
arrTempAliens5 = arrAliens5;

arrAliens1 = arrAliens0;
arrAliens2 = arrTempAliens1;
arrAliens3 = arrTempAliens2;
arrAliens4 = arrTempAliens3;
arrAliens5 = arrTempAliens4;*/

int a = rand() % 4;

if (a == 0)
{
    arrAliens0[0] = '0';
    arrAliens0[1] = ' ';
    arrAliens0[2] = ' ';
    arrAliens0[3] = ' ';
    arrAliens0[4] = ' ';
}

else if (a == 1)
{
    arrAliens0[0] = '0';
    arrAliens0[1] = '1';
    arrAliens0[2] = ' ';
    arrAliens0[3] = ' ';
    arrAliens0[4] = ' ';
}

else if (a == 2)
{
    arrAliens0[0] = '0';
    arrAliens0[1] = '1';
    arrAliens0[2] = '2';
    arrAliens0[3] = ' ';
    arrAliens0[4] = ' ';
}

else if (a == 3)
{
    arrAliens0[0] = '0';
    arrAliens0[1] = '1';
    arrAliens0[2] = '2';
    arrAliens0[3] = '3';
    arrAliens0[4] = ' ';
}

else if (a == 4)
{
    arrAliens0[0] = '0';
    arrAliens0[1] = '1';
    arrAliens0[2] = '2';
    arrAliens0[3] = '3';
    arrAliens0[4] = '4';
}

displayAliens();
}

void inputCheck()
{
unsigned char currKey = getKey();

if ((arrAliens5[0] && arrAliens5[1] && arrAliens5[2] && arrAliens5[3] && arrAliens5[4]) == ' ')
{

    if (currKey == '0')
    {

        if ((currKey == '0') && (arrAliens4[0] == '0'))
        {
            arrAliens4[0] = ' ';
            setLeds(0x30);
        }

        else if ((currKey == '0') && (arrAliens4[0] != '0') && (arrAliens3[0] == '0'))
        {
            arrAliens3[0]= ' ';
        }

        else if ((currKey == '0') && (arrAliens4[0] != '0') && (arrAliens3[0] != '0') && (arrAliens2[0] == '0'))
        {
            arrAliens2[0] = ' ';
        }

        else if ((currKey == '0') && (arrAliens4[0] != '0') && (arrAliens3[0] != '0') && (arrAliens2[0] != '0') && (arrAliens1[0] == '0'))
        {
            arrAliens1[0] = ' ';
        }

        else if ((currKey == '0') && (arrAliens4[0] != '0') && (arrAliens3[0] != '0') && (arrAliens2[0] != '0') && (arrAliens1[0] != '0') && (arrAliens0[0] == '0'))
        {
            arrAliens0[0] = ' ';
        }
    }

    if (currKey == '1')
    {

        if ((currKey == '1') && (arrAliens4[1] == '1'))
        {
            arrAliens4[1] = ' ';
        }

        else if ((currKey == '1') && (arrAliens4[1] != '1') && (arrAliens3[1] == '1'))
        {
            arrAliens3[1]= ' ';
        }

        else if ((currKey == '1') && (arrAliens4[1] != '1') && (arrAliens3[1] != '1') && (arrAliens2[1] == '1'))
        {
            arrAliens2[1] = ' ';
        }

        else if ((currKey == '1') && (arrAliens4[1] != '1') && (arrAliens3[1] != '1') && (arrAliens2[1] != '1') && (arrAliens1[1] == '1'))
        {
            arrAliens1[1] = ' ';
        }

        else if ((currKey == '1') && (arrAliens4[1] != '1') && (arrAliens3[1] != '1') && (arrAliens2[1] != '1') && (arrAliens1[1] != '1') && (arrAliens0[1] == '1'))
        {
            arrAliens0[1] = ' ';
        }
    }

    if (currKey == '2')
    {

        if ((currKey == '2') && (arrAliens4[2] == '2'))
        {
            arrAliens4[2] = ' ';
        }

        else if ((currKey == '2') && (arrAliens4[2] != '2') && (arrAliens3[2] == '2'))
        {
            arrAliens3[2]= ' ';
        }

        else if ((currKey == '2') && (arrAliens4[2] != '2') && (arrAliens3[2] != '2') && (arrAliens2[2] == '2'))
        {
            arrAliens2[2] = ' ';
        }

        else if ((currKey == '2') && (arrAliens4[2] != '2') && (arrAliens3[2] != '2') && (arrAliens2[2] != '2') && (arrAliens1[2] == '2'))
        {
            arrAliens1[2] = ' ';
        }

        else if ((currKey == '2') && (arrAliens4[2] != '2') && (arrAliens3[2] != '2') && (arrAliens2[2] != '2') && (arrAliens1[2] != '2') && (arrAliens0[2] == '2'))
        {
            arrAliens0[2] = ' ';
        }
    }

    if (currKey == '3')
    {

        if ((currKey == '3') && (arrAliens4[3] == '3'))
        {
            arrAliens4[3] = ' ';
        }

        else if ((currKey == '3') && (arrAliens4[3] != '3') && (arrAliens3[3] == '3'))
        {
            arrAliens3[3]= ' ';
        }

        else if ((currKey == '3') && (arrAliens4[3] != '3') && (arrAliens3[3] != '3') && (arrAliens2[3] == '3'))
        {
            arrAliens2[3] = ' ';
        }

        else if ((currKey == '3') && (arrAliens4[3] != '3') && (arrAliens3[3] != '3') && (arrAliens2[3] != '3') && (arrAliens1[3] == '3'))
        {
            arrAliens1[3] = ' ';
        }

        else if ((currKey == '3') && (arrAliens4[3] != '3') && (arrAliens3[3] != '3') && (arrAliens2[3] != '3') && (arrAliens1[3] != '3') && (arrAliens0[3] == '3'))
        {
            arrAliens0[3] = ' ';
        }
    }

    if (currKey == '4')
    {

        if ((currKey == '4') && (arrAliens4[4] == '4'))
        {
            arrAliens4[4] = ' ';
        }

        else if ((currKey == '4') && (arrAliens4[4] != '4') && (arrAliens3[4] == '4'))
        {
            arrAliens3[4]= ' ';
        }

        else if ((currKey == '4') && (arrAliens4[4] != '4') && (arrAliens3[4] != '4') && (arrAliens2[4] == '4'))
        {
            arrAliens2[4] = ' ';
        }

        else if ((currKey == '4') && (arrAliens4[4] != '4') && (arrAliens3[4] != '4') && (arrAliens2[4] != '4') && (arrAliens1[4] == '4'))
        {
            arrAliens1[4] = ' ';
        }

        else if ((currKey == '4') && (arrAliens4[4] != '4') && (arrAliens3[4] != '4') && (arrAliens2[4] != '4') && (arrAliens1[4] != '4') && (arrAliens0[4] == '4'))
        {
            arrAliens0[4] = ' ';
        }
    }
}

else if ((arrAliens5[0] | arrAliens5[1] | arrAliens5[2] | arrAliens5[3] | arrAliens5[4]) != ' ')
{
    game = 0;
}
}

void displayAliens()
{
        GrClearDisplay(&g_sContext);
        GrStringDrawCentered(&g_sContext, arrAliens0, 5, 48, 5, TRANSPARENT_TEXT);
        GrStringDrawCentered(&g_sContext, arrAliens1, 5, 48, 25, TRANSPARENT_TEXT);
        GrStringDrawCentered(&g_sContext, arrAliens2, 5, 48, 45, TRANSPARENT_TEXT);
        GrStringDrawCentered(&g_sContext, arrAliens3, 5, 48, 65, TRANSPARENT_TEXT);
        GrStringDrawCentered(&g_sContext, arrAliens4, 5, 48, 85, TRANSPARENT_TEXT);
        GrFlush(&g_sContext);
        swDelay(3);
}

void swDelay(char numLoops)
{
volatile unsigned int i,j;  // volatile to prevent optimization
                                    // by compiler
for (j=0; j<numLoops; j++)
{
    i = 50000 ;                 // SW Delay
    while (i > 0)               // could also have used while (i)
       i--;
}
}
4

1 回答 1

1
if ((arrAliens5[0] && arrAliens5[1] && arrAliens5[2] && arrAliens5[3] && arrAliens5[4]) == ' ')

This condition is never true because && is a boolean operator and results in either 0 or 1, which can never be equal to the value of the space character.

于 2017-02-07T21:16:01.307 回答