写一个俄罗斯方块的python代码
时间: 2023-12-31 10:06:57 浏览: 25
好的,以下是一个简单的俄罗斯方块的 Python 代码示例:
```python
import random
import pygame
# 定义方块形状及其对应的颜色
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],
[5, 5, 5]],
[[6, 6],
[6, 6]],
[[7, 7, 7, 7]]
]
colors = [
(0, 0, 0),
(255, 0, 0),
(0, 255, 0),
(0, 0, 255),
(255, 255, 0),
(0, 255, 255),
(255, 0, 255),
(255, 125, 125)
]
class Piece:
def __init__(self, shape):
self.shape = shape
self.color = colors[shapes.index(shape)]
self.x = 4
self.y = 0
def move(self, dx, dy):
self.x += dx
self.y += dy
def rotate(self):
self.shape = [[self.shape[y][x] for y in range(len(self.shape))] for x in range(len(self.shape[0])-1, -1, -1)]
def get_cords(self):
cords = []
for y in range(len(self.shape)):
for x in range(len(self.shape[0])):
if self.shape[y][x] != 0:
cords.append((self.x + x, self.y + y))
return cords
class Board:
def __init__(self, width, height):
self.width = width
self.height = height
self.grid = [[0 for x in range(width)] for y in range(height)]
self.score = 0
def add_piece(self, piece):
for x, y in piece.get_cords():
self.grid[y][x] = piece.color
def is_valid_position(self, piece):
for x, y in piece.get_cords():
if x < 0 or x >= self.width or y < 0 or y >= self.height or self.grid[y][x] != 0:
return False
return True
def remove_complete_lines(self):
lines_removed = 0
y = self.height - 1
while y >= 0:
if 0 not in self.grid[y]:
del self.grid[y]
self.grid.insert(0, [0 for x in range(self.width)])
self.score += 10
lines_removed += 1
else:
y -= 1
return lines_removed
def is_game_over(self):
return any(self.grid[0])
def draw(self, screen):
for y in range(self.height):
for x in range(self.width):
pygame.draw.rect(screen, colors[self.grid[y][x]], (x*30, y*30, 30, 30), 0)
for y in range(self.height+1):
pygame.draw.line(screen, (128,128,128), (0, y*30), (self.width*30, y*30))
for x in range(self.width+1):
pygame.draw.line(screen, (128,128,128), (x, 0), (x, self.height*30))
text = pygame.font.Font(None, 36).render("Score: " + str(self.score), True, (255,255,255))
screen.blit(text, (10, self.height*30+10))
def run_game():
pygame.init()
width, height = 10, 20
screen = pygame.display.set_mode((width*30, (height+1)*30))
pygame.display.set_caption("俄罗斯方块")
board = Board(width, height)
current_piece = Piece(random.choice(shapes))
clock = pygame.time.Clock()
fall_time = 0
fall_speed = 0.5
while True:
fall_time += clock.tick() / 1000.0
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and board.is_valid_position(current_piece.move(-1, 0)):
current_piece.move(-1, 0)
elif event.key == pygame.K_RIGHT and board.is_valid_position(current_piece.move(1, 0)):
current_piece.move(1, 0)
elif event.key == pygame.K_DOWN and board.is_valid_position(current_piece.move(0, 1)):
current_piece.move(0, 1)
elif event.key == pygame.K_UP:
current_piece.rotate()
if fall_time >= fall_speed:
current_piece.move(0,1)
fall_time = 0
if not board.is_valid_position(current_piece):
current_piece.move(0,-1)
board.add_piece(current_piece)
lines_removed = board.remove_complete_lines()
if lines_removed:
fall_speed = max(0.5 - (board.score // 10) * 0.1, 0.1)
current_piece = Piece(random.choice(shapes))
if not board.is_valid_position(current_piece):
board.draw(screen)
pygame.display.update()
pygame.time.delay(500)
print("游戏结束,您的得分是: ", board.score)
return
screen.fill((0,0,0))
board.draw(screen)
current_piece.draw(screen)
pygame.display.update()
if __name__ == '__main__':
run_game()
```
请注意,这是一个简单的示例代码,可能还需要一些完善与调整才能适应实际需求。感谢您的提问!