您是否想要制作自己的俄罗斯方块游戏?pyGame带你飞!

开发 后端
Pygame是一个基于Python语言的跨平台游戏开发库,提供了丰富的API来创建2D游戏。它结合了Python和SDL(Simple DirectMedia Layer)库的功能,使得可以在Python中轻松地创建游戏,包括图形、声音和用户输入等方面的处理。

安装

使用以下命令即可安装

pip install pyGame

API介绍

以下是Pygame库中常用的一些API的介绍:

  • pygame.init(): 初始化Pygame库。
  • pygame.display.set_mode(): 创建一个显示窗口。
  • pygame.display.set_caption(): 设置窗口的标题。
  • pygame.Surface: Surface是Pygame库中的一个类,表示一个图像表面,可以用来绘制图像和文本,以及进行图像处理。
  • pygame.draw: 这个模块包含了许多绘制图形的函数,例如绘制直线、矩形、圆等。
  • pygame.event.get(): 获取当前的事件队列。
  • pygame.event.type: 用于判断事件的类型,例如QUIT、KEYDOWN等。
  • pygame.KEYDOWN: 表示键盘某个键被按下。
  • pygame.K_UP: 表示键盘上的上箭头键。
  • pygame.K_DOWN: 表示键盘上的下箭头键。
  • pygame.K_LEFT: 表示键盘上的左箭头键。
  • pygame.K_RIGHT: 表示键盘上的右箭头键。
  • pygame.time.Clock(): 创建一个时钟对象,用于控制游戏帧率。
  • pygame.time.delay(): 在指定的毫秒数内暂停游戏。
  • pygame.font.SysFont(): 创建一个字体对象,用于绘制文本。
  • pygame.mixer.Sound(): 创建一个声音对象,用于播放声音。

通过使用这些API,可以轻松地创建2D游戏,实现图形绘制、文本显示、声音播放、用户输入等功能。需要注意的是,Pygame库并不支持3D图形渲染和网络通信等高级功能,如果需要这些功能,可以使用其他游戏引擎或网络库。

示例

以下是一个简单的Pygame库的demo,用于显示一个窗口和一个文本框:

import pygame

# 初始化Pygame库
pygame.init()

# 创建窗口
win_width, win_height = 640, 480
win = pygame.display.set_mode((win_width, win_height))

# 设置窗口标题
pygame.display.set_caption("Pygame Demo")

# 创建字体对象
font = pygame.font.SysFont('Arial', 30)

# 创建文本对象
text = font.render('Hello, Pygame!', True, (255, 255, 255))

# 游戏循环
while True:
    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

    # 填充窗口背景色
    win.fill((0, 0, 0))

    # 将文本对象绘制到窗口上
    win.blit(text, (win_width/2 - text.get_width()/2, win_height/2 - text.get_height()/2))

    # 刷新窗口
    pygame.display.update()

这个demo中,我们首先使用pygame.init()函数初始化Pygame库,然后使用pygame.display.set_mode()函数创建一个窗口,并使用
pygame.display.set_caption()函数设置窗口的标题。

接下来,我们使用pygame.font.SysFont()函数创建一个字体对象,用于绘制文本。然后使用font.render()函数创建一个文本对象,指定要显示的文本、是否开启抗锯齿和文本颜色。

在游戏循环中,我们处理事件,然后使用win.fill()函数填充窗口的背景色。接着,我们使用win.blit()函数将文本对象绘制到窗口上,并指定文本的位置。最后,使用pygame.display.update()函数刷新窗口。

运行这个demo,就可以看到一个显示了文本的窗口。这个demo只是一个简单的例子,但是可以帮助你了解如何使用Pygame库的一些基本功能。

俄罗斯方块

下面是一个使用Pygame实现的俄罗斯方块游戏的示例代码:

import pygame
import random

# 初始化 Pygame 库
pygame.init()

# 设置游戏窗口的大小和标题
screen_width = 640
screen_height = 480
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("俄罗斯方块")

# 设置常量
block_size = 20
play_width = 10 * block_size
play_height = 20 * block_size
top_left_x = (screen_width - play_width) // 2
top_left_y = screen_height - play_height - 30

# 定义颜色常量
white = (255, 255, 255)
black = (0, 0, 0)
gray = (128, 128, 128)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
yellow = (255, 255, 0)
orange = (255, 165, 0)
purple = (128, 0, 128)
colors = [gray, red, green, blue, yellow, orange, purple]

# 定义方块形状
s_shape = [['.....', '.....', '..00.', '.00..', '.....'],
           ['.....', '..0..', '..00.', '...0.', '.....']]
