Exemplo n.º 1
0
    def __init__(self,
                 size=(MATRIX_WIDTH, MATRIX_HEIGHT),
                 blocksize=BLOCKSIZE):
        self.size = {'width': size[0], 'height': size[1]}
        self.blocksize = blocksize
        self.surface = Surface((self.size['width'] * self.blocksize,
                                (self.size['height'] - 2) * self.blocksize))

        self.matrix = dict()
        for y in range(self.size['height']):
            for x in range(self.size['width']):
                self.matrix[(y, x)] = None

        self.next_tetromino = random.choice(list_of_tetrominoes)
        self.set_tetrominoes()
        self.tetromino_rotation = 0
        self.downwards_timer = 0
        self.base_downwards_speed = 0.4  # Move down every 400 ms

        self.movement_keys = {'left': 0, 'right': 0}
        self.movement_keys_speed = 0.05
        self.movement_keys_timer = (-self.movement_keys_speed) * 2

        self.level = 1
        self.score = 0
        self.lines = 0

        self.combo = 1  # Combo will increase when you clear lines with several tetrominos in a row

        self.paused = False
        self.gameover = False

        self.highscore = load_score()
        self.played_highscorebeaten_sound = False
Exemplo n.º 2
0
 def construct_highscoresurf(self):
     """
     Loads high score from file
     """
     font = pygame.font.Font(None, 50)
     highscore = load_score()
     text = "Highscore: {}".format(highscore)
     return font.render(text, True, (255,255,255))
Exemplo n.º 3
0
 def construct_highscoresurf(self):
     """
     Loads high score from file
     """
     font = pygame.font.Font(None, 50)
     highscore = load_score()
     text = "Highscore: {}".format(highscore)
     return font.render(text, True, (255,255,255))
Exemplo n.º 4
0
    def __init__(self):
        self.surface = screen.subsurface(
            Rect((MATRIS_OFFSET + BORDERWIDTH, MATRIS_OFFSET + BORDERWIDTH),
                 (MATRIX_WIDTH * BLOCKSIZE, (MATRIX_HEIGHT - 2) * BLOCKSIZE)))

        self.matrix = dict()
        for y in range(MATRIX_HEIGHT):
            for x in range(MATRIX_WIDTH):
                self.matrix[(y, x)] = None
        """
        `self.matrix` is the current state of the tetris board, that is, it records which squares are
        currently occupied. It does not include the falling tetromino. The information relating to the
        falling tetromino is managed by `self.set_tetrominoes` instead. When the falling tetromino "dies",
        it will be placed in `self.matrix`.
        """

        self.next_tetromino = Tetromino(
            color=(random.randint(0, 255), random.randint(0, 255),
                   random.randint(0, 255)),
            shape=((O, O, O, O, O, O, O), (O, X, X, O, X, O, O),
                   (X, O, O, O, X, O, O), (X, O, X, O, X, O, O),
                   (X, O, X, O, X, O, O), (O, X, X, O, X, X, X), (O, O, O, O,
                                                                  O, O, O)))

        self.set_tetrominoes()
        self.tetromino_position = (5, self.tetromino_position[1]
                                   )  # Making the good luck visible

        self.tetromino_rotation = 0
        self.downwards_timer = 0
        self.base_downwards_speed = 1  # Move down every second

        self.movement_keys = {'left': 0, 'right': 0}
        self.movement_keys_speed = 0.05
        self.movement_keys_timer = (-self.movement_keys_speed) * 2

        self.level = 1
        self.score = 0
        self.lines = 0

        self.combo = 1  # Combo will increase when you clear lines with several tetrominos in a row

        self.paused = False

        self.highscore = load_score()
        self.played_highscorebeaten_sound = False

        self.levelup_sound = get_sound("levelup.wav")
        self.gameover_sound = get_sound("gameover.wav")
        self.linescleared_sound = get_sound("linecleared.wav")
        self.highscorebeaten_sound = get_sound("highscorebeaten.wav")
