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--;
}
}
Mel Moore
  • 79
  • 2
  • 3
  • 9
  • Nobody knows what CPU, keypad, and library you're using, or what the correct behaviour would be, or what you actually observe. – CL. Feb 07 '17 at 08:00
  • @CL. I'm using the MSP430F5529 Launch Pad. I'm not really sure what the keypad is though its just a simple add on with numbers 0 - 9 along with a star and pound sign. When I ran my program what I saw was the aliens being generated and after some time a new line of aliens would spawn while the previous aliens moved a notch down the screen. This is what I wanted but when I would hit the keypad to destroy an alien nothing happened and the game would just go until the aliens reached the bottom and the game ended. I put in a light to see if the input was ever checked but the light never came on. – Mel Moore Feb 07 '17 at 17:42
  • So they unknown keypad doesn't work at all. Might be hardware, might be the unkown `getKey()`. – CL. Feb 07 '17 at 18:23
  • @CL. No the keypad works. In the beginning there is a welcome screen and you need to hit the star sign to start the countdown to the game. I believe it works during the actual game but I use a function to check/destroy aliens. I think it just goes by to fast or something so I wanted to set up a place to actually poll for the keypads input during the game so that it wouldn't miss when I hit it. Also the aliens are just numbers 0 - 4 so on the keypad if I hit 0 the lowest 0 gets destroyed. It could also just be my inputCheck() function is just wrong. – Mel Moore Feb 07 '17 at 18:34

1 Answers1

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.

CL.
  • 173,858
  • 17
  • 217
  • 259
  • You are right changing them to single & like I did with the or operators in the game ending situation made it work. The losing condition also isn't executed anymore. Also its slow so it only deletes one alien per generation is there a way to make it take inputs faster because there is no way to 'get ahead' its you lose by 8 turns usually. – Mel Moore Feb 07 '17 at 23:31
  • I fixed the losing condition part. Now I'm just focusing on deleting aliens faster or running the generation slower. – Mel Moore Feb 07 '17 at 23:58