python语言制作俄罗斯方块的代码
时间: 2023-08-30 08:04:07 浏览: 97
下面是使用 Python 语言制作俄罗斯方块的示例代码:
```python
import pygame
import random
# 设置游戏窗口大小和方块大小
WINDOW_WIDTH, WINDOW_HEIGHT = 320, 480
BLOCK_SIZE = 20
# 定义方块形状
SHAPES = [
[[1, 1, 1], [0, 1, 0]],
[[2, 2], [2, 2]],
[[0, 3, 3], [3, 3, 0]],
[[4, 4, 0], [0, 4, 4]],
[[5, 5, 5, 5]],
[[6, 6, 6], [0, 0, 6]],
[[7, 7, 7], [7, 0, 0]],
]
# 定义方块颜色
COLORS = [
(0, 0, 0),
(255, 0, 0),
(0, 255, 0),
(0, 0, 255),
(255, 255, 0),
(255, 0, 255),
(0, 255, 255),
(128, 128, 128),
]
class Block:
def __init__(self, x, y, shape):
self.x = x
self.y = y
self.shape = shape
def move(self, dx, dy):
self.x += dx
self.y += dy
def rotate(self):
self.shape = [[self.shape[j][i] for j in range(len(self.shape))] for i in range(len(self.shape[0])-1, -1, -1)]
def get_rect(self):
return pygame.Rect(self.x*BLOCK_SIZE, self.y*BLOCK_SIZE, len(self.shape[0])*BLOCK_SIZE, len(self.shape)*BLOCK_SIZE)
class Tetris:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('Tetris')
self.clock = pygame.time.Clock()
self.font = pygame.font.Font(None, 30)
self.reset()
def reset(self):
self.board = [[0 for _ in range(WINDOW_WIDTH//BLOCK_SIZE)] for _ in range(WINDOW_HEIGHT//BLOCK_SIZE)]
self.score = 0
self.game_over = False
self.current_block = Block(WINDOW_WIDTH//BLOCK_SIZE//2, 0, random.choice(SHAPES))
self.next_block = Block(0, 0, random.choice(SHAPES))
def draw_block(self, block, offset_x=0, offset_y=0):
for i in range(len(block.shape)):
for j in range(len(block.shape[0])):
if block.shape[i][j] != 0:
pygame.draw.rect(self.screen, COLORS[block.shape[i][j]], (BLOCK_SIZE*(block.x+j+offset_x), BLOCK_SIZE*(block.y+i+offset_y), BLOCK_SIZE, BLOCK_SIZE))
def draw_board(self):
for i in range(len(self.board)):
for j in range(len(self.board[0])):
if self.board[i][j] != 0:
pygame.draw.rect(self.screen, COLORS[self.board[i][j]], (BLOCK_SIZE*j, BLOCK_SIZE*i, BLOCK_SIZE, BLOCK_SIZE))
def check_collision(self, block, dx=0, dy=0):
for i in range(len(block.shape)):
for j in range(len(block.shape[0])):
if block.shape[i][j] != 0:
x = block.x + j + dx
y = block.y + i + dy
if x < 0 or x >= WINDOW_WIDTH//BLOCK_SIZE or y < 0 or y >= WINDOW_HEIGHT//BLOCK_SIZE or self.board[y][x] != 0:
return True
return False
def place_block(self, block):
for i in range(len(block.shape)):
for j in range(len(block.shape[0])):
if block.shape[i][j] != 0:
x = block.x + j
y = block.y + i
self.board[y][x] = block.shape[i][j]
def remove_lines(self):
lines_removed = 0
for i in range(len(self.board)):
if all(self.board[i]):
del self.board[i]
self.board.insert(0, [0 for _ in range(WINDOW_WIDTH//BLOCK_SIZE)])
lines_removed += 1
self.score += lines_removed**2
def update(self):
if self.check_collision(self.current_block, 0, 1):
self.place_block(self.current_block)
self.remove_lines()
self.current_block = self.next_block
self.next_block = Block(0, 0, random.choice(SHAPES))
if self.check_collision(self.current_block):
self.game_over = True
self.current_block.move(0, 1)
if self.check_collision(self.current_block):
self.current_block.move(0, -1)
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_block, -1):
self.current_block.move(-1, 0)
elif event.key == pygame.K_RIGHT and not self.check_collision(self.current_block, 1):
self.current_block.move(1, 0)
elif event.key == pygame.K_UP:
self.current_block.rotate()
if self.check_collision(self.current_block):
self.current_block.rotate()
self.current_block.rotate()
self.current_block.rotate()
elif event.key == pygame.K_DOWN:
while not self.check_collision(self.current_block, 0, 1):
self.current_block.move(0, 1)
self.screen.fill((255, 255, 255))
self.draw_board()
self.draw_block(self.current_block)
self.draw_block(self.next_block, WINDOW_WIDTH//BLOCK_SIZE+2, 2)
score_text = self.font.render("Score: {}".format(self.score), True, (0, 0, 0))
self.screen.blit(score_text, (10, WINDOW_HEIGHT-30))
if self.game_over:
game_over_text = self.font.render("Game Over!", True, (255, 0, 0))
self.screen.blit(game_over_text, (WINDOW_WIDTH//2-75, WINDOW_HEIGHT//2-15))
pygame.display.flip()
def run(self):
while True:
self.clock.tick(10)
self.update()
if __name__ == '__main__':
tetris = Tetris()
tetris.run()
```
运行后,您应该可以看到一个窗口,其中包含俄罗斯方块游戏。您可以使用左右箭头键移动方块,上箭头键旋转方块,下箭头键加速方块下落。游戏结束时,窗口将显示“Game Over!”。
阅读全文