用qbasic编写俄罗斯方块640*240黑白
时间: 2023-11-29 15:03:43 浏览: 124
以下是用 QBASIC 编写的俄罗斯方块游戏,尺寸为 640x240,黑白颜色:
```
SCREEN 12, , 0, 1
' 定义常量
CONST BLOCK_SIZE = 20
CONST BOARD_LEFT = 80
CONST BOARD_TOP = 20
CONST BOARD_RIGHT = 380
CONST BOARD_BOTTOM = 220
CONST BOARD_WIDTH = BOARD_RIGHT - BOARD_LEFT
CONST BOARD_HEIGHT = BOARD_BOTTOM - BOARD_TOP
' 定义变量
DIM Block(7, 4, 4) AS INTEGER
DIM Board(BOARD_WIDTH \ BLOCK_SIZE + 1, BOARD_HEIGHT \ BLOCK_SIZE + 1) AS INTEGER
DIM x AS INTEGER, y AS INTEGER, c AS INTEGER
DIM cx AS INTEGER, cy AS INTEGER, ci AS INTEGER, cj AS INTEGER
DIM BlockType AS INTEGER, BlockRotation AS INTEGER
DIM GameOver AS INTEGER, Score AS INTEGER
' 初始化方块数据
Block(0,0,0) = 1: Block(0,1,0) = 1: Block(0,2,0) = 1: Block(0,3,0) = 1
Block(1,0,0) = 1: Block(1,1,0) = 1: Block(1,2,0) = 0: Block(1,3,0) = 0
Block(1,0,1) = 0: Block(1,1,1) = 1: Block(1,2,1) = 1: Block(1,3,1) = 0
Block(2,0,0) = 0: Block(2,1,0) = 1: Block(2,2,0) = 1: Block(2,3,0) = 1
Block(2,0,1) = 1: Block(2,1,1) = 1: Block(2,2,1) = 0: Block(2,3,1) = 0
Block(3,0,0) = 1: Block(3,1,0) = 1: Block(3,2,0) = 1: Block(3,3,0) = 0
Block(3,0,1) = 0: Block(3,1,1) = 0: Block(3,2,1) = 1: Block(3,3,1) = 1
Block(4,0,0) = 1: Block(4,1,0) = 1: Block(4,2,0) = 0: Block(4,3,0) = 0
Block(4,0,1) = 0: Block(4,1,1) = 1: Block(4,2,1) = 1: Block(4,3,1) = 1
Block(5,0,0) = 0: Block(5,1,0) = 1: Block(5,2,0) = 0: Block(5,3,0) = 0
Block(5,0,1) = 1: Block(5,1,1) = 1: Block(5,2,1) = 1: Block(5,3,1) = 0
Block(6,0,0) = 0: Block(6,1,0) = 1: Block(6,2,0) = 1: Block(6,3,0) = 0
Block(6,0,1) = 0: Block(6,1,1) = 1: Block(6,2,1) = 1: Block(6,3,1) = 0
' 主循环
DO
' 初始化游戏状态
GameOver = 0
Score = 0
FOR i = 0 TO BOARD_WIDTH \ BLOCK_SIZE
FOR j = 0 TO BOARD_HEIGHT \ BLOCK_SIZE
Board(i, j) = 0
NEXT j
NEXT i
' 随机选择方块
BlockType = INT(RND * 7)
BlockRotation = INT(RND * 4)
cx = 0: cy = 0
' 游戏循环
DO
' 绘制游戏界面
SCREENLOCK
CLS
LINE (BOARD_LEFT, BOARD_TOP)-(BOARD_RIGHT, BOARD_BOTTOM)
FOR i = 0 TO BOARD_WIDTH \ BLOCK_SIZE
FOR j = 0 TO BOARD_HEIGHT \ BLOCK_SIZE
x = BOARD_LEFT + i * BLOCK_SIZE
y = BOARD_TOP + j * BLOCK_SIZE
IF Board(i, j) THEN
CIRCLE (x + BLOCK_SIZE / 2, y + BLOCK_SIZE / 2), BLOCK_SIZE / 2 - 1, 1, , , FILL
ELSE
LINE (x, y)-(x + BLOCK_SIZE - 1, y)
LINE (x, y)-(x, y + BLOCK_SIZE - 1)
LINE (x + BLOCK_SIZE - 1, y + BLOCK_SIZE - 1)-(x, y + BLOCK_SIZE - 1)
LINE (x + BLOCK_SIZE - 1, y + BLOCK_SIZE - 1)-(x + BLOCK_SIZE - 1, y)
END IF
NEXT j
NEXT i
FOR i = 0 TO 3
FOR j = 0 TO 3
IF Block(BlockType, i, j) THEN
x = BOARD_LEFT + (cx + i) * BLOCK_SIZE
y = BOARD_TOP + (cy + j) * BLOCK_SIZE
CIRCLE (x + BLOCK_SIZE / 2, y + BLOCK_SIZE / 2), BLOCK_SIZE / 2 - 1, 1, , , FILL
END IF
NEXT j
NEXT i
LINE (BOARD_LEFT - 10, BOARD_TOP + 50)-(BOARD_LEFT - 10, BOARD_TOP + 70)
LINE (BOARD_LEFT - 10, BOARD_TOP + 50)-(BOARD_LEFT - 20, BOARD_TOP + 60)
LINE (BOARD_LEFT - 10, BOARD_TOP + 50)-(BOARD_LEFT, BOARD_TOP + 60)
LINE (BOARD_LEFT - 10, BOARD_TOP + 70)-(BOARD_LEFT - 20, BOARD_TOP + 60)
LINE (BOARD_LEFT - 10, BOARD_TOP + 70)-(BOARD_LEFT, BOARD_TOP + 60)
PRINT "SCORE: " & Score
SCREENUNLOCK
' 等待下一帧
SLEEP 50
' 处理方块移动和旋转
IF KEY(75) THEN cx = cx - 1 ' 左移
IF KEY(77) THEN cx = cx + 1 ' 右移
IF KEY(72) THEN BlockRotation = (BlockRotation + 1) MOD 4 ' 顺时针旋转
IF KEY(80) THEN BlockRotation = (BlockRotation + 3) MOD 4 ' 逆时针旋转
' 处理方块下落
IF cy <= BOARD_HEIGHT \ BLOCK_SIZE - 4 THEN
FOR i = 0 TO 3
FOR j = 0 TO 3
IF Block(BlockType, i, j) THEN
ci = cx + i
cj = cy + j + 1
IF cj <= BOARD_HEIGHT \ BLOCK_SIZE AND Board(ci, cj) = 0 THEN
c = c + 1
END IF
END IF
NEXT j
NEXT i
IF c = 4 THEN
cy = cy + 1
ELSE
FOR i = 0 TO 3
FOR j = 0 TO 3
IF Block(BlockType, i, j) THEN
ci = cx + i
cj = cy + j
Board(ci, cj) = 1
END IF
NEXT j
NEXT i
EXIT DO
END IF
ELSE
GameOver = 1
EXIT DO
END IF
' 清空计数器
c = 0
' 处理消除行
FOR j = BOARD_HEIGHT \ BLOCK_SIZE DOWNTO 0
FOR i = 0 TO BOARD_WIDTH \ BLOCK_SIZE
IF Board(i, j) = 0 THEN EXIT FOR
NEXT i
IF i > BOARD_WIDTH \ BLOCK_SIZE THEN
Score = Score + 1
FOR k = j TO 1 STEP -1
FOR i = 0 TO BOARD_WIDTH \ BLOCK_SIZE
Board(i, k) = Board(i, k - 1)
NEXT i
NEXT k
FOR i = 0 TO BOARD_WIDTH \ BLOCK_SIZE
Board(i, 0) = 0
NEXT i
j = j + 1
END IF
NEXT j
LOOP
' 显示游戏结束
SCREENLOCK
CLS
LINE (BOARD_LEFT, BOARD_TOP)-(BOARD_RIGHT, BOARD_BOTTOM)
FOR i = 0 TO BOARD_WIDTH \ BLOCK_SIZE
FOR j = 0 TO BOARD_HEIGHT \ BLOCK_SIZE
x = BOARD_LEFT + i * BLOCK_SIZE
y = BOARD_TOP + j * BLOCK_SIZE
IF Board(i, j) THEN
CIRCLE (x + BLOCK_SIZE / 2, y + BLOCK_SIZE / 2), BLOCK_SIZE / 2 - 1, 1, , , FILL
ELSE
LINE (x, y)-(x + BLOCK_SIZE - 1, y)
LINE (x, y)-(x, y + BLOCK_SIZE - 1)
LINE (x + BLOCK_SIZE - 1, y + BLOCK_SIZE - 1)-(x, y + BLOCK_SIZE - 1)
LINE (x + BLOCK_SIZE - 1, y + BLOCK_SIZE - 1)-(x + BLOCK_SIZE - 1, y)
END IF
NEXT j
NEXT i
LINE (BOARD_LEFT - 10, BOARD_TOP + 50)-(BOARD_LEFT - 10, BOARD_TOP + 70)
LINE (BOARD_LEFT - 10, BOARD_TOP + 50)-(BOARD_LEFT - 20, BOARD_TOP + 60)
LINE (BOARD_LEFT - 10, BOARD_TOP + 50)-(BOARD_LEFT, BOARD_TOP + 60)
LINE (BOARD_LEFT - 10, BOARD_TOP + 70)-(BOARD_LEFT - 20, BOARD_TOP + 60)
LINE (BOARD_LEFT - 10, BOARD_TOP + 70)-(BOARD_LEFT, BOARD_TOP + 60)
PRINT "GAME OVER"
PRINT "SCORE: " & Score
SCREENUNLOCK
' 等待重新开始
DO UNTIL INKEY$ <> ""
LOOP
```
此代码可能需要在 DOSBox 中运行。
阅读全文