Exemplo n.º 5
0
    def __init__(self, size=(MATRIX_WIDTH, MATRIX_HEIGHT), blocksize=BLOCKSIZE):
        self.size = {'width': size[0], 'height': size[1]}
        self.blocksize = blocksize
        self.surface = Surface((self.size['width']  * self.blocksize,
                                (self.size['height']-2) * self.blocksize))


        self.matrix = dict()
        for y in range(self.size['height']):
            for x in range(self.size['width']):
                self.matrix[(y,x)] = None
                
        self.matrix_pretransform = copy.deepcopy(self.matrix)
                
        self.processing_matrix = np.zeros((MATRIX_HEIGHT, MATRIX_WIDTH))

        self.next_tetromino = random.choice(list_of_tetrominoes)
        self.set_tetrominoes()
        self.tetromino_rotation = 0
        self.downwards_timer = 0
        self.base_downwards_speed = 0.4 # Move down every 400 ms

        self.movement_keys = {'left': 0, 'right': 0}
        self.movement_keys_speed = 0.05
        self.movement_keys_timer = (-self.movement_keys_speed)*2

        self.level = 1
        self.score = 0
        self.lines = 0

        self.combo = 1 # Combo will increase when you clear lines with several tetrominos in a row
        
        self.paused = False
        self.gameover = False

        self.highscore = load_score()
        self.played_highscorebeaten_sound = False
        
        # self.levelup_sound  = get_sound("levelup.wav")
        # self.gameover_sound = get_sound("gameover.wav")
        # self.linescleared_sound = get_sound("linecleared.wav")
        # self.highscorebeaten_sound = get_sound("highscorebeaten.wav")
        
        
        """
        Instance variables for the AI
        """
        self.tetromino_rotation_ai = 0
        self.tetromino_position_ai = None
Exemplo n.º 6
0
    def __init__(self,
                 size=(MATRIX_WIDTH, MATRIX_HEIGHT),
                 blocksize=BLOCKSIZE):
        self.size = {'width': size[0], 'height': size[1]}
        self.blocksize = blocksize
        self.surface = Surface((self.size['width'] * self.blocksize,
                                (self.size['height'] - 2) * self.blocksize))

        self.matrix = dict()
        for y in range(self.size['height']):
            for x in range(self.size['width']):
                self.matrix[(y, x)] = None
        """
        `self.matrix` is the current state of the tetris board, that is, it records which squares are
        currently occupied. It does not include the falling tetromino. The information relating to the
        falling tetromino is managed by `self.set_tetrominoes` instead. When the falling tetromino "dies",
        it will be placed in `self.matrix`.
        """

        self.next_tetromino = random.choice(list_of_tetrominoes)
        self.set_tetrominoes()
        self.tetromino_rotation = 0
        self.downwards_timer = 0
        self.base_downwards_speed = 0.4  # Move down every 400 ms

        self.movement_keys = {'left': 0, 'right': 0}
        self.movement_keys_speed = 0.05
        self.movement_keys_timer = (-self.movement_keys_speed) * 2

        self.level = 1
        self.score = 0
        self.lines = 0

        self.combo = 1  # Combo will increase when you clear lines with several tetrominos in a row

        self.paused = False
        self.gameover = False

        self.highscore = load_score()
        self.played_highscorebeaten_sound = False

        self.levelup_sound = get_sound("levelup.wav")
        self.gameover_sound = get_sound("gameover.wav")
        self.linescleared_sound = get_sound("linecleared.wav")
        self.highscorebeaten_sound = get_sound("highscorebeaten.wav")
Exemplo n.º 7
0
    def __init__(self,game,seed):
        self.surface = screen.subsurface(Rect((MATRIS_OFFSET+BORDERWIDTH, MATRIS_OFFSET+BORDERWIDTH),
                                              (MATRIX_WIDTH * BLOCKSIZE, (MATRIX_HEIGHT-2) * BLOCKSIZE)))
        self.seed = seed
        self.matrix = dict()
        for y in range(MATRIX_HEIGHT):
            for x in range(MATRIX_WIDTH):
                self.matrix[(y,x)] = None
        """
        `self.matrix` is the current state of the tetris board, that is, it records which squares are
        currently occupied. It does not include the falling tetromino. The information relating to the
        falling tetromino is managed by `self.set_tetrominoes` instead. When the falling tetromino "dies",
        it will be placed in `self.matrix`.
        """
        self.game = game
        random.seed(self.seed)
        self.next_tetromino = random.choice(list_of_tetrominoes)
        self.set_tetrominoes()
        self.tetromino_rotation = 0
        self.downwards_timer = 0
        self.base_downwards_speed = 0.4 # Move down every 400 ms

        self.movement_keys = {'left': 0, 'right': 0}
        self.movement_keys_speed = 0.05
        self.movement_keys_timer = (-self.movement_keys_speed)*2
        
        self.moves = []
        self.level = 1
        self.score = 0
        self.lines = 0

        self.combo = 1 # Combo will increase when you clear lines with several tetrominos in a row
        
        self.paused = False

        self.highscore = load_score()
        self.played_highscorebeaten_sound = False

