俄罗斯方块,4个方块的形状有5个,5个方块呢?6个?

转自:http://www.zhihu.com/question/20540487

曾经做过一个俄罗斯方块游戏,收集资料时见识过几种:


四格骨牌(Tetromino)[1]
俄罗斯方块(Tetris )名称就是变形自词缀「四」( tetra),可以发现这种可变形状比较少。

五格骨牌(Pentomino)[2]


六格骨牌(Hexomino)[3]


[1]  en.wikipedia.org/wiki/T
[2]  en.wikipedia.org/wiki/P
[3]  en.wikipedia.org/wiki/H


==== 补充 ====
想了解程序里面如何简单地存储方块形状,可以看我的业余练习  github.com/ambar/Tetris
样例:
{
Z: {
color: new HSLColor(0, 1, .47),
shape: [
[1, 1, 0],
[0, 1, 1]
]
},
L: {
color: new HSLColor(40, 1, .47),
shape: [
[1, 1, 1],
[1, 0, 0]
]
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以,以下是一个简单的俄罗斯方块的 Python 代码示例: ``` python import pygame import random # 初始化Pygame pygame.init() # 创建游戏窗口 screen_width = 640 screen_height = 480 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("俄罗斯方块") # 定义颜色 BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) YELLOW = (255, 255, 0) # 定义方块大小和格子数 block_size = 20 grid_width = screen_width // block_size grid_height = screen_height // block_size # 定义方块形状 shapes = [ [[1, 1], [1, 1]], [[0, 2, 0], [2, 2, 2]], [[0, 3, 3], [3, 3, 0]], [[4, 4, 0], [0, 4, 4]], [[5, 5, 5, 5]], [[6, 6, 6], [0, 6, 0], [0, 6, 0]], [[0, 7, 0], [7, 7, 7], [0, 7, 0]] ] # 定义方块颜色 colors = [ BLACK, RED, GREEN, BLUE, YELLOW ] # 定义游戏变量 score = 0 grid = [[0] * grid_width for _ in range(grid_height)] cur_shape = random.choice(shapes) cur_color = random.choice(colors) cur_x = grid_width // 2 - len(cur_shape[0]) // 2 cur_y = 0 # 定义游戏函数 def draw_block(x, y, color): pygame.draw.rect(screen, color, (x * block_size, y * block_size, block_size, block_size)) def draw_shape(shape, x, y, color): for i in range(len(shape)): for j in range(len(shape[i])): if shape[i][j] != 0: draw_block(x + j, y + i, color) def check_collision(shape, x, y): for i in range(len(shape)): for j in range(len(shape[i])): if shape[i][j] != 0 and (y + i >= grid_height or x + j < 0 or x + j >= grid_width or grid[y + i][x + j] != 0): return True return False def place_shape(shape, x, y, color): for i in range(len(shape)): for j in range(len(shape[i])): if shape[i][j] != 0: grid[y + i][x + j] = color def remove_lines(): global score lines_removed = 0 for i in range(grid_height): if all(grid[i]): grid.pop(i) grid.insert(0, [0] * grid_width) lines_removed += 1 score += lines_removed ** 2 # 游戏循环 clock = pygame.time.Clock() while True: # 处理游戏事件 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: cur_x -= 1 if check_collision(cur_shape, cur_x, cur_y): cur_x += 1 elif event.key == pygame.K_RIGHT: cur_x += 1 if check_collision(cur_shape, cur_x, cur_y): cur_x -= 1 elif event.key == pygame.K_UP: old_shape = cur_shape cur_shape = [[cur_shape[j][i] for j in range(len(cur_shape))] for i in range(len(cur_shape[0]) - 1, -1, -1)] if check_collision(cur_shape, cur_x, cur_y): cur_shape = old_shape elif event.key == pygame.K_DOWN: cur_y += 1 if check_collision(cur_shape, cur_x, cur_y): cur_y -= 1 # 更新游戏状态 cur_y += 1 if check_collision(cur_shape, cur_x, cur_y): cur_y -= 1 place_shape(cur_shape, cur_x, cur_y, cur_color) remove_lines() cur_shape = random.choice(shapes) cur_color = random.choice(colors) cur_x = grid_width // 2 - len(cur_shape[0]) // 2 cur_y = 0 if check_collision(cur_shape, cur_x, cur_y): pygame.quit() exit() # 绘制游戏界面 screen.fill(WHITE) for i in range(grid_height): for j in range(grid_width): if grid[i][j] != 0: draw_block(j, i, colors[grid[i][j]]) draw_shape(cur_shape, cur_x, cur_y, cur_color) pygame.display.set_caption("俄罗斯方块 - 得分: {}".format(score)) pygame.display.flip() # 控制游戏速度 clock.tick(10) ``` 这是一个非常简单的俄罗斯方块示例,只有基本的游戏逻辑和图形界面。你可以根据自己的需要进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值