用python写俄罗斯方块的游戏,100行代码
时间: 2023-11-03 17:06:10 浏览: 112
以下是一个简单的俄罗斯方块游戏,只需要100行代码就可以实现。请注意,这个游戏还有很多地方可以优化和改进,但是它可以作为一个入门级别的示例。
```python
import pygame
import random
# 初始化pygame
pygame.init()
# 游戏区域大小
window_width = 320
window_height = 480
# 方块大小
block_size = 20
# 游戏区域的宽度和高度,以方块大小为单位
play_width = 10 * block_size # 10个方块宽
play_height = 20 * block_size # 20个方块高
# 游戏区域的位置
play_x = (window_width - play_width) // 2
play_y = window_height - play_height - 20
# 定义方块的形状和颜色
shapes = [
(255, 0, 0, [(0, 0), (1, 0), (2, 0), (3, 0)]), # I
(0, 255, 0, [(0, 0), (1, 0), (0, 1), (1, 1)]), # O
(0, 0, 255, [(1, 0), (2, 0), (0, 1), (1, 1)]), # S
(255, 255, 0, [(0, 0), (1, 0), (1, 1), (2, 1)]), # Z
(255, 165, 0, [(1, 0), (0, 1), (1, 1), (2, 1)]), # L
(138, 43, 226, [(0, 0), (0, 1), (1, 1), (2, 1)]), # J
(0, 255, 255, [(1, 0), (0, 1), (1, 1), (2, 1)]) # T
]
# 定义游戏区域
play_area = [[(0, 0, 0) for _ in range(10)] for _ in range(20)]
def new_shape():
"""生成新的方块"""
shape = random.choice(shapes)
x = (play_width // block_size // 2) - 2
y = 0
return shape, x, y
def draw_play_area(window):
"""绘制游戏区域"""
for y, row in enumerate(play_area):
for x, color in enumerate(row):
pygame.draw.rect(window, color, (play_x + x * block_size, play_y + y * block_size, block_size, block_size), 0)
pygame.draw.rect(window, (255, 255, 255), (play_x, play_y, play_width, play_height), 4)
def rotate(shape):
"""旋转方块"""
return [(y, -x) for x, y in shape]
def move(shape, x, y):
"""移动方块"""
return [(x + dx, y + dy) for dx, dy in shape]
def valid(shape, x, y):
"""检查方块是否合法"""
for dx, dy in shape:
if x + dx < 0 or x + dx >= 10 or y + dy < 0 or y + dy >= 20 or play_area[y + dy][x + dx] != (0, 0, 0):
return False
return True
def lock_shape(shape, x, y):
"""锁定方块"""
for dx, dy in shape:
play_area[y + dy][x + dx] = shape[0]
def clear_rows():
"""清除行"""
rows_cleared = 0
for y, row in enumerate(play_area):
if (0, 0, 0) not in row:
play_area.pop(y)
play_area.insert(0, [(0, 0, 0) for _ in range(10)])
rows_cleared += 1
return rows_cleared
def draw_text(window, text, size, x, y):
"""绘制文字"""
font = pygame.font.SysFont('comicsansms', size)
text_surface = font.render(text, True, (255, 255, 255))
text_rect = text_surface.get_rect()
text_rect.center = (x, y)
window.blit(text_surface, text_rect)
def main():
# 创建窗口
window = pygame.display.set_mode((window_width, window_height))
# 游戏循环
clock = pygame.time.Clock()
shape, x, y = new_shape()
game_over = False
while not game_over:
clock.tick(10)
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
break
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and valid(shape, x - 1, y):
x -= 1
elif event.key == pygame.K_RIGHT and valid(shape, x + 1, y):
x += 1
elif event.key == pygame.K_UP:
rotated_shape = rotate(shape[3:])
if valid(rotated_shape, x, y):
shape = (shape[0], shape[1], shape[2], rotated_shape)
elif event.key == pygame.K_DOWN:
if valid(shape, x, y + 1):
y += 1
# 移动方块
if valid(shape, x, y + 1):
y += 1
else:
lock_shape(shape[3:], x, y)
rows_cleared = clear_rows()
if rows_cleared:
score += rows_cleared ** 2
shape, x, y = new_shape()
if not valid(shape, x, y):
game_over = True
break
# 绘制游戏界面
window.fill((0, 0, 0))
draw_play_area(window)
draw_text(window, f'Score: {score}', 24, window_width // 2, 20)
pygame.display.update()
# 游戏结束
window.fill((0, 0, 0))
draw_text(window, 'Game Over', 48, window_width // 2, window_height // 2)
pygame.display.update()
pygame.time.delay(2000)
# 退出pygame
pygame.quit()
if __name__ == '__main__':
main()
```
这个游戏使用了 Pygame 库来实现图形界面和游戏循环。在游戏循环中,我们首先处理事件,然后移动方块并检查是否可以合法移动。如果方块不能继续下落,则锁定方块并清除行。最后,我们绘制游戏界面并检查游戏是否结束。
阅读全文