#        self.levelup_sound  = get_sound("levelup.wav")
#        self.gameover_sound = get_sound("gameover.wav")
#        self.linescleared_sound = get_sound("linecleared.wav")
#        self.highscorebeaten_sound = get_sound("highscorebeaten.wav")
        self.new_tetromino_here = True
Exemplo n.º 8
0
    def __init__(self):
        self.surface = screen.subsurface(Rect((MATRIS_OFFSET+BORDERWIDTH, MATRIS_OFFSET+BORDERWIDTH),
                                              (MATRIX_WIDTH * BLOCKSIZE, (MATRIX_HEIGHT-2) * BLOCKSIZE)))

        self.matrix = dict()
        for y in range(MATRIX_HEIGHT):
            for x in range(MATRIX_WIDTH):
                self.matrix[(y,x)] = None
        """
        `self.matrix` is the current state of the tetris board, that is, it records which squares are
        currently occupied. It does not include the falling tetromino. The information relating to the
        falling tetromino is managed by `self.set_tetrominoes` instead. When the falling tetromino "dies",
        it will be placed in `self.matrix`.
        """

        self.next_tetromino = random.choice(list_of_tetrominoes)
        self.set_tetrominoes()
        self.tetromino_rotation = 0
        self.downwards_timer = 0
        self.base_downwards_speed = 0.4 # Move down every 400 ms

        self.movement_keys = {'left': 0, 'right': 0}
        self.movement_keys_speed = 0.05
        self.movement_keys_timer = (-self.movement_keys_speed)*2

        self.level = 1
        self.score = 0
        self.lines = 0

        self.combo = 1 # Combo will increase when you clear lines with several tetrominos in a row
        
        self.paused = False

        self.highscore = load_score()
        self.played_highscorebeaten_sound = False

        self.levelup_sound  = get_sound("levelup.wav")
        self.gameover_sound = get_sound("gameover.wav")
        self.linescleared_sound = get_sound("linecleared.wav")
        self.highscorebeaten_sound = get_sound("highscorebeaten.wav")
Exemplo n.º 9
0
    def __init__(self, size=(MATRIX_WIDTH, MATRIX_HEIGHT), blocksize=BLOCKSIZE):
        self.size = {'width': size[0], 'height': size[1]}
        self.blocksize = blocksize
        self.surface = Surface((self.size['width']  * self.blocksize,
                                (self.size['height']-2) * self.blocksize))


        self.matrix = dict()
        for y in range(self.size['height']):
            for x in range(self.size['width']):
                self.matrix[(y,x)] = None


        self.next_tetromino = random.choice(list_of_tetrominoes)
        self.set_tetrominoes()
        self.tetromino_rotation = 0
        self.downwards_timer = 0
        self.base_downwards_speed = 0.4 # Move down every 400 ms

        self.movement_keys = {'left': 0, 'right': 0}
        self.movement_keys_speed = 0.05
        self.movement_keys_timer = (-self.movement_keys_speed)*2

        self.level = 1
        self.score = 0
        self.lines = 0

        self.combo = 1 # Combo will increase when you clear lines with several tetrominos in a row
        
        self.paused = False
        self.gameover = False

        self.highscore = load_score()
        self.played_highscorebeaten_sound = False

        self.levelup_sound  = get_sound("levelup.wav")
        self.gameover_sound = get_sound("gameover.wav")
        self.linescleared_sound = get_sound("linecleared.wav")
        self.highscorebeaten_sound = get_sound("highscorebeaten.wav")
