| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- #!/usr/bin/python3
- """
- Competitive tetris server
- """
- import random
- import pygame
- import tetrominos
- import game
- 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()
- 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()
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_LEFT:
- game_state = game.game_state_shift(game_state, -1)
- if event.key == pygame.K_RIGHT:
- game_state = game.game_state_shift(game_state, 1)
- if event.key == pygame.K_UP:
- game_state = game.game_state_rotate(game_state)
- if event.key == pygame.K_DOWN:
- game_state = game.game_state_land(game_state)
- 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)
|