python俄罗斯方块游戏代码
时间: 2023-07-23 20:01:24 浏览: 234
下面是一个简单的Python俄罗斯方块游戏代码,使用了pygame库来绘制游戏界面和处理用户输入:
```python
import pygame
import random
# 游戏区域大小
WIDTH, HEIGHT = 300, 600
# 俄罗斯方块大小
BLOCK_SIZE = 30
# 游戏区域大小(格子数)
GRID_WIDTH, GRID_HEIGHT = WIDTH // BLOCK_SIZE, HEIGHT // BLOCK_SIZE
# 定义方块类型和形状
SHAPES = [
[[1, 1, 1], [0, 1, 0]],
[[0, 2, 2], [2, 2, 0]],
[[3, 3, 0], [0, 3, 3]],
[[4, 0, 0], [4, 4, 4]],
[[0, 0, 5, 0], [5, 5, 5, 5]],
[[0, 6, 0], [6, 6, 6], [0, 6, 0]],
[[7, 7], [7, 7]]
]
# 随机生成下一个方块的类型和形状
def new_block():
shape = random.choice(SHAPES)
return shape, BLOCK_SIZE * (GRID_WIDTH // 2 - len(shape[0]) // 2), 0
# 将方块添加到游戏区域中
def add_block(grid, block, pos):
for i in range(len(block)):
for j in range(len(block[i])):
if block[i][j] != 0:
x, y = pos[0] + j, pos[1] + i
grid[y][x] = block[i][j]
# 检查方块是否与游戏区域的边界或已有方块重叠
def check_collision(grid, block, pos):
for i in range(len(block)):
for j in range(len(block[i])):
if block[i][j] != 0:
x, y = pos[0] + j, pos[1] + i
if x < 0 or x >= GRID_WIDTH or y >= GRID_HEIGHT or grid[y][x] != 0:
return True
return False
# 将游戏区域中已满一行的方块删除
def remove_full_rows(grid):
rows_removed = 0
row = GRID_HEIGHT - 1
while row >= 0:
if all(grid[row]):
for r in range(row, 0, -1):
grid[r] = grid[r-1][:]
grid[0] = [0] * GRID_WIDTH
rows_removed += 1
else:
row -= 1
return rows_removed
# 主函数
def main():
# 初始化pygame
pygame.init()
# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Tetris")
# 创建字体对象
font = pygame.font.Font(None, 36)
# 初始化游戏区域
grid = [[0] * GRID_WIDTH for _ in range(GRID_HEIGHT)]
# 随机生成下一个方块
next_block = new_block()
# 初始化当前方块的类型、形状和位置
cur_block, cur_x, cur_y = new_block()
# 游戏循环
clock = pygame.time.Clock()
score = 0
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
cur_x -= BLOCK_SIZE
elif event.key == pygame.K_RIGHT:
cur_x += BLOCK_SIZE
elif event.key == pygame.K_DOWN:
cur_y += BLOCK_SIZE
elif event.key == pygame.K_UP:
# 旋转方块
cur_block = [list(reversed(x)) for x in zip(*cur_block)]
# 检查方块是否与游戏区域的边界或已有方块重叠
if check_collision(grid, cur_block, (cur_x, cur_y)):
# 将方块添加到游戏区域中
add_block(grid, cur_block, (cur_x, cur_y))
# 将游戏区域中已满一行的方块删除
rows_removed = remove_full_rows(grid)
# 更新得分
score += rows_removed ** 2
# 随机生成下一个方块
next_block = new_block()
# 初始化当前方块的类型、形状和位置
cur_block, cur_x, cur_y = next_block
if check_collision(grid, cur_block, (cur_x, cur_y)):
# 游戏结束
text = font.render("Game Over", True, (255, 0, 0))
screen.blit(text, (WIDTH//2 - text.get_width()//2, HEIGHT//2 - text.get_height()//2))
pygame.display.flip()
pygame.time.wait(2000)
return
# 更新方块位置
cur_y += BLOCK_SIZE
# 绘制游戏界面
screen.fill((0, 0, 0))
for i in range(GRID_HEIGHT):
for j in range(GRID_WIDTH):
if grid[i][j] != 0:
pygame.draw.rect(screen, (255, 255, 255), (j*BLOCK_SIZE, i*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
for i in range(len(cur_block)):
for j in range(len(cur_block[i])):
if cur_block[i][j] != 0:
pygame.draw.rect(screen, (255, 0, 0), (cur_x+j*BLOCK_SIZE, cur_y+i*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
# 绘制下一个方块
text = font.render("Next Block", True, (255, 255, 255))
screen.blit(text, (WIDTH+20, 20))
for i in range(len(next_block[0])):
for j in range(len(next_block[0][i])):
if next_block[0][i][j] != 0:
pygame.draw.rect(screen, (255, 0, 0), (WIDTH+j*BLOCK_SIZE+20, i*BLOCK_SIZE+60, BLOCK_SIZE, BLOCK_SIZE))
# 绘制得分
text = font.render("Score: {}".format(score), True, (255, 255, 255))
screen.blit(text, (WIDTH+20, 200))
# 更新屏幕
pygame.display.flip()
# 控制游戏速度
clock.tick(10)
if __name__ == "__main__":
main()
```
这个代码可以让你在Python中玩俄罗斯方块游戏,通过上下左右按键来控制方块的移动和旋转,尝试在游戏中获得更高的得分吧!
阅读全文