Esempio n. 1
0
    def game_last_save(self):
        self.reinit_pygame()
        pygame.mixer.Sound.play(self.click_sound)
        # Продолжение игры c последнего сохранения
        file_level = open('data/save.txt', encoding='utf8')
        data = file_level.read().split()
        num_level, max_level = int(data[0]), int(data[1])
        try:
            name_last_save = open('data/saves/last_save.txt').readlines()[0]
        except Exception:
            return
        num_level = int(open(name_last_save).readlines()[0])
        file_level = open('data/save.txt', "w")
        file_level.write(str(num_level) + " " + str(max_level))
        file_level.close()

        self.reinit_pygame()
        self.hide()
        reinit_groups()
        win_flag = load_level(name_last_save)
        reinit_groups()

        while num_level < 10 and win_flag == 1:
            max_level = max(num_level, max_level)
            num_level += 1
            file_level = open('data/save.txt', 'w')
            file_level.write(str(num_level) + ' ' + str(max_level))
            file_level.close()
            self.reinit_pygame()
            reinit_groups()
            win_flag = load_level()
        if num_level == 10 and win_flag == 1:
            file_level = open('data/save.txt', 'w')
            file_level.write(str(num_level) + ' ' + str(max_level + 1))
            file_level.close()
            reinit_groups()
            self.win = FinishWindow()
            self.win.show()
        elif win_flag == 2:
            reinit_groups()
            self.reinit_pygame()
            self.game_last_save()
        elif win_flag == 3:
            reinit_groups()
            self.reinit_pygame()
            self.game_continue()
        else:
            reinit_groups()
            self.reinit_pygame()
            self.show()
Esempio n. 2
0
    def __init__(self, level_name):
        self.player_table = {}  # Holds all players {Class: client, ...}
        self.entdict = {}  # Holds all entities {id: Class}
        self.static_entities = []  # Entities that are static in the level
        self.level = game.load_level(level_name)
        self.level_name = level_name
        self.client_world = False

        # Todo refactor static entities as their own thing
        for level_ent in self.level:
            etable = entity_table.static_entity_table
            if level_ent in etable:
                for params in self.level[level_ent]:
                    self.add_new_static_entity(etable[level_ent][0](
                        *params))  # The most sweaty python technique

        self.dt = 1.5
        self.snapshots = [
        ]  # Holds entdicts from last MAX_SNAPSHOT_HISTORY frames [oldest frame, ..., newest frame]
        self.snapshot_number = 0  # Number tells what snapshot this is
        self.create_ents = []  # a list of any ents that were created
        self.delete_ents = []  # a list of ents that need to be deleted
        self.collision_sectors = [[]]
        self.collision_sector_size = 256
        self.entites_to_spawn = [
        ]  #Q ueue up entities to spawn in the next frame
        self.add_new_entity(entity_table.entity_table[4][0](128, 128))
Esempio n. 3
0
def main():
    graphics = graph.init_video()
    graphics.screen.fill((32, 0, 64))
    pygame.mouse.set_visible(0)
    pygame.display.flip()
    level = game.load_level()
    mygame = game.init_game(level)
    graphics.tilemap = level.tilemap
    graphics.tilemap_height = level.height
    graphics.tilemap_width = level.width
    next = "title"
    mygame.ingame = 0
    while next != "quit":
        if next == "menu":
            next = menu.main_menu(mygame, level, graphics)
        if next == "edit":
            next = "menu"
            edit.edit(level, graphics)
        if next == "play":
            next = game.game(mygame, level, graphics)
        if next == "title":
            mygame.sound_12.play()
            next = menu.title_screen(graphics)
        if next == "intro":
            mygame.sound_12.play()
            next = menu.intro(graphics)
        if next == "win":
            mygame.sound_0.play()
            next = menu.win(graphics)
            
    graph.free_video()
Esempio n. 4
0
    def load_save(self, savename):
        self.reinit_pygame()
        pygame.mixer.Sound.play(self.click_sound)
        # Загрузка сохранения
        file_level = open('data/save.txt', encoding='utf8')
        data = file_level.read().split()
        num_level, max_level = int(data[0]), int(data[1])
        num_level = int(open(savename).readlines()[0])
        file_level = open('data/save.txt', "w")
        file_level.write(str(num_level) + " " + str(max_level))
        file_level.close()

        self.reinit_pygame()
        self.hide()
        reinit_groups()
        win_flag = load_level(filename=savename)
        reinit_groups()

        while num_level < 10 and win_flag == 1:
            max_level = max(num_level, max_level)
            num_level += 1
            file_level = open('data/save.txt', 'w')
            file_level.write(str(num_level) + ' ' + str(max_level))
            file_level.close()
            self.reinit_pygame()
            reinit_groups()
            win_flag = load_level()
        if num_level == 10 and win_flag == 1:
            file_level = open('data/save.txt', 'w')
            file_level.write(str(num_level) + ' ' + str(max_level + 1))
            file_level.close()
            reinit_groups()
            self.win = FinishWindow()
            self.win.show()
        elif win_flag == 2:
            reinit_groups()
            self.reinit_pygame()
            self.game_last_save()
        elif win_flag == 3:
            reinit_groups()
            self.reinit_pygame()
            self.game_continue()
        else:
            reinit_groups()
            self.reinit_pygame()
            self.show()
