#!/usr/bin/python3 """ Competitive tetris server """ import random import pygame import zmq import flatbuffers import tetrominos import game import wire_protocol.Command import wire_protocol.CommandRequest import wire_protocol.CommandResponse import wire_protocol.Playfield import wire_protocol.CommandResponseError pygame.font.init() # GLOBALS VARS S_WIDTH = 800 S_HEIGHT = 700 PLAY_WIDTH = 300 # meaning 300 // 10 = 30 width per block PLAY_HEIGHT = 600 # meaning 600 // 20 = 30 height per block BLOCK_SIZE = 30 TOP_LEFT_X = (S_WIDTH - PLAY_WIDTH) // 2 TOP_LEFT_Y = S_HEIGHT - PLAY_HEIGHT def draw_text_middle(surface, text, size, color): font = pygame.font.SysFont("comicsans", size, bold=True) label = font.render(text, 1, color) surface.blit(label, (TOP_LEFT_X + PLAY_WIDTH /2 - (label.get_width()/2), TOP_LEFT_Y + PLAY_HEIGHT/2 - label.get_height()/2)) def draw_grid(surface, grid): sx = TOP_LEFT_X sy = TOP_LEFT_Y for i in range(len(grid)): pygame.draw.line(surface, (128,128,128), (sx, sy + i*BLOCK_SIZE), (sx+PLAY_WIDTH, sy+ i*BLOCK_SIZE)) for j in range(len(grid[i])): pygame.draw.line(surface, (128, 128, 128), (sx + j*BLOCK_SIZE, sy),(sx + j*BLOCK_SIZE, sy + PLAY_HEIGHT)) def draw_next_shape(shape, surface): font = pygame.font.SysFont('comicsans', 30) label = font.render('Next Shape', 1, (255,255,255)) sx = TOP_LEFT_X + PLAY_WIDTH + 0 sy = TOP_LEFT_Y + PLAY_HEIGHT/2 - 100 shape_raw = tetrominos.SHAPES_RAW[shape][0] for i, line in enumerate(shape_raw): row = list(line) for j, column in enumerate(row): if column == '0': pygame.draw.rect( surface, (255,0,0), (sx + j*BLOCK_SIZE, sy + i*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0) surface.blit(label, (sx + 10, sy - 30)) def draw_game_state_to_window(surface, game_state): surface.fill((0, 0, 0)) pygame.font.init() font = pygame.font.SysFont('comicsans', 60) label = font.render('Tetris', 1, (255, 255, 255)) surface.blit(label, (TOP_LEFT_X + PLAY_WIDTH / 2 - (label.get_width() / 2), 30)) # current score font = pygame.font.SysFont('comicsans', 30) label = font.render(f'Score: {game_state.score}', 1, (255,255,255)) sx = TOP_LEFT_X + PLAY_WIDTH + 50 sy = TOP_LEFT_Y + PLAY_HEIGHT/2 - 100 surface.blit(label, (sx + 20, sy + 160)) field = game.get_game_state_field(game_state) for row in range(field.shape[0]): for col in range(field.shape[1]): color = (0,0,0) tile = field[row,col] if tile == 1: color = (0,255,0) elif tile == 2: color = (255,0,0) pygame.draw.rect(surface, color, (TOP_LEFT_X + col*BLOCK_SIZE, TOP_LEFT_Y + row*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0) pygame.draw.rect(surface, (255, 0, 0), (TOP_LEFT_X, TOP_LEFT_Y, PLAY_WIDTH, PLAY_HEIGHT), 5) draw_grid(surface, field) def main(win): run = True frame_time = 0 clock = pygame.time.Clock() zmq_context = zmq.Context() socket = zmq_context.socket(zmq.REP) socket.bind("tcp://*:5555") poller = zmq.Poller() poller.register(socket, zmq.POLLIN) def _next_piece_func(): return random.choice(range(len(tetrominos.SHAPES))) game_state = game.initial_game_state(_next_piece_func(), _next_piece_func()) while run: clock.tick() dt_millis = clock.get_time() frame_time += dt_millis for event in pygame.event.get(): if event.type == pygame.QUIT: run = False pygame.display.quit() for sock, _ in poller.poll(timeout=0): req_buf = sock.recv() req = wire_protocol.CommandRequest.CommandRequest.GetRootAs(req_buf) error_code = wire_protocol.CommandResponseError.CommandResponseError.OK cells = None if req.Command() == wire_protocol.Command.Command.ShiftLeft: game_state = game.game_state_shift(game_state, -1) elif req.Command() == wire_protocol.Command.Command.ShiftRight: game_state = game.game_state_shift(game_state, 1) elif req.Command() == wire_protocol.Command.Command.Rotate: game_state = game.game_state_rotate(game_state) elif req.Command() == wire_protocol.Command.Command.Land: game_state = game.game_state_land(game_state) elif req.Command() == wire_protocol.Command.Command.GetGameState: cells = game.get_game_state_field(game_state) else: error_code = wire_protocol.CommandResponseError.CommandResponseError.UNKNOWN_COMMAND builder = flatbuffers.Builder() playfield = None if cells is not None: wire_protocol.Playfield.StartCellsVector(builder, cells.size) builder.head -= cells.size builder.Bytes[builder.head:(builder.head + cells.size)] = cells.tobytes() cells_vector = builder.EndVector(cells.size) wire_protocol.Playfield.Start(builder) wire_protocol.Playfield.AddRows(builder, cells.shape[0]) wire_protocol.Playfield.AddCols(builder, cells.shape[1]) wire_protocol.Playfield.AddCells(builder, cells_vector) playfield = wire_protocol.Playfield.End(builder) wire_protocol.CommandResponse.Start(builder) wire_protocol.CommandResponse.AddError(builder, error_code) if playfield is not None: wire_protocol.CommandResponse.AddPlayfield(builder, playfield) rsp = wire_protocol.CommandResponse.End(builder) builder.Finish(rsp) socket.send(builder.Output()) is_game_running = True if frame_time >= 500: game_state, is_game_running = game.advance_game_state( game_state, _next_piece_func) frame_time = 0 draw_game_state_to_window(win, game_state) draw_next_shape(game_state.next_piece, win) pygame.display.update() if not is_game_running: draw_text_middle(win, "YOU LOST!", 80, (255,255,255)) pygame.display.update() pygame.time.delay(1500) run = False def main_menu(win): run = True while run: win.fill((0,0,0)) draw_text_middle(win, 'Press Any Key To Play', 60, (255,255,255)) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: run = False if event.type == pygame.KEYDOWN: main(win) pygame.display.quit() win = pygame.display.set_mode((S_WIDTH, S_HEIGHT)) pygame.display.set_caption('Tetris') main_menu(win)