competitive_tetris.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #!/usr/bin/python3
  2. """
  3. Competitive tetris server
  4. """
  5. import random
  6. import pygame
  7. import zmq
  8. import flatbuffers
  9. import tetrominos
  10. import game
  11. import wire_protocol.Command
  12. import wire_protocol.CommandRequest
  13. import wire_protocol.CommandResponse
  14. import wire_protocol.CommandResponseError
  15. pygame.font.init()
  16. # GLOBALS VARS
  17. S_WIDTH = 800
  18. S_HEIGHT = 700
  19. PLAY_WIDTH = 300 # meaning 300 // 10 = 30 width per block
  20. PLAY_HEIGHT = 600 # meaning 600 // 20 = 30 height per block
  21. BLOCK_SIZE = 30
  22. TOP_LEFT_X = (S_WIDTH - PLAY_WIDTH) // 2
  23. TOP_LEFT_Y = S_HEIGHT - PLAY_HEIGHT
  24. def draw_text_middle(surface, text, size, color):
  25. font = pygame.font.SysFont("comicsans", size, bold=True)
  26. label = font.render(text, 1, color)
  27. surface.blit(label, (TOP_LEFT_X + PLAY_WIDTH /2 - (label.get_width()/2), TOP_LEFT_Y + PLAY_HEIGHT/2 - label.get_height()/2))
  28. def draw_grid(surface, grid):
  29. sx = TOP_LEFT_X
  30. sy = TOP_LEFT_Y
  31. for i in range(len(grid)):
  32. pygame.draw.line(surface, (128,128,128), (sx, sy + i*BLOCK_SIZE), (sx+PLAY_WIDTH, sy+ i*BLOCK_SIZE))
  33. for j in range(len(grid[i])):
  34. pygame.draw.line(surface, (128, 128, 128), (sx + j*BLOCK_SIZE, sy),(sx + j*BLOCK_SIZE, sy + PLAY_HEIGHT))
  35. def draw_next_shape(shape, surface):
  36. font = pygame.font.SysFont('comicsans', 30)
  37. label = font.render('Next Shape', 1, (255,255,255))
  38. sx = TOP_LEFT_X + PLAY_WIDTH + 0
  39. sy = TOP_LEFT_Y + PLAY_HEIGHT/2 - 100
  40. shape_raw = tetrominos.SHAPES_RAW[shape][0]
  41. for i, line in enumerate(shape_raw):
  42. row = list(line)
  43. for j, column in enumerate(row):
  44. if column == '0':
  45. pygame.draw.rect(
  46. surface, (255,0,0),
  47. (sx + j*BLOCK_SIZE, sy + i*BLOCK_SIZE,
  48. BLOCK_SIZE, BLOCK_SIZE),
  49. 0)
  50. surface.blit(label, (sx + 10, sy - 30))
  51. def draw_game_state_to_window(surface, game_state):
  52. surface.fill((0, 0, 0))
  53. pygame.font.init()
  54. font = pygame.font.SysFont('comicsans', 60)
  55. label = font.render('Tetris', 1, (255, 255, 255))
  56. surface.blit(label,
  57. (TOP_LEFT_X + PLAY_WIDTH / 2 - (label.get_width() / 2), 30))
  58. # current score
  59. font = pygame.font.SysFont('comicsans', 30)
  60. label = font.render(f'Score: {game_state.score}', 1, (255,255,255))
  61. sx = TOP_LEFT_X + PLAY_WIDTH + 50
  62. sy = TOP_LEFT_Y + PLAY_HEIGHT/2 - 100
  63. surface.blit(label, (sx + 20, sy + 160))
  64. field = game.get_game_state_field(game_state)
  65. for row in range(field.shape[0]):
  66. for col in range(field.shape[1]):
  67. color = (0,0,0)
  68. tile = field[row,col]
  69. if tile == 1:
  70. color = (0,255,0)
  71. elif tile == 2:
  72. color = (255,0,0)
  73. pygame.draw.rect(surface, color,
  74. (TOP_LEFT_X + col*BLOCK_SIZE,
  75. TOP_LEFT_Y + row*BLOCK_SIZE,
  76. BLOCK_SIZE, BLOCK_SIZE), 0)
  77. pygame.draw.rect(surface, (255, 0, 0),
  78. (TOP_LEFT_X, TOP_LEFT_Y, PLAY_WIDTH, PLAY_HEIGHT), 5)
  79. draw_grid(surface, field)
  80. def main(win):
  81. run = True
  82. frame_time = 0
  83. clock = pygame.time.Clock()
  84. zmq_context = zmq.Context()
  85. socket = zmq_context.socket(zmq.REP)
  86. socket.bind("tcp://*:5555")
  87. poller = zmq.Poller()
  88. poller.register(socket, zmq.POLLIN)
  89. def _next_piece_func():
  90. return random.choice(range(len(tetrominos.SHAPES)))
  91. game_state = game.initial_game_state(_next_piece_func(),
  92. _next_piece_func())
  93. while run:
  94. clock.tick()
  95. dt_millis = clock.get_time()
  96. frame_time += dt_millis
  97. for event in pygame.event.get():
  98. if event.type == pygame.QUIT:
  99. run = False
  100. pygame.display.quit()
  101. for sock, _ in poller.poll(timeout=0):
  102. req_buf = sock.recv()
  103. req = wire_protocol.CommandRequest.CommandRequest.GetRootAs(req_buf)
  104. error_code = wire_protocol.CommandResponseError.CommandResponseError.OK
  105. if req.Command() == wire_protocol.Command.Command.ShiftLeft:
  106. game_state = game.game_state_shift(game_state, -1)
  107. elif req.Command() == wire_protocol.Command.Command.ShiftRight:
  108. game_state = game.game_state_shift(game_state, 1)
  109. elif req.Command() == wire_protocol.Command.Command.Rotate:
  110. game_state = game.game_state_rotate(game_state)
  111. elif req.Command() == wire_protocol.Command.Command.Land:
  112. game_state = game.game_state_land(game_state)
  113. else:
  114. error_code = wire_protocol.CommandResponseError.CommandResponseError.UNKNOWN_COMMAND
  115. builder = flatbuffers.Builder()
  116. wire_protocol.CommandResponse.Start(builder)
  117. wire_protocol.CommandResponse.AddError(builder, error_code)
  118. rsp = wire_protocol.CommandResponse.End(builder)
  119. builder.Finish(rsp)
  120. socket.send(builder.Output())
  121. is_game_running = True
  122. if frame_time >= 500:
  123. game_state, is_game_running = game.advance_game_state(
  124. game_state, _next_piece_func)
  125. frame_time = 0
  126. draw_game_state_to_window(win, game_state)
  127. draw_next_shape(game_state.next_piece, win)
  128. pygame.display.update()
  129. if not is_game_running:
  130. draw_text_middle(win, "YOU LOST!", 80, (255,255,255))
  131. pygame.display.update()
  132. pygame.time.delay(1500)
  133. run = False
  134. def main_menu(win):
  135. run = True
  136. while run:
  137. win.fill((0,0,0))
  138. draw_text_middle(win, 'Press Any Key To Play', 60, (255,255,255))
  139. pygame.display.update()
  140. for event in pygame.event.get():
  141. if event.type == pygame.QUIT:
  142. run = False
  143. if event.type == pygame.KEYDOWN:
  144. main(win)
  145. pygame.display.quit()
  146. win = pygame.display.set_mode((S_WIDTH, S_HEIGHT))
  147. pygame.display.set_caption('Tetris')
  148. main_menu(win)