Python 俄罗斯方块游戏代码示例
当然可以!下面是一个简单的俄罗斯方块游戏示例,使用 Python 和 Pygame 库编写:
import pygame
import random
# 初始化游戏
pygame.init()
# 设置游戏窗口尺寸
screen_width = 800
screen_height = 600
play_width = 300
play_height = 600
block_size = 30
top_left_x = (screen_width - play_width) // 2
top_left_y = screen_height - play_height
# 定义游戏区域
play_surface = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('俄罗斯方块')
# 定义颜色
black = (0, 0, 0)
white = (255, 255, 255)
blue = (0, 0, 255)
red = (255, 0, 0)
green = (0, 255, 0)
yellow = (255, 255, 0)
purple = (128, 0, 128)
orange = (255, 165, 0)
cyan = (0, 255, 255)
# 定义方块形状及对应颜色
tetris_shapes = [
[[1, 1, 1, 1]], # I
[[1, 1], [1, 1]], # O
[[1, 1, 1], [0, 1, 0]], # T
[[1, 1, 0], [0, 1, 1]], # Z
[[0, 1, 1], [1, 1, 0]], # S
[[1, 1, 1], [0, 0, 1]], # J
[[1, 1, 1], [1, 0, 0]] # L
]
# 初始化游戏区域
def create_grid(locked_positions={}):
grid = [[black for _ in range(10)] for _ in range(20)]
for i in range(len(grid)):
for j in range(len(grid[i])):
if (j, i) in locked_positions:
color = locked_positions[(j, i)]
grid[i][j] = color
return grid
# 旋转方块
def rotate_shape(shape):
return list(zip(*shape[::-1]))
# 检查方块是否在游戏区域内
def valid_space(shape, grid):
accepted_positions = [[(j, i) for j in range(10) if grid[i][j] == black] for i in range(20)]
accepted_positions = [j for sub in accepted_positions for j in sub]
formatted_shape = format_shape(shape)
for pos in formatted_shape:
if pos not in accepted_positions:
if pos[1] > -1:
return False
return True
# 检查游戏是否结束
def check_lost(positions):
for pos in positions:
if pos[1] < 1:
return True
return False
# 创建随机方块
def get_shape():
return random.choice(tetris_shapes)
# 格式化方块形状
def format_shape(shape):
formatted_shape = []
for i in range(len(shape)):
for j in range(len(shape[i])):
if shape[i][j] == 1:
formatted_shape.append((j, i))
return formatted_shape
# 绘制方块及游戏区域
def draw_grid(surface, grid):
for i in range(len(grid)):
for j in range(len(grid[i])):
pygame.draw.rect(surface, grid[i][j], (top_left_x + j*block_size, top_left_y + i*block_size, block_size, block_size))
pygame.draw.rect(surface, red, (top_left_x, top_left_y, play_width, play_height), 4)
# 绘制当前方块
def draw_shape(surface, shape):
for pos in shape:
pygame.draw.rect(surface, yellow, (top_left_x + pos[0]*block_size, top_left_y + pos[1]*block_size, block_size, block_size))
# 更新游戏区域
def update_grid(grid, locked_positions):
for pos in locked_positions:
x, y = pos
grid[y][x] = yellow
return grid
# 消除满行
def clear_rows(grid, locked_positions):
full_rows = []
for i in range(len(grid)):
if black not in grid[i]:
full_rows.append(i)
for row in full_rows:
del grid[row]
grid.insert(0, [black for _ in range(10)])
# 更新锁定位置
for pos in full_rows:
for key in list(locked_positions):
if key[1] < pos:
new_key = (key[0], key[1] + 1)
locked_positions[new_key] = locked_positions.pop(key)
return len(full_rows)
# 主游戏循环
def main():
locked_positions = {}
grid = create_grid(locked_positions)
current_shape = get_shape()
next_shape = get_shape()
clock = pygame.time.Clock()
fall_time = 0
fall_speed = 0.27
score = 0
running = True
while running:
grid = create_grid(locked_positions)
fall_time += clock.get_rawtime()
clock.tick()
if fall_time / 1000 >= fall_speed:
fall_time = 0
current_shape_pos = format_shape(current_shape)
for pos in current_shape_pos:
if pos[1] < 20:
new_pos = (pos[0], pos[1] + 1)
if new_pos not in current_shape_pos and new_pos[1] < 20:
if new_pos[1] > -1 and new_pos[0] > -1:
locked_positions[new_pos] = yellow
if not valid_space(current_shape, grid) or check_lost(locked_positions):
running = False
current_shape = next_shape
next_shape = get_shape()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
current_shape_pos = format_shape(current_shape)
if all(pos[0] > 0 for pos in current_shape_pos):
if all((pos[0]-1, pos[1]) not in current_shape_pos and grid[pos[1]][pos[0]-1] == black for pos in current_shape_pos):
current_shape = [(pos[0] - 1, pos[1]) for pos in current_shape]
if event.key == pygame.K_RIGHT:
current_shape_pos = format_shape(current_shape)
if all(pos[0] < 9 for pos in current_shape_pos):
if all((pos[0]+1, pos[1]) not in current_shape_pos and grid[pos[1]][pos[0]+1] == black for pos in current_shape_pos):
current_shape = [(pos[0] + 1, pos[1]) for pos in current_shape]
if event.key == pygame.K_DOWN:
current_shape_pos = format_shape(current_shape)
if all(pos[1] < 19 for pos in current_shape_pos):
if all((pos[0], pos[1]+1) not in current_shape_pos and grid[pos[1]+1][pos[0]] == black for pos in current_shape_pos):
current_shape = [(pos[0], pos[1] + 1) for pos in current_shape]
if event.key == pygame.K_UP:
current_shape = rotate_shape(current_shape)
if not valid_space(current_shape, grid):
current_shape = rotate_shape(current_shape)
shape_pos = format_shape(current_shape)
if all(pos[1] < 20 for pos in shape_pos):
if all((pos[0], pos[1]+1) not in shape_pos and grid[pos[1]+1][pos[0]] == black for pos in shape_pos):
current_shape = [(pos[0], pos[1] + 1) for pos in current_shape]
score += clear_rows(grid, locked_positions) * 10
play_surface.fill(black)
draw_grid(play_surface, grid)
draw_shape(play_surface, current_shape)
pygame.display.update()
print('游戏结束!得分:', score)
pygame.quit()
# 运行游戏
if __name__ == '__main__':
main()
运行以上代码,将会打开一个窗口并启动俄罗斯方块游戏。你可以使用左右箭头键来移动方块,向上箭头键来旋转方块,向下箭头键来加速下落。游戏结束时,窗口将关闭,并显示你的得分。
请注意,此示例中的俄罗斯方块游戏是一个基础版本,可以根据需要进行更多的功能和界面扩展。
原文地址: https://www.cveoy.top/t/topic/4y6 著作权归作者所有。请勿转载和采集!