//Collision Check void EnemyCol(void){int j; for(j=0;j<15;j++){ if(Missile.life){ if ((Missile.x - Enemy[j].x) < 35){ if ((Missile.y - Enemy[j].y) < 30){ if(Enemy[j].life>0){ Missile.life = 0; LCD_DrawBMP(missile1, Missile.x, Missile.y);; Enemy[j].life--; if(Enemy[j].life==0){ LCD_DrawFilledRect(Enemy[j].x,Enemy[j].y,32,20,BLACK); } } } } } } for(j=0;j<4;j++){ if(Missile.life){ if ((Missile.x - Bunker[j].x) < 32){ if ((Missile.y - Bunker[j].y) < 5){ if(Bunker[j].life>0){ Missile.life = 0; LCD_DrawBMP(missile1, Missile.x, Missile.y);; Bunker[j].life--; if(Bunker[j].life==0){ LCD_DrawFilledRect(Bunker[j].x,Bunker[j].y,32,5,BLACK); } } } } } } }
void Board_Init(void){int tileLen, row, col, tileX, tileY; // Background Color LCD_DrawFilledRect(0,0,320,240,BLACK); // Sidebar Color LCD_DrawFilledRect(0,0,91,240,RED); LCD_DrawRect(0,0,91,239,BURNTORANGE); // Board Frame LCD_DrawFilledRect(boardX, boardY, boardLen, boardLen, ORANGE); // Title LCD_DrawBMP(Title, 13, 7); // Scoreboard/Credits LCD_DrawFilledRect(6,59,79,175,BLACK); LCD_DrawRect(6,59,79,174,YELLOW); LCD_SetTextColor(255,245,200); LCD_Goto(2,9); printf("Score:"); LCD_Goto(2,15); printf("Highscore:"); LCD_Goto(2,12); printf("Time:"); // Credits LCD_Goto(4,7); LCD_SetTextColor(255,255,240); printf("LAB 10"); LCD_Goto(2,23); printf("Created by:"); LCD_Goto(2,24); printf("Ryan Norton"); LCD_Goto(2,25); printf("Andrew Grado"); // Initialize Tile Position Array and Empty Board tileLen = (boardLen - (5 * tilePadding)) / 4; for (row=0; row<4; row++){ for (col=0; col<4; col++){ // Calculate tile coordinates tileX = row * (tileLen + tilePadding) + tilePadding + boardX; tileY = col * (tileLen + tilePadding) + tilePadding + boardY; // Initialize board with empty tiles // LCD_DrawFilledRect(tileX, tileY, tileLen, tileLen, GREEN); // Initialize tile position array tileCoords[col * 4 + row + 1][0] = tileX; tileCoords[col * 4 + row + 1][1] = tileY - 1; } } }
void drawArrow(void){int x, y; // cover old arrow LCD_DrawFilledRect(prevX,prevY,20,20,BLACK); // draw new arrow if (ADCMail < 1024){ x = 92; y = 201; y -= (185 * (ADCMail/1024.0)); LCD_DrawBMP(RightArrow, x, y); } else if (ADCMail < 2048){ x = 103; x += (185 * ((ADCMail-1024)/1024.0)); y = 5; LCD_DrawBMP(DownArrow, x, y); } else if (ADCMail < 3072){ x = 300; y = 16; y += (185 * ((ADCMail-2048)/1024.0)); LCD_DrawBMP(LeftArrow, x, y); } else{ x = 289; x -= (185 * ((ADCMail-3072)/1024.0)); y = 213; LCD_DrawBMP(UpArrow, x, y); } prevX = x; prevY = y; }
void drawPauseMode(void){ LCD_DrawFilledRect(boardX,boardY,boardLen,boardLen,BLACK); LCD_SetTextColor(255,255,240); LCD_Goto(25,13); printf("Continue"); LCD_Goto(38,13); printf("Restart"); LCD_DrawRect(144,112,58,16,WHITE); }
void drawGameOver(int score, int time){ LCD_DrawFilledRect(boardX,boardY,boardLen,boardLen,BLACK); LCD_SetTextColor(255,255,240); LCD_Goto(30,5); printf("GAME OVER"); LCD_Goto(23,8); printf("Score:"); LCD_Goto(29,8); LCD_PrintInteger(score); LCD_Goto(23,10); printf("Time:"); LCD_Goto(28,10); LCD_PrintInteger(time); LCD_Goto(29,19); printf("Play Again?"); LCD_DrawRect(170,166,72,16,WHITE); }
//GAME int main(void){unsigned long i=64,j=0; PLL_Init(); // Set the clocking to run at 80MHz from the PLL. LCD_Init(); // Initialize LCD Piano_Init(); Sound_Init(); ADC_Init(); //initialize ADC ADC_SetChannel(1); //PE2 Missile_Init(); Enemy1_Init(); LCD_Goto(10,0); LCD_SetTextColor(255,0,0); // yellow= red+green, no blue printf("Welcome to Pokemon Capture!"); delay_long(); LCD_DrawFilledRect(0,0,320,80,0x00); delay_short(); while((GPIO_PORTE_DATA_R&0x01)==0){ LCD_Goto(10,0); printf("Press any button to begin..."); delay_blink(); LCD_DrawFilledRect(0,0,320,80,0x00); delay_blink(); } delay_short(); LCD_SetTextColor(255,255,0); LCD_Goto(0,0); printf("Tragedy has struck Pallet Town! Four Pokemon have escaped from their Pokeballs, and it's your job to put them back where they belong."); delay_long(); delay_long(); delay_long(); LCD_DrawFilledRect(0,0,320,80,0x00); delay_short(); LCD_Goto(0,0); printf("Use the slider to aim, and when you think you have a shot, press the button to throw your Pokeball."); delay_long(); delay_long(); delay_long(); LCD_DrawFilledRect(0,0,320,80,0x00); delay_blink(); LCD_Goto(0,0); printf("You only have so many Pokeballs, so be smart where you throw them!"); delay_long(); delay_long(); LCD_DrawFilledRect(0,0,320,80,0x00); delay_short(); LCD_Goto(0,0); printf("Good luck!"); delay_long(); LCD_DrawFilledRect(0,0,320,80,0x00); delay_short(); LCD_SetTextColor(255,0,0); LCD_Goto(0,0); printf("Captured:"); LCD_SetTextColor(0,255,0); LCD_Goto(14,0); printf("Pokeballs Left:"); LCD_SetTextColor(0,0,255); LCD_Goto(35,0); printf("Accuracy:"); LCD_DrawLine(10,16,310,16,BURNTORANGE); Timer2_Init(7256); // 11kHz SysTick_Init(2666667); //30 Hz EnableInterrupts(); Pokeballs = 25; while(1){ if((25-(hits+misses))==0){ if(kill!=4){ DisableInterrupts(); LCD_SetTextColor(255,0,0); LCD_DrawFilledRect(0,0,320,80,0x00); delay_short(); LCD_Goto(0,0); printf("Sorry, you were not able to catch all the Pokemon."); delay_long(); delay_long(); LCD_DrawFilledRect(0,0,320,80,0x00); delay_short(); LCD_Goto(0,0); printf("Try again."); delay_long(); LCD_DrawFilledRect(0,0,320,80,0x00); delay_short(); while(1){ LCD_Goto(0,0); printf("Press RESET to play again."); delay_blink(); LCD_DrawFilledRect(0,0,320,80,0x00); delay_blink(); } } } if(kill==4){ DisableInterrupts(); LCD_SetTextColor(0,255,255); LCD_DrawFilledRect(0,0,320,80,0x00); delay_short(); LCD_Goto(0,0); printf("Congratulations! You have captured all the Pokemon!"); delay_long(); delay_long(); LCD_DrawFilledRect(0,0,320,80,0x00); delay_short(); LCD_Goto(0,0); printf("You are now a Pokemon Master!"); delay_long(); delay_long(); LCD_DrawFilledRect(0,0,320,80,0x00); delay_short(); while(1){ LCD_Goto(0,0); printf("Press RESET to play again."); delay_blink(); LCD_DrawFilledRect(0,0,320,80,0x00); delay_blink(); } } if(Semaphore){ LCD_SetTextColor(255,0,0); LCD_Goto(10,0); LCD_PrintInteger(kill); LCD_SetTextColor(0,255,0); LCD_Goto(30,0); LCD_PrintInteger(25-(hits+misses)); if((25-(hits+misses))<10){ LCD_Goto(31,0); printf(" "); } LCD_SetTextColor(0,0,255); LCD_Goto(45,0); LCD_PrintInteger(hits/(hits+misses)); //draw ship if(Ship_Position>288){ Ship_Position=288; } LCD_DrawBMP(PlayerShip,Ship_Position,220); LCD_DrawFilledRect(0,220,Ship_Position,9,0x00); LCD_DrawFilledRect(Ship_Position+32,220,320-(Ship_Position+32),9,0x00); for(j=0;j<1;j++){ if(Missile[j].life==1){ LCD_DrawBMP(Missile[j].image, Missile[j].x, Missile[j].y); } else{ LCD_DrawFilledRect(Missile[j].x,Missile[j].y,16,18,0x00); } if(Missile[j].y<=18){ LCD_DrawFilledRect(Missile[j].x,Missile[j].y,16,18,0x00); Missile[j].life=0; misses++; } } for(i=0;i<4;i++){ if(Enemy1[i].life!=0){ LCD_DrawBMP(Enemy1[i].image,Enemy1[i].x,Enemy1[i].y); } else{ LCD_DrawFilledRect(Enemy1[i].x,Enemy1[i].y,32,32,0x00); } } LCD_DrawFilledRect(0,17,320,4,0x00); Semaphore = 0; } } }
void eraseBoard(void){ LCD_DrawFilledRect(boardX,boardY,boardLen,boardLen,ORANGE); }
void GL_LCD_DrawFilledRect(uint8_t Xpos, uint16_t Ypos, uint8_t Height, uint16_t Width) { LCD_DrawFilledRect(Xpos, Ypos, Height, Width); }
int main(void) { int addScore, position, pauseSel; // Initializations PLL_Init(); // Clock set at 80 MHz LCD_Init(); Board_Init(); Input_Init(); DAC_Init(); Random_Init(NVIC_ST_CURRENT_R); Timer2_Init(80000000); // time interrupt Timer1_Init(2000); // sound interrupt EnableInterrupts(); generateRandomTile(); drawAllTiles(); writeScore(0); writeHighscore(0); writeTime(0); displayHighestTile(); while(1) { // draw arrow if ready if (arrowReady == 1) { // acknowledge flag arrowReady = 0; // draw arrow drawArrow(); } // write time if ready if (timeReady == 1) { // acknowledge flag timeReady = 0; // write time writeTime(elapsedTime); } // Play mode and button1 is pushed if(Button1 && !pauseMode && !gameOver) { // Play sound playSound = 1; // shift and merge tiles towards arrow position = getSliderPosition(); if (position == 1) { shiftLeft(); addScore = mergeLeft(); shiftLeft(); } else if (position == 2) { shiftUp(); addScore = mergeUp(); shiftUp(); } else if (position == 3) { shiftRight(); addScore = mergeRight(); shiftRight(); } else { shiftDown(); addScore = mergeDown(); shiftDown(); } eraseBoard(); drawAllTiles(); // update score score += addScore; addScore = 0; writeScore(score); displayHighestTile(); // delay before adding new tile delay(200); // create new tile if (countEmptyTiles() != 0) { generateRandomTile(); } drawAllTiles(); // update highest tile image displayHighestTile(); // check if game over if (checkGameOver() == 1) { gameOver = 1; } // unset flag Button1 = 0; } // button 2 is pause else if (Button2 && !gameOver && !pauseMode) { pauseMode = 1; pauseSel = 0; // disable arrow and timer NVIC_ST_CTRL_R = 0; TIMER2_CTL_R = 0x00000000; LCD_DrawFilledRect(prevX,prevY,20,20,BLACK); // draw pause mode screen drawPauseMode(); // acknowledge button Button2 = 0; // wait until button is pushed while (pauseMode) { // Button 1 selects current pause selection button if (Button1) { // acknowledge button Button1 = 0; Button2 = 0; pauseMode = 0; // if pause selection = "continue" (pauseSel = 0), continue with game if (pauseSel == 0) { // redraw screen eraseBoard(); drawAllTiles(); // enable gameplay NVIC_ST_CTRL_R = 0x07; TIMER2_CTL_R = 0x00000001; } // if pause selection = "restart" (pauseSel = 0), end game else if (pauseSel == 1) { if (score > highscore) { writeHighscore(score); } score = 0; eraseScore(); writeScore(0); clearBoard(); eraseBoard(); pauseMode = 0; generateRandomTile(); drawAllTiles(); elapsedTime = 0; eraseTime(); writeTime(0); displayHighestTile(); NVIC_ST_CTRL_R = 0x07; TIMER2_CTL_R = 0x00000001; } } // Button 2 changes pause selection else if (Button2) { Button2 = 0; if (pauseSel == 0) { pauseSel = 1; LCD_DrawRect(144,112,58,16,BLACK); LCD_DrawRect(222,112,51,16,WHITE); } else if (pauseSel == 1) { pauseSel = 0; LCD_DrawRect(222,112,51,16,BLACK); LCD_DrawRect(144,112,58,16,WHITE); } } } } // game over if (gameOver == 1) { NVIC_ST_CTRL_R = 0; TIMER2_CTL_R = 0x00000000; LCD_DrawFilledRect(156,38,100,20,BLACK); LCD_SetTextColor(255,255,240); LCD_Goto(30,5); printf("GAME OVER"); while (Button1 == 0 && Button2 == 0) {} Button1 = 0; Button2 = 0; LCD_DrawFilledRect(prevX,prevY,20,20,BLACK); drawGameOver(score, elapsedTime); if (score > highscore) { writeHighscore(score); gameOverHighscore(score); } // wait til button is pushed while (Button1 == 0 && Button2 == 0) {} // acknowledge buttons Button1 = 0; Button2 = 0; // start new game score = 0; eraseScore(); writeScore(0); clearBoard(); eraseBoard(); pauseMode = 0; generateRandomTile(); drawAllTiles(); elapsedTime = 0; eraseTime(); writeTime(0); displayHighestTile(); NVIC_ST_CTRL_R = 0x07; TIMER2_CTL_R = 0x00000001; // finish game over mode gameOver = 0; } } }