' QBLOCKS.BAS
'
' Press Page Down for information on running and modifying QBlocks.
'
' To run this game, press Shift+F5.
'
' To exit this program, press ALT, F, X.
'
' To get help on a BASIC keyword, move the cursor to the keyword and press
' F1 or click the right mouse button.
'
' Suggested Changes
' -----------------
'
' There are many ways that you can modify this BASIC game. The CONST
' statements below these comments and the DATA statements at the end
' of this screen can be modified to change the following:
'
' Block shapes
' Block rotation
' Number of different block shapes
' Score needed to advance to next level
' Width of the game well
' Height of the game well
' Songs played during game
'
' On the right side of each CONST statement, there is a comment that tells
' you what it does and how big or small you can set the value. Above the
' DATA statements, there are comments that tell you the format of the
' information stored there.
'
' On your own, you can also add exciting sound and visual effects or make any
' other changes that your imagination can dream up. By reading the
' Learn BASIC Now book, you'll learn the techniques that will enable you
' to fully customize this game and to create games of your own.
'
' If the game won't run after you have changed it, you can exit without
' saving your changes by pressing Alt, F, X and choosing NO.
'
' If you do want to save your changes, press Alt, F, A and enter a filename
' for saving your version of the program. Before you save your changes,
' however, you should make sure they work by running the program and
' verifying that your changes produce the desired results. Also, always
' be sure to keep a backup of the original program.
'
' Here are the BASIC CONST statements you can change. The comments tell
' you the range that each CONST value can be changed, or any limitations.
CONST WELLWIDTH
= 10 ' Width of playing field (well). Range 5 to 13.
CONST WELLHEIGHT
= 21 ' Height of playing field. Range 4 to 21.
CONST NUMSTYLES
= 7 ' Number of unique shapes. Range 1 to 20. Make sure you read the notes above the DATA statements at the end of the main program before you change this number!
CONST WINGAME
= 1000000 ' Points required to win the game. Range 200 to 9000000.
CONST NEXTLEVEL
= 300 ' Helps determine when the game advances to the next level. (Each cleared level gives player 100 points) Range 100 to 2000.
CONST BASESCORE
= 1000 ' Number of points needed to advance to first level.
CONST ROTATEDIR
= 1 ' Control rotation of blocks. Can be 1 for clockwise, or 3 for counterclockwise.
' The following sound constants are used by the PLAY command to
' produce music during the game. To change the sounds you hear, change
' these constants. Refer to the online help for PLAY for the correct format.
' To completely remove sound from the game set the constants equal to null.
' For example: PLAYINTRO = ""
CONST PLAYCLEARROW
= "MBT255L16O4CDEGO6C" ' Tune played when a row is cleared. Range unlimited.
CONST PLAYINTRO
= "MBT170O1L8CO2CO1CDCA-A-FGFA-F" ' Song played at game start. Range unlimited.
CONST PLAYGAMEOVER
= "MBT255L16O6CO4GEDC" ' Song when the game is lost. Range unlimited.
CONST PLAYNEWBLOCK
= "MBT160L28N20L24N5" ' Song when a new block is dropped. Range unlimited.
CONST PLAYWINGAME
= "T255L16O6CO4GEDCCDEFGO6CEG" ' Song when game is won. Range unlimited.
' The following CONST statements should not be changed like the ones above
' because the program relies on them being this value.
CONST FALSE
= 0 ' 0 means FALSE.
CONST TRUE
= NOT FALSE
' Anything but 0 can be thought of as TRUE.
CONST SPACEBAR
= 32 ' ASCII value for space character. Drops the shape.
CONST DOWNARROW
= 80 ' Down arrow key. Drops the shape.
CONST RIGHTARROW
= 77 ' Right arrow key. Moves the shape right.
CONST UPARROW
= 72 ' Up arrow key. Rotates the shape.
CONST LEFTARROW
= 75 ' Left arrow key. Moves the shape left.
CONST DOWNARROW2
= 50 ' 2 key. Drops the shape.
CONST RIGHTARROW2
= 54 ' 6 key. Moves the shape right.
CONST UPARROW2
= 56 ' 8 key. Rotates the shape.
CONST LEFTARROW2
= 52 ' 4 key. Moves the shape left.
CONST UPARROW3
= 53 ' 5 key. Rotates the shape.
CONST QUIT
= "Q" ' Q key. Quits the game.
CONST PAUSE
= "P" ' P key. Pauses the game.
CONST XMATRIX
= 3 ' Width of the matrix that forms each falling unit. See the discussions in Suggested Changes #2 and #3.
CONST YMATRIX
= 1 ' Depth of the matrix that forms each falling unit.
CONST BYTESPERBLOCK
= 76 ' Number of bytes required to store one block in Screen mode 7.
CONST BLOCKVOLUME
= (XMATRIX
+ 1) * (YMATRIX
+ 1) ' Number of blocks in each shape.
CONST ELEMENTSPERBLOCK
= BLOCKVOLUME
* BYTESPERBLOCK \
2 ' Number of INTEGER array elements needed to store an image of a shape.
CONST XSIZE
= 13 ' Width, in pixels, of each block. QBlocks assumes that the entire screen is 25 blocks wide. Since the screen is 320 pixels wide, each block is approximately 13 pixels wide.
CONST YSIZE
= 8 ' Height, in pixels, of each block. Again, QBlocks assumes that screen is 25 blocks high. At 200 pixels down, each block is exactly 8 pixels high.
CONST XOFFSET
= 10 ' X position, in blocks, of the well.
CONST YOFFSET
= 2 ' Y position, in blocks, of the well.
CONST WELLX
= XSIZE
* XOFFSET
' X position, in pixels, of the start of the well.
CONST WELLY
= YSIZE
* YOFFSET
' Y position.
CONST TILTVALUE
= 9999000 ' Points required for QBlocks to tilt.
CONST WELLCOLOR7
= 0 ' Well color for SCREEN 7.
CONST WELLCOLOR1
= 0 ' Well color for SCREEN 1.
CONST BORDERCOLOR1
= 8 ' Border color for SCREEN 1.
CONST BORDERCOLOR7
= 15 ' Border color for SCREEN 7.
TYPE BlockType
' Block datatype.
X
AS INTEGER ' Horizontal location within the well.
Y
AS INTEGER ' Vertical location within the well.
Style
AS INTEGER ' Define shape (and color, indirectly).
Rotation
AS INTEGER ' 4 possible values (0 to 3).
' SUB and FUNCTION declarations
' DIM SHARED indicates that a variable is available to all subprograms.
' Without this statement, a variable used in one subprogram cannot be
' used by another subprogram or the main program.
DIM SHARED WellBlocks
(WELLWIDTH
, WELLHEIGHT
) AS INTEGER ' 2 dimensional array to hold the falling shapes that have stopped falling and become part of the well.
DIM SHARED BlockShape
(0 TO XMATRIX
, 0 TO YMATRIX
, 1 TO NUMSTYLES
) ' Holds the data required to make each shape. Values determined by the DATA statements at the end of this window.
DIM SHARED PrevScore
AS LONG ' Holds the previous level for scoring purposes.
DIM SHARED OldBlock
AS BlockType
' An image of the last CurBlock. Used to erase falling units when they move.
DIM SHARED GameTiltScore
AS LONG ' Holds the value that this game will tilt at.
DIM SHARED Temp
(11175) AS INTEGER ' Used by several GET and PUT statements to store temporary screen images.
DIM SHARED BlockImage
((NUMSTYLES
* 4 + 3) * ELEMENTSPERBLOCK
) AS INTEGER ' Holds the binary image of each rotation of each shape for the PutBlock subprogram to use.
DIM KeyFlags
AS INTEGER ' Internal state of the keyboard flags when game starts. Hold the state so it can be restored when the games ends.
DIM BadMode
AS INTEGER ' Store the status of a valid screen mode.
ON ERROR GOTO ScreenError
' Set up a place to jump to if an error occurs in the program.
BadMode = FALSE
ScreenMode = 7
SCREEN ScreenMode
' Attempt to go into SCREEN 7 (EGA screen).
IF BadMode
= TRUE
THEN ' If this attempt failed.
ScreenMode = 1
BadMode = FALSE
SCREEN ScreenMode
' Attempt to go into SCREEN 1 (CGA screen).
IF BadMode
= TRUE
THEN ' If no graphics adapter.
LOCATE 10, 12:
PRINT "CGA, EGA Color, or VGA graphics required to run QBLOCKS.BAS"
RANDOMIZE TIMER ' Create a new sequence of random numbers based on the clock.
DisplayIntro ' Show the opening screen.
DEF SEG = 0 ' Set the current segment to the low memory area.
KeyFlags
= PEEK(1047) ' Read the location that holds the keyboard flag.
IF (KeyFlags
AND 32) = 0 THEN ' If the NUM LOCK key is off
POKE 1047, KeyFlays
OR 32 ' set the NUM LOCK key to on.
DEF SEG ' Restore the default segment.
' Read the pattern for each QBlocks shape.
FOR i
= 1 TO NUMSTYLES
' Loop for the each shape
FOR j
= 0 TO YMATRIX
' and for the Y and X dimensions of
FOR k
= 0 TO XMATRIX
' each shape.
READ BlockShape
(k
, j
, i
) ' Actually read the data.
DrawAllShapes ' Draw all shapes in all four rotations.
PerformGame ' Play the game until the player quits.
DisplayChanges ' Show the suggested changes.
DEF SEG = 0 ' Set the current segment back to low memory where the keyboard flags are.
POKE 1047, KeyFlags
AND 233 ' Set the NUM LOCK key back to where it was at the game start.
DEF SEG ' Restore the current segment back to BASIC's data group area.
IF ScreenMode
= 7 THEN PALETTE ' Restore the default color palette if SCREEN 7 was used.
END ' End of the main program code.
' The DATA statements below define the block shapes used in the game.
' Each shape contains 8 blocks (4 x 2). A "1" means that there
' is a block in that space; "0" means that the block is blank. The pattern
' for Style 1, for example, creates a shape that is 4 blocks wide.
' To change an existing block's shape, change a "0" to a "1" or a "1" to
' a "0". To add new shapes, insert new DATA statements with the same format
' as those below, after the last group of DATA statements (style 7). Be sure
' to change the NUMSTYLES constant at the beginning of this program to reflect
' the new number of block shapes for the game.
' IMPORTANT! Creating a completely blank block will cause QBlocks to fail.
' Data for Style 1: Long
' Data for Style 2: L Right
' Data for Style 3: L Left
' Data for Style 4: Z Right
' Data for Style 5: Z Left
' Data for Style 6: T
' Data for Style 7: Square
ScreenError: ' QBlocks uses this error handler to determine the highest available video mode.
BadMode = TRUE
'----------------------------------------------------------------------------
' AddBlockToWell
'
' After a shape stops falling, put it into the WellBlocks array
' so later falling shapes know where to stop.
'
' PARAMETERS: None.
'----------------------------------------------------------------------------
FOR i
= 0 TO XMATRIX
' Loop through all elements in the array.
IF BlockShape
(i
, j
, CurBlock.Style
) = 1 THEN ' If there is a block in that space.
SELECT CASE CurBlock.Rotation
' Use the Rotation to determine how the blocks should map into the WellBlocks array.
WellBlocks(CurBlock.X + i, CurBlock.Y + j) = CurBlock.Style
CASE 1 ' Rotated 90 degrees clockwise.
WellBlocks(CurBlock.X - j + 2, CurBlock.Y + i - 1) = CurBlock.Style
CASE 2 ' Rotated 180 degrees.
WellBlocks(CurBlock.X - i + 3, CurBlock.Y - j + 1) = CurBlock.Style
CASE 3 ' Rotated 270 degrees clockwise.
WellBlocks(CurBlock.X + j + 1, CurBlock.Y - i + 2) = CurBlock.Style
'----------------------------------------------------------------------------
' Center
'
' Centers a string of text on a specified row.
'
' PARAMETERS: Text$ - Text to display on the screen.
' Row - Row on the screen where the text$ is
' displayed.
'----------------------------------------------------------------------------
LOCATE Row
, (ScreenWidth
- LEN(text$
)) \
2 + 1
'----------------------------------------------------------------------------
' CheckFit
'
' Checks to see if the shape will fit into its new position.
' Returns TRUE if it fits and FALSE if it does not fit.
'
' PARAMETERS: None
'
'----------------------------------------------------------------------------
CheckFit = TRUE ' Assume the shape will fit.
FOR i
= 0 TO XMATRIX
' Loop through all the blocks in the
FOR j
= 0 TO YMATRIX
' shape and see if any would
' overlap blocks already in the well.
IF BlockShape
(i
, j
, CurBlock.Style
) = 1 THEN ' 1 means that space, within the falling shape, is filled with a block.
SELECT CASE CurBlock.Rotation
' Base the check on the rotation of the shape.
NewX = CurBlock.X + i
NewY = CurBlock.Y + j
CASE 1 ' Rotated 90 degrees clockwise, or 270 degrees counterclockwise.
NewX = CurBlock.X - j + 2
NewY = CurBlock.Y + i - 1
CASE 2 ' Rotated 180 degrees.
NewX = CurBlock.X - i + 3
NewY = CurBlock.Y - j + 1
CASE 3 ' Rotated 270 degrees clockwise, or 90 degrees counterclockwise.
NewX = CurBlock.X + j + 1
NewY = CurBlock.Y - i + 2
' Set CheckFit to false if the block would be out of the well.
IF (NewX
> WELLWIDTH
- 1 OR NewX
< 0 OR NewY
> WELLHEIGHT
- 1 OR NewY
< 0) THEN
CheckFit = FALSE
' Otherwise, set CheckFit to false if the block overlaps
' an existing block.
CheckFit = FALSE
'----------------------------------------------------------------------------
' CheckForFullRows
'
' Checks for filled rows. If a row is filled, delete it and move
' the blocks above down to fill the deleted row.
'
' PARAMETERS: None
'----------------------------------------------------------------------------
DIM RowsToDelete
(WELLHEIGHT
) ' Temporary array to track rows that should be deleted.
NumRowsToDelete = 0
i = WELLHEIGHT ' Begin scanning from the bottom up.
DeleteRow = TRUE ' Assume the row should be deleted.
j = 0
DO ' Scan within each row for blocks.
DeleteRow
= DeleteRow
* SGN(WellBlocks
(j
, i
)) ' If any position is blank, DeleteRow is 0 (FALSE).
j = j + 1
' Walk up the rows and copy them down in the WellBlocks array.
NumRowsToDelete = NumRowsToDelete + 1 ' Number of rows to delete.
RowsToDelete(i - NumDeleted) = TRUE ' Mark the rows to be deleted, compensating for rows that have already been deleted below it.
NumDeleted = NumDeleted + 1 ' Compensates for rows that have been deleted already.
' Logically delete the row by moving all WellBlocks values down.
WellBlocks(Col, Row) = WellBlocks(Col, Row - 1)
ELSE ' This row will not be deleted.
i = i - 1
LOOP WHILE i
>= 1 ' Stop looping when the top of the well is reached.
Score = Score + 100 * NumRowsToDelete ' Give 100 points for every row.
' Set Highest and Lowest such that any deleted row will initially set them.
Highest = -1
Lowest = 100
' Find where the highest and lowest rows to delete are.
IF RowsToDelete
(i
) = TRUE
THEN
IF i
> Highest
THEN Highest
= i
IF (Highest
- Lowest
) + 1 = NumRowsToDelete
THEN ' Only one contiguous group of rows to delete.
DeleteChunk Highest, Lowest
ELSE ' Two groups of rows to delete.
' Begin at Lowest and scan down for a row NOT to be deleted.
' Then delete everything from Lowest to the row not to be deleted.
i = Lowest
IF RowsToDelete
(i
) = FALSE
THEN
DeleteChunk i - 1, Lowest
i = i + 1
' Now look for the second group and delete those rows.
Lowest = i
DO WHILE RowsToDelete
(Lowest
) = FALSE
Lowest = Lowest + 1
DeleteChunk Highest, Lowest
'----------------------------------------------------------------------------
' DeleteChunk
'
' Deletes a group of one or more rows.
'
' PARAMETERS: Highest - Highest row to delete (physically lowest
' on screen).
' Lowest - Lowest row to delete (physically highest
' on screen).
'----------------------------------------------------------------------------
SUB DeleteChunk
(Highest
, Lowest
)
' GET the image of the row to delete.
GET (WELLX
, Lowest
* YSIZE
+ WELLY
)-(WELLX
+ WELLWIDTH
* XSIZE
, (Highest
+ 1) * YSIZE
+ WELLY
- 1), Temp
()
' Flash the rows 3 times.
PUT (WELLX
, Lowest
* YSIZE
+ WELLY
), Temp
(), PRESET
PUT (WELLX
, Lowest
* YSIZE
+ WELLY
), Temp
(), PSET
' Move all the rows above the deleted ones down.
GET (WELLX
, WELLY
)-(WELLX
+ WELLWIDTH
* XSIZE
, Lowest
* YSIZE
+ WELLY
), Temp
()
PUT (WELLX
, (Highest
- Lowest
+ 1) * YSIZE
+ WELLY
), Temp
(), PSET
'Erase the area above the block which just moved down.
LINE (WELLX
, WELLY
)-(WELLX
+ WELLWIDTH
* XSIZE
, WELLY
+ (Highest
- Lowest
+ 1) * YSIZE
), WellColor
, BF
'----------------------------------------------------------------------------
' DisplayChanges
'
' Displays list of changes that the player can easily make.
'
' PARAMETERS: None
'----------------------------------------------------------------------------
DisplayGameTitle ' Print game title.
Center "The following game characteristics can be easily changed from", 5
Center "within the QuickBASIC Interpreter. To change the values of ", 6
Center "these characteristics, locate the corresponding CONST or DATA", 7
Center "statements in the source code and change their values, then ", 8
Center "restart the program (press Shift + F5). ", 9
Center "Block shapes ", 11
Center "Block rotation ", 12
Center "Number of different block shapes ", 13
Center "Score needed to advance to next level", 14
Center "Width of the game well ", 15
Center "Height of the game well ", 16
Center "Songs played during game ", 17
Center "The CONST statements and instructions on changing them are ", 19
Center "located at the beginning of the main program. ", 20
'----------------------------------------------------------------------------
' DisplayGameTitle
'
' Displays title of the game.
'
' PARAMETERS: None.
'----------------------------------------------------------------------------
WIDTH 80, 25 ' Set width to 80, height to 25.
COLOR 4, 0 ' Set colors for red on black.
ScreenWidth = 80 ' Set screen width variable to match current width.
' Draw outline around screen with extended ASCII characters.
'Print game title centered at top of screen
Center " Microsoft ", 1
Center " Q B L O C K S ", 2
Center " Press any key to continue ", 25 ' Center prompt on line 25.
'----------------------------------------------------------------------------
' DisplayIntro
'
' Explains the object of the game and how to play.
'
' PARAMETERS: None
'----------------------------------------------------------------------------
DisplayGameTitle
Center "QBlocks challenges you to keep the well from filling. Do this by", 5
Center "completely filling rows with blocks, making the rows disappear. ", 6
Center "Move and rotate the falling shapes to get them into the best ", 7
Center "position. The game will get faster as you score more points. ", 8
COLOR 4 ' Change foreground color for line to red.
Center
STRING$(74, 196), 11 ' Put horizontal red line on screen.
COLOR 7 ' White (7) letters. ' Change foreground color back to white
Center " Game Controls ", 11 ' Display game controls.
Center " General Block Control ", 13
Center " (Rotate)", 15
Center
" P - Pause " + CHR$(24) + " (or 5) ", 16
Center
" Q - Quit (Left) " + CHR$(27) + " " + CHR$(26) + " (Right) ", 17
Center
" " + CHR$(25), 18
Center " (Drop) ", 19
DO ' Wait for any key to be pressed.
IF kbd$
= "Q" THEN 'Allow player to quit now
'----------------------------------------------------------------------------
' DrawAllShapes
'
' Quickly draws all shapes in all four rotations. Uses GET
' to store the images so they can be PUT onto the screen
' later very quickly.
'
' PARAMETERS: None.
'----------------------------------------------------------------------------
SCREEN ScreenMode
' Set the appropriate screen mode.
' On EGA and VGA systems, appear to blank the screen.
DIM Colors
(0 TO 15) ' DIM an array of 16 elements. By default, all elements are 0.
FOR i
= 1 TO NUMSTYLES
' Set block colors EGA, VGA
BlockColor
(i
) = ((i
- 1) MOD 7) + 1
FOR i
= 1 TO NUMSTYLES
'Set block colors for CGA
BlockColor
(i
) = ((i
- 1) MOD 3) + 1
Count = 0 ' Count determines how many shapes have been drawn on the screen and vertically where.
FOR shape
= 1 TO NUMSTYLES
' Loop through all shapes.
RtSide = 4
IF BlockShape
(RtSide
- 1, 0, shape
) = 1 OR BlockShape
(RtSide
- 1, 1, shape
) = 1 THEN EXIT DO
RtSide = RtSide - 1
LtSide = 0
IF BlockShape
(LtSide
, 0, shape
) = 1 OR BlockShape
(LtSide
, 1, shape
) = 1 THEN EXIT DO
LtSide = LtSide + 1
FOR Rotation
= 0 TO 3 ' Loop through all rotations.
b.X = Rotation * 4 + 2 ' Determine where to put the shape.
b.Y = Count + 2
b.Rotation = Rotation
b.Style = shape
Show b ' Draw the shape.
X = b.X: Y = b.Y
SELECT CASE Rotation
' Based on Rotation, determine where the shape really is on the screen.
x1 = X: x2 = X + RtSide: y1 = Y: y2 = Y + 2
CASE 1 ' Rotated 90 degrees clockwise.
x1 = X + 1: x2 = X + 3: y1 = Y - 1: y2 = Y + RtSide - 1
x1 = X: x2 = X + 4 - LtSide: y1 = Y: y2 = Y + 2
CASE 3 ' Rotated 270 degrees clockwise.
x1 = X + 1: x2 = X + 3: y1 = Y - 1: y2 = Y + 3 - LtSide
' Store the image of the rotated shape into an array for fast recall later.
GET (x1
* XSIZE
, y1
* YSIZE
)-(x2
* XSIZE
, y2
* YSIZE
), BlockImage
(((shape
- 1) * 4 + Rotation
) * ELEMENTSPERBLOCK
)
Count = Count + 5 ' Increase Count by 5 to leave at least one blank line between shapes.
IF Count
= 20 THEN ' No space for any more shapes.
Count = 0
' Changes the color palette if SCREEN is used.
PALETTE ' Restore default color settings.
PALETTE 6, 14 ' Make brown (6) look like yellow (14).
PALETTE 14, 15 ' Make yellow (14) look like bright white (15).
'----------------------------------------------------------------------------
' DrawBlock
'
' Draws one block of a QBlocks shape.
'
' PARAMETERS: X - Horizontal screen location.
' Y - Vertical screen location.
' FillColor - The primary color of the block.
' The top and left edges will be the
' brighter shade of that color.
'----------------------------------------------------------------------------
SUB DrawBlock
(X
, Y
, FillColor
)
LINE (X
* XSIZE
+ 2, Y
* YSIZE
+ 2)-((X
+ 1) * XSIZE
- 2, (Y
+ 1) * YSIZE
- 2), FillColor
, BF
LINE (X
* XSIZE
+ 1, Y
* YSIZE
+ 1)-((X
+ 1) * XSIZE
- 1, Y
* YSIZE
+ 1), FillColor
+ 8
LINE (X
* XSIZE
+ 1, Y
* YSIZE
+ 1)-(X
* XSIZE
+ 1, (Y
+ 1) * YSIZE
- 1), FillColor
+ 8
'----------------------------------------------------------------------------
' DrawPattern
'
' Draws a background pattern that is 32 pixels wide by 20 pixels
' deep. Gets the pattern and duplicates it to fill the screen.
'
' PARAMETERS: Pattern - Which of the 10 available patterns to
' draw.
'----------------------------------------------------------------------------
SUB DrawPattern
(Pattern
)
X = 1: Y = 1
DIM Temp2
(215) AS INTEGER ' Create an array to store the image.
' Draw the pattern specified.
j = Y + 21
j = j - 2
LINE (i
, j
)-(i
, Y
+ 19), 12, BF
LINE (X
, Y
)-(X
+ 30, Y
+ 19), 4, B
LINE (X
+ 1, Y
+ 1)-(X
+ 31, Y
+ 18), 4, B
LINE (X
, Y
)-(X
+ 8, Y
+ 12), 1, BF
LINE (X
+ 9, Y
+ 8)-(X
+ 24, Y
+ 20), 2, BF
LINE (X
+ 25, Y
)-(X
+ 32, Y
+ 12), 3, BF
LINE (X
, Y
)-(X
+ 29, Y
+ 18), X
/ 32 + 1, B
LINE (X
+ 1, Y
+ 1)-(X
+ 28, Y
+ 17), X
/ 32 + 2, B
LINE (X
+ i
, Y
+ i
)-(X
+ 29 - i
, Y
+ 18 - i
), i
, B
j = 0
LINE (X
+ i
, Y
+ j
)-(X
+ 30 - i
, Y
+ j
), i
LINE (X
+ i
, Y
+ 19 - j
)-(X
+ 30 - i
, Y
+ 19 - j
), i
j = j + 2
LINE (X
, Y
)-(X
+ 29, Y
+ 4), 1, BF
LINE (X
, Y
)-(X
+ 4, Y
+ 18), 1, BF
LINE (X
+ 7, Y
+ 7)-(X
+ 29, Y
+ 11), 5, BF
LINE (X
+ 7, Y
+ 7)-(X
+ 11, Y
+ 18), 5, BF
LINE (X
+ 14, Y
+ 14)-(X
+ 29, Y
+ 18), 4, BF
LINE (X
+ 15, Y
)-(X
+ 17, Y
+ 19), 1
LINE (X
, Y
+ 9)-(X
+ 31, Y
+ 11), 2
LINE (X
, Y
+ 1)-(X
+ 31, Y
+ 18), 9
LINE (X
+ 30, Y
)-(X
+ 1, Y
+ 19), 10
CIRCLE (i
, Y
+ 9), 10, Y
/ 20 + 1
LINE (X
+ 1, Y
)-(X
+ 1, Y
+ 18), 3
LINE (X
+ 1, Y
)-(X
+ 12, Y
+ 18), 3
LINE (X
+ 1, Y
+ 18)-(X
+ 12, Y
+ 18), 3
LINE (X
+ 30, Y
)-(X
+ 30, Y
+ 18), 3
LINE (X
+ 30, Y
)-(X
+ 19, Y
+ 18), 3
LINE (X
+ 30, Y
+ 18)-(X
+ 19, Y
+ 18), 3
LINE (X
+ 4, Y
)-(X
+ 26, Y
), 1
LINE (X
+ 4, Y
)-(X
+ 15, Y
+ 18), 1
LINE (X
+ 26, Y
)-(X
+ 15, Y
+ 18), 1
GET (0, 0)-(31, 19), Temp2
() ' GET the image.
' Duplicate the image 10 times across by 10 times down.
'----------------------------------------------------------------------------
' DrawPlayingField
'
' Draws the playing field, including the well, the title, the
' score/level box, etc.
'
' PARAMETERS: None
'----------------------------------------------------------------------------
SELECT CASE ScreenMode
' Choose the screen colors based on the current mode.
WellColor = WELLCOLOR7
BorderColor = BORDERCOLOR7
WellColor = WELLCOLOR1
BorderColor = BORDERCOLOR1
ScreenWidth = 40 ' Set to proper width and colors.
' Draw the background pattern.
DrawPattern Level
' Draw the well box.
LINE (WELLX
- 1, WELLY
- 5)-(WELLX
+ WELLWIDTH
* XSIZE
+ 1, WELLY
+ WELLHEIGHT
* YSIZE
+ 1), WellColor
, BF
LINE (WELLX
- 1, WELLY
- 5)-(WELLX
+ WELLWIDTH
* XSIZE
+ 1, WELLY
+ WELLHEIGHT
* YSIZE
+ 1), BorderColor
, B
' Draw the title box.
LINE (XSIZE
, WELLY
- 5)-(XSIZE
* 8, WELLY
+ 12), WellColor
, BF
LINE (XSIZE
, WELLY
- 5)-(XSIZE
* 8, WELLY
+ 12), BorderColor
, B
' Draw the scoring box.
LINE (XSIZE
, WELLY
+ 20)-(WELLX
- 2 * XSIZE
, 78), WellColor
, BF
LINE (XSIZE
, WELLY
+ 20)-(WELLX
- 2 * XSIZE
, 78), BorderColor
, B
MakeInfoBox ' Draw the Information Box.
LOCATE 3, 5:
PRINT "QBLOCKS" ' Center the program name on line 2.
' Draw the scoring area.
'----------------------------------------------------------------------------
' GameOver
'
' Ends the game and asks the player if he/she wants to play
' again. GameOver returns TRUE if the player wishes to stop
' or FALSE if the player wants another game.
'
' PARAMETERS: None
'----------------------------------------------------------------------------
PLAY PLAYGAMEOVER
' Play the game over tune.
MakeInfoBox
' Put Game Over messages into the InfoBox.
' Wait for the player to press either Y or N.
a$
= UCASE$(INKEY$) ' UCASE$ assures that the key will be uppercase. This eliminates the need to check for y and n in addition to Y and N.
IF a$
= "Y" THEN ' If player selects "Y",
GameOver = FALSE ' game is not over,
GameOver = TRUE ' the game is over.
'----------------------------------------------------------------------------
' InitScreen
'
' Draws the playing field and ask for the desired starting level.
'
' PARAMETERS: None
'----------------------------------------------------------------------------
DrawPlayingField ' Draw playing field assuming Level 0.
' Prompt for starting level.
COLOR 12 ' Change foreground color to bright red.
COLOR BorderColor
' Restore the default text color to BorderColor (white).
Level = TRUE ' Use level as flag as well as a real value. Level remain TRUE if Q (Quit) is pressed instead of a level.
' Get a value for Level or accept a Q.
IF Level
> 0 THEN DrawPlayingField
' Draw new playing field because the background pattern depends on the level.
RedrawControls ' Draw the controls.
'----------------------------------------------------------------------------
' MakeInfoBox
'
' Draws the information box.
'
' PARAMETERS: None
'----------------------------------------------------------------------------
LINE (WELLX
- 9 * XSIZE
, 90)-(WELLX
- 2 * XSIZE
, 185), WellColor
, BF
' Clear the Info area.
LINE (WELLX
- 9 * XSIZE
, 90)-(WELLX
- 2 * XSIZE
, 185), BorderColor
, B
' Draw a border around it.
'----------------------------------------------------------------------------
' NewBlock
'
' Initializes a new falling shape about to enter the well.
'
' PARAMETERS: None
'----------------------------------------------------------------------------
CurBlock.Style
= INT(RND(1) * NUMSTYLES
) + 1 ' Randomly pick a block style.
CurBlock.X = (WELLWIDTH \ 2) - 1 ' Put the new shape in the horizontal middle of the well
CurBlock.Y = 0 ' and at the top of the well.
CurBlock.Rotation = 0 ' Begin with no rotation.
'----------------------------------------------------------------------------
' PerformGame
'
' Continues to play the game until the player quits.
'
' PARAMETERS: None
'----------------------------------------------------------------------------
DO ' Loop for repetitive games
a$ = ""
ERASE WellBlocks
' Set all the elements in the WellBlocks array to 0.
Score = 0 ' Clear initial score.
Level = 0 ' Assume Level 0.
PrevScore = BASESCORE - NEXTLEVEL ' Set score needed to get to first level
GameTiltScore = WINGAME ' Set the initial win game value.
InitScreen ' Prepare the screen and get the difficulty level.
IF Level
= -1 THEN EXIT SUB ' Player pressed Q instead of a level.
TargetTime
= TIMER + 1 / (Level
+ 1) ' TargetTime is when the falling shape will move down again.
DO ' Create new falling shapes until the game is over.
DoneWithThisBlock = FALSE ' This falling shape is not done falling yet.
NewBlock ' Create a new falling unit.
IF CheckFit
= FALSE
THEN EXIT DO ' If it does not fit, then the game is over.
PutBlock CurBlock ' Display the new shape.
DO ' Continue dropping the falling shape.
OldBlock = CurBlock ' Save current falling shape for possible later use.
DO ' Loop until enough time elapses.
ValidEvent = TRUE ' Assume a key was pressed.
MakeInfoBox
' SELECT CASE will do different actions based on the
' value of the SELECTED variable.
' Play sounds to tell the player that Q was pressed.
' Confirm that the player really wants to quit.
RedrawControls ' Redraw controls if either Q or P is pressed.
ELSE ' A key was pressed but not Q or P.
ans
= ASC(RIGHT$(CHR$(0) + ans$
, 1)) ' Convert the key press to an ASCII code for faster processing.
CASE DOWNARROW
, DOWNARROW2
, SPACEBAR
' Drop shape immediately.
DO ' Loop to drop the falling unit one row at a time.
CurBlock.Y = CurBlock.Y + 1
LOOP WHILE CheckFit
= TRUE
' Keep looping while the falling unit isn't stopped.
CurBlock.Y = CurBlock.Y - 1 ' Went one down too far, restore to previous.
TargetTime
= TIMER - 1 ' Ensure that the shape falls immediately.
CASE RIGHTARROW
, RIGHTARROW2
CurBlock.X = CurBlock.X + 1 ' Move falling unit right.
CASE LEFTARROW
, LEFTARROW2
CurBlock.X = CurBlock.X - 1 ' Move falling unit left.
CASE UPARROW
, UPARROW2
, UPARROW3
CurBlock.Rotation
= ((CurBlock.Rotation
+ ROTATEDIR
) MOD 4) ' Rotate falling unit.
ValidEvent = FALSE
IF CheckFit
= TRUE
THEN ' If the move is valid and the shape fits in the new position,
PutBlock OldBlock ' erase the shape from its old position
PutBlock CurBlock ' and display it in the new position.
OldBlock = CurBlock
CurBlock = OldBlock ' If it does not fit then reset CurBlock to the OldBlock.
LOOP UNTIL TIMER >= TargetTime
' Keep repeating the loop until it is time to drop the shape. This allows many horizontal movements and rotations per vertical step.
TargetTime
= TIMER + 1 / (Level
+ 1) ' The player has less time between vertical movements as the skill level increases.
CurBlock.Y = CurBlock.Y + 1 ' Try to drop the falling unit one row.
IF CheckFit
= FALSE
THEN ' Cannot fall any more.
DoneWithThisBlock = TRUE ' Done with this block.
CurBlock = OldBlock
PutBlock OldBlock ' Erase the falling shape from the old position,
PutBlock CurBlock ' and display it in the new position.
OldBlock = CurBlock
LOOP UNTIL DoneWithThisBlock
' Continue getting keys and moving shapes until the falling shape stops.
AddBlockToWell ' Shape has stopped so logically add it to the well.
CheckForFullRows ' Check to see if a row(s) is now full. If so, deletes it.
UpdateScoring ' Use the UpdateScoring subprogram to add to the score.
IF Score
>= GameTiltScore
THEN ' See if the score has hit the tilt score.
MakeInfoBox
IF GameTiltScore
= TILTVALUE
THEN ' If the player has tilted the game.
ELSE ' If they just met the WINGAME value.
DO ' DO loop to wait for the player to press anything.
a$
= UCASE$(INKEY$) ' The UCASE$ function assures that a$ always has an uppercase letter in it.
IF a$
<> "Y" THEN EXIT DO ' Exit this main loop if the player pressed anything but Y.
GameTiltScore = TILTVALUE ' Reset to the tilt value.
RedrawControls
LOOP ' Unconditional loop. Each game is stopped by the EXIT DO command at the top of this loop that executes when a new block will not fit in the well.
LOOP UNTIL GameOver
' GameOver is always TRUE (-1) unless the user presses X or the well is full.
'----------------------------------------------------------------------------
' PutBlock
'
' Uses very fast graphics PUT command to draw the shape.
'
' PARAMETERS: B - Block to be put onto the screen.
'----------------------------------------------------------------------------
SUB PutBlock
(b
AS BlockType
)
SELECT CASE b.Rotation
' Base exact placement on the rotation.
x1 = b.X: y1 = b.Y
CASE 1 ' Rotated 90 degrees clockwise, or 270 degrees counterclockwise.
x1 = b.X + 1: y1 = b.Y - 1
CASE 2 ' Rotated 180 degrees.
x1 = b.X: y1 = b.Y
CASE 3 ' Rotated 270 degrees clockwise, or 90 degrees counterclockwise.
x1 = b.X + 1: y1 = b.Y - 1
' Actually PUT the rotated shape on the screen. The XOR option makes the
' new image blend with whatever used to be there in such a way that
' identical colors cancel each other out. Therefore, one PUT with the XOR
' option can draw an object while the second PUT to that same location
' erases it without affecting anything else near it. Often used for animation.
PUT (x1
* XSIZE
+ WELLX
, y1
* YSIZE
+ WELLY
), BlockImage
(((b.Style
- 1) * 4 + b.Rotation
) * ELEMENTSPERBLOCK
), XOR ' XOR mixes what used to be there on the screen with the new image. Two identical colors cancel each other.
'----------------------------------------------------------------------------
' RedrawControls
'
' Puts control keys information into the information box.
'
' PARAMETERS: None
'----------------------------------------------------------------------------
' Draw the InfoBox and erase anything that used to be in it.
MakeInfoBox
' Print the key assignments within the Info Box.
'----------------------------------------------------------------------------
' Show
'
' Draws the falling shape one block at a time. Only used by
' DisplayAllShapes. After that, PutBlock draws all falling
' shapes.
'
' PARAMETERS: B - Block to be put onto the screen.
'----------------------------------------------------------------------------
SUB Show
(b
AS BlockType
)
' Loop through all possible block locations.
IF BlockShape
(i
, j
, b.Style
) = 1 THEN ' 1 means there is a block there.
SELECT CASE b.Rotation
' Exact screen position is determined by the rotation.
DrawBlock b.X + i, b.Y + j, BlockColor(b.Style)
CASE 1 ' Rotated 90 degrees clockwise, or 270 degrees counterclockwise.
DrawBlock b.X - j + 2, b.Y - 1 + i, BlockColor(b.Style)
CASE 2 ' Rotated 180 degrees.
DrawBlock b.X + 3 - i, b.Y - j + 1, BlockColor(b.Style)
CASE 3 ' Rotated 270 degrees clockwise, or 90 degrees counterclockwise.
DrawBlock b.X + j + 1, b.Y - i + 2, BlockColor(b.Style)
'---------------------------------------------------------------------------
' UpdateScoring
'
' Puts the new score on the screen. Checks if the new score forces
' a new level. If so, change the background pattern to match the
' new level.
'
' PARAMETERS: None
'----------------------------------------------------------------------------
' Increase the level if the score is high enough and the level is not
' maximum already.
IF Level
< 9 AND Score
>= (NEXTLEVEL
* (Level
+ 1) + PrevScore
) THEN
' Store the entire well image to quickly PUT it back after the
' background changes.
GET (WELLX
, WELLY
)-(WELLX
+ WELLWIDTH
* XSIZE
, WELLY
+ WELLHEIGHT
* YSIZE
), Temp
()
PrevScore = Score ' Save previous Score for next level.
Level = Level + 1
DrawPlayingField ' Draw playing field again, this time with the new background pattern.
PUT (WELLX
, WELLY
), Temp
() ' Restore the image of the old well.
RedrawControls ' Show the controls again.