//Timer interrupt for the game. void timer_interrupt_handler() { if(!paused) { secondTimer++; alienMarchTimer++; bulletMoveTimer++; controllerTimer++; if(getShipAlive()) shipMoveTimer++; } tankMoveTimer++; //Poll the buttons if(tankMoveTimer >= TANK_SPEED) { handleButton(currentButtonState); handleSwitches(currentSwitchesState); } if(controllerTimer >= POLL_CONTROLLER_TIMER) { controllerTimer = 0; //readController(); } //Advance the aliens and fire bullets if(alienMarchTimer >= ALIEN_MARCH_SPEED) { alienMarch(); if(rand() % BULLET_CHANCE == 0) { fireAlienBullet(); } alienMarchTimer = 0; } //Advance the bullets if(bulletMoveTimer >= BULLET_SPEED) { bulletMove(); bulletMoveTimer = 0; } //Move the saucer if it is active if(getShipActive()&&!getPaused()) { setSound(ufolowpitch_getSound(),ufolowpitch_getNumFrames(), SAUCER_PRIORITY); if(shipMoveTimer >= SHIP_MOVE_MAX_TIMER) { shipMoveTimer = 0; marchShip(); } } //If the saucer is killed flash the score animation if(getMothershipKilled() && saucerFlash == 0) { saucerFlash = 1; } //If the saucer is killed flash the score animation if(saucerFlash > 0) { //Paint score if(secondTimer == HALF_SECOND) { setSound(ufohighpitch_getSound(),ufohighpitch_getNumFrames(), SAUCER_DEATH_PRIORITY); paintShipScore(1); saucerFlash++; } //paint black if(secondTimer == SECOND_TIMER_MAX) { paintShipScore(0); saucerFlash++; } if(saucerFlash >= 7) { paintShipScore(0); saucerFlash = 0; setMothershipKilled(0); if(getShipDirection()) { setShipPos(-SHIP_WIDTH); setShipDirection(1); } else { setShipPos(SCREEN_WIDTH + SHIP_WIDTH); setShipDirection(0); } } } //Spawn the saucer every X seconds if(saucerTime == 10 && !paused) { saucerTime = 0; if(!getShipActive() && saucerFlash == 0) { setShipActive(1); setShipAlive(1); setMothershipKilled(0); } } //Tic the clock one second if(secondTimer >= SECOND_TIMER_MAX) { secondTimer = 0; gameRunTime++; saucerTime++; } }
bool MyFrameListener::placeShips(int aShips[_NUMSHIPS], int aBoard[_XMAX][_YMAX]) { // Reset for (int i=0; i<_XMAX; i++) { for (int j=0; j<_YMAX; j++) { aBoard[i][j]=0; } } // semilla de rand() SYSTEMTIME st; GetSystemTime(&st); srand(st.wMilliseconds); for (int i=0; i<_NUMSHIPS; i++) { int myX = 0; int myY = 0; int shipLenght = aShips[i]; int shipPointer = 0; int shipDirection = 0; // Posicion aleatoria no ocupada por barco do { myX = rand()%_XMAX; myY = rand()%_YMAX; shipDirection = getShipDirection(myX, myY, shipLenght, aBoard); // 0-derecha, 1-abajo, 2-izquierda, 3-derecha, 4-error; } while (/*(aBoard[myX][myY] == 1) || */shipDirection==4); // colocacion de barco dependiendo de la direccion switch (shipDirection) { case 0: // derecha while (shipLenght>0) { aBoard[myX+shipPointer][myY] = 1; shipPointer++; shipLenght--; } break; case 1: // abajo while (shipLenght>0) { aBoard[myX][myY+shipPointer] = 1; shipPointer++; shipLenght--; } break; case 2: // izquierda while (shipLenght>0) { aBoard[myX-shipPointer][myY] = 1; shipPointer++; shipLenght--; } break; case 3: // arriba while (shipLenght>0) { aBoard[myX][myY-shipPointer] = 1; shipPointer++; shipLenght--; } break; }; } return true; }