Exemple #1
0
 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
Exemple #2
0
    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
Exemple #3
0
 def generation(self, holesFactor, linesFactor, heightFactor):
     points = 0
     '''gamesList = []
     with futures.ThreadPoolExecutor() as executor:'''
     for x in range(self.games):
         points += Tetris().start(
             AI(holesFactor, linesFactor, heightFactor), self.drawing)
     return (points / self.games, holesFactor, linesFactor, heightFactor)
Exemple #4
0
def train():
    record = 0
    game = Tetris()
    agent = Agent()
    while True:
        board = game.board
        agent.board = game.board
        piece = game.figure
        next_move = agent.predict_best_move(board, piece, game)
        game.game_loop(next_move)
        if game.game_over:
            print("Game", agent.games, "over")
            agent.games += 1
            if game.score > record:
                record = game.score
                print("New high score:", record)

            game.reset()
            game.game_over = False
Exemple #5
0
#          and Isaac Engel (13th September 2017)
# Last updated: 13th September 2017
# ####################################################

from main import Tetris
import utils
import timeit

# Example target shape
target = [[1, 0, 1, 1], [1, 1, 1, 1], [0, 1, 1, 1],
          [1, 1, 0, 0]]  # NOTE: in your test, you may not use this example.

# Uncomment the following line to generate a random target shape
#target = utils.generate_target(width=10, height=10, density=0.6)  # NOTE: it is recommended to keep density below 0.8

solution = Tetris(target)

valid, missing, excess, error_pieces = utils.check_solution(
    target, solution)  # checks if the solution is valid

if not valid:

    print("The solution is not valid!")

else:  # if the solution is valid, test time performance and accuracy

    # TIME PERFORMANCE
    # There will be three different values of the parameter 'target' with increasing complexity in real test.

    time_set = timeit.timeit('Tetris({})'.format(target),
                             'from main import Tetris',
Exemple #6
0
# ####################################################

from main import Tetris
import utils
import timeit
from copy import deepcopy

# Example target shape
# target = [[1, 0, 1, 1], [1, 1, 1, 1], [0, 1, 1, 1], [1, 1, 0, 0]]  # NOTE: in your test, you may not use this example.

# Uncomment the following line to generate a random target shape
target = utils.generate_target(
    width=100, height=100,
    density=0.6)  # NOTE: it is recommended to keep density below 0.8

solution = Tetris(deepcopy(target))

valid, missing, excess, error_pieces = utils.check_solution(
    target, solution)  # checks if the solution is valid

if not valid:

    print("The solution is not valid!")

else:  # if the solution is valid, test time performance and accuracy

    # TIME PERFORMANCE
    # There will be three different values of the parameter 'target' with increasing complexity in real test.

    time_set = timeit.timeit('Tetris({})'.format(target),
                             'from main import Tetris',
from main import Tetris
import utils
import timeit
import time

# Example target shape
#target = [[1, 0, 1, 1], [1, 1, 1, 1], [0, 1, 1, 1], [1, 1, 0, 0]]  # NOTE: in your test, you may not use this example.

# Uncomment the following line to generate a random target shape
target = utils.generate_target(
    width=20, height=20,
    density=0.7)  # NOTE: it is recommended to keep density below 0.8

begin_time = time.time()
solution, S = Tetris(target)
end_time = time.time()

valid, missing, excess, error_pieces = utils.check_solution(
    target, solution)  # checks if the solution is valid

if not valid:

    print("The solution is not valid!")

else:  # if the solution is valid, test time performance and accuracy

    # TIME PERFORMANCE
    # There will be three different values of the parameter 'target' with increasing complexity in real test.

    time_set = end_time - begin_time
Exemple #8
0
    19: 0
}

perfect_solution = [[(0, 0), (0, 0), (8, 1), (0, 0), (0, 0)],
                    [(0, 0), (0, 0), (8, 1), (1, 2), (1, 2)],
                    [(0, 0), (8, 1), (8, 1), (1, 2), (1, 2)],
                    [(0, 0), (13, 3), (18, 4), (18, 4), (0, 0)],
                    [(13, 3), (13, 3), (13, 3), (18, 4), (18, 4)]]
# NOTE: This example is used for the mock solution from 'main.py' only.

# Uncomment the following line to generate a random target shape
target, limit_tetris, perfect_solution = utils.generate_target(
    width=20, height=20,
    density=0.8)  # NOTE: it is recommended to keep density below 0.8

solution = Tetris(deepcopy(target), deepcopy(limit_tetris))

valid, missing, excess, error_pieces, use_diff = utils.check_solution(
    target, solution, limit_tetris)  # checks if the solution is valid

if not valid or len(error_pieces) != 0:
    if len(error_pieces) != 0:
        print(
            'WARNING: {} pieces have a wrong shapeID. They are labelled in image of the solution, and their PieceID are: {}.'
            .format(len(error_pieces), error_pieces))
        print("Displaying solution...")
        utils.visual_perfect(perfect_solution, solution)
    print("WARNING: The solution is not valid, no score will be given!")

else:  # if the solution is valid, test time performance and accuracy
Exemple #9
0
        MainState = Menu1.state

    if MainState == "hard":
        speed += 4
        MainState = "menu"
        Menu.menu.switched = True
        continue
    if MainState == "normal":
        speed -= 4
        MainState = "menu"
        Menu.menu.switched = False
        continue

    size = (Menu1.width * 20 + 200, Menu1.height * 20 + 100)
    gamesize = (Menu1.width * 20, Menu1.height * 20)
    game = Tetris(Menu1.height, Menu1.width, speed, Menu1.width // 2 - 2)
    screen = pygame.display.set_mode(size)

    if MainState == "load":
        x, y, s, z = game.load()
        size = (x * 20 + 200, y * 20 + 100)
        gamesize = (x * 20, y * 20)
        game1 = Tetris(y, x, speed, x // 2 - 2)
        game1.field = z
        screen = pygame.display.set_mode(size)
        MainState = "start"
        game1.score = s

        game = game1
    if speed == 2:
        gameBackground = Menu.Background("game_background_main.jpg",
Exemple #10
0
 def setUp(self):
     self.tetris = Tetris()
     self.display = pygame.display.set_mode((800, 700))
     self.gameloop = GameLoop(self.display)
     self.blocks = Blocks()
Exemple #11
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