#include "stdio.h" #include #include "uart.h" #include "GLCD.h" #include #include #include "glcd_scroll.h" #include "stdbool.h" #include "lfsr113.h" #define FG White #define BG Blue uint16_t player_bmp[] = { BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, FG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG }; uint16_t bullet_bmp[] = { BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, FG, FG, FG, FG, FG, FG, FG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, FG, FG, FG, FG, FG, FG, FG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, FG, FG, FG, FG, FG, FG, FG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, FG, FG, FG, FG, FG, FG, FG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, FG, FG, FG, FG, FG, FG, FG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, FG, FG, FG, FG, FG, FG, FG }; uint16_t removelag[] = { BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG, BG }; uint32_t pos; bool fireButton; struct Bullet{ uint32_t x,y; bool fired; }; struct Enemy{ uint32_t x,y; bool alive; }; struct Enemy enemy[4]; struct Bullet bullets[7]; void input(void const *arg){ LPC_PINCON->PINSEL1 &= ~(0x03<<18); LPC_PINCON->PINSEL1 |= (0x01<<18); LPC_SC->PCONP |= (1 <<12); LPC_ADC->ADCR = (1 << 2) | // select AD0.2 pin (4 << 8) | // ADC clock is 25MHz/5 (1 << 21); // enable ADC LPC_ADC->ADCR |= (1 << 24); while(1){ if(LPC_ADC->ADGDR & (1 << 31)){ int value = (LPC_ADC->ADGDR & 0xfff0); pos = ( value >> 4 )/17; LPC_ADC->ADCR |= (1 << 24); } if (!(LPC_GPIO2->FIOPIN & (1 << 10))) fireButton = true; else fireButton = false; } } void display(void const *arg) { GLCD_Init(); GLCD_Clear(Blue); GLCD_SetBackColor(Blue); GLCD_SetTextColor(White); while(1) { //First is x, y, matrix dimensions GLCD_Bitmap (1, pos, 11, 80, (unsigned char*)player_bmp); for (int i = 0; i < 7; i++) { if (bullets[i].fired){ GLCD_Bitmap(bullets[i].x, bullets[i].y, 18, 6, (unsigned char*)bullet_bmp); GLCD_Bitmap(bullets[i].x-6, bullets[i].y, 18, 6, (unsigned char*)removelag); } for(int j=0; j<4; j++){ if (enemy[j].alive){ GLCD_Bitmap(enemy[j].x, enemy[j].y, 18, 6, (unsigned char*)bullet_bmp); GLCD_Bitmap(enemy[i].x+6, enemy[i].y, 18, 6, (unsigned char*)removelag); } } } } } void gameLogic(void const *arg) { uint16_t fireRateTimer = 0; uint16_t bulletIterator = 0; uint16_t enemytimer = 0; uint16_t enemyIterator=0; while(1){ if (fireRateTimer > 0){ fireRateTimer--; } // initializes bullets if (fireButton && fireRateTimer == 0) { fireRateTimer = 5; bullets[bulletIterator].fired = true; bullets[bulletIterator].y = pos + 32; bullets[bulletIterator].x = 0; bulletIterator++; if (bulletIterator >= 7){ bulletIterator = 0; } } // Updates positions of bullets as they are fired for (int i = 0; i < 7; i++) { if (bullets[i].fired){ bullets[i].x += 10; } if (bullets[i].x > 320) { bullets[i].fired = false; } } //Initializes the enemy if(enemytimer>0){ enemytimer--; } if(enemytimer==0){ enemytimer=100; enemy[enemyIterator].alive = true; enemy[enemyIterator].y=lfsr113()/17895697; enemy[enemyIterator].x=320; //randomize enemyIterator++; if(enemyIterator>=4){ enemyIterator=0; } } //Updates the position of the enemy for (int k = 0; k < 4; k++) { if (enemy[k].alive){ enemy[k].x -= 5; } if (enemy[k].x < 0) { enemy[k].alive = false; } } osDelay(100); } } int main(void){ osKernelInitialize(); printf("Initialize"); osThreadNew(input, NULL, NULL); osThreadNew(gameLogic, NULL, NULL); osThreadNew(display, NULL,NULL); osKernelStart(); }