📄 game.c
字号:
else
{
g_ppcMaze[93][1] = 0;
g_ppcMaze[93][2] = 0;
}
//
// Done adding the exit.
//
break;
}
}
//
// The maze is now constructed, but the proper wall types need to be
// selected. Loop through the rows of the maze.
//
for(iY = 0; iY < 94; iY++)
{
//
// Loop through the columns of the maze.
//
for(iX = 0; iX < 127; iX++)
{
//
// Skip this cell if it does not contain a wall.
//
if(g_ppcMaze[iY][iX] == 0)
{
continue;
}
//
// The cell type starts as zero until the adjacent wall segments
// are found. The wall graphics are arranged such that the zero
// bit indicates a wall exists above this cell, the one bit
// indicates a wall exists below this cell, the two bit indicates a
// wall exists to the right of this cell, and the three bit
// indicates a wall exists to the left of this cell.
//
iTemp = 0;
//
// See if there is a wall segment above this cell, and set the bit
// mask if so.
//
if((iY != 0) && (g_ppcMaze[iY - 1][iX] != 0))
{
iTemp |= 1;
}
//
// See if there is a wall segment below this cell, and set the bit
// mask if so.
//
if((iY != 93) && (g_ppcMaze[iY + 1][iX] != 0))
{
iTemp |= 2;
}
//
// See if there is a wall segment to the right of this cell, and
// set the bit mask if so.
//
if((iX != 126) && (g_ppcMaze[iY][iX + 1] != 0))
{
iTemp |= 4;
}
//
// See if there is a wall segment to the left of this cell, and set
// the bit mask if so.
//
if((iX != 0) && (g_ppcMaze[iY][iX - 1] != 0))
{
iTemp |= 8;
}
//
// Replace this cell of the maze with the appropriate value so that
// the wall is drawn correctly.
//
g_ppcMaze[iY][iX] = iTemp;
}
}
}
//*****************************************************************************
//
// Draws the portion of the maze centered around the player.
//
//*****************************************************************************
static void
DrawMaze(void)
{
long lXPos, lYPos, lX1, lY1, lX2, lY2, lXCell, lYCell, lXTemp;
//
// Find the upper left corner of the display based on the position of the
// player.
//
lXPos = (long)g_usPlayerX - (64 - 6);
lYPos = (long)g_usPlayerY - (47 - 6);
//
// Get the cell of the maze for the upper left corner of the display.
//
lXCell = lXPos / 12;
lYCell = lYPos / 12;
//
// Get the screen position of the maze cell that is at the upper left of
// the display. Typically, these coordinates will be negative.
//
lXPos = (lXCell * 12) - lXPos;
lYPos = (lYCell * 12) - lYPos;
//
// Loop over the maze cells that are visible (or partially visible) on the
// vertical extent of the display.
//
for(lY1 = lYPos; lY1 < 94; lY1 += 12, lYCell++)
{
//
// Do not draw this cell if it is not part of the maze.
//
if((lYCell < 0) || (lYCell > 93))
{
continue;
}
//
// Get the starting maze cell for the left side of the display.
//
lXTemp = lXCell;
//
// Loop over the maze cells that are visible (or partially visible) on
// the horizontal extent of the display.
//
for(lX1 = lXPos; lX1 < 128; lX1 += 12, lXTemp++)
{
//
// Do not draw this column if it is not part of the maze.
//
if((lXTemp < 0) || (lXTemp > 126))
{
continue;
}
//
// Loop over the scan lines of this maze cell that are visible on
// the display.
//
for(lY2 = (lY1 < 0) ? -lY1 : 0; (lY2 < 12) && ((lY1 + lY2) < 94);
lY2++)
{
//
// Loop over the columns of this maze cell that are visible on
// the display.
//
for(lX2 = (lX1 < 0) ? -lX1 : 0;
(lX2 < 12) && ((lX1 + lX2) < 128); lX2 += 2)
{
//
// Copy the graphics data for this cell of the maze into
// the local frame buffer.
//
g_pucFrame[((lY1 + lY2) * 64) + ((lX1 + lX2) / 2)] =
g_ppucSprites[(unsigned long)g_ppcMaze[lYCell][lXTemp]]
[(lY2 * 6) + (lX2 / 2)];
}
}
}
}
}
//*****************************************************************************
//
// Draws the entire maze on the display, providing a cheat mode.
//
//*****************************************************************************
static void
DrawCheat(void)
{
long lX, lY;
//
// Loop over the 64 scan lines of the display.
//
for(lY = 0; lY < 94; lY++)
{
//
// Loop over the 128 columns of the display.
//
for(lX = 0; lX < 127; lX += 2)
{
//
// See if this is the last column pair of the display.
//
if(lX == 126)
{
//
// Display the last column of the maze.
//
g_pucFrame[(lY * 64) + (lX / 2)] =
g_ppcMaze[lY][lX] ? 0xf0 : 0x00;
}
else
{
//
// Display these two columns of the maze.
//
g_pucFrame[(lY * 64) + (lX / 2)] =
((g_ppcMaze[lY][lX] ? 0xf0 : 0x00) |
(g_ppcMaze[lY][lX + 1] ? 0x0f : 0x00));
}
}
}
//
// Loop over the seven rows surrounding the position of the player.
//
for(lY = (g_usPlayerY / 12) - 3; lY < (g_usPlayerY / 12) + 4; lY++)
{
//
// If this row is not part of the maze then skip this row.
//
if(lY < 0)
{
continue;
}
//
// Loop over the eleven columns surrounding the position of the player.
//
for(lX = (g_usPlayerX / 12) - 5; lX < (g_usPlayerX / 12) + 6; lX++)
{
//
// If this column is not part of the maze then skip this column.
//
if(lX < 0)
{
continue;
}
//
// See if this is an even or odd column of the display.
//
if(lX & 1)
{
//
// This is an odd pixel, so if this pixel of the display is off
// (i.e. there is no wall at this position) then make it grey.
//
if((g_pucFrame[(lY * 64) + (lX / 2)] & 0x0f) == 0)
{
g_pucFrame[(lY * 64) + (lX / 2)] |= 0x06;
}
}
else
{
//
// This is an even pixel, so if this pixel of the dispaly is
// off (i.e. there is no wall at this position) then make it
// grey.
//
if((g_pucFrame[(lY * 64) + (lX / 2)] & 0xf0) == 0)
{
g_pucFrame[(lY * 64) + (lX / 2)] |= 0x60;
}
}
}
}
}
//*****************************************************************************
//
// Moves the monsters towards the player, regardless of how far they are from
// the player within the maze (they can't move through the walls of course).
//
//*****************************************************************************
static void
MoveMonsters(void)
{
unsigned long ulLoop, ulIdx;
tBoolean bDown, bRight;
long lX, lY;
//
// Loop through all the monsters.
//
for(ulLoop = 0; ulLoop < 100; ulLoop++)
{
//
// Skip this monster if it is dead.
//
if((g_pusMonsterX[ulLoop] == 0) && (g_pusMonsterY[ulLoop] == 0))
{
continue;
}
//
// Determine the distance between the player and the monster in both
// coordinates.
//
lX = g_usPlayerX - g_pusMonsterX[ulLoop];
lY = g_usPlayerY - g_pusMonsterY[ulLoop];
//
// See if the X coordinate is less than zero.
//
if(lX < 0)
{
//
// The monster should move to the left.
//
bRight = false;
//
// Make the X coordinate be positive.
//
lX = -lX;
}
else
{
//
// The monster should move to the right.
//
bRight = true;
}
//
// See if the Y coordinate is less than zero.
//
if(lY < 0)
{
//
// The monster should move up.
//
bDown = false;
//
// Make the Y coordinate be positive.
//
lY = -lY;
}
else
{
//
// The monster should move down.
//
bDown = true;
}
//
// Try two times to move the monster. The first attempt will be along
// the axis that the monster is furthest from the player, and the
// second attempt will be along he shorter axis.
//
for(ulIdx = 0; ulIdx < 2; ulIdx++)
{
//
// See if the monster is further away on the X or Y axis.
//
if(lX > lY)
{
//
// The monster is further away on the X axis. See if it should
// move to the left or right.
//
if(bRight && (lX != 0))
{
//
// See if there is a wall blocking the monster's movement
// to the right.
//
if((g_ppcMaze[g_pusMonsterY[ulLoop] / 12]
[(g_pusMonsterX[ulLoop] + 12) / 12] == 0) &&
(g_ppcMaze[(g_pusMonsterY[ulLoop] + 11) / 12]
[(g_pusMonsterX[ulLoop] + 12) / 12] == 0))
{
//
// There is no wall obstructing the monster, so move it
// to the right.
//
g_pusMonsterX[ulLoop] += 2;
//
// The monster has been moved, so do not attempt to
// move it further.
//
break;
}
}
else if(lX != 0)
{
//
// See if there is a wall blocking the monster's movement
// to the left.
//
if((g_ppcMaze[g_pusMonsterY[ulLoop] / 12]
[(g_pusMonsterX[ulLoop] - 2) / 12] == 0) &&
(g_ppcMaze[(g_pusMonsterY[ulLoop] + 11) / 12]
[(g_pusMonsterX[ulLoop] - 2) / 12] == 0))
{
//
// There is no wall obstructing the monster, so move it
// to the left.
//
g_pusMonsterX[ulLoop] -= 2;
//
// The monster has been moved, so do not attempt to
// move it further.
//
break;
}
}
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -