Exemple #1
0
class LanSecondPlayer(SecondPlayer):
    def __init__(self, menu: Menu, lan):
        self.figure = Figure([])  # type: Figure
        self.stage = Stage()  # type: Stage
        self.stage.block_drawer = WiredBlockDrawer()
        self.figure.block_drawer = GhostBlockDrawer()
        self.menu = menu
        self.lan = lan

    def receive_data(self):
        attack = 0
        data = self.lan.get_data()

        if data:
            data_array = eval(data)
            if len(data_array) == 7:
                self.figure.blocks = data_array[0]
                self.figure.x = data_array[1]
                self.figure.y = data_array[2]
                self.stage.blocks = data_array[3]
                self.stage.score = data_array[4]
                self.stage.completed_lines = data_array[5]
                attack = data_array[6]

        return attack

    def send_data(self, figure: Figure, stage: Stage, attack_power):
        data_array = list()
        data_array.append(figure.blocks)
        data_array.append(figure.x)
        data_array.append(figure.y)
        data_array.append(stage.blocks)
        data_array.append(stage.score)
        data_array.append(stage.completed_lines)
        data_array.append(attack_power)
        self.lan.send_data(data_array)

    def draw(self, stage_surface, menu_surface):
        self.stage.draw(stage_surface)
        self.figure.draw(stage_surface)
        self.menu.draw_second_player(menu_surface, self.stage.score,
                                     self.stage.completed_lines)
Exemple #2
0
class GameLogic:
    def __init__(self, config):
        self.config = config
        self.falling_cycles = int(config.falling_rate / config.refresh_rate)
        self.current_cycle_count = 0
        self.tetromino = None
        self.score = 0
        self.game_ended = False

    def is_step_down_cycle(self):
        self.current_cycle_count = (self.current_cycle_count +
                                    1) % self.falling_cycles
        if self.current_cycle_count == 0:
            return True
        return False

    def set_game_ended(self, has_ended):
        self.game_ended = has_ended

    def get_score(self):
        return self.score

    def has_full_row(self):
        # start check with the bottom line
        reverse_grid = copy.deepcopy(self.config.grid)
        reverse_grid.reverse()
        for i, line in enumerate(reverse_grid):
            full_blocks = 0
            for block in line:
                if block == "X":
                    full_blocks += 1
            if full_blocks == len(line):
                self.score += 1
                del self.config.grid[len(self.config.grid) - i - 1]
                self.config.grid.insert(
                    0, [" " for _ in range(self.config.columns)])
                return True
        return False

    def do_logic(self, input_key):
        if not self.game_ended:
            self.has_full_row()

            if self.tetromino is None or self.tetromino.is_dead:
                self.tetromino = Figure(self.config.columns, self.config.line,
                                        self.config)
            else:
                if input_key == 100:
                    self.tetromino.move_right()
                if input_key == 97:
                    self.tetromino.move_left()
                if input_key == 115:
                    self.tetromino.move_to_bottom()
                if input_key == 119:
                    self.tetromino.rotate_right()

                if self.is_step_down_cycle():
                    self.tetromino.move_step_down()

                    if self.tetromino is not None and self.tetromino.y <= 0:
                        raise Exception("Initial collision")

        return self.tetromino.draw(copy.deepcopy(self.config.grid))
Exemple #3
0
class App:
    def __init__(self):
        # self.running = True
        self.block_size = 50
        self.block_count = (10, 15)
        self.res = (self.block_count[0] * self.block_size,
                    self.block_count[1] * self.block_size)
        self.decor_grid = [
            pygame.Rect(x * self.block_size, y * self.block_size,
                        self.block_size, self.block_size)
            for x in range(self.block_count[0])
            for y in range(self.block_count[1])
        ]

        self.figures_grid = [[0 for x in range(self.block_count[0] + 2)]
                             for y in range(self.block_count[1])]

        self.screen = pygame.display.set_mode(self.res)
        self.figure = Figure(self.screen, self.block_size, self.block_count,
                             self.figures_grid)
        self.keys = None
        self.clock = pygame.time.Clock()
        pygame.init()
        # [pygame.draw.rect(self.screen, (255, 255, 255), block, 1) for block in self.grid]
        # self.figure.draw()
        # pygame.display.flip()

    # figure1 = [[(-1, -1), (-2, -1), (0, -1), (1, -1)]]
    # figure1 = [[(0, -1), (0, -2), (1, -2), (0, 0)]]

    # def move():
    #     for i in range(4):
    #         figure2[i].y += 1

    def event_loop(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()

            elif event.type in (pygame.KEYDOWN, pygame.KEYUP):
                self.keys = pygame.key.get_pressed()

    def update(self, dt):
        self.figure.update(self.keys, dt)
        # self.figure.update(self.keys)
        # self.figure.figure[0].y += 1
        # a = deepcopy(self.figure)
        # self.figure.update(self.keys)
        # self.figure.move()
        # self.figure.move()
        # self.figure.check_borders()
        # self.figure.move_with_check_borders()

    # if figure2[0].y * self.block_size >= blocks_count[1] * block_size:
    #     figure2 = a

    def render(self):
        self.screen.fill(pygame.Color('black'))
        [
            pygame.draw.rect(self.screen, (255, 255, 255), block, 1)
            for block in self.decor_grid
        ]
        self.figure.draw()
        self.draw_finished_figures()

        pygame.display.flip()

    def draw_finished_figures(self):
        for y, row in enumerate(self.figures_grid):
            for x, column in enumerate(row):
                if column:
                    # pygame.draw.rect(self.screen, (255, 255, 255), pygame.Rect(x, y, 50, 50), 50)
                    # self.rect.x = self.figure[i].x * self.block_size
                    # self.rect.y = self.figure[i].y * self.block_size
                    self.figure.rect.x, self.figure.rect.y = x * self.block_size, y * self.block_size
                    pygame.draw.rect(self.screen, (255, 255, 255),
                                     self.figure.rect)

    def run(self):
        self.keys = pygame.key.get_pressed()
        while True:
            dt = self.clock.tick(5)
            # print(dt)
            self.event_loop()
            self.update(dt)
            self.render()