用Python写一段俄罗斯方块游戏代码
时间: 2024-03-13 15:19:24 浏览: 230
好的,以下是一个简单的俄罗斯方块游戏的代码示例,使用 Python 语言编写,希望能对你有所帮助:
```python
import pygame
import random
# 游戏区域大小
WINDOW_WIDTH, WINDOW_HEIGHT = 480, 600
# 方块大小
BLOCK_SIZE = 20
# 方块颜色
COLORS = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255), (255, 255, 255)]
# 方块形状
BLOCKS = [
[[1, 1],
[1, 1]],
[[1, 0, 0],
[1, 1, 1]],
[[0, 0, 1],
[1, 1, 1]],
[[1, 1, 0],
[0, 1, 1]],
[[0, 1, 1],
[1, 1, 0]],
[[0, 1, 0],
[1, 1, 1]],
[[1, 1, 1, 1]]
]
class Block:
def __init__(self, x, y):
self.x = x
self.y = y
self.color = random.choice(COLORS)
self.shape = random.choice(BLOCKS)
self.rotation = 0
def rotate(self):
self.rotation = (self.rotation + 1) % len(self.shape)
def get_current_shape(self):
return self.shape[self.rotation]
class Game:
def __init__(self):
pygame.init()
self.window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("俄罗斯方块")
self.clock = pygame.time.Clock()
self.score = 0
self.block = Block(5, -2)
self.board = [[None] * 10 for _ in range(20)]
def run(self):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.move_left()
elif event.key == pygame.K_RIGHT:
self.move_right()
elif event.key == pygame.K_DOWN:
self.move_down()
elif event.key == pygame.K_UP:
self.rotate()
if not self.move_down():
if self.block.y < 0:
break
self.lock_block()
self.clear_lines()
self.block = Block(5, -2)
self.draw()
self.clock.tick(10)
pygame.quit()
def draw(self):
self.window.fill((0, 0, 0))
self.draw_board()
self.draw_block()
pygame.display.update()
def draw_board(self):
for y, row in enumerate(self.board):
for x, color in enumerate(row):
if color:
pygame.draw.rect(self.window, color, (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0)
def draw_block(self):
shape = self.block.get_current_shape()
for y, row in enumerate(shape):
for x, color in enumerate(row):
if color:
pygame.draw.rect(self.window, self.block.color, ((self.block.x + x) * BLOCK_SIZE, (self.block.y + y) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0)
def move_left(self):
self.block.x -= 1
if self.is_collision():
self.block.x += 1
def move_right(self):
self.block.x += 1
if self.is_collision():
self.block.x -= 1
def move_down(self):
self.block.y += 1
if self.is_collision():
self.block.y -= 1
return False
return True
def rotate(self):
self.block.rotate()
if self.is_collision():
self.block.rotate_back()
def is_collision(self):
shape = self.block.get_current_shape()
for y, row in enumerate(shape):
for x, _ in enumerate(row):
if self.block.y + y < 0:
continue
if shape[y][x] and self.board[self.block.y + y][self.block.x + x]:
return True
return False
def lock_block(self):
shape = self.block.get_current_shape()
for y, row in enumerate(shape):
for x, color in enumerate(row):
if color:
self.board[self.block.y + y][self.block.x + x] = self.block.color
def clear_lines(self):
lines_cleared = 0
for y, row in enumerate(self.board):
if None not in row:
self.board.pop(y)
self.board.insert(0, [None] * 10)
lines_cleared += 1
self.score += lines_cleared ** 2
if __name__ == '__main__':
game = Game()
game.run()
```
这个代码非常基本,只包含了游戏逻辑的核心部分,如果需要完整的游戏,还需要添加一些额外的功能,例如游戏菜单、音效、排行榜等等。
阅读全文