Program MazeGame Structure Cell North As Bool South As Bool East As Bool West As Bool Index As Int Visited As Bool End Structure Define MazeColumns As Int = 25 Define MazeRows As Int = 25 Define CellSize As Int = 25 Define PlayerX As Int = 1 Define PlayerY As Int = 1 Define PlayerLastX As Int = 1 Define PlayerLastY As Int = 1 Define LastRandomValue As Int Define Maze As Cell[ MazeColumns * MazeRows ] Function GetRandomNeighbor( CurrentCellIndex As Int ) As Int Define Row As Int = (CurrentCellIndex - 1) / MazeColumns + 1 Define Column As Int = (CurrentCellIndex - 1) - ( ( Row - 1 ) * MazeColumns ) + 1 Define I As Int Define Neighbor As Int = Random( 1, 4 ) For I = 1 To 4 Select Neighbor Case 1 If Column > 1 Then If Not Maze[ CurrentCellIndex - 1 ].Visited Then Return CurrentCellIndex - 1 End If End If End Case Case 2 If Column < MazeColumns Then If Not Maze[ CurrentCellIndex + 1 ].Visited Then Return CurrentCellIndex + 1 End If End If End Case Case 3 If Row > 1 Then If Not Maze[ CurrentCellIndex - MazeColumns ].Visited Then Return CurrentCellIndex - MazeColumns End If End If End Case Case 4 If Row < MazeRows Then If Not Maze[ CurrentCellIndex + MazeColumns ].Visited Then Return CurrentCellIndex + MazeColumns End If End If End Case End Select If Neighbor = 4 Then Neighbor = 1 Else Neighbor = Neighbor + 1 End If Next // Didn't find any neighbors that are not yet visited, so return -1 Return -1 End Function Method RemoveWallBetween( Index1 As Int, Index2 As Int ) Define Cell1 As Cell = Maze[ Index1 ] Define Cell2 As Cell = Maze[ Index2 ] Define y1 As Int = Cell1.Index / MazeColumns + 1 Define x1 As Int = Cell1.Index - ( ( y1 - 1 ) * MazeColumns ) + 1 Define y2 As Int = Cell2.Index / MazeColumns + 1 Define x2 As Int = Cell2.Index - ( ( y2 - 1 ) * MazeColumns ) + 1 // Do not even try to remove a wall if the cells are not direct neighbors If Abs( y1 - y2 ) + Abs( x1 - x2 ) <> 1 Then Return End If // See if one cell is directly above the other If Abs( y1 - y2 ) = 1 Then If y1 < y2 Then Cell1.South = False Cell2.North = False Else Cell1.North = False Cell2.South = False End If End If // See if one cell is directly beside the other If Abs( x1 - x2 ) = 1 Then If x1 < x2 Then Cell1.East = False Cell2.West = False Else Cell1.West = False Cell2.East = False End If End If Cell1.Visited = True Cell2.Visited = True End Method Method DrawCell( TheCell As Cell ) Define Row As Int = TheCell.Index / MazeColumns + 1 Define Column As Int = TheCell.Index - ( ( Row - 1 ) * MazeColumns ) + 1 Define x As Int = Column * CellSize - CellSize / 2 Define y As Int = Row * CellSize - CellSize / 2 If TheCell.North Then DrawLine( x, y, x + CellSize, y ) End If If TheCell.South Then DrawLine( x, y + CellSize, x + CellSize, y + CellSize ) End If If TheCell.East Then DrawLine( x + CellSize, y, x + CellSize, y + CellSize ) End If If TheCell.West Then DrawLine( x, y, x, y + CellSize ) End If End Method Method DrawMaze() Clear( white ) Color( darkblue ) PenWidth( 2 ) Define I As Int For I = 1 To MazeRows * MazeColumns DrawCell( Maze[ I ] ) Next RefreshScreen() End Method Method InitializeCells() Define i As Int Define Total As Int = MazeColumns * MazeRows For i = 1 To Total Maze[ i ].North = True Maze[ i ].South = True Maze[ i ].East = True Maze[ i ].West = True Maze[ i ].Visited = False Maze[ i ].Index = i - 1 Next End Method Method GenerateMaze() InitializeCells() Define TotalCells As Int = ArrayLength( Maze ) Define CurrentCell As Int = Random( 1, TotalCells ) Define VisitedCells As Int = 1 Define Neighbor As Int Define Stack As Int[ TotalCells ] Define StackPointer As Int = 0 While VisitedCells < TotalCells Neighbor = GetRandomNeighbor( CurrentCell ) If Neighbor <> -1 Then RemoveWallBetween( CurrentCell, Neighbor ) VisitedCells = VisitedCells + 1 // Push CurrentCell onto the stack StackPointer = StackPointer + 1 Stack[ StackPointer ] = CurrentCell CurrentCell = Neighbor Else // Pop CurrentCell from the stack CurrentCell = Stack[ StackPointer ] StackPointer = StackPointer - 1 End If End While End Method Method ShowPlayer() PlayerX = 1 PlayerY = 1 PlayerLastX = 1 PlayerLastY = 1 MoveSpriteToPoint( "Player", PlayerX * CellSize - CellSize / 3, PlayerY * CellSize - CellSize / 3 ) ShowSprite( "Player" ) Define Timeline As Int[ GetSpriteFrameCount( "player" ) ] Define i As Int For i = 1 To ArrayLength( Timeline ) Timeline[ i ] = 250 Next SetSpriteAnimationTimeline( "Player", True, Timeline ) End Method Method ShowEnd() MoveSpriteToPoint( "End", CellSize * MazeColumns - GetSpriteWidth( "End" ) / 2, CellSize * MazeRows - GetSpriteHeight( "End" ) / 2 ) ShowSprite( "End" ) End Method Method HandleKeys() Define CellIndex As Int = ( PlayerY - 1 ) * MazeColumns + ( PlayerX - 1 ) + 1 Define CurrentCell As Cell = Maze[ CellIndex ] Define CurrentKey As String = GetKey() If CurrentKey = "up" And PlayerY > 1 Then If Not CurrentCell.North Then PlayerY = PlayerY - 1 End If End If If CurrentKey = "down" And PlayerY < MazeRows Then If Not CurrentCell.South Then PlayerY = PlayerY + 1 End If End If If CurrentKey = "left" And PlayerX > 1 Then If Not CurrentCell.West Then PlayerX = PlayerX - 1 End If End If If CurrentKey = "right" And PlayerX < MazeColumns Then If Not CurrentCell.East Then PlayerX = PlayerX + 1 End If End If If PlayerLastX <> PlayerX Or PlayerLastY <> PlayerY Then DrawLine( PlayerLastX * CellSize, PlayerLastY * CellSize, PlayerX * CellSize, PlayerY * CellSize ) MoveSpriteToPoint( "Player", PlayerX * CellSize - CellSize / 3, PlayerY * CellSize - CellSize / 3 ) PlayerLastX = PlayerX PlayerLastY = PlayerY End If End Method Method ResetMaze() GenerateMaze() DrawMaze() ShowPlayer() ShowEnd() PenWidth( 3 ) Color( Yellow ) End Method Method Main() SetDeviceCoordinates() LoadSprite( "Player", "lol.gif" ) ScaleSprite( "Player", .95 ) LoadSprite( "End", "block.gif" ) ScaleSprite( "End", 0.65 ) CellSize = GetSpriteWidth( "Player" ) * 1.25 SetScreenSize( MazeColumns * CellSize + CellSize, MazeRows * CellSize + CellSize ) Status( "Use the arrow keys to move the player through the maze" ) ResetMaze() While True HandleKeys() RefreshScreen() If SpritesIntersect( "Player", "End" ) Then PlaySound( "tada" ) Delay( 3000 ) ResetMaze() End If End While End Method End Program