def __init__(self, gridchoice="none", obs_type="image", mode="rgb_array"): super(TetrisSingleInterface, self).__init__(gridchoice, obs_type, mode) self.num_players = 1 # The second player is dummy, it is used for # self.renderer.drawByName("transparent", *opponent["pos"]["transparent"]) at around line 339 for i in range(self.num_players + 1): info_dict = {"id": i} # adding the action information for k, v in self._action_meaning.items(): info_dict[v] = k self.tetris_list.append({ 'info_dict': info_dict, 'tetris': Tetris(Player(info_dict), gridchoice), 'com_event': ComEvent(), 'pos': POS_LIST[i], 'curr_repeat_time': 0, 'last_action': 0 }) self.reset()
def __init__(self, gridchoice="none", obs_type="image", mode="rgb_array"): super(TetrisDoubleInterface, self).__init__(gridchoice, obs_type, mode) self.num_players = 2 for i in range(self.num_players): info_dict = {"id": i} # adding the action information for k, v in self._action_meaning.items(): info_dict[v] = k self.tetris_list.append({ 'info_dict': info_dict, 'tetris': Tetris(Player(info_dict), gridchoice), 'com_event': ComEvent(), 'pos': POS_LIST[i] }) self.reset()
def start(self):#parameters are FP/s rate and timer countdown ################################################################################ gridchoice = "none" #calling the setmap function for a choice of grid self.timer2p.tick() #the code below is what happens when you set a map #different maps = differnet grids self.game_init() #SCREEN=pygame.display.set_mode((800,600)) running = True #necessity # SCREEN.blit(IMAGES["gamescreen"], (0, 0))# blitting the main background self.renderer.drawByName("gamescreen", 0, 0) #these two used for countdown #of the timer time = MAX_TIME # milisecond delaytime = time info_dict_list = [{ "id": self.ai_id, "hold": 1, "drop": 2, "rotate_right": 3, "rotate_left": 4, "right": 5, "left": 6, "down": 7 } ] tetris_list = [] for i in range(self.num_players): tetris_list.append({ 'info_dict': info_dict_list[i], 'tetris': Tetris(Player(info_dict_list[i]), gridchoice), 'pos': POS_LIST[i] }) self.ai.set_env(tetris_list[self.ai_id]["tetris"]) winner = 0 force_quit = 0 # self.countdown() #main loop while running: self.sound_manager.bgm_loop(True) for tetris_dict in tetris_list: tetris_dict["tetris"].natural_down() for evt in pygame.event.get(): if evt.type == pygame.QUIT: running = False force_quit = 1 for tetris_dict in tetris_list: tetris_dict["tetris"].trigger(evt) for tetris_dict in tetris_list: tetris_dict["tetris"].move() # simulate ai move ai_event = pygame.event.Event(pygame.KEYDOWN) ai_event.key = self.ai.predict(time) tetris_list[self.ai_id]["tetris"].trigger(ai_event) for i, tetris_dict in enumerate(tetris_list): opponent = tetris_list[self.num_players - 1 - i] tetris, pos = tetris_dict["tetris"], tetris_dict["pos"] if tetris.check_fallen(): # compute the scores and attack the opponent scores = tetris.clear() opponent["tetris"].add_attacked(scores) self.renderer.drawCombo(tetris, *pos["combo"]) self.renderer.drawTetris(tetris, *pos["tetris"]) self.renderer.drawTspin(tetris, *pos["tspin"]) self.renderer.drawBack2Back(tetris, *pos["back2back"]) if tetris.check_KO(): self.renderer.drawBoard(tetris, *pos["board"]) opponent["tetris"].update_ko() tetris.clear_garbage() self.renderer.drawByName("ko", *pos["ko"]) self.renderer.drawByName("transparent", *pos["transparent"]) # screen.blit(kos[tetris_2.get_KO() - 1], (426, 235)) pygame.display.flip() freeze(0.5) # scores -= 1 # end = 1 tetris.new_block() self.renderer.drawGameScreen(tetris) tetris.increment_timer() if tetris.attacked == 0: pygame.draw.rect(self.screen, (30, 30, 30), pos["attack_clean"]) if tetris.attacked != 0: for j in range(tetris.attacked): pos_attack_alarm = list(pos["attack_alarm"]) # modified the y axis of the rectangle, according to the strength of attack pos_attack_alarm[1] = pos_attack_alarm[1] - 18 * j pygame.draw.rect(self.screen, (255, 0, 0), pos_attack_alarm) if tetris.KO > 0: self.renderer.drawKO(tetris.KO, *pos["big_ko"]) self.renderer.drawScreen(tetris, *pos["drawscreen"]) if Judge.check_ko_win(tetris, max_ko=3): running = False winner = tetris.get_id() if Judge.check_ko_win(opponent["tetris"], max_ko=3): running = False winner = opponent["tetris"].get_id() time, running = self.update_time(time, running) if not running: winner = Judge.who_win(tetris, opponent["tetris"]) self.renderer.drawTime2p(time) self.myClock.tick(FPS) pygame.display.flip() if force_quit: self.sound_manager.bgm_loop(False) return "menu" for i, tetris_dict in enumerate(tetris_list): opponent = tetris_list[self.num_players - 1 - i] tetris, pos = tetris_dict["tetris"], tetris_dict["pos"] self.renderer.drawByName("transparent", *pos["transparent"]) if i == winner: # is winner self.renderer.drawByName("you_win", *pos["you_win"]) else: self.renderer.drawByName("you_lose", *pos["you_lose"]) pygame.display.flip() freeze(2.0) self.sound_manager.bgm_loop(False) return "menu"
def start(self):#parameters are FP/s rate and timer countdown ################################################################################ gridchoice = self.setmap()#calling the setmap function for a choice of grid self.timer2p.tick() #the code below is what happens when you set a map #different maps = differnet grids pygame.init() #for music battlemusic = pygame.mixer.Sound(MUSIC_PATH)#importing sound file #SCREEN=pygame.display.set_mode((800,600)) running = True #necessity self.renderer.drawByName("gamescreen", 0, 0) # a = SCREEN.copy()#used for image coverage #these two used for countdown #of the timer time = MAX_TIME # milisecond delaytime = time info_dict = { "id": 0, "hold": pygame.K_c, "drop": pygame.K_SPACE, "rotate_right": pygame.K_UP, "rotate_left": pygame.K_z, "right": pygame.K_RIGHT, "left": pygame.K_LEFT, "down": pygame.K_DOWN } tetris = Tetris(Player(info_dict), gridchoice) pos = POS_LIST[info_dict["id"]] opponent_pos = POS_LIST[-1] # print("213") #main loop force_quit = 0 kk = 0 while running: # battlemusic.play()#plays music tetris.natural_down() for evt in pygame.event.get(): if evt.type == pygame.QUIT: running = False force_quit = 1 tetris.trigger(evt) tetris.move() if tetris.check_fallen(): # compute the scores and attack the opponent scores = tetris.clear() self.renderer.drawCombo(tetris, *pos["combo"]) self.renderer.drawTetris(tetris, *pos["tetris"]) self.renderer.drawTspin(tetris, *pos["tspin"]) self.renderer.drawBack2Back(tetris, *pos["back2back"]) if tetris.check_KO(): self.renderer.drawBoard(tetris, *pos["board"]) tetris.clear_garbage() pygame.display.flip() running = False tetris.new_block() infos = {'is_fallen': tetris.is_fallen} if tetris.is_fallen: height_sum, diff_sum, max_height, holes = get_infos(tetris.get_board()) # store the different of each information due to the move infos['height_sum'] = height_sum - self.last_infos['height_sum'] infos['diff_sum'] = diff_sum - self.last_infos['diff_sum'] infos['max_height'] = max_height - self.last_infos['max_height'] infos['holes'] = holes - self.last_infos['holes'] infos['n_used_block'] = tetris.n_used_block - self.last_infos['n_used_block'] infos['is_fallen'] = tetris.is_fallen infos['scores'] = scores infos['cleared'] = tetris.cleared self.last_infos = {'height_sum': height_sum, 'diff_sum': diff_sum, 'max_height': max_height, 'holes': holes, 'n_used_block': tetris.n_used_block} print(infos) self.renderer.drawGameScreen(tetris) self.renderer.drawScreen(tetris, *pos["drawscreen"]) self.renderer.drawByName("transparent", *opponent_pos["transparent"]) tetris.increment_timer() # pygame.display.update(r) time, running = self.update_time(time, running) self.renderer.drawTime2p(time) # time goes until it hits zero # when it hits zero return endgame SCREEN self.myClock.tick(FPS) pygame.display.flip() if force_quit: return "menu" # self.renderer.drawByObj(a, 0, 0) self.renderer.drawByName("transparent", *pos["transparent"]) pygame.display.flip() freeze(2.0) # pygame.quit() return "menu"
def start(self): #parameters are FP/s rate and timer countdown ################################################################################ gridchoice = self.setmap( ) #calling the setmap function for a choice of grid self.timer2p.tick() #the code below is what happens when you set a map #different maps = differnet grids pygame.init() #for music battlemusic = pygame.mixer.Sound(MUSIC_PATH) #importing sound file #SCREEN=pygame.display.set_mode((800,600)) running = True #necessity # SCREEN.blit(IMAGES["gamescreen"], (0, 0))# blitting the main background self.renderer.drawByName("gamescreen", 0, 0) #these two used for countdown #of the timer time = MAX_TIME # milisecond delaytime = time info_dict_list = [{ "id": 0, "hold": pygame.K_c, "drop": pygame.K_SPACE, "rotate_right": pygame.K_UP, "rotate_left": pygame.K_z, "right": pygame.K_RIGHT, "left": pygame.K_LEFT, "down": pygame.K_DOWN }, { "id": 1, "hold": pygame.K_e, "drop": pygame.K_w, "rotate_right": pygame.K_u, "rotate_left": pygame.K_q, "right": pygame.K_k, "left": pygame.K_h, "down": pygame.K_j }] tetris_list = [] for i in range(self.num_players): tetris_list.append({ 'info_dict': info_dict_list[i], 'tetris': Tetris(Player(info_dict_list[i]), gridchoice), 'pos': POS_LIST[i] }) winner = 0 force_quit = 0 #main loop while running: # battlemusic.play()#plays music for tetris_dict in tetris_list: tetris_dict["tetris"].natural_down() for evt in pygame.event.get(): if evt.type == pygame.QUIT: running = False force_quit = 1 for tetris_dict in tetris_list: tetris_dict["tetris"].trigger(evt) for tetris_dict in tetris_list: tetris_dict["tetris"].move() for i, tetris_dict in enumerate(tetris_list): opponent = tetris_list[self.num_players - 1 - i] tetris, pos = tetris_dict["tetris"], tetris_dict["pos"] if tetris.check_fallen(): # compute the scores and attack the opponent scores = tetris.clear() opponent["tetris"].add_attacked(scores) self.renderer.drawCombo(tetris, *pos["combo"]) self.renderer.drawTetris(tetris, *pos["tetris"]) self.renderer.drawTspin(tetris, *pos["tspin"]) self.renderer.drawBack2Back(tetris, *pos["back2back"]) if tetris.check_KO(): self.renderer.drawBoard(tetris, *pos["board"]) opponent["tetris"].update_ko() tetris.clear_garbage() self.renderer.drawByName("ko", *pos["ko"]) self.renderer.drawByName("transparent", *pos["transparent"]) # screen.blit(kos[tetris_2.get_KO() - 1], (426, 235)) pygame.display.flip() freeze(0.5) # scores -= 1 # end = 1 tetris.new_block() self.renderer.drawGameScreen(tetris) tetris.increment_timer() if tetris.attacked == 0: pygame.draw.rect(self.screen, (30, 30, 30), pos["attack_clean"]) if tetris.attacked != 0: for j in range(tetris.attacked): pos_attack_alarm = list(pos["attack_alarm"]) # modified the y axis of the rectangle, according to the strength of attack pos_attack_alarm[1] = pos_attack_alarm[1] - 18 * j pygame.draw.rect(self.screen, (255, 0, 0), pos_attack_alarm) if tetris.KO > 0: self.renderer.drawKO(tetris.KO, *pos["big_ko"]) self.renderer.drawScreen(tetris, *pos["drawscreen"]) if Judge.check_ko_win(tetris, max_ko=3): running = False winner = tetris.get_id() if Judge.check_ko_win(opponent["tetris"], max_ko=3): running = False winner = opponent["tetris"].get_id() time, running = self.update_time(time, running) if not running: winner = Judge.who_win(tetris, opponent["tetris"]) self.renderer.drawTime2p(time) self.myClock.tick(FPS) pygame.display.flip() if force_quit: return "menu" for i, tetris_dict in enumerate(tetris_list): opponent = tetris_list[self.num_players - 1 - i] tetris, pos = tetris_dict["tetris"], tetris_dict["pos"] self.renderer.drawByName("transparent", *pos["transparent"]) if i == winner: # is winner self.renderer.drawByName("you_win", *pos["you_win"]) else: self.renderer.drawByName("you_lose", *pos["you_lose"]) # # end game # # self.renderer.drawByObj(a, 0, 0) # self.renderer.drawByName("transparent", 110, 135) # self.renderer.drawByName("transparent", 494, 135) # if winner == 0: # self.renderer.drawByName("you_win", 120, 230) # self.renderer.drawByName("you_lose", 515, 230) # else: # self.renderer.drawByName("you_win", 520, 230) # self.renderer.drawByName("you_lose", 115, 230) pygame.display.flip() freeze(2.0) return "menu"