Esempio n. 5
0
    def game_start(self):
        self.reinit_pygame()
        pygame.mixer.Sound.play(self.click_sound)
        # Запуск игры с самого начала (со сбросом предыдущего прогресса)
        num_level, max_level = 1, 1
        file_level = open('data/save.txt', 'w')
        file_level.write(str(num_level) + ' ' + str(max_level))
        file_level.close()

        self.reinit_pygame()
        self.hide()
        reinit_groups()
        try:
            win_flag = load_level()
        except Exception as error:
            print(error)
        reinit_groups()

        while num_level < 10 and win_flag == 1:
            max_level = max(num_level, max_level)
            num_level += 1
            file_level = open('data/save.txt', 'w')
            file_level.write(str(num_level) + ' ' + str(max_level))
            file_level.close()
            self.reinit_pygame()
            reinit_groups()
            win_flag = load_level()
        if num_level == 10 and win_flag == 1:
            file_level = open('data/save.txt', 'w')
            file_level.write(str(num_level) + ' ' + str(max_level + 1))
            file_level.close()
            reinit_groups()
            self.win = FinishWindow()
            self.win.show()
        elif win_flag == 2:
            reinit_groups()
            self.reinit_pygame()
            self.game_last_save()
        elif win_flag == 3:
            reinit_groups()
            self.reinit_pygame()
            self.game_continue()
        else:
            reinit_groups()
            self.reinit_pygame()
            self.show()
Esempio n. 6
0
 def test_simple_game(self):
     level = load_level("levels/level1.json")
     game_room = GameRoom([level], 2, self._send_func)
     game_room.add_player(1)
     game_room.add_player(2)
     game_room.handle_client_message(1, json.dumps({"action": "start"}))
     for i in xrange(len(level.edges)):
         game_room.handle_client_message(1, json.dumps({"action": "fill", "data": {"edge_id": i}}))
Esempio n. 7
0
    def game_continue(self):
        self.reinit_pygame()
        pygame.mixer.Sound.play(self.click_sound)
        # Продолжение игры
        file_level = open('data/save.txt', encoding='utf8')
        data = file_level.read().split()
        num_level, max_level = int(data[0]), int(data[1])

        self.reinit_pygame()
        self.hide()
        reinit_groups()
        win_flag = load_level()
        reinit_groups()

        while num_level < 10 and win_flag == 1:
            max_level = max(num_level, max_level)
            num_level += 1
            file_level = open('data/save.txt', 'w')
            file_level.write(str(num_level) + ' ' + str(max_level))
            file_level.close()
            self.reinit_pygame()
            reinit_groups()
            win_flag = load_level()
        if num_level == 10 and win_flag == 1:
            file_level = open('data/save.txt', 'w')
            file_level.write(str(num_level) + ' ' + str(max_level + 1))
            file_level.close()
            reinit_groups()
            self.win = FinishWindow()
            self.win.show()
        elif win_flag == 2:
            reinit_groups()
            self.reinit_pygame()
            self.game_last_save()
        elif win_flag == 3:
            reinit_groups()
            self.reinit_pygame()
            self.game_continue()
        else:
            reinit_groups()
            self.reinit_pygame()
            self.show()
Esempio n. 8
0
 def test_lose_level(self):
     level = load_level("levels/level1.json")
     game_room = GameRoom([level], 2, self._send_func, 2, 3)
     game_room.add_player(1)
     game_room.add_player(2)
     game_room.handle_client_message(1, json.dumps({"action": "start"}))
     game_room.handle_client_message(2, json.dumps({"action": "start_level"}))
     game_room.update()
     self.assertTrue(game_room.is_in_level)
     from time import sleep
     sleep(3)
     game_room.update()
Esempio n. 9
0
 def test_multiple_levels(self):
     levels = [load_level(fn) for fn in get_level_names()]
     game_room = GameRoom(levels, 2, self._send_func)
     game_room.add_player(1)
     game_room.add_player(2)
     game_room.handle_client_message(1, json.dumps({"action": "start"}))
     for level in levels:
         game_room.handle_client_message(2, json.dumps({"action": "start_level"}))
         for i in xrange(len(level.edges)):
             game_room.handle_client_message(1, json.dumps({"action": "fill", "data": {"edge_id": i}}))
     game_room.handle_client_message(2, json.dumps({"action": "start_level"}))
     self.assertFalse(game_room.is_room_active)
Esempio n. 10
0
 def test_disconnect_before_start(self):
     level = load_level("levels/level1.json")
     game_room = GameRoom([level], 2, self._send_func)
     game_room.add_player(1)
     game_room.add_player(2)
     game_room.add_player(3)
     game_room.remove_player(1)
     game_room.add_player(4)
     game_room.handle_client_message(2, json.dumps({"action": "start"}))
     game_room.handle_client_message(2, json.dumps({"action": "start_level"}))
     for i in xrange(len(level.edges)):
         game_room.handle_client_message(4, json.dumps({"action": "fill", "data": {"edge_id": i}}))
Esempio n. 11
0
    def load_lev(self, num_level):
        self.reinit_pygame()
        pygame.mixer.Sound.play(self.click_sound)
        # Продолжение игры
        self.reinit_pygame()
        self.hide()
        reinit_groups()
        win_flag = load_level(val=num_level)
        reinit_groups()

        if win_flag == 2:
            reinit_groups()
            self.reinit_pygame()
            self.game_last_save()
        elif win_flag == 3:
            reinit_groups()
            self.reinit_pygame()
            self.load_lev(num_level)
        else:
            reinit_groups()
            self.reinit_pygame()
            self.show()
Esempio n. 12
0
 def test_loading_levels(self):
     for fn in get_level_names():
         level = load_level(fn)
         print fn
         print level.to_primitive()