[PYTHON] Programming improvement course 2

This is the second programming improvement course.

Consider a program based on Tetris.

Click here for the explanation video

I'm sorry if it doesn't appear

Consider the data structure of Tetris

Tetris is a tetrimino composed of 4 blocks It is a puzzle game that arranges in the field.

Puzzle games like this Perfect for programming material !!

Let's think about it immediately.

1. Let's draw the material of the Tetris game

1.1 Field data conversion

With the field of the board (10 x 20 squares) Blocks surrounding other than above Blocks as shown below Let's convert the field into data.

■□□□□□□□□□□■ ■□□□□□□□□□□■ ■□□□□□□□□□□■ ■□□□□□□□□□□■ ■□□□□□□□□□□■ ■□□□□□□□□□□■ ■□□□□□□□□□□■ ■□□□□□□□□□□■ ■□□□□□□□□□□■ ■□□□□□□□□□□■ ■□□□□□□□□□□■ ■□□□□□□□□□□■ ■□□□□□□□□□□■ ■□□□□□□□□□□■ ■□□□□□□□□□□■ ■□□□□□□□□□□■ ■□□□□□□□□□□■ ■□□□□□□□□□□■ ■□□□□□□□□□□■ ■□□□□□□□□□□■ ■■■■■■■■■■■■

Tip:

It's annoying to display black and white squares one by one. Let's use repetition.

The total is 21 lines multiplied by 12 squares.

1.1 Answer

Simply create and output the data as above. Keeping the data together makes it easier to reuse.

If you use a list type for data that is aligned vertically and horizontally It is easy to express and manage.

Multiplication of the list is the repetition of elements, and addition is the concatenation of the elements.

data = [['■'] + ['□']*10 + ['■']]*20 + [['■']*12]

for y in data:
    print(''.join(y))

In this case, the data is output as characters,

In the case of a puzzle game, if you store the data as a character string There are also some troublesome things, so I often think numerically.


data = [[1] + [0]*10 + [1]]*20 + [[1]*12]

for y in data:
    for x in y:
        print('■' if x==1 else '□',end='')
    print()

■ If 1 If □, the data is stored as 0 and converted at the time of output.

Now the results are together.

1.2 Tetromino data conversion

Tetromino (7) □■□□ □■□□ □■□□ □□■□ □■□□ □□■□ □□□□ □■□□ □■■□ □■□□ □□■□ □■■□ □■■□ □■■□ □■□□ □■□□ □■■□ □■■□ □□■□ □■□□ □■■□ □■□□ □□□□ □□□□ □□□□ □□□□ □□□□ □□□□

Let's convert the above 7 types of tetrimino into data

1.2 Answer

This is also like a list type that uses numerical values and letters as values when using it in games It will be easier to express if you use a data type with a fixed number of vertical and horizontal numbers.

If you think about 7 tetriminos independently and consider rotation etc. It would be easier to reuse if you define it as a list type of 4x4 squares.


line = [[0,1,0,0],[0,1,0,0],[0,1,0,0],[0,1,0,0]]
tblo = [[0,1,0,0],[0,1,1,0],[0,1,0,0],[0,0,0,0]]
lr   = [[0,1,0,0],[0,1,0,0],[0,1,1,0],[0,0,0,0]]
ll   = [[0,0,1,0],[0,0,1,0],[0,1,1,0],[0,0,0,0]]
hr   = [[0,1,0,0],[0,1,1,0],[0,0,1,0],[0,0,0,0]]
hl   = [[0,0,1,0],[0,1,1,0],[0,1,0,0],[0,0,0,0]]
sq   = [[0,0,0,0],[0,1,1,0],[0,1,1,0],[0,0,0,0]]

t_minos = [line,tblo,lr,ll,hr,hl,sq]

for mino in t_minos:
    for y in mino:
        for x in y:
            print('■' if x==1 else '□',end='')
        print()
    print()

□■□□ □■□□ □■□□ □■□□

□■□□ □■■□ □■□□ □□□□

□■□□ □■□□ □■■□ □□□□

□□■□ □□■□ □■■□ □□□□

□■□□ □■■□ □□■□ □□□□

□□■□ □■■□ □■□□ □□□□

□□□□ □■■□ □■■□ □□□□

2. Let's create a function that rotates tetrimino

Using the tetrimino data I created earlier Let's create a function rotate that rotates by 90 degrees

def rotate(data ,angle):
processing
    return data

□■□□ □■■□ □■□□ □□□□

If you rotate this 90 degrees

□□□□ □■■■ □□■□ □□□□

2. Answer

First, prepare the tetrimino data.

data = [[0,1,0,0],[0,1,1,0],[0,1,0,0],[0,0,0,0]]

It is troublesome to write the drawing code every time, so make it a function.

def print_block(block):
    for y in block:
        for x in y:
            print('■' if x==1 else '□',end='')
        print()
print_block(data)

□■□□ □■■□ □■□□ □□□□

2. How to use answer [:: -1] and zip function

Using [:: -1] on a list will make a shallow copy of the original list in reverse order. The zip function repeatedly extracts one element from the beginning of each argument to create a Tuple.

When combined, the elements are rotated 90 degrees.

data = [[0,1,0,0],[0,1,1,0],[0,1,0,0],[0,0,0,0]]
print_block(data[::-1])

□□□□ □■□□ □■■□ □■□□

print_block(zip(*data[::-1]))

□□□□ □■■■ □□■□ □□□□

To summarize in a function

def rotate(data,num):
    tmp = data.copy()
    if num==0:
        return tmp
    rotated = list(map(list, zip(*tmp[::-1])))
    for i in range(num//90-1):
        rotated = list(map(list, zip(*rotated[::-1])))
    return rotated
​
data = [[0,1,0,0],[0,1,1,0],[0,1,0,0],[0,0,0,0]]
block = rotate(data,270)
print_block(block)

□□□□ □■□□ ■■■□ □□□□

2. Answer How to use numpy library

The numpy library contains such matrix-like data Some have the ability to operate.

numpy.rot90 (data, rotation speed)

Rotate the array of numbers 90 degrees counterclockwise Specify 0 ~ for the number of rotations, and rotate 90 degrees with 1 Clockwise when the number of revolutions is set to-

import numpy as np

def rotate2(data,num):
    return np.rot90(data.copy(),-num//90)

data = [[0,1,0,0],[0,1,1,0],[0,1,0,0],[0,0,0,0]]
block = rotate2(data,90)
print_block(block)

□□□□ □■■■ □□■□ □□□□

This can be done in 2 lines.

Summary

How to think about data structure and how to operate data A puzzle game is recommended.

We will continue to do so, so please try to solve it in various ways.

Well then.

Author information

Otsu py's HP: http://www.otupy.net/

Youtube: https://www.youtube.com/channel/UCaT7xpeq8n1G_HcJKKSOXMw

Twitter: https://twitter.com/otupython

Recommended Posts

Programming improvement course 1
Programming improvement course 2
Programming Improvement Course 4: Pixel Logic
Poop programming
Graphic programming
Shell programming