z_shape = [['.....', '.....', '.00..', '..00.', '.....'],
           ['.....', '..0..', '.00..', '.0...', '.....']]
i_shape = [['..0..', '..0..', '..0..', '..0..', '.....'],
           ['.....', '.....', '0000.', '.....', '.....']]
o_shape = [['.....', '.....', '.00..', '.00..', '.....']]
j_shape = [['.....', '.0...', '.000.', '.....', '.....'],
           ['.....', '..00.', '..0..', '..0..', '.....'],
           ['.....', '.....', '.000.', '...0.', '.....'],
           ['.....', '..0..', '..0..', '.00..', '.....']]
l_shape = [['.....', '...0.', '.000.', '.....', '.....'],
           ['.....', '..0..', '..0..', '..00.', '.....'],
           ['.....', '.....', '.000.', '.0...', '.....'],
           ['.....', '.00..', '..0..', '..0..', '.....']]
t_shape = [['.....', '..0..', '.000.', '.....', '.....'],
           ['.....', '..0..', '..00.', '..0..', '.....'],
           ['.....', '.....', '.000.', '..0..', '.....'],
           ['.....', '..0..', '.00..', '..0..', '.....']]
shapes = [s_shape, z_shape, i_shape, o_shape, j_shape, l_shape, t_shape]

# 定义方块类
class Piece:
    def __init__(self, x, y, shape):
        self.x = x
        self.y = y
        self.shape = shape
        self.color = colors[shapes.index(shape)]
        self.rotation = 0

# 定义函数:创建游戏界面
def create_grid(locked_positions={}):
    grid = [[black for _ in range(10)] for _ in range(20)]
    for i in range(len(grid)):
        for j in range(len(grid[i])):
            if (j, i) in locked_positions:
                c = locked_positions[(j, i)]
                grid[i][j] = c
    return grid

# 定义函数:检查方块是否在有效位置
def valid_space(piece, grid):
    accepted_positions = [[(j, i) for j in range(10) if grid[i][j] == black] for i in range(20)]
    accepted_positions = [j for sub in accepted_positions for j in sub]
    formatted = convert_shape_format(piece)
    for pos in formatted:
        if pos not in accepted_positions:
            if pos[1] > -1:
                return False
    return True

# 定义函数:将方块形状转换成坐标形式
def convert_shape_format(piece):
    positions = []
    shape_format = piece.shape[piece.rotation % len(piece.shape)]
    for i, line in enumerate(shape_format):
        row = list(line)
        for j, column in enumerate(row):
            if column == '0':
                positions.append((piece.x + j, piece.y + i))
    for i, pos in enumerate(positions):
        positions[i] = (pos[0] - 2, pos[1] - 4)
    return positions

# 定义函数:检查是否有整行已填满
def check_clear_rows(grid, locked):
    inc = 0
    for i in range(len(grid)-1, -1, -1):
        row = grid[i]
        if black not in row:
            inc += 1
            ind = i
            for j in range(len(row)):
                try:
                    del locked[(j, i)]
                except:
                    continue
    if inc > 0:
        for key in sorted(list(locked), key=lambda x: x[1])[::-1]:
            x, y = key
            if y < ind:
                new_key = (x, y + inc)
                locked[new_key] = locked.pop(key)
    return inc * 10

# 定义函数:检查是否游戏结束
def check_lost(positions):
    for pos in positions:
        x, y = pos
        if y < 1:
            return True
    return False

# 定义函数:绘制游戏界面
def draw_window(surface, grid, score=0):
    surface.fill(black)
    pygame.font.init()
    font = pygame.font.SysFont('comicsans', 60)
    font_small = pygame.font.SysFont('comicsans', 30)
    label = font.render('俄罗斯方块', 1, white)
    surface.blit(label, (top_left_x + play_width/2 - (label.get_width()/2), 30))
    score_label = font_small.render('得分:' + str(score), 1, white)
    surface.blit(score_label, (top_left_x + play_width + 50, top_left_y + 20))
    for i in range(len(grid)):
        for j in range(len(grid[i])):
            pygame.draw.rect(surface, grid[i][j], (top_left_x + j*block_size, top_left_y + i*block_size, block_size, block_size), 0)
            pygame.draw.rect(surface, white, (top_left_x + j*block_size, top_left_y + i*block_size, block_size, block_size), 1)
    pygame.draw.rect(surface, red, (top_left_x, top_left_y, play_width, play_height), 5)

