利用pygame 编写一个俄罗斯方块小游戏
时间: 2024-02-17 10:24:36 浏览: 76
基于pygame的俄罗斯方块小游戏.7z
当然可以!下面是一个使用 Pygame 编写的俄罗斯方块小游戏的示例代码:
```python
import pygame
import random
# 初始化 Pygame
pygame.init()
# 设置游戏窗口宽度和高度
window_width = 800
window_height = 600
# 设置方块大小
block_size = 30
# 设置游戏区域的宽度和高度
play_width = 10 * block_size
play_height = 20 * block_size
# 设置游戏区域的位置
play_x = (window_width - play_width) // 2
play_y = window_height - play_height - 50
# 设置颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
blue = (0, 0, 255)
green = (0, 255, 0)
yellow = (255, 255, 0)
purple = (128, 0, 128)
orange = (255, 165, 0)
cyan = (0, 255, 255)
# 定义形状的类
class Shape:
def __init__(self):
self.x = play_x // block_size
self.y = play_y // block_size
self.color = random.choice([red, blue, green, yellow, purple, orange, cyan])
self.rotation = random.randint(0, 3)
def rotate(self):
self.rotation = (self.rotation + 1) % 4
def move_left(self):
self.x -= 1
def move_right(self):
self.x += 1
def move_down(self):
self.y += 1
# 创建游戏窗口
win = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("俄罗斯方块")
def draw_grid():
for x in range(play_x, play_x + play_width, block_size):
pygame.draw.line(win, white, (x, play_y), (x, play_y + play_height))
for y in range(play_y, play_y + play_height, block_size):
pygame.draw.line(win, white, (play_x, y), (play_x + play_width, y))
def draw_shape(shape):
for i in range(len(shape)):
for j in range(len(shape[i])):
if shape[i][j] == 'X':
pygame.draw.rect(win, shape.color, (play_x + (shape.x + j) * block_size, play_y + (shape.y + i) * block_size, block_size, block_size))
def check_collision(shape):
for i in range(len(shape)):
for j in range(len(shape[i])):
if shape[i][j] == 'X':
if shape.y + i >= 20 or shape.x + j < 0 or shape.x + j >= 10:
return True
return False
def remove_row(play_area):
full_rows = []
for i in range(len(play_area)):
if all(play_area[i]):
full_rows.append(i)
for row in full_rows:
del play_area[row]
play_area.insert(0, [0] * 10)
return len(full_rows)
def draw_play_area(play_area):
for i in range(len(play_area)):
for j in range(len(play_area[i])):
if play_area[i][j] == 1:
pygame.draw.rect(win, white, (play_x + j * block_size, play_y + i * block_size, block_size, block_size))
def update_score(score):
font = pygame.font.SysFont(None, 30)
text = font.render("Score: " + str(score), True, white)
win.blit(text, (window_width - 100, 20))
def game_loop():
clock = pygame.time.Clock()
shape = Shape()
play_area = [[0] * 10 for _ in range(20)]
score = 0
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
shape.move_left()
if check_collision(shape):
shape.move_right()
elif event.key == pygame.K_RIGHT:
shape.move_right()
if check_collision(shape):
shape.move_left()
elif event.key == pygame.K_DOWN:
shape.move_down()
if check_collision(shape):
shape.y -= 1
for i in range(len(shape)):
for j in range(len(shape[i])):
if shape[i][j] == 'X':
play_area[shape.y + i][shape.x + j] = 1
score += remove_row(play_area)
shape = Shape()
if check_collision(shape):
game_over = True
elif event.key == pygame.K_UP:
shape.rotate()
if check_collision(shape):
for _ in range(3):
shape.rotate()
if not game_over:
shape.move_down()
if check_collision(shape):
shape.y -= 1
for i in range(len(shape)):
for j in range(len(shape[i])):
if shape[i][j] == 'X':
play_area[shape.y + i][shape.x + j] = 1
score += remove_row(play_area)
shape = Shape()
if check_collision(shape):
game_over = True
win.fill(black)
draw_grid()
draw_shape(shape)
draw_play_area(play_area)
update_score(score)
pygame.display.update()
clock.tick(10)
pygame.quit()
game_loop()
```
这段代码使用 Pygame 创建了一个窗口,实现了俄罗斯方块的基本功能,包括方块的移动、旋转、碰撞检测、行消除和得分计算。你可以运行这段代码来体验俄罗斯方块小游戏。希望能对你有所帮助!
阅读全文