Commit 462a6704 by BellCodeEditor

save project

parent 9c41b293
File added
bg.png

46.1 KB

import pygame
from pygame import locals
import random
pygame.init() # 初始化
score = 0
grid_size = 20 # 格子大小
grid_num_width = 15 # 横向格子数量
grid_num_height = 25 # 纵向格子数量
FPS = 30
# 创建窗口
screen = pygame.display.set_mode((460, 500))
pygame.display.set_caption("俄罗斯方块")
clock = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
# 载入素材
background = pygame.image.load('bg.png')
font = pygame.font.Font('STKAITI.TTF', 60) # 字体
# 俄罗斯方块所有形状
O = [[(0, 0), (0, 1), (1, 0), (1, 1)]]
I = [[(0, -1), (0, 0), (0, 1), (0, 2)],
[(-1, 0), (0, 0), (1, 0), (2, 0)]]
Z = [[(0, -1), (0, 0), (1, 0), (1, 1)],
[(-1, 0), (0, 0), (0, -1), (1, -1)]]
S = [[(-1, 0), (0, 0), (0, 1), (1, 1)],
[(1, -1), (1, 0), (0, 0), (0, 1)]]
T = [[(0, -1), (0, 0), (0, 1), (-1, 0)],
[(-1, 0), (0, 0), (1, 0), (0, 1)],
[(0, -1), (0, 0), (0, 1), (1, 0)],
[(-1, 0), (0, 0), (1, 0), (0, -1)]]
J = [[(-1, 0), (0, 0), (1, 0), (1, -1)],
[(0, -1), (0, 0), (0, 1), (-1, -1)],
[(-1, 0), (0, 0), (1, 0), (-1, 1)],
[(0, -1), (0, 0), (0, 1), (1, 1)]]
L = [[(-1, 0), (0, 0), (1, 0), (1, 1)],
[(0, -1), (0, 0), (0, 1), (1, -1)],
[(-1, 0), (0, 0), (1, 0), (-1, -1)],
[(0, -1), (0, 0), (0, 1), (-1, 1)]]
shape_list = [I, J, L, O, S, T, Z] # 7种类型俄罗斯方块
# 一些RGB颜色
cube_colors = [
(204, 153, 153), (102, 102, 153),(153, 0, 102),
(255, 204, 0), (204, 0, 51),(255, 0, 51), (0, 102, 153),
(153, 0, 51), (204, 255, 102), (255, 153, 0)]
center = [2, 8] # 第2行第8列
shape = random.choice(shape_list)
index = random.randint(0,len(shape)-1)
current_shape = shape[index]
color = random.choice(cube_colors)
def check(center):
for cube in current_shape:
cube = (cube[0]+center[0],cube[1]+center[1])
if cube[0] <1 or cube[1] <1 or cube[0] >grid_num_height \
or cube[1] > grid_num_width:
return False
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
exit()
if event.type == locals.KEYDOWN:
if event.key == locals.K_RIGHT: # 向右
center[1] += 1
if check(center) == False:
center[1] -= 1
elif event.key == locals.K_LEFT: # 向左
center[1] -= 1
if check(center) == False:
center[1] += 1
elif event.key == locals.K_DOWN: # 向下
center[0] += 1
if check(center) == False:
center[0] -= 1
elif event.key == locals.K_UP:
old_index =index
index +=1
if index >=len(shape):
index = 0
current_shape = shape[index]
if check(center) == False:
index = old_index
current_shape = shape[index]
# 将背景图画上去
screen.blit(background, (0, 0))
# 计算出所有小方块的行、列位置
current_pos = []
for cube in current_shape:
pos = (cube[0] + center[0], cube[1] + center[1])
current_pos.append(pos)
# 取出所有小方块的行、列位置,计算坐标,绘制俄罗斯方块
for cube in current_pos:
pygame.draw.rect(screen, color,
(cube[1] * 20-20, cube[0] * 20-20, 20, 20), 0)
pygame.draw.rect(screen, (255, 255, 255),
(cube[1] * 20-20, cube[0] * 20-20, 20, 20), 1)
# 得分
text_surface = font.render(str(score), True, (0, 0, 0))
screen.blit(text_surface, (350,70))
# 刷新画面
pygame.display.update()
clock.tick(FPS)
import pygame
import pygame
import random
# 游戏参数设置
cell_size = 30
board_width = 10
board_height = 20
game_speed = 10 # 游戏速度
# 颜色
white = (255, 255, 255)
black = (0, 0, 0)
gray = (150, 150, 150)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
yellow = (255, 255, 0)
cyan = (0, 255, 255)
purple = (255, 0, 255)
tetromino_colors = [gray, red, green, blue, yellow, cyan, purple]
# 定义方块形状
tetrominos = [
# I 形状
[[1, 1, 1, 1]],
# J 形状
[[1, 0, 0],
[1, 1, 1]],
# L 形状
[[0, 0, 1],
[1, 1, 1]],
# O 形状
[[1, 1],
[1, 1]],
# S 形状
[[0, 1, 1],
[1, 1, 0]],
# T 形状
[[0, 1, 0],
[1, 1, 1]],
# Z 形状
[[1, 1, 0],
[0, 1, 1]]
]
class Tetromino:
"""表示俄罗斯方块的类"""
def __init__(self, shape, x, y, color):
self.shape = shape
self.x = x
self.y = y
self.color = color
def rotate(self):
"""将方块旋转 90 度"""
self.shape = list(zip(*self.shape[::-1]))
def move_left(self):
"""将方块向左移动一个单位"""
self.x -= 1
def move_right(self):
"""将方块向右移动一个单位"""
self.x += 1
def move_down(self):
"""将方块向下移动一个单位"""
self.y += 1
def draw(self, surface):
"""绘制方块"""
for i in range(len(self.shape)):
for j in range(len(self.shape[i])):
if self.shape[i][j] == 1:
pygame.draw.rect(surface, self.color, (self.x * cell_size + j * cell_size, self.y * cell_size + i * cell_size, cell_size, cell_size))
class Game:
"""表示游戏的类"""
def __init__(self):
self.board = [[0] * board_width for _ in range(board_height)]
self.current_tetromino = None
self.next_tetromino = None
self.score = 0
self.level = 1
self.lines = 0
def new_tetromino(self):
"""生成新的方块"""
if self.next_tetromino is None:
self.current_tetromino = Tetromino(random.choice(tetrominos), board_width // 2 - 2, 0, random.choice(tetromino_colors[1:]))
else:
self.current_tetromino = self.next_tetromino
self.current_tetromino.x = board_width // 2 - 2
self.current_tetromino.y = 0
self.current_tetromino.color = random.choice(tetromino_colors[1:])
self.next_tetromino = Tetromino(random.choice(tetrominos), board_width + 2, 2, random.choice(tetromino_colors[1:]))
# 如果新方块出现的位置已经有方块存在,则游戏结束
if not self.check_valid(self.current_tetromino):
pygame.quit()
quit()
def check_valid(self, tetromino):
"""检查方块是否出界或与已存在方块重叠"""
for i in range(len(tetromino.shape)):
for j in range(len(tetromino.shape[i])):
if tetromino.shape[i][j] == 1:
if x < 0 or x >= board_width or y >= board_height:
return False
elif y >= 0 and self.board[y][x] != 0:
return False
return True
def update_board(self):
"""将当前正在运动的方块加入到游戏面板中"""
for i in range(len(self.current_tetromino.shape)):
for j in range(len(self.current_tetromino.shape[i])):
if self.current_tetromino.shape[i][j] == 1:
x, y = self.current_tetromino.x + j, self.current_tetromino.y + i
self.board[y][x] = self.current_tetromino.color
def check_full_lines(self):
"""检查是否有满行"""
count = 0
for i in range(board_height):
if 0 not in self.board[i]:
self.board.pop(i)
self.board.insert(0, [0] * board_width)
count += 1
if count > 0:
self.score += count ** 2 * 100
self.lines += count
if self.lines >= self.level * 10:
self.level += 1
def draw_board(self, surface):
"""绘制游戏面板"""
for i in range(board_width):
for j in range(board_height):
pygame.draw.rect(surface, gray, (i * cell_size, j * cell_size, cell_size, cell_size), 1)
if self.board[j][i] != 0:
pygame.draw.rect(surface, self.board[j][i], (i * cell_size, j * cell_size, cell_size, cell_size))
def draw_score(self, surface):
"""绘制分数和等级"""
font = pygame.font.SysFont(None, 30)
text = font.render('Score: {}'.format(self.score), True, white)
surface.blit(text, (board_width * cell_size + 20, 50))
text = font.render('Level: {}'.format(self.level), True, white)
surface.blit(text, (board_width * cell_size + 20, 100))
def draw_next_tetromino(self, surface):
"""绘制下一个方块"""
font = pygame.font.SysFont(None, 30)
text = font.render('Next:', True, white)
surface.blit(text, (board_width * cell_size + 20, 200))
if self.next_tetromino is not None:
self.next_tetromino.draw(surface)
def run(self):
"""游戏主循环"""
pygame.init()
screen = pygame.display.set_mode((board_width * cell_size + 200, board_height * cell_size))
pygame.display.set_caption('Tetris')
self.new_tetromino()
clock = pygame.time.Clock()
while True:
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:
self.current_tetromino.move_left()
if not self.check_valid(self.current_tetromino):
self.current_tetromino.move_right()
elif event.key == pygame.K_RIGHT:
self.current_tetromino.move_right()
if not self.check_valid(self.current_tetromino):
self.current_tetromino.move_left()
elif event.key == pygame.K_UP:
self.current_tetromino.rotate()
if not self.check_valid(self.current_tetromino):
self.current_tetromino.rotate()
self.current_tetromino.rotate()
self.current_tetromino.rotate()
elif event.key == pygame.K_DOWN:
self.current_tetromino.move_down()
if not self.check_valid(self.current_tetromino):
self.current_tetromino.move_up()
self.update_board()
self.check_full_lines()
self.new_tetromino()
# 绘制游戏界面
screen.fill(black)
self.draw_board(screen)
self.current_tetromino.draw(screen)
self.draw_score(screen)
self.draw_next_tetromino(screen)
pygame.display.update()
# 控制游戏速度
clock.tick(game_speed * self)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment