The source code of a simple Tetris game implemented by Python based on thinker: 1. Build the interface

Record the game ideas and process of the Tetris mini-game

1. Build the interface

**Goal:** Build a basic interface and display 7 shapes of Tetris in a fixed position

Build the basic form

Use the tkinter library to implement the basic form, without adding any functions, just the following three lines of code

import tkinter as tk
#它可以用来创建一个新的图形界面(GUI)窗口。 tk.Tk() 调用这个函数并创建一个新的Tkinter窗口,可以用来添加其他gui组件,如按钮,文本框,标签等
windows = tk.Tk()

#mainloop()函数是python中tkinter模块的主要方法,它将一个python程序作为GUI应用程序运行,该函数可以让你的程序在循环中保持运行,以便捕获用户的输入事件和重绘GUI。
windows.mainloop()

The result of the operation is as follows:

image-20230207175943785

Step 1: Create the Artboard Size

Next, we need to add a canvas container in the form to "hold" Tetris, that is, let this canvas be used as a panel, and the movement and drawing of Tetris are realized on this canvas.

#定义一些参数:
#画板的行数:R=20
#画板的列数:C=12
#俄罗斯方块的大小cell_size=30
#俄罗斯方块的高度:Height=cell_size*C
#俄罗斯方块的宽度:Width=cell_size*R
R=20
C=12
cell_size=30
height=cell_size*R
width=cell_size*C

#创建画板
canvas = tk.Canvas(windows, width=width, height=height)

# pack()是一个tkinter模块中的函数,它可以自动调整画布大小来适应其中包含的元素。
canvas.pack()
windows.mainloop()

The result of the operation is as follows:

image-20230207181848273

Step 2: Draw on the canvas

Now we can draw on the artboard, the next step is to draw Tetris on it

There are two steps here:

Step 1: Draw a square

Step 2: Draw all the squares

#画出一个方块
def draw_cell_by_cr(canvas, c, r, color="#CCCCCC"):
    """
    :param canvas:画板,用于绘制一个方块的Canvas对象
    :param c:方块的列数
    :param r:方块的行数
    :color:方块的颜色
    :return
    """
    x0 = c * cell_size
    y0 = r * cell_size
    x1 = c * cell_size + cell_size
    y1 = r * cell_size + cell_size
    # 用于在Canvas上绘制矩形。参数x0,y0,x1,y1是矩形的左上角和右下角的坐标;fill=color设置矩形的填充颜色;outline="white"设置矩形轮廓的颜色;width=2设置矩形边框的粗细。。
     canvas.create_rectangle(x0, y0, x1, y1, fill=color, outline="white",width=2)
        
#画出所有的方块
#通过两层for循环遍历出画板所有方块
def draw_blank_board(canvas):
    for ri in range(R):
        for ci in range(C):
            draw_cell_by_cr(canvas, ci, ri)

The running effect is as follows:

image-20230207192010371

Draw Tetris

There are 7 shapes of Tetris

Step 1: Draw an o-shaped Tetris

Start with the simplest O-shaped Tetris (that is, matts, as shown below)

picture

如何确定田字格的位置?

Analysis process:

田字型方块It consists of four 格子, if 4 grids are determined 坐标, then the position of the matt square can be determined

solution:

Take the coordinates of the lower left corner of the 4 grids to represent the coordinates of the grid

The lower left corners of the four grids are:方块一(-1,0)、方块二(0,0)、方块三(0,-1)、方块四(-1,-1)

Taking the midpoint of matts as the origin, the coordinates of the four grids of matts Tetris are shown in the figure below

image-20230208104806192

Documented with lists and tuples as

[
    (-1, -1), 
    (0, -1), 
    (-1, 0), 
    (0, 0)
]

We store the mapping relationship between the Tetris shape string and the coordinate list in the dictionary SHAPES (the mapping relationship can be understood as a one-to-one relationship here),

At the same time, a dictionary SHAPESCOLOR is established to record the one-to-one correspondence between Tetris shape strings and colors.

Since a shape can be seen as composed of multiple squares, we can create a new function draw_cells to draw this shape

Finally choose a place to draw this shape.

# 定义形状
SHAPES = {
    
    
    "O": [(-1, -1), (0, -1), (-1, 0), (0, 0)],
}

# 定义形状的颜色
SHAPESCOLOR = {
    
    
    "O": "blue",
}

#该函数的作用是在给定的画布上绘制特定数量的单元格,单元格列表指定了单元格的位置,而颜色指定了单元格的颜色
def draw_cells(canvas, c, r, cell_list, color="#CCCCCC"):
    """
    绘制指定形状指定颜色的俄罗斯方块
    :param canvas: 画板
    :param r: 该形状设定的原点所在的行
    :param c: 该形状设定的原点所在的列
    :param cell_list: 该形状各个方格相对自身所处位置
    :param color: 该形状颜色
    :return:
    """
    for cell in cell_list:
        cell_c, cell_r = cell
        ci = cell_c + c
        ri = cell_r + r
        # 判断该位置方格在画板内部(画板外部的方格不再绘制)
        if 0 <= c < C and 0 <= r < R:
            draw_cell_by_cr(canvas, ci, ri, color)

# 下面这行代码放在draw_blank_board(canvas) 下面
# 任取一个位置,如(3,3)绘制一个o型俄罗斯方块,用于展示
draw_cells(canvas, 3, 3, SHAPES['O'], SHAPESCOLOR['O'])

The result of the operation is as follows:

image-20230208111818050

Step 2: Draw other shape squares

There are seven main types of Tetris, except for the O type above, the other six Tetris are shown in the figure

picture

Correspondingly, to add the coordinates and colors of other squares in SHAPES and SHAPESCOLOR, the addition is as follows

# 定义各种形状
SHAPES = {
    
    
    "O": [(-1, -1), (0, -1), (-1, 0), (0, 0)],
    "S": [(-1, 0), (0, 0), (0, -1), (1, -1)],
    "T": [(-1, 0), (0, 0), (0, -1), (1, 0)],
    "I": [(0, 1), (0, 0), (0, -1), (0, -2)],
    "L": [(-1, 0), (0, 0), (-1, -1), (-1, -2)],
    "J": [(-1, 0), (0, 0), (0, -1), (0, -2)],
    "Z": [(-1, -1), (0, -1), (0, 0), (1, 0)],
}

# 定义各种形状的颜色
SHAPESCOLOR = {
    
    
    "O": "blue",
    "S": "red",
    "T": "yellow",
    "I": "green",
    "L": "purple",
    "J": "orange",
    "Z": "Cyan",
}

Change the code where the original O-shaped Tetris was drawn to the following code to draw these seven types of Tetris

draw_cells(canvas, 3, 3, SHAPES['O'], SHAPESCOLOR['O'])
draw_cells(canvas, 3, 8, SHAPES['S'], SHAPESCOLOR['S'])
draw_cells(canvas, 3, 13, SHAPES['T'], SHAPESCOLOR['T'])
draw_cells(canvas, 8, 3, SHAPES['I'], SHAPESCOLOR['I'])
draw_cells(canvas, 8, 8, SHAPES['L'], SHAPESCOLOR['L'])
draw_cells(canvas, 8, 13, SHAPES['J'], SHAPESCOLOR['J'])
draw_cells(canvas, 5, 18, SHAPES['Z'], SHAPESCOLOR['Z'])

The result of the operation is as follows:

image-20230208112226225Complete code download address: Simple Tetris game source code implemented by Python based on thinker

Guess you like

Origin blog.csdn.net/2301_76484015/article/details/129022783#comments_26791207
Recommended