Pygame is a set of Python program modules for developing game software, based on the SDL library. Pygame allows you to create feature-rich games and multimedia programs within Python programs. Pygame is a highly portable module that supports multiple operating systems. It’s great for developing small games.
Today is to use Pygame to write tetris this small game.
# S form squares S_BLOCK = [Block ([' OO ', 'OO.', '... '), Point (0, 0), Point (2, 1), 'S', 1), Block ([' O.. ', 'OO.', '. O. '], Point (0, 0), Point (1, 2), 'S', 0)] # Z square Z_BLOCK = [Block ([' OO. ', 'OO', '... '), Point (0, 0), Point (2, 1), 'Z', 1), Block (['. O. ', 'OO.', 'O..'], Point (0, 0), Point (1, 2), 'Z', 0) I_BLOCK = [Block] # type I squares (['. O.. ', '. O.. ', '. O.. ', '.O..'], Point(1, 0), Point(1, 3), 'I', 1), Block(['....', '....', 'OOOO', '....'], Point(0, 2), Point(3, 2), 'I', O_BLOCK = 0)] # O squares [Block ([' OO ', 'OO], Point (0, 0), Point (1, 1),' O ', 0)] # L square L_BLOCK = [Block (['.. O ', 'OOO, '...'], Point(0, 0), Point(2, 1), 'L', 1), Block(['.O.', '.O.', '.OO'], Point(1, 0), Point(2, 2), 'L', 2), Block(['...', 'OOO', 'O..'], Point(0, 1), Point(2, 2), 'L', 3), Block(['OO.', '.O.', '.O.'], Point(0, 0), Point(1, 2), 'L', 0)] # T square T_BLOCK = [Block (['. O. ', 'OOO', '... '), Point (0, 0), Point (2, 1), 'T', 1), Block (['. O. ', 'OO', '. O. ']. Point(1, 0), Point(2, 2), 'T', 2), Block(['...', 'OOO', '.O.'], Point(0, 1), Point(2, 2), 'T', 3), Block(['.O.', 'OO.', '.O.'], Point(0, 0), Point(1, 2), 'T', 0)] BLOCKS = {'O': O_BLOCK, 'I': I_BLOCK, 'Z': Z_BLOCK, 'T': T_BLOCK, 'L': L_BLOCK, 'S': S_BLOCK,}Copy the code
First, we need to define the shape of Tetris. This is the first step. We need to know what it looks like in space.
if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: if not game_over and not pause: If time.time() - last_press_time > 0.1: last_press_time = time.time() if cur_POS_x > -cur_block.start_pos. X: if _judge(cur_pos_x - 1, cur_pos_y, cur_block): cur_pos_x -= 1 if event.key == pygame.K_RIGHT: If not game_over and not pause: if time.time() - last_press_time > 0.1: Last_press_time = time.time() # if cur_pos_x + cur_block.end_pos.X + 1 < BLOCK_WIDTH: last_press_time = time.time() # if cur_pos_x + cur_block.end_pos.X + 1 < BLOCK_WIDTH: if _judge(cur_pos_x + 1, cur_pos_y, cur_block): cur_pos_x += 1 if event.key == pygame.K_DOWN: If not game_over and not pause: if time.time() - last_press_time > 0.1: last_press_time = time.time() if not _judge(cur_pos_x, cur_pos_y + 1, cur_block): _dock() else: last_drop_time = time.time() cur_pos_y += 1Copy the code
This is a piece of code that says you can use the keyboard keys to manipulate Tetris, and then you press the left and right key, and you can move it left and right, and you press the key, and you accelerate it down.
def _dock(): nonlocal cur_block, next_block, game_area, cur_pos_x, cur_pos_y, game_over, score, speed for _i in range(cur_block.start_pos.Y, cur_block.end_pos.Y + 1): for _j in range(cur_block.start_pos.X, cur_block.end_pos.X + 1): if cur_block.template[_i][_j] ! = '.': game_area[cur_pos_y + _i][cur_pos_x + _j] = '0' if cur_pos_y + cur_block.start_pos.Y <= 0: Game_over = True else: # remove remove_idxs = [] for _i in range(cur_block.start_pos.Y, cur_block.end_pos.Y + 1): if all(_x == '0' for _x in game_area[cur_pos_y + _i]): remove_idxs.append(cur_pos_y + _i) if remove_idxs: Remove_count = len(remove_idxs) if remove_count == 1: score += 100 elif count == 2: score += 300 elif remove_count == 3: score += 700 elif remove_count == 4: Score += 1500 speed = orispeed-0.03 * (score // 10000) # remove_i = _j = remove_idxs[-1] while _i >= 0: while _j in remove_idxs: _j -= 1 if _j < 0: game_area[_i] = ['.'] * BLOCK_WIDTH else: game_area[_i] = game_area[_j] _i -= 1 _j -= 1 cur_block = next_block next_block = blocks.get_block() cur_pos_x, cur_pos_y = (BLOCK_WIDTH - cur_block.end_pos.X - 1) // 2, -1 - cur_blocCopy the code
Finally, you need to determine if the game is over when the square hits the bottom. And then whether to eliminate the square, give extra points.
In general, the game logic is not too difficult, interested students can pay attention to the public account: poetry like code, find me to discuss learning.