Exemple #1
0
 def generate_rooms(self):  # Idea is to process everything at once so we can switch between rooms with ease.
     self.room = list()
     i = 0
     while True:  # Generate all possible rooms. if there is a gap anywhere, the generator will stop there.
         try:
             get_room(i)
             self.room.append(GameScene(i))
         except KeyError:
             break
         i += 1
     print("Generated " + str(i) + " rooms.")
 def move(self, dir):
     newroom = self.loc._neighbor(dir)
     if newroom is None:
         print("You can't go that way.")
     else:
         self.loc = get_room(newroom)
         self.look()
Exemple #3
0
 def leave(self):
     globals.chara.go_to_room(rooms.get_room('room_area1'))
     self.return_ = False
Exemple #4
0
    def __init__(self, room=0):
        super(GameScene, self).__init__()
        lines = get_room(room)
        self.lines = lines
        self.nextScene = None
        self.nextSceneThread = None
        self.roomNumber = room
        lineLength = len(max(lines, key=len))
        charset = pygame.image.load(os.path.join('images', 'charset3.png')).convert_alpha()
        shadow = pygame.image.load(os.path.join('images', 'shadow.png')).convert_alpha()
        walls = pygame.image.load(os.path.join('images', 'veggur2.png')).convert_alpha()
        heart = pygame.image.load(os.path.join('images', 'hearts.png')).convert_alpha()
        floor_tile = pygame.image.load(os.path.join('images', 'floor.png')).convert_alpha()
        door = pygame.image.load(os.path.join('images', 'hurd2.png')).convert_alpha()
        self.arrowSprite = pygame.image.load(os.path.join('images', 'arrow.png')).convert_alpha()
        self.sword_texture = aspect_scale(pygame.image.load(os.path.join('images', 'swords3.png')).convert_alpha(), (100000, drawSize))
        self.sword_icon = pygame.image.load(os.path.join('images', 'sword icon.png')).convert_alpha()
        self.keyTexture = pygame.image.load(os.path.join('images', 'Small_Key_MC.gif')).convert_alpha()
        self.heartTexture = heart
        self.paused = False
        self.entities = pygame.sprite.LayeredUpdates()
        self.npcs = pygame.sprite.Group()
        self.animations = list()
        self.collidables = list()
        charset = aspect_scale(charset, (drawSize * 16, 100000)) # Even though it shouldn't, this does affect character width and therefore AI
        character_sprite_size = (charset.get_width() / 18, charset.get_height() / 8, drawSize * 0.9)
        charsetRect = charset.get_rect()
        self.player = Player(pygame.Rect(30, 30, drawSize-1, drawSize / 5 * 3), charset.subsurface(pygame.Rect(0, charsetRect.bottom - (charsetRect.height / 8 * 4), charset.get_width() / 18 * 3, charsetRect.height / 8 * 4)), character_sprite_size)
        self.block_group = pygame.sprite.OrderedUpdates()
        self.action_group = pygame.sprite.RenderUpdates()
        self.background_group = pygame.sprite.Group()
        self.grand_clock = pygame.time.Clock()
        self.levelrect = pygame.Rect(0, 0, lineLength * drawSize, len(lines) * drawSize)
        self.gameSurface = pygame.Surface(self.levelrect.size)
        self.camera = pygame.Rect(0, 0, min(window_width, self.gameSurface.get_width()), min(window_height, self.gameSurface.get_height()))


        #Custom event timers
        self.timers = list()

        #Grid
        self.grid = Grid([lineLength, len(lines)])
        screenrect = pygame.Rect(0, 0, window_width, window_height)
        self.cameraLeeway = pygame.Rect(0, 0, window_width / (drawSize / 8), window_height / (drawSize / 8))
        self.cameraLeeway.center = screenrect.center
        self.levelrect.center = screenrect.center
        self.doors = list()
        """ Guide to room element codes:
        Stone floor  = 0
        Stone wall = 1
        Wood door = 2, add a "L" to make it locked
        Player = "Player"
        Melee enemy = "Stalker"
        Bow enemy = "Bowman"
        Chest = "Chest", contents can be found at [0][0]
        """
        for i in xrange(len(lines)):
            for j in xrange(len(lines[i])):
                rect = pygame.Rect(j * drawSize, i * drawSize, drawSize, drawSize)
                if 1 in lines[i][j]:
                    sliced = self.make_array_slice(lines, i, j, [1])  # array, i, j, filler
                    sprite = self.make_wall_block(walls, sliced)
                    sprite = SimpleRectSprite(rect, sprite.image, True)
                    self.block_group.add(sprite)
                if "Stalker" in lines[i][j]:
                    stalker = Stalker(pygame.Rect(rect.x, rect.y, drawSize-1, drawSize / 5 * 3), charset.subsurface(pygame.Rect(charset.get_width() / 18 * 3, charsetRect.bottom - (charsetRect.height / 8 * 4), charset.get_width() / 18 * 3, charsetRect.height / 8 * 4)), character_sprite_size, self.player)
                    self.npcs.add(stalker)
                if "Bowman" in lines[i][j]:
                    bowman = Bowman(pygame.Rect(rect.x, rect.y, drawSize-1, drawSize / 5 * 3), charset.subsurface(pygame.Rect(charset.get_width() / 18 * 6, charsetRect.bottom - (charsetRect.height / 8 * 4), charset.get_width() / 18 * 3, charsetRect.height / 8 * 4)), character_sprite_size, self.player)
                    self.npcs.add(bowman)
                    #pygame.time.set_timer(bowmanShootEvent, 5000)
                if "Player" in lines[i][j]:
                    self.player.update_player(pygame.Rect(rect.x, rect.y, drawSize - 1, drawSize / 5 * 3), 180)
                if 2 in lines[i][j]:
                    # Check if vertical door or horizontal door
                    # If door has already been made, skip this
                    # Assuming that this is the top-left corner of the door
                    array_slice = self.make_array_slice(lines, i, j, [0])
                    temp_rect = pygame.Rect(rect)
                    if not (2 in array_slice[0][1] or 2 in array_slice[1][0]):
                        rotation = 0
                        if len(lines) - 1 >= (i + 1) and len(lines[i]) - 1 >= (j + 2) and 2 in lines[i+1][j+2]:
                            if i > len(lines) / 2:
                                rotation = 180
                            else:
                                rotation = 0
                            temp_rect.w = drawSize * 3
                            temp_rect.h = drawSize * 2
                        elif len(lines) - 1 >= (i + 2) and len(lines[i]) - 1 >= (j + 1) and 2 in lines[i+2][j+1]:
                            if j > len(lines[i]) / 2:
                                rotation = 270
                            else:
                                rotation = 90
                            temp_rect.w = drawSize * 2
                            temp_rect.h = drawSize * 3
                        else:
                            print("Level not set up properly.")
                            raise Exception
                        inner_rect = pygame.Rect(75, 0, 75, 50) if "L" in lines[i][j] else pygame.Rect(0, 0, 75, 50)
                        door_texture = door.copy().subsurface(inner_rect)
                        door_texture = pygame.transform.scale(door_texture, (drawSize * 3, drawSize * 2))
                        door_texture = pygame.transform.rotate(door_texture, rotation)
                        temp_door = Door(temp_rect, door_texture, rotation, pygame.transform.scale(door, (drawSize * 6, drawSize * 2)), True if "L" in lines[i][j] else False)
                        self.doors.append(temp_door)
                        self.action_group.add(temp_door)
                        self.collidables.append(temp_door)
                if 0 in lines[i][j]:
                    sprite = SimpleRectSprite(rect, floor_tile, True)
                    sprite.image = pygame.transform.rotate(sprite.image, random.randrange(0, 360, 90))
                    self.background_group.add(sprite)
        self.triggers = list()
        for tag in lines[0][0]:
            if type(tag) is Trigger:
                self.triggers.append(tag)
                try:
                    tag.placeWhere
                except:
                    pass
                else:
                    tag.set_place(lines)

        # Merge individual blocks into lines
        self.block_lines = pygame.sprite.OrderedUpdates()
        lastBlock = None
        currentLineSpriteGroup = pygame.sprite.Group()
        for block in self.block_group:
            if lastBlock is None:
                lastBlock = block
                currentLineSpriteGroup.add(block)
                continue
            if (lastBlock.rect.y == block.rect.y and lastBlock.rect.height == block.rect.height and lastBlock.rect.right == block.rect.left):
                currentLineSpriteGroup.add(block)
            else:
                self.block_lines.add(ConjoinedSpriteGroup(currentLineSpriteGroup))
                currentLineSpriteGroup = pygame.sprite.Group(block)
            lastBlock = block
        self.block_lines.add(ConjoinedSpriteGroup(currentLineSpriteGroup))

        #Merge lines into large blocks
        """self.large_block_group = pygame.sprite.Group()
        tempasd = pygame.sprite.Group()
        for line in self.block_lines:
            for line2 in self.block_lines:
                if (lastBlock.rect.x == block.rect.x and lastBlock.rect.width == block.rect.width and (lastBlock.rect.top == block.rect.bottom)):
                    for large_block in self.large_block_group:
                        if large_block.rect.contains(line2):
                            break
                    else:
                        tempasd.add(line2)
        self.large_block_group.add(ConjoinedSpriteGroup(pygame.sprite.Group()))"""

        self.collidables.extend(self.block_lines)

        self.entities.add(self.player, self.npcs)
        self.character_collision_boxes = [char.get_collision_box() for char in self.entities]
        #self.grid.update_grid(self.collidables + self.character_collision_boxes)
        self.grid.update_grid(self.collidables)
        if self.character_collision_boxes:
            self.shadow = pygame.transform.scale(shadow, self.character_collision_boxes[0].rect.size)
        self.update_hearts(heart)
        self.update_keys(self.keyTexture)
        self.swordsprite = SimpleSprite(self.player.rect.midtop, pygame.Surface((0, 0)))
        """self.backgroundFill = pygame.Surface((window_size))
        self.backgroundFill.fill(BLACK)
        self.gameSurface.blit(self.backgroundFill, self.camera)"""
        backgroundSurface = self.gameSurface.copy()
        backgroundSurface.fill(BLACK)

        self.block_group.draw(backgroundSurface)
        self.background_group.draw(backgroundSurface)
        backgroundSprite = SimpleSprite((0, 0), backgroundSurface)
        self.background = pygame.sprite.RenderUpdates(backgroundSprite,)
        self.windowRect = pygame.Rect((0, 0), window_size)
        self.offset = pygame.Rect(0, 0, 0, 0)
        if self.levelrect.w < window_width:
            self.offset.x = (window_width - self.levelrect.w) / 2
        if self.levelrect.h < window_height:
            self.offset.y = (window_height - self.levelrect.h) / 2
        self.cameraLeeway.center = self.player.collision_rect.center
        self.arrows = pygame.sprite.RenderUpdates()
        self.timerThread = threading.Thread()
    def __init__(self):
        cmd.Cmd.__init__(self)

        self.loc = get_room("A31")
        self.look()
Exemple #6
0
 def load(self, file: str):
     """
     Load a SAVE file.
     If passed a file-like object, read from it.
     If passed a string, open a file with that name.
     If passed a string and contains a newline, use that as the SAVE file.
     """
     try:
         if isinstance(file, str) and '\n' in file:
             i = file.split('\n')
         else:
             try:
                 f = open(file)
             except TypeError:
                 f = file
             finally:
                 i = f.read().split("\n")
                 f.close()
         i = [None] + i  # for easier indexing
         self.charname = i[1]
         self.lv = int(i[2])
         self.maxhp = int(i[3])
         self.maxen = int(i[4])
         self.at = int(i[5])
         self.wstrength = int(i[6])
         self.df = int(i[7])
         self.adef = int(i[8])
         self.sp = int(i[9])
         self.xp = int(i[10])
         self.gold = int(i[11])
         self.kills = int(i[12])
         self.inventory = [None] * 8
         for j, k in zip(range(8), i[13:29:2]):
             self.inventory[j] = item.get_item(int(k))
         self.inventory = list(filter(None, self.inventory))
         self.phone = [None] * 8
         for j, k in zip(range(8), i[14:29:2]):
             self.phone[j] = item.get_item(int(k))
         self.phone = list(filter(None, self.phone))
         self.weapon = item.get_item(int(i[29]))
         self.armor = item.get_item(int(i[30]))
         for j, k in zip(range(512), i[31:543]):
             self.flags[j] = int(k)
         self.plot = i[543]
         for j, k in zip(range(4), i[544:547]):
             self.menuchoice[j] = int(k)
         self.currentsong = int(
             i[547])  # TODO: figure out how to define songs.
         if int(i[547]) in list(range(0, 5)) + list(range(239, 264)) + list(
                 range(239, 264)) and not globals.DEBUG:
             raise globals.UndertaleError
         self.room = int(i[547])
         self.time = int(i[549])
         globals.time = self.time
         globals.room = rooms.get_room(self.room)
         globals.last_save_room_name = globals.room.name
         self.custom_data = i[550:]
     except:
         exc_type, exc_value, exc_traceback = sys.exc_info()
         output = traceback.format_exception(exc_type, exc_value,
                                             exc_traceback)
         if globals.DEBUG:
             print('Load FAILED because:')
             print('\n'.join(output))
         raise globals.UndertaleError