Exemplo n.º 10
0
    def __init__(self, screen):
        self.surface = None
        if screen:
            self.surface = screen.subsurface(
                Rect(
                    (MATRIS_OFFSET + BORDERWIDTH, MATRIS_OFFSET + BORDERWIDTH),
                    (MATRIX_WIDTH * BLOCKSIZE,
                     (MATRIX_HEIGHT - 2) * BLOCKSIZE)))

        self.color_map = {
            "blue": 1,
            "yellow": 2,
            "pink": 3,
            "green": 4,
            "red": 5,
            "cyan": 6,
            "orange": 7,
            "grey": 8
        }
        self.colors = [
            "none", "blue", "yellow", "pink", "green", "red", "cyan", "orange",
            "grey"
        ]

        self.matrix = np.zeros([3, MATRIX_HEIGHT, MATRIX_WIDTH], dtype=int)
        """
        `self.matrix` is the current state of the tetris board, that is, it records which squares are
        currently occupied. It does not include the falling tetromino. The information relating to the
        falling tetromino is managed by `self.set_tetrominoes` instead. When the falling tetromino "dies",
        it will be placed in `self.matrix`.
        """

        # Use 7-Packed random piece
        next_shape = random.choice(list_of_start_tetrominoes)
        self.next_tetromino = tetrominoes[next_shape]
        # self.next_tetromino = tetrominoes[list_of_tetrominoes[2]]
        self.next_tetromino_bag = list(
            np.random.permutation(list_of_tetrominoes))
        repeat_idx = self.next_tetromino_bag.index(next_shape)
        self.next_tetromino_bag = [next_shape] + self.next_tetromino_bag
        self.next_tetromino_idx = 1

        self.held_tetromino = None
        self.recently_swapped = True
        self.drop_trials = DROP_TRIALS
        self.garbage_block = self.block('grey')
        self.t_spin = 0  # 0: no t-spin, 1: t-spin mini, 2: t-spin
        self.difficult_clear = False
        self.done = False
        self.locked = False
        self.needs_redraw = True

        self.set_tetrominoes()
        self.tetromino_rotation = 0
        self.downwards_timer = 0
        self.base_downwards_speed = 0.4  # Move down every 400 ms

        self.movement_keys = {'left': 0, 'right': 0}
        self.movement_keys_speed = 0.05
        self.movement_keys_timer = (-self.movement_keys_speed) * 2

        self.level = 1
        self.score = 0
        self.lines = 0

        self.combo = 1  # Combo will increase when you clear lines with several tetrominos in a row

        self.paused = False

        self.highscore = load_score()
        self.played_highscorebeaten_sound = False

        self.levelup_sound = get_sound("levelup.wav")
        self.gameover_sound = get_sound("gameover.wav")
        self.linescleared_sound = get_sound("linecleared.wav")
        self.highscorebeaten_sound = get_sound("highscorebeaten.wav")
Exemplo n.º 11
0
 def construct_highscoresurf(self):
     font = pg.font.Font(None, 50)
     highscore = load_score()
     text = "Highscore: {}".format(highscore)
     return font.render(text, True, (150,150,150))
Exemplo n.º 12
0
    def __init__(self):
        self.surface = screen.subsurface(
            Rect((MATRIS_OFFSET + BORDERWIDTH, MATRIS_OFFSET + BORDERWIDTH),
                 (MATRIX_WIDTH * BLOCKSIZE, (MATRIX_HEIGHT - 2) * BLOCKSIZE)))

        self.matrix = dict()
        for y in range(MATRIX_HEIGHT):
            for x in range(MATRIX_WIDTH):
                self.matrix[(y, x)] = None
        """
        `self.matrix` is the current state of the tetris board, that is, it records which squares are
        currently occupied. It does not include the falling tetromino. The information relating to the
        falling tetromino is managed by `self.set_tetrominoes` instead. When the falling tetromino "dies",
        it will be placed in `self.matrix`.
        """

        self.next_tetromino = random.choice(list_of_tetrominoes)
        self.set_tetrominoes()

        if self.agent_mode == True:
            #Creates a representation of the initial board
            self.board.update_board_representation(
                self.create_board_representation())
            self.board.set_board_height()
            self.board.set_holes()
            self.board.set_column_differences()
            print(str(self.board))
            print("Column Height Differences:" +
                  str(self.board.get_column_differences()))

            #Set up the the agent
            self.agent.set_current_board(self.board)
            self.agent.set_agent_tetromino(self.current_tetromino)

        self.tetromino_rotation = 0
        self.downwards_timer = 0
        self.base_downwards_speed = 0.4  # Move down every 400 ms

        self.movement_keys = {'left': 0, 'right': 0}
        self.movement_keys_speed = 0.05
        self.movement_keys_timer = (-self.movement_keys_speed) * 2

        self.level = 1
        self.score = 0
        self.lines = 0

        self.combo = 1  # Combo will increase when you clear lines with several tetrominos in a row

        self.paused = False

        self.highscore = load_score()
        self.played_highscorebeaten_sound = False

        self.levelup_sound = get_sound("levelup.wav")
        self.gameover_sound = get_sound("gameover.wav")
        self.linescleared_sound = get_sound("linecleared.wav")
        self.highscorebeaten_sound = get_sound("highscorebeaten.wav")

        if self.agent_mode == True:
            #Agent's first move
            self.tetromino_placement = self.agent.make_move()
            self.tetromino_position = (0, self.tetromino_placement[2])
            for rotations in range(self.tetromino_placement[0]):
                self.request_rotation()