# 定义函数:绘制下一个方块
def draw_next_shape(piece, surface):
    font = pygame.font.SysFont('comicsans', 30)
    label = font.render('下一个方块', 1, white)
    x = top_left_x + play_width + 50
    y = top_left_y + 100
    shape_format = piece.shape[piece.rotation % len(piece.shape)]
    for i, line in enumerate(shape_format):
        row = list(line)
        for j, column in enumerate(row):
            if column == '0':
                pygame.draw.rect(surface, piece.color, (x + j*block_size, y + i*block_size, block_size, block_size), 0)
    surface.blit(label, (x, y - 30))

# 定义函数:绘制游戏结束界面
def draw_game_over(surface):
    font = pygame.font.SysFont('comicsans', 60)
    label = font.render('游戏结束', 1, white)
    surface.blit(label, (top_left_x + play_width/2 - (label.get_width()/2), top_left_y + play_height/2 - (label.get_height()/2)))

def main():
    # 初始化变量
    locked_positions = {}
    grid = create_grid(locked_positions)
    change_piece = False
    run = True
    current_piece = Piece(5, 0, random.choice(shapes))
    next_piece = Piece(5, 0, random.choice(shapes))
    clock = pygame.time.Clock()
    fall_time = 0
    fall_speed = 0.27
    level_time = 0
    score = 0

    # 游戏循环
    while run:
        # 更新变量
        grid = create_grid(locked_positions)
        fall_time += clock.get_rawtime()
        level_time += clock.get_rawtime()
        clock.tick()

        # 每过一定时间提高下落速度
        if level_time/1000 > 5:
            level_time = 0
            if fall_speed > 0.12:
                fall_speed -= 0.005

        # 方块下落
        if fall_time/1000 > fall_speed:
            fall_time = 0
            current_piece.y += 1
            if not valid_space(current_piece, grid) and current_piece.y > 0:
                current_piece.y -= 1
                change_piece = True

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.display.quit()
                quit()

            # 方块左右移动
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    current_piece.x -= 1
                    if not valid_space(current_piece, grid):
                        current_piece.x += 1
                elif event.key == pygame.K_RIGHT:
                    current_piece.x += 1
                    if not valid_space(current_piece, grid):
                        current_piece.x -= 1
                elif event.key == pygame.K_DOWN:
                    current_piece.y += 1
                    if not valid_space(current_piece, grid):
                        current_piece.y -= 1
                elif event.key == pygame.K_UP:
                    current_piece.rotation = (current_piece.rotation + 1) % len(current_piece.shape)
                    if not valid_space(current_piece, grid):
                        current_piece.rotation = (current_piece.rotation - 1) % len(current_piece.shape)

        # 将当前方块加入已锁定方块列表
        shape_pos = convert_shape_format(current_piece)
        for pos in shape_pos:
            x, y = pos
            if y > -1:
                grid[y][x] = current_piece.color
        if change_piece:
            for pos in shape_pos:
                p = (pos[0], pos[1])
                locked_positions[p] = current_piece.color
            current_piece = next_piece
            next_piece = Piece(5, 0, random.choice(shapes))
            change_piece = False
            score += check_clear_rows(grid, locked_positions)

        # 清除整行并更新得分
        draw_window(screen, grid, score)
        draw_next_shape(next_piece, screen)
        pygame.display.update()

        if check_lost(locked_positions):
            draw_game_over(screen)
            pygame.time.delay(1500)
            run = False

    pygame.display.quit()
    quit()

if __name__ == '__main__':
    main()
责任编辑:姜华 来源: 今日头条
相关推荐

2014-10-08 10:04:14

代码解释俄罗斯方块

2011-06-13 18:21:12

2015-01-22 15:36:46

游戏源码

2023-09-25 12:35:27

Python

2021-01-12 12:16:55

鸿蒙HarmonyOS游戏

2014-05-26 10:07:18

Javascript俄罗斯方块

2015-04-28 09:21:28

JSJS俄罗斯方块游戏帝国

2020-02-27 13:43:14

Emacs俄罗斯方块应用

2021-12-29 11:56:16

Linux俄罗斯方块

2020-05-19 17:26:21

Python俄罗斯方块游戏开发

2016-06-13 10:21:49

二维码条形码二进制

2011-11-17 16:14:25

Jscex

2014-06-09 12:47:35

俄罗斯方块

2020-12-11 12:45:04

鸿蒙Hi3861游戏

2011-07-06 10:12:26

Objective-CCSSJavaScript

2012-11-05 10:50:50

程序员万圣节俄罗斯方块

2020-12-17 10:02:16

鸿蒙Hi3861开发板

2009-06-08 09:59:24

谷歌俄罗斯方块版权

2023-10-17 10:20:53

VueReact

2020-09-02 08:52:16

地图Echarts可视化
点赞
收藏

51CTO技术栈公众号