Ejemplo n.º 1
0
    else:
        gameBackground = Menu.Background("game_background_Dark.jpg",
                                         [0, -660 + size[1]])

    try:
        while MainState == "start":

            if game.figure is None:
                game.new_block()
            counter += 1
            if counter > 100000:
                counter = 0

            if counter % (fps // game.level // 2) == 0 or pressing_down:
                if game.gameState == "start":
                    game.go_down()

            for event in pygame.event.get():
                if event.type == pygame.QUIT or event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                    t = time.time() + 5
                    if not game.gameState == "gameover":

                        while time.time() < t:
                            g = pygame.event.poll()
                            pygame.draw.rect(screen, WHITE, [10, 190, 220, 50])
                            textsave = font.render(
                                "Save?(Enter) " +
                                str(int((t - time.time()) % 10)), True,
                                (255, 125, 0))
                            screen.blit(text_game_over2, [1, size[1] - 85])
                            screen.blit(textsave, [20, 200])
Ejemplo n.º 2
0
class TetrisEngine(InitEnvironment):
    def __init__(self, *args, **kwargs):

        super(TetrisEngine, self).__init__(*args, **kwargs)
        self._init_environment = super(TetrisEngine, self)
        self.BLACK = self._init_environment.BLACK
        self.WHITE = self._init_environment.WHITE
        self.GRAY = self._init_environment.GRAY
        self.CYAN = self._init_environment.CYAN
        self.clock = pygame.time.Clock()
        self.fps = 25
        self.flag_set_level = False
        self.game = Tetris(20, 10)
        self.counter = 0
        self.pressing_down = False



    def __enter__(self):
        self.clock = pygame.time.Clock()
        self.fps = 25
        self.flag_set_level = False
        self.game = Tetris(20, 10)
        self.counter = 0
        self.pressing_down = False
        return self

    def restart(self):
        self.game.state = "start"
        _field = np.asarray(self.game.field)
        _field = np.zeros(_field.shape)
        self.game.field = [list(field) for field in _field]

    def quit(self):
        pygame.display.quit()

    def __call__(self ):
        done = False
        _lines_popped = 0

        if self.game.figure is None:
            self.game.new_figure()
        # TODO @HIGH #discuss self.counter
        self.counter += 1
        if self.counter > 100000:
            self.counter = 0

        if self.counter % (self.fps // self.game.level // 2) == 0 or self.pressing_down:
            if self.game.state == "start":
                _lines_popped = self.game.go_down()

        if self.game.score % 1 == 0 and self.game.score > 0 and self.flag_set_level:
            if (self.fps // self.game.level // 2) == 1:
                pass
                #self.screen.blit(text_game_master, [10, 200])
            else:
                self.game.level += 1
                self.flag_set_level = False
                self.screen.blit(text_game_score_update, [10, 200])

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    self.game.rotate()
                if event.key == pygame.K_DOWN:
                    self.pressing_down = True
                if event.key == pygame.K_LEFT:
                    self.game.go_side(-1)
                if event.key == pygame.K_RIGHT:
                    self.game.go_side(1)
                if event.key == pygame.K_SPACE:
                    _lines_popped = self.game.go_space()

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_DOWN:
                    self.pressing_down = False

        self.screen.fill(self.CYAN)

        for i in range(self.game.height):
            for j in range(self.game.width):
                pygame.draw.rect(
                    self.screen,
                    self.GRAY,
                    [
                        self.game.x + self.game.zoom * j,
                        self.game.y + self.game.zoom * i,
                        self.game.zoom,
                        self.game.zoom,
                    ],
                    1,
                )
                if self.game.field[i][j] > 0:
                    pygame.draw.rect(
                        self.screen,
                        self.colors[self.game.field[i][j]],
                        [
                            self.game.x + self.game.zoom * j + 1,
                            self.game.y + self.game.zoom * i + 1,
                            self.game.zoom - 2,
                            self.game.zoom - 1,
                        ],
                    )

        if self.game.figure is not None:
            for i in range(4):
                for j in range(4):
                    p = i * 4 + j
                    if p in self.game.figure.image():
                        pygame.draw.rect(
                            self.screen,
                            self.colors[self.game.figure.color],
                            [
                                self.game.x + self.game.zoom * (j + self.game.figure.x) + 1,
                                self.game.y + self.game.zoom * (i + self.game.figure.y) + 1,
                                self.game.zoom - 2,
                                self.game.zoom - 2,
                            ],
                        )
        # Loop until the user clicks the close button.
        font = pygame.font.SysFont("Calibri", 25, True, False)
        font1 = pygame.font.SysFont("Calibri", 65, True, False)
        font_master = pygame.font.SysFont("Calibri", 25, True, False)
        font_update = pygame.font.SysFont("Calibri", 25, True, False)
        text = font.render("Score: " + str(self.game.score), True, self.BLACK)
        text_game_over = font1.render("Game Over :( ", True, (255, 0, 0))
        text_game_master = font_master.render("soja abbb", True, (255, 0, 0))
        text_game_score_update = font_update.render(
            "New Level Unlocked ", True, (255, 0, 0)
        )

        self.screen.blit(text, [0, 0])
        if self.game.state == "gameover":
            done = True
            #self.screen.blit(text_game_over, [10, 200])

        pygame.display.flip()
        self.clock.tick(self.fps)
        image_data = pygame.surfarray.array3d(pygame.display.get_surface())
        return done ,_lines_popped , image_data , self.game.field