competitive_tetris.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #!/usr/bin/python3
  2. """
  3. Competitive tetris server
  4. """
  5. import random
  6. import pygame
  7. import tetrominos
  8. import game
  9. pygame.font.init()
  10. # GLOBALS VARS
  11. S_WIDTH = 800
  12. S_HEIGHT = 700
  13. PLAY_WIDTH = 300 # meaning 300 // 10 = 30 width per block
  14. PLAY_HEIGHT = 600 # meaning 600 // 20 = 30 height per block
  15. BLOCK_SIZE = 30
  16. TOP_LEFT_X = (S_WIDTH - PLAY_WIDTH) // 2
  17. TOP_LEFT_Y = S_HEIGHT - PLAY_HEIGHT
  18. def draw_text_middle(surface, text, size, color):
  19. font = pygame.font.SysFont("comicsans", size, bold=True)
  20. label = font.render(text, 1, color)
  21. surface.blit(label, (TOP_LEFT_X + PLAY_WIDTH /2 - (label.get_width()/2), TOP_LEFT_Y + PLAY_HEIGHT/2 - label.get_height()/2))
  22. def draw_grid(surface, grid):
  23. sx = TOP_LEFT_X
  24. sy = TOP_LEFT_Y
  25. for i in range(len(grid)):
  26. pygame.draw.line(surface, (128,128,128), (sx, sy + i*BLOCK_SIZE), (sx+PLAY_WIDTH, sy+ i*BLOCK_SIZE))
  27. for j in range(len(grid[i])):
  28. pygame.draw.line(surface, (128, 128, 128), (sx + j*BLOCK_SIZE, sy),(sx + j*BLOCK_SIZE, sy + PLAY_HEIGHT))
  29. def draw_next_shape(shape, surface):
  30. font = pygame.font.SysFont('comicsans', 30)
  31. label = font.render('Next Shape', 1, (255,255,255))
  32. sx = TOP_LEFT_X + PLAY_WIDTH + 0
  33. sy = TOP_LEFT_Y + PLAY_HEIGHT/2 - 100
  34. shape_raw = tetrominos.SHAPES_RAW[shape][0]
  35. for i, line in enumerate(shape_raw):
  36. row = list(line)
  37. for j, column in enumerate(row):
  38. if column == '0':
  39. pygame.draw.rect(
  40. surface, (255,0,0),
  41. (sx + j*BLOCK_SIZE, sy + i*BLOCK_SIZE,
  42. BLOCK_SIZE, BLOCK_SIZE),
  43. 0)
  44. surface.blit(label, (sx + 10, sy - 30))
  45. def draw_game_state_to_window(surface, game_state):
  46. surface.fill((0, 0, 0))
  47. pygame.font.init()
  48. font = pygame.font.SysFont('comicsans', 60)
  49. label = font.render('Tetris', 1, (255, 255, 255))
  50. surface.blit(label,
  51. (TOP_LEFT_X + PLAY_WIDTH / 2 - (label.get_width() / 2), 30))
  52. # current score
  53. font = pygame.font.SysFont('comicsans', 30)
  54. label = font.render(f'Score: {game_state.score}', 1, (255,255,255))
  55. sx = TOP_LEFT_X + PLAY_WIDTH + 50
  56. sy = TOP_LEFT_Y + PLAY_HEIGHT/2 - 100
  57. surface.blit(label, (sx + 20, sy + 160))
  58. field = game.get_game_state_field(game_state)
  59. for row in range(field.shape[0]):
  60. for col in range(field.shape[1]):
  61. color = (0,0,0)
  62. tile = field[row,col]
  63. if tile == 1:
  64. color = (0,255,0)
  65. elif tile == 2:
  66. color = (255,0,0)
  67. pygame.draw.rect(surface, color,
  68. (TOP_LEFT_X + col*BLOCK_SIZE,
  69. TOP_LEFT_Y + row*BLOCK_SIZE,
  70. BLOCK_SIZE, BLOCK_SIZE), 0)
  71. pygame.draw.rect(surface, (255, 0, 0),
  72. (TOP_LEFT_X, TOP_LEFT_Y, PLAY_WIDTH, PLAY_HEIGHT), 5)
  73. draw_grid(surface, field)
  74. def main(win):
  75. run = True
  76. frame_time = 0
  77. clock = pygame.time.Clock()
  78. def _next_piece_func():
  79. return random.choice(range(len(tetrominos.SHAPES)))
  80. game_state = game.initial_game_state(_next_piece_func(),
  81. _next_piece_func())
  82. while run:
  83. clock.tick()
  84. dt_millis = clock.get_time()
  85. frame_time += dt_millis
  86. for event in pygame.event.get():
  87. if event.type == pygame.QUIT:
  88. run = False
  89. pygame.display.quit()
  90. if event.type == pygame.KEYDOWN:
  91. if event.key == pygame.K_LEFT:
  92. game_state = game.game_state_shift(game_state, -1)
  93. if event.key == pygame.K_RIGHT:
  94. game_state = game.game_state_shift(game_state, 1)
  95. if event.key == pygame.K_UP:
  96. game_state = game.game_state_rotate(game_state)
  97. if event.key == pygame.K_DOWN:
  98. game_state = game.game_state_land(game_state)
  99. is_game_running = True
  100. if frame_time >= 500:
  101. game_state, is_game_running = game.advance_game_state(
  102. game_state, _next_piece_func)
  103. frame_time = 0
  104. draw_game_state_to_window(win, game_state)
  105. draw_next_shape(game_state.next_piece, win)
  106. pygame.display.update()
  107. if not is_game_running:
  108. draw_text_middle(win, "YOU LOST!", 80, (255,255,255))
  109. pygame.display.update()
  110. pygame.time.delay(1500)
  111. run = False
  112. def main_menu(win):
  113. run = True
  114. while run:
  115. win.fill((0,0,0))
  116. draw_text_middle(win, 'Press Any Key To Play', 60, (255,255,255))
  117. pygame.display.update()
  118. for event in pygame.event.get():
  119. if event.type == pygame.QUIT:
  120. run = False
  121. if event.type == pygame.KEYDOWN:
  122. main(win)
  123. pygame.display.quit()
  124. win = pygame.display.set_mode((S_WIDTH, S_HEIGHT))
  125. pygame.display.set_caption('Tetris')
  126. main_menu(win)