📄 timer.c
字号:
#pragma hdrstop
#include "invaders.h"
BOMB Bombs[32];
unsigned int SSScore[] = {
50, 50, 150, 50, 100, 150, 50, 100,
200, 50, 50, 100, 150, 50, 100, 200,
50, 50, 100, 150, 50, 100, 300
};
void DelayStartGame()
{
SetGameText();
Stopped = TRUE;
KillGameTimers();
SetTimer(hWndMain, TID_RESUME, 5000, StartGameProc);
}
void CALLBACK StartGameProc(
HWND hWnd,
UINT message,
UINT wParam,
DWORD lParam)
{
KillTimer(hWndMain, TID_RESUME);
ClearScrn();
PaintObject(14, 35, Shelter);
PaintObject(42, 35, Shelter);
PaintObject(70, 35, Shelter);
PaintObject(98, 35, Shelter);
PaintObject( 4, 45, Gun);
Stopped = FALSE;
SetGameTimers();
}
void KillGameTimers()
{
KillTimer(hWndMain, TID_GUN);
KillTimer(hWndMain, TID_BULLET);
KillTimer(hWndMain, TID_GREMLIN);
KillTimer(hWndMain, TID_SPACESHIP_LAUNCH);
KillTimer(hWndMain, TID_SPACESHIP);
KillTimer(hWndMain, TID_BOMB_LAUNCH);
KillTimer(hWndMain, TID_BOMB);
KillTimer(hWndMain, TID_TIMER);
}
void SetGameTimers()
{
SetTimer(hWndMain, TID_GUN, Tid_Gun_Delay, MoveGunProc);
if (BulletPresent)
SetTimer(hWndMain, TID_BULLET, Tid_Bullet_Delay, MoveBulletProc);
SetTimer(hWndMain, TID_GREMLIN, Tid_Gremlin_Delay, MoveGremlinProc);
SetTimer(hWndMain, TID_SPACESHIP_LAUNCH, Tid_SpaceShip_Launch_Delay, LaunchSpaceShipProc);
if (SpaceShipPresent)
SetTimer(hWndMain, TID_SPACESHIP, Tid_SpaceShip_Delay, MoveSpaceShipProc);
SetTimer(hWndMain, TID_BOMB_LAUNCH, Tid_Bomb_Launch_Delay, LaunchBombProc);
SetTimer(hWndMain, TID_BOMB, Tid_Bomb_Delay, MoveAllBombsProc);
if (TimerFrame)
SetTimer(hWndMain, TID_TIMER, TID_TIMER_TICK, TimerFrameTickProc);
}
void InitBombs()
{
int i;
ActiveBombs = NULL;
FreeBombs = &Bombs[0];
for (i = 0; i < 31; i++)
Bombs[i].next = &Bombs[i+1];
Bombs[i].next = NULL;
}
void CALLBACK LaunchBombProc(
HWND hWnd,
UINT message,
UINT wParam,
DWORD lParam)
{
int i, x, y;
GREMLINPTR gp, Launchgp;
i = rand()%4;
if (!i || Paused || Stopped || NumGremlins == 0 || FreeBombs == NULL)
return;
i = rand()%6;
switch (i) {
case 0: // Launch at Left End of Gremlin group
gp = GetLeftEndGremlin();
break;
case 1: // Launch at Right End of Gremlin group
gp = GetRightEndGremlin();
break;
case 2: // Launch at Gun
gp = NULL;
for (x = 0; x < 11; x++) {
for (y = 4; y >= 0; y--) {
if (Gremlins[y][x].alive == TRUE) {
Launchgp = gp;
gp = &Gremlins[y][x];
break;
}
}
if (y >= 0)
if ((GunPos+6-(gp->xpos+1+(*(gp->object[0])-MovingLeft-MovingDownLeft-2)/2)) < 0)
break;
}
if (x < 11 && Launchgp != NULL)
gp = Launchgp;
break;
case 3: // Launch from Random possible Gremlin
case 4:
case 5:
gp = NULL;
while (gp == NULL) {
x = rand()%11;
for (y = 4; y >= 0; y--) {
if (Gremlins[y][x].alive == TRUE) {
gp = &Gremlins[y][x];
break;
}
}
}
break;
}
LaunchBombFromGremlin(gp);
}
GREMLINPTR GetLeftEndGremlin()
{
int x, y;
for (x = 0; x < 11; x++)
for (y = 4; y >= 0; y--)
if (Gremlins[y][x].alive == TRUE)
return(&Gremlins[y][x]);
return(NULL);
}
GREMLINPTR GetRightEndGremlin()
{
int x, y;
for (x = 10; x >= 0; x--)
for (y = 4; y >= 0; y--)
if (Gremlins[y][x].alive == TRUE)
return(&Gremlins[y][x]);
return(NULL);
}
void LaunchBombFromGremlin(
GREMLINPTR gp)
{
int i, x, y;
BOMBPTR bp;
if (gp->ypos > 41 || gp->ypos < 0 || FreeBombs == NULL)
return;
x = gp->xpos+1+(*(gp->object[0])-MovingLeft-MovingDownLeft-2)/2;
y = gp->ypos+3;
for (i = 0; i < 3; i++)
if (VirtScrn[x][y+i] == 1) {
PaintObject((x/2)*2, y+i, EatShelter);
break;
}
if (i >= 3) {
bp = FreeBombs;
FreeBombs = bp->next;
bp->xpos = x;
bp->ypos = y;
if (ActiveBombs == NULL)
bp->next = bp;
else {
bp->next = ActiveBombs->next;
ActiveBombs->next = bp;
}
ActiveBombs = bp;
PaintObject(x, y, Bomb);
}
}
void CALLBACK MoveAllBombsProc(
HWND hWnd,
UINT message,
UINT wParam,
DWORD lParam)
{
BOMBPTR bp, bpPrev;
if (!Paused && !Stopped && ActiveBombs != NULL) {
bp = ActiveBombs;
do {
bpPrev = bp;
bp = bp->next;
if (bp->ypos > 44) {
RemoveBomb(bp, bpPrev);
bp = bpPrev;
} else if (VirtScrn[bp->xpos][bp->ypos+3]) {
RemoveBomb(bp, bpPrev);
BombHit(bp->xpos, bp->ypos+3);
bp = bpPrev;
} else {
PaintObject(bp->xpos, bp->ypos, BombLong);
bp->ypos++;
}
} while (ActiveBombs != NULL && bp != ActiveBombs);
}
}
void RemoveBomb(
BOMBPTR bp,
BOMBPTR bpPrev)
{
PaintObject(bp->xpos, bp->ypos, BombBlack);
if (bp == bpPrev) {
ActiveBombs = NULL;
} else {
bpPrev->next = bp->next;
if (bp == ActiveBombs)
ActiveBombs = bpPrev;
}
bp->next = FreeBombs;
FreeBombs = bp;
}
void BombHit(
int x,
int y)
{
if ((unsigned int)x == BullPosX && (unsigned int)y == BullPosY) {
KillTimer(hWndMain, TID_BULLET);
BulletPresent = FALSE;
PaintObject(x, y, BulletBlack);
} else if (y > 44)
GunHit();
else
PaintObject((x/2)*2, y, EatShelter);
}
void CALLBACK MoveGunProc(
HWND hWnd,
UINT message,
UINT wParam,
DWORD lParam)
{
if (!Paused && !Stopped && GunMotion) {
GunPos += GunMotion;
if ((GunPos < 10) || (GunPos > 117))
GunPos -= GunMotion;
else
PaintObject(GunPos-6, 45, Gun);
}
}
void CALLBACK MoveBulletProc(
HWND hWnd,
UINT message,
UINT wParam,
DWORD lParam)
{
if (!Paused && !Stopped && BulletPresent)
if (BullPosY) {
if (VirtScrn[BullPosX][BullPosY-1]) {
KillTimer(hWndMain, TID_BULLET);
BulletPresent = FALSE;
PaintObject(BullPosX, BullPosY, BulletBlack);
BulletHit();
return;
}
PaintObject(BullPosX, --BullPosY, BulletLong);
} else {
KillTimer(hWndMain, TID_BULLET);
BulletPresent = FALSE;
ExplosionPresent = TRUE;
PaintObject(BullPosX, BullPosY, BulletBlack);
PaintObject(BullPosX-1, 0, BulletFlat);
SetTimer(hWndMain, TID_BULLET_FLAT, Tid_Bullet_Flat_Delay, FlatBulletProc);
}
}
void CALLBACK FlatBulletProc(
HWND hWnd,
UINT message,
UINT wParam,
DWORD lParam)
{
KillTimer(hWndMain, TID_BULLET_FLAT);
PaintObject(BullPosX-1, 0, BulletFlatBlack);
ExplosionPresent = FALSE;
}
void CALLBACK MoveGremlinProc(
HWND hWnd,
UINT message,
UINT wParam,
DWORD lParam)
{
int i;
if (!Paused && !Stopped) {
if (NumGremlins == 0) {
KillTimer(hWndMain, TID_GREMLIN);
if (TimerFrame)
KillTimer(hWndMain, TID_TIMER);
i = rand()%3;
if (!i)
LaunchBombFromGremlin(LastGremlinMoved);
Frame++;
SetTimer(hWndMain, TID_RESUME, 5000, FrameEndOneProc);
return;
}
if (MovingRight) {
LastGremlinMoved = LastGremlinMoved->left;
if (LastGremlinMoved == NULL) {
while (++LastGremlinRow < 5 && RowEndRight[LastGremlinRow] == NULL)
;
if (LastGremlinRow < 5) {
LastGremlinMoved = RowEndRight[LastGremlinRow];
MoveGremlinRight(LastGremlinMoved);
} else if (GremlinsHitWall) {
PlayWAV(SND_GREMLIN);
Tick = Tick ? 0 : 1;
MovingRight = FALSE;
MovingDownLeft = TRUE;
GremlinsHitWall = FALSE;
LastGremlinRow = 4;
while (RowEndLeft[LastGremlinRow] == NULL && --LastGremlinRow >= 0)
;
LastGremlinMoved = RowEndLeft[LastGremlinRow];
MoveGremlinDownLeft(LastGremlinMoved);
} else {
PlayWAV(SND_GREMLIN);
Tick = Tick ? 0 : 1;
LastGremlinRow = 0;
while (RowEndRight[LastGremlinRow] == NULL && ++LastGremlinRow < 5)
;
LastGremlinMoved = RowEndRight[LastGremlinRow];
MoveGremlinRight(LastGremlinMoved);
}
} else
MoveGremlinRight(LastGremlinMoved);
} else if (MovingDownLeft) {
LastGremlinMoved = LastGremlinMoved->right;
if (LastGremlinMoved == NULL) {
while (--LastGremlinRow >= 0 && RowEndLeft[LastGremlinRow] == NULL)
;
if (LastGremlinRow >= 0) {
LastGremlinMoved = RowEndLeft[LastGremlinRow];
MoveGremlinDownLeft(LastGremlinMoved);
} else {
PlayWAV(SND_GREMLIN);
Tick = Tick ? 0 : 1;
MovingDownLeft = FALSE;
for (i = 0; RowEndRight[i] == NULL && ++i < 5;)
;
if (MovingDown && i < 5 && RowEndRight[i]->ypos < StartFrameRow) {
MovingDownRight = TRUE;
LastGremlinRow = 4;
while (RowEndRight[LastGremlinRow] == NULL && --LastGremlinRow >= 0)
;
LastGremlinMoved = RowEndRight[LastGremlinRow];
MoveGremlinDownRight(LastGremlinMoved);
} else {
MovingLeft = TRUE;
LastGremlinRow = 0;
while (RowEndLeft[LastGremlinRow] == NULL && ++LastGremlinRow < 5)
;
LastGremlinMoved = RowEndLeft[LastGremlinRow];
MoveGremlinLeft(LastGremlinMoved);
}
}
} else
MoveGremlinDownLeft(LastGremlinMoved);
} else if (MovingLeft) {
LastGremlinMoved = LastGremlinMoved->right;
if (LastGremlinMoved == NULL) {
while (++LastGremlinRow < 5 && RowEndLeft[LastGremlinRow] == NULL)
;
if (LastGremlinRow < 5) {
LastGremlinMoved = RowEndLeft[LastGremlinRow];
MoveGremlinLeft(LastGremlinMoved);
} else if (GremlinsHitWall) {
PlayWAV(SND_GREMLIN);
Tick = Tick ? 0 : 1;
MovingLeft = FALSE;
MovingDownRight = TRUE;
GremlinsHitWall = FALSE;
LastGremlinRow = 4;
while (RowEndRight[LastGremlinRow] == NULL && --LastGremlinRow >= 0)
;
LastGremlinMoved = RowEndRight[LastGremlinRow];
MoveGremlinDownRight(LastGremlinMoved);
} else {
PlayWAV(SND_GREMLIN);
Tick = Tick ? 0 : 1;
LastGremlinRow = 0;
while (RowEndLeft[LastGremlinRow] == NULL && ++LastGremlinRow < 5)
;
LastGremlinMoved = RowEndLeft[LastGremlinRow];
MoveGremlinLeft(LastGremlinMoved);
}
} else
MoveGremlinLeft(LastGremlinMoved);
} else if (MovingDownRight) {
LastGremlinMoved = LastGremlinMoved->left;
if (LastGremlinMoved == NULL) {
while (--LastGremlinRow >= 0 && RowEndRight[LastGremlinRow] == NULL)
;
if (LastGremlinRow >= 0) {
LastGremlinMoved = RowEndRight[LastGremlinRow];
MoveGremlinDownRight(LastGremlinMoved);
} else {
PlayWAV(SND_GREMLIN);
Tick = Tick ? 0 : 1;
MovingDownRight = FALSE;
for (i = 0; RowEndRight[i] == NULL && ++i < 5;)
;
if (MovingDown && i < 5 && RowEndRight[i]->ypos < StartFrameRow) {
MovingDownLeft = TRUE;
LastGremlinRow = 4;
while (RowEndLeft[LastGremlinRow] == NULL && --LastGremlinRow >= 0)
;
LastGremlinMoved = RowEndLeft[LastGremlinRow];
MoveGremlinDownLeft(LastGremlinMoved);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -