⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timer.c

📁 Space Invaders game
💻 C
📖 第 1 页 / 共 2 页
字号:
						  } else {
								MovingRight = TRUE;
								LastGremlinRow = 0;
								while (RowEndRight[LastGremlinRow] == NULL && ++LastGremlinRow < 5)
									 ;
								LastGremlinMoved = RowEndRight[LastGremlinRow];
								MoveGremlinRight(LastGremlinMoved);
						  }
					 }
				} else
					 MoveGremlinDownRight(LastGremlinMoved);
		  }
	 }
}


void MoveGremlinRight(
GREMLINPTR Grem)
{
	 ++(Grem->xpos);
	 if (Grem->xpos + *(Grem->object[0]) > 127)
		  GremlinsHitWall = TRUE;
	 PaintObject(Grem->xpos, Grem->ypos, Grem->object[Tick]);
}


void MoveGremlinDownLeft(
GREMLINPTR Grem)
{
	 if (Grem->ypos >= 0)
		  PaintObject(Grem->xpos, Grem->ypos, Grem->object[2]);
	 Grem->xpos -= 10 - *(Grem->object[0]);
	 Grem->ypos += 3;
	 if (Grem->ypos >= 0)
		  PaintObject(Grem->xpos, Grem->ypos, Grem->object[Tick]);
	 if (Grem->ypos > 44)
		  Invaded();
}


void MoveGremlinLeft(
GREMLINPTR Grem)
{
	 if (!TimerFrame)
	     --(Grem->xpos);
	 if (Grem->xpos == 0)
		  GremlinsHitWall = TRUE;
	 PaintObject(Grem->xpos, Grem->ypos, Grem->object[Tick]);
}


void MoveGremlinDownRight(
GREMLINPTR Grem)
{
	 if (Grem->ypos >= 0)
		  PaintObject(Grem->xpos, Grem->ypos, Grem->object[2]);
	 Grem->xpos += 10 - *(Grem->object[0]);
	 Grem->ypos += 3;
	 if (Grem->ypos >= 0)
		  PaintObject(Grem->xpos, Grem->ypos, Grem->object[Tick]);
	 if (Grem->ypos > 44)
		  Invaded();
}


void CALLBACK  FrameEndOneProc(
HWND hWnd,
UINT message,
UINT wParam,
DWORD lParam)
{
	 int delay;

	 KillTimer(hWndMain, TID_RESUME);

	 if (NumGremlins == 0 && (LastGremlinMoved->value == 10 || TimerFrame)) {
		  KillGameTimers();
		  ClearScrn();
		  Score += 1000;
		  if (!BonusLifeGiven && Score >= 1500) {
				BonusLifeGiven = TRUE;
				Lives++;
				SetWindowText(hWndMain, "***   Extra Life   ***");
				PlayWAV(SND_EXTRA_LIFE);
		  } else
				SetGameText();
		  PaintObject( 12,  17, character_B);
		  PaintObject( 24,  17, character_O);
		  PaintObject( 36,  17, character_N);
		  PaintObject( 48,  17, character_U);
		  PaintObject( 60,  17, character_S);
		  PaintObject( 78,  17, digit[1]);
		  PaintObject( 88,  17, digit[0]);
		  PaintObject( 98,  17, digit[0]);
		  PaintObject(108,  17, digit[0]);
		  delay = 5000;
	 } else
		  delay = 1;

	 SetTimer(hWndMain, TID_RESUME, delay, FrameEndTwoProc);
}


void CALLBACK  FrameEndTwoProc(
HWND hWnd,
UINT message,
UINT wParam,
DWORD lParam)
{
	 int i, x;
	 char buf[32];

	 KillTimer(hWndMain, TID_RESUME);

	 if (Lives == 0) {
		  ClearScrn();
		  KillGameTimers();

		  sprintf(buf, "%f", (double)Score);
		  for (i = 0; buf[i] != '.' && i < sizeof buf; i++)
				;
		  buf[i] = '\0';
		  x = 64-5*strlen(buf);
		  for (i = 0; isdigit(buf[i]); i++) {
				PaintObject(x, 1, digit[buf[i]-'0']);
				x += 10;
		  }

		  PaintObject( 15,  17, character_G);
		  PaintObject( 27,  17, character_A);
		  PaintObject( 39,  17, character_M);
		  PaintObject( 51,  17, character_E);
		  PaintObject( 69,  17, character_O);
		  PaintObject( 81,  17, character_V);
		  PaintObject( 93,  17, character_E);
		  PaintObject(105,  17, character_R);
		  SetTimer(hWndMain, TID_RESUME, 5000, GameEndOneProc);
	 } else
		  InitFrame();
}


void CALLBACK  GameEndOneProc(
HWND hWnd,
UINT message,
UINT wParam,
DWORD lParam)
{
	 int delay, i, x;
	 char buf[32];

	 KillTimer(hWndMain, TID_RESUME);
	 if (Score > HiScore) {
		  ClearScrn();

		  HiScore = Score;
		  sprintf(buf, "%f", (double)HiScore);
		  for (i = 0; buf[i] != '.' && i < sizeof buf; i++)
				;
		  buf[i] = '\0';
		  x = 64-5*strlen(buf);
		  for (i = 0; isdigit(buf[i]); i++) {
				PaintObject(x, 1, digit[buf[i]-'0']);
				x += 10;
		  }

		  PaintObject( 18,  17, character_H);
		  PaintObject( 30,  17, character_I);
		  PaintObject( 42,  17, character_MINUS);
		  PaintObject( 54,  17, character_S);
		  PaintObject( 66,  17, character_C);
		  PaintObject( 78,  17, character_O);
		  PaintObject( 90,  17, character_R);
		  PaintObject(102,  17, character_E);
		  delay = 5000;
	 } else
		  delay = 1;

	 SetTimer(hWndMain, TID_RESUME, delay, GameEndTwoProc);
}


void CALLBACK  GameEndTwoProc(
HWND hWnd,
UINT message,
UINT wParam,
DWORD lParam)
{
	 KillTimer(hWndMain, TID_RESUME);
	 InitGame();
}


void GunHit()
{
	 char buf[32];

	 KillTimer(hWndMain, TID_RESUME);     //If we are called after All Gremlins Killed abort.
	 Lives--;
	 sprintf(buf, "***   Lives   %d   ***", Lives);
	 SetWindowText(hWndMain, buf);
	 Stopped = TRUE;
	 while (ActiveBombs != NULL)
		  RemoveBomb(ActiveBombs->next, ActiveBombs);
	 Flashes = 11;
	 FlashExplosion = TRUE;
	 PaintObject(GunPos-6, 45, GunBlack);
	 PaintObject(GunPos-4, 45, Explosion);
	 PlayWAV(SND_GUN_HIT);
	 SetTimer(hWndMain, TID_RESUME, 100, FlashProc);
}


void CALLBACK  FlashProc(
HWND hWnd,
UINT message,
UINT wParam,
DWORD lParam)
{
	 int even;

	 even = ((Flashes/2)*2 == Flashes);
	 if (FlashExplosion) {
		  if (even)
				PaintObject(GunPos-6, 45, GunBlack);
		  else
				PaintObject(GunPos-4, 45, Explosion);
	 } else {
		  if (even)
				PaintObject(GunPos-6, 45, Gun);
		  else
				PaintObject(GunPos-6, 45, GunBlack);
	 }
	 Flashes--;
	 if (Flashes < 0) {
		  KillTimer(hWndMain, TID_RESUME);
		  if (Lives == 0)
				SetTimer(hWndMain, TID_RESUME, 1, FrameEndOneProc);
		  else if (FlashExplosion) {
				FlashExplosion = FALSE;
				Flashes = 6;
				SetTimer(hWndMain, TID_RESUME, 200, FlashProc);
				GunPos = 10;
		  } else {
				Stopped = FALSE;
            SetGameText();
				if (NumGremlins == 0)
					 SetTimer(hWndMain, TID_RESUME, 5000, FrameEndOneProc);
		  }
	 }
}


void Invaded()
{
	 Lives = 0;
	 Stopped = TRUE;
	 PlayWAV(SND_GUN_HIT);
	 SetTimer(hWndMain, TID_RESUME, 5000, FrameEndTwoProc);
}


void BulletHit()
{
	 GREMLINPTR gp;
	 BOMBPTR bp, bpPrev;
	 int y;
	 char buf[32];

	 for (y = 4; y >= 0; y--) {
		  gp = RowEndLeft[y];
		  while (gp != NULL) {
				if (
					 BullPosY >= (unsigned int)gp->ypos+1 &&
					 BullPosY <= (unsigned int)gp->ypos+3 &&
					 BullPosX >= (unsigned int)gp->xpos+1 &&
					 BullPosX <= (unsigned int)gp->xpos+*(gp->object[0])-2
				) {
					 gp->alive = FALSE;

					 if (gp->right != NULL)
						  gp->right->left = gp->left;
					 else
						  RowEndRight[y] = gp->left;
					 if (gp->left != NULL)
						  gp->left->right = gp->right;
					 else
						  RowEndLeft[y] = gp->right;

					 PaintObject(gp->xpos, gp->ypos, gp->object[2]);

					 // Draw Explosion

					 PlayWAV(SND_GREMLIN_EXPLODE);
					 ExplosionPosX =
					 gp->xpos-3+(*(gp->object[0])-MovingLeft-MovingDownLeft-2)/2;
					 ExplosionPosY = gp->ypos;
					 PaintObject(ExplosionPosX, ExplosionPosY, Explosion);
					 ExplosionPresent = TRUE;
					 SetTimer(hWndMain, TID_EXPLOSION, Tid_Explosion_Delay, ExplosionProc);
					 NumGremlins--;
					 Score += gp->value;
					 if (!BonusLifeGiven && Score >= 1500) {
						  BonusLifeGiven = TRUE;
						  Lives++;
						  SetWindowText(hWndMain, "***   Extra Life   ***");
						  Stopped = TRUE;
						  PlayWAV(SND_EXTRA_LIFE);
						  SetTimer(hWndMain, TID_RESUME, 3000, RefreshTextProc);
					 } else
						  SetGameText();
					 return;
			  }
			  gp = gp->right;
		  }
	 }
	 if (BullPosY <= 2 && BullPosY >=1) {       //Hit SpaceShip
		  KillTimer(hWndMain, TID_SPACESHIP);
		  SpaceShipPresent = FALSE;
		  if (SS500) {
				Score += 500;
				SetWindowText(hWndMain, "***   500   ***");
				Stopped = TRUE;
				PlayWAV(SND_SS500);
				SetTimer(hWndMain, TID_RESUME, 3000, RefreshTextProc);
				return;
		  } else {
				PaintObject(SSx, 0, SpaceShipBlack);
				Score += SSScore[BulletsFired];
				sprintf(buf, "***   %d   ***", SSScore[BulletsFired]);
				SetWindowText(hWndMain, buf);
				PlayWAV(SND_SS_EXPLODE);
				SetTimer(hWndMain, TID_RESUME, 3000, RefreshTextProc);
				return;
		  }
	 }
	 if (ActiveBombs != NULL) {
		  bp = ActiveBombs;
		  do {
				bpPrev = bp;
				bp = bp->next;
				if (BullPosX == (unsigned int)bp->xpos && BullPosY == (unsigned int)bp->ypos+3) {
					 RemoveBomb(bp, bpPrev);
					 break;
				}
		  } while (bp != ActiveBombs);
	 }
	 //  Hit Shelter
	 PaintObject(BullPosX, BullPosY-1, PixelBlack);
}


void CALLBACK  RefreshTextProc(
HWND hWnd,
UINT message,
UINT wParam,
DWORD lParam)
{
	 KillTimer(hWndMain, TID_RESUME);
	 PaintObject(SSx, 0, SpaceShipBlack);
	 if (!BonusLifeGiven && Score >= 1500) {
		  BonusLifeGiven = TRUE;
		  Lives++;
		  SetWindowText(hWndMain, "***   Extra Life   ***");
		  Stopped = TRUE;
		  PlayWAV(SND_EXTRA_LIFE);
		  SetTimer(hWndMain, TID_RESUME, 3000, RefreshTextProc);
	 } else {
		  SetGameText();
		  Stopped  = FALSE;
	 }
}


void CALLBACK  ExplosionProc(
HWND hWnd,
UINT message,
UINT wParam,
DWORD lParam)
{
	 KillTimer(hWndMain, TID_EXPLOSION);
	 PaintObject(ExplosionPosX, ExplosionPosY, ExplosionBlack);
	 ExplosionPresent = FALSE;
}


void CALLBACK  LaunchSpaceShipProc(
HWND hWnd,
UINT message,
UINT wParam,
DWORD lParam)
{
	 int i;

	 i = rand()%4;
	 if (i || Paused || Stopped || SpaceShipPresent || TimerFrame || NumGremlins < 8)
		  return;
	 while (RowEndLeft[i] == NULL && ++i < 5)
		  ;
	 if (i < 5)
		  if (min(RowEndLeft[i]->ypos, RowEndRight[i]->ypos) >= 3) {
				i = rand()%2;
				if (i) {
					 SSMovingRight = TRUE;
					 SSx = -SpaceShip[0];
				} else {
					 SSMovingRight = FALSE;
					 SSx = 127;
				}
				i = rand()%3;
				if (i == 0)
					 SS500 = TRUE;
				else
					 SS500 = FALSE;
				SetTimer(hWndMain, TID_SPACESHIP, Tid_SpaceShip_Delay, MoveSpaceShipProc);
				SpaceShipPresent = TRUE;
		  }
}


void CALLBACK  MoveSpaceShipProc(
HWND hWnd,
UINT message,
UINT wParam,
DWORD lParam)
{
	 if (!Paused && !Stopped) {
		  if (SSMovingRight) {
				if (++SSx > 127) {
					 KillTimer(hWndMain, TID_SPACESHIP);
					 SpaceShipPresent = FALSE;
					 return;
				}
		  } else if (--SSx < -SpaceShip[0]) {
				KillTimer(hWndMain, TID_SPACESHIP);
				SpaceShipPresent = FALSE;
				return;
		  }
		  if (SS500 && (SSx%4 == 0 || SSx%4 == 1))
				PaintObject(SSx, 0, SpaceShipBlack);
		  else
				PaintObject(SSx, 0, SpaceShip);
	 }
}


void CALLBACK  TimerFrameTickProc(
HWND hWnd,
UINT message,
UINT wParam,
DWORD lParam)
{
	  TimerSeconds--;
	  SetGameText();
	  if (TimerSeconds <= 0) {
			KillTimer(hWndMain, TID_TIMER);
         Stopped = TRUE;
			Frame++;
			SetTimer(hWndMain, TID_RESUME, 5000, FrameEndOneProc);
	  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -