Path: blob/master/python-igra-tetris/main_pygame.py
5925 views
import pygame1from copy import deepcopy2from random import choice, randrange34W, H = 10, 205TILE = 456GAME_RES = W * TILE, H * TILE7RES = 750, 9408FPS = 60910pygame.init()11sc = pygame.display.set_mode(RES)12game_sc = pygame.Surface(GAME_RES)13clock = pygame.time.Clock()1415grid = [pygame.Rect(x * TILE, y * TILE, TILE, TILE) for x in range(W) for y in range(H)]1617figures_pos = [[(-1, 0), (-2, 0), (0, 0), (1, 0)],18[(0, -1), (-1, -1), (-1, 0), (0, 0)],19[(-1, 0), (-1, 1), (0, 0), (0, -1)],20[(0, 0), (-1, 0), (0, 1), (-1, -1)],21[(0, 0), (0, -1), (0, 1), (-1, -1)],22[(0, 0), (0, -1), (0, 1), (1, -1)],23[(0, 0), (0, -1), (0, 1), (-1, 0)]]2425figures = [[pygame.Rect(x + W // 2, y + 1, 1, 1) for x, y in fig_pos] for fig_pos in figures_pos]26figure_rect = pygame.Rect(0, 0, TILE - 2, TILE - 2)27field = [[0 for i in range(W)] for j in range(H)]2829anim_count, anim_speed, anim_limit = 0, 60, 20003031bg = pygame.image.load('img/bg.jpg').convert()32game_bg = pygame.image.load('img/bg2.jpg').convert()3334main_font = pygame.font.Font('font/font.ttf', 65)35font = pygame.font.Font('font/font.ttf', 45)3637title_tetris = main_font.render('TETRIS', True, pygame.Color('red'))38title_score = font.render('score:', True, pygame.Color('white'))39title_record = font.render('record:', True, pygame.Color('white'))4041get_color = lambda : (randrange(30, 256), randrange(30, 256), randrange(30, 256))4243figure, next_figure = deepcopy(choice(figures)), deepcopy(choice(figures))44color, next_color = get_color(), get_color()4546score, lines = 0, 047scores = {0: 0, 1: 100, 2: 300, 3: 700, 4: 1500}484950def check_borders():51if figure[i].x < 0 or figure[i].x > W - 1:52return False53elif figure[i].y > H - 1 or field[figure[i].y][figure[i].x]:54return False55return True565758def get_record():59try:60with open('record') as f:61return f.readline()62except FileNotFoundError:63with open('record', 'w') as f:64f.write('0')656667def set_record(record, score):68rec = max(int(record), score)69with open('record', 'w') as f:70f.write(str(rec))717273while True:74record = get_record()75dx, rotate = 0, False76sc.blit(bg, (0, 0))77sc.blit(game_sc, (20, 20))78game_sc.blit(game_bg, (0, 0))79# delay for full lines80for i in range(lines):81pygame.time.wait(200)82# control83for event in pygame.event.get():84if event.type == pygame.QUIT:85exit()86if event.type == pygame.KEYDOWN:87if event.key == pygame.K_LEFT:88dx = -189elif event.key == pygame.K_RIGHT:90dx = 191elif event.key == pygame.K_DOWN:92anim_limit = 10093elif event.key == pygame.K_UP:94rotate = True95# move x96figure_old = deepcopy(figure)97for i in range(4):98figure[i].x += dx99if not check_borders():100figure = deepcopy(figure_old)101break102# move y103anim_count += anim_speed104if anim_count > anim_limit:105anim_count = 0106figure_old = deepcopy(figure)107for i in range(4):108figure[i].y += 1109if not check_borders():110for i in range(4):111field[figure_old[i].y][figure_old[i].x] = color112figure, color = next_figure, next_color113next_figure, next_color = deepcopy(choice(figures)), get_color()114anim_limit = 2000115break116# rotate117center = figure[0]118figure_old = deepcopy(figure)119if rotate:120for i in range(4):121x = figure[i].y - center.y122y = figure[i].x - center.x123figure[i].x = center.x - x124figure[i].y = center.y + y125if not check_borders():126figure = deepcopy(figure_old)127break128# check lines129line, lines = H - 1, 0130for row in range(H - 1, -1, -1):131count = 0132for i in range(W):133if field[row][i]:134count += 1135field[line][i] = field[row][i]136if count < W:137line -= 1138else:139anim_speed += 3140lines += 1141# compute score142score += scores[lines]143# draw grid144[pygame.draw.rect(game_sc, (40, 40, 40), i_rect, 1) for i_rect in grid]145# draw figure146for i in range(4):147figure_rect.x = figure[i].x * TILE148figure_rect.y = figure[i].y * TILE149pygame.draw.rect(game_sc, color, figure_rect)150# draw field151for y, raw in enumerate(field):152for x, col in enumerate(raw):153if col:154figure_rect.x, figure_rect.y = x * TILE, y * TILE155pygame.draw.rect(game_sc, col, figure_rect)156# draw next figure157for i in range(4):158figure_rect.x = next_figure[i].x * TILE + 380159figure_rect.y = next_figure[i].y * TILE + 185160pygame.draw.rect(sc, next_color, figure_rect)161# draw titles162sc.blit(title_tetris, (505, 30))163sc.blit(title_score, (535, 780))164sc.blit(font.render(str(score), True, pygame.Color('white')), (550, 840))165sc.blit(title_record, (525, 650))166sc.blit(font.render(record, True, pygame.Color('gold')), (550, 710))167# game over168for i in range(W):169if field[0][i]:170set_record(record, score)171field = [[0 for i in range(W)] for i in range(H)]172anim_count, anim_speed, anim_limit = 0, 60, 2000173score = 0174for i_rect in grid:175pygame.draw.rect(game_sc, get_color(), i_rect)176sc.blit(game_sc, (20, 20))177pygame.display.flip()178clock.tick(200)179180pygame.display.flip()181clock.tick(FPS)182183184