python实现俄罗斯方块小游戏
2023-12-15 20:12:25
以下是一个简单版本的俄罗斯方块游戏的Python代码。
游戏说明:
- 在这个游戏中,方块会不断地从上方落下,你可以使用左、右、下箭头键控制方块的移动,使用上箭头键旋转方块。
- 游戏中的目标是填满一行或多行,填满的行会消除,游戏区域没有空间时游戏结束。
- 游戏结束后,控制台会输出消除的行数。
你可以根据需要修改游戏的速度、颜色和其他参数。
import pygame
import random
pygame.init()
# 定义窗口大小和方块大小
SCREEN_WIDTH, SCREEN_HEIGHT = 300, 600
BLOCK_SIZE = 30
# 定义颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# 定义形状的列表
SHAPES = [
[[1, 1, 1],
[0, 1, 0]],
[[1, 1, 1, 1]],
[[1, 1],
[1, 1]],
[[1, 1, 0],
[0, 1, 1]],
[[0, 1, 1],
[1, 1, 0]],
[[1, 1, 1],
[1, 0, 0]],
[[1, 1, 1],
[0, 0, 1]]
]
class TetrisGame:
def __init__(self):
self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('俄罗斯方块')
self.clock = pygame.time.Clock()
self.game_over = False
self.board = [[0] * (SCREEN_WIDTH // BLOCK_SIZE) for _ in range(SCREEN_HEIGHT // BLOCK_SIZE)]
self.current_shape = self.new_shape()
def new_shape(self):
shape = random.choice(SHAPES)
color = random.choice(pygame.color.THECOLORS.keys())
return {'shape': shape, 'color': pygame.color.Color(color), 'x': SCREEN_WIDTH // 2 // BLOCK_SIZE * BLOCK_SIZE - len(shape[0]) * BLOCK_SIZE // 2, 'y': 0}
def draw_shape(self, shape, offset):
for y, row in enumerate(shape):
for x, cell in enumerate(row):
if cell:
pygame.draw.rect(self.screen, shape['color'], (shape['x'] + x * BLOCK_SIZE + offset[0], shape['y'] + y * BLOCK_SIZE + offset[1], BLOCK_SIZE, BLOCK_SIZE))
def check_collision(self, shape, offset):
for y, row in enumerate(shape):
for x, cell in enumerate(row):
if cell:
board_x = (shape['x'] + x * BLOCK_SIZE + offset[0]) // BLOCK_SIZE
board_y = (shape['y'] + y * BLOCK_SIZE + offset[1]) // BLOCK_SIZE
if board_x < 0 or board_x >= len(self.board[0]) or board_y >= len(self.board) or self.board[board_y][board_x]:
return True
return False
def merge_shape(self):
for y, row in enumerate(self.current_shape['shape']):
for x, cell in enumerate(row):
if cell:
board_x = (self.current_shape['x'] // BLOCK_SIZE) + x
board_y = (self.current_shape['y'] // BLOCK_SIZE) + y
self.board[board_y][board_x] = self.current_shape['color']
self.current_shape = self.new_shape()
if self.check_collision(self.current_shape['shape'], (0, 0)):
self.game_over = True
def check_lines(self):
lines_to_clear = [i for i, row in enumerate(self.board) if all(row)]
for i in lines_to_clear:
del self.board[i]
self.board.insert(0, [0] * (SCREEN_WIDTH // BLOCK_SIZE))
return len(lines_to_clear)
def run(self):
while not self.game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and not self.check_collision(self.current_shape['shape'], (-BLOCK_SIZE, 0)):
self.current_shape['x'] -= BLOCK_SIZE
elif event.key == pygame.K_RIGHT and not self.check_collision(self.current_shape['shape'], (BLOCK_SIZE, 0)):
self.current_shape['x'] += BLOCK_SIZE
elif event.key == pygame.K_DOWN and not self.check_collision(self.current_shape['shape'], (0, BLOCK_SIZE)):
self.current_shape['y'] += BLOCK_SIZE
elif event.key == pygame.K_UP:
rotated_shape = list(zip(*reversed(self.current_shape['shape'])))
if not self.check_collision(rotated_shape, (0, 0)):
self.current_shape['shape'] = rotated_shape
if not self.check_collision(self.current_shape['shape'], (0, BLOCK_SIZE)):
self.current_shape['y'] += BLOCK_SIZE
else:
self.merge_shape()
lines_cleared = self.check_lines()
print(f"Lines cleared: {lines_cleared}")
self.screen.fill(WHITE)
for y, row in enumerate(self.board):
for x, color in enumerate(row):
if color:
pygame.draw.rect(self.screen, color, (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
self.draw_shape(self.current_shape, (0, 0))
pygame.display.update()
self.clock.tick(10)
if __name__ == '__main__':
game = TetrisGame()
game.run()
文章来源:https://blog.csdn.net/m0_48904153/article/details/134869909
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!