俄罗斯方块python代码
时间: 2023-07-23 12:26:29 浏览: 171
以下是一个简单的俄罗斯方块游戏的 Python 代码示例:
```python
import pygame
import random
# 游戏板的大小
BOARD_WIDTH = 10
BOARD_HEIGHT = 20
# 方块的大小
BLOCK_SIZE = 30
# 方块的类型和形状
BLOCK_TYPES = [
# I型方块
[[1, 1, 1, 1]],
# J型方块
[[1, 0, 0], [1, 1, 1]],
# L型方块
[[0, 0, 1], [1, 1, 1]],
# O型方块
[[1, 1], [1, 1]],
# S型方块
[[0, 1, 1], [1, 1, 0]],
# T型方块
[[0, 1, 0], [1, 1, 1]],
# Z型方块
[[1, 1, 0], [0, 1, 1]]
]
# 方块的颜色
BLOCK_COLORS = [
(0, 128, 255), # I型方块的颜色
(255, 128, 0), # J型方块的颜色
(255, 0, 128), # L型方块的颜色
(255, 255, 0), # O型方块的颜色
(0, 255, 128), # S型方块的颜色
(128, 0, 255), # T型方块的颜色
(255, 0, 0) # Z型方块的颜色
]
# 游戏板
board = [[0 for _ in range(BOARD_WIDTH)] for _ in range(BOARD_HEIGHT)]
# 当前方块
current_block = None
# 当前方块的位置
current_position = None
# 下一个方块
next_block = None
# 初始化游戏板
def init_board():
global board
board = [[0 for _ in range(BOARD_WIDTH)] for _ in range(BOARD_HEIGHT)]
# 从方块类型列表中随机选择下一个方块
def get_next_block():
block_type = random.randint(0, len(BLOCK_TYPES) - 1)
return BLOCK_TYPES[block_type]
# 在游戏板上放置当前方块
def place_block():
global board, current_block, current_position
for y in range(len(current_block)):
for x in range(len(current_block[y])):
if current_block[y][x]:
board[current_position[1] + y][current_position[0] + x] = current_block[y][x]
# 检查当前方块是否与游戏板上的方块重叠
def check_collision():
global board, current_block, current_position
for y in range(len(current_block)):
for x in range(len(current_block[y])):
if current_block[y][x] and (current_position[0] + x < 0 or current_position[0] + x >= BOARD_WIDTH or current_position[1] + y >= BOARD_HEIGHT or board[current_position[1] + y][current_position[0] + x]):
return True
return False
# 移动当前方块
def move_block(dx, dy):
global current_position
current_position[0] += dx
current_position[1] += dy
if check_collision():
current_position[0] -= dx
current_position[1] -= dy
return False
return True
# 旋转当前方块
def rotate_block():
global current_block
current_block = [[current_block[y][x] for y in range(len(current_block))] for x in range(len(current_block[0]) - 1, -1, -1)]
if check_collision():
current_block = [[current_block[y][x] for y in range(len(current_block[0]))] for x in range(len(current_block))]
# 消除满行并向下移动上方的方块
def clear_lines():
global board
rows_cleared = 0
y = BOARD_HEIGHT - 1
while y >= 0:
if all(board[y]):
rows_cleared += 1
for y2 in range(y, 0, -1):
board[y2] = board[y2 - 1]
board[0] = [0 for _ in range(BOARD_WIDTH)]
else:
y -= 1
return rows_cleared
# 检查游戏是否结束
def check_game_over():
global board
return any(board[0])
# 绘制游戏板和当前方块
def draw_board(screen):
global board, current_block, current_position
for y in range(BOARD_HEIGHT):
for x in range(BOARD_WIDTH):
if board[y][x]:
pygame.draw.rect(screen, BLOCK_COLORS[board[y][x] - 1], (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
if current_block:
for y in range(len(current_block)):
for x in range(len(current_block[y])):
if current_block[y][x]:
pygame.draw.rect(screen, BLOCK_COLORS[len(BLOCK_TYPES) - 1], ((current_position[0] + x) * BLOCK_SIZE, (current_position[1] + y) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
# 主函数
def main():
global current_block, current_position, next_block
pygame.init()
screen = pygame.display.set_mode((BOARD_WIDTH * BLOCK_SIZE, BOARD_HEIGHT * BLOCK_SIZE))
pygame.display.set_caption("Tetris")
clock = pygame.time.Clock()
init_board()
current_block = get_next_block()
current_position = [BOARD_WIDTH // 2 - len(current_block[0]) // 2, 0]
next_block = get_next_block()
game_over = False
while not game_over:
clock.tick(10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
move_block(-1, 0)
elif event.key == pygame.K_RIGHT:
move_block(1, 0)
elif event.key == pygame.K_DOWN:
move_block(0, 1)
elif event.key == pygame.K_UP:
rotate_block()
if not move_block(0, 1):
place_block()
lines_cleared = clear_lines()
if lines_cleared > 0:
score += lines_cleared * 10
current_block = next_block
current_position = [BOARD_WIDTH // 2 - len(current_block[0]) // 2, 0]
next_block = get_next_block()
if check_game_over():
game_over = True
screen.fill((0, 0, 0))
draw_board(screen)
pygame.display.update()
pygame.quit()
if __name__ == "__main__":
main()
```
这个代码实现了一个简单的俄罗斯方块游戏,玩家可以使用键盘控制方块的移动和旋转,当方块到达底部或者与已经放置的方块重叠时,方块会被放置在游戏板上,如果有一行被填满,则该行会被消除并向下移动上方的方块,当游戏板的顶部被填满时,游戏结束。
阅读全文