예제 #1
0
 def test_sort_squares(self):
     s1, s2 = Square("wheat", 0), Square("grass", 1)
     t1 = Tile(s1, s2)
     self.assertEqual(str(t1), "[1grass|0wheat]", "Crowns should be t1")
     s1, s2 = Square("forest", 0), Square("wheat", 0)
     t2 = Tile(s1, s2)
     self.assertEqual(str(t2), "[0wheat|0forest]", "wheat is before forest")
예제 #2
0
    def __init__(self, origin, size, scale, rand=True):
        """Creates a map of tiles using"""

        # Call parent constructor
        super().__init__(pygame.Rect(origin, (size * scale, size * scale)))

        # Create tile group
        self.tileGroup = pygame.sprite.Group()

        # Create tile map
        for y in range(size):
            for x in range(size):

                # Generate random tile of the 4 variants
                if rand:
                    tile = Tile.variant(random.randrange(4), scale)
                # Dunno if this is useful
                else:
                    tile = Tile(scale)

                # Place the tile depeding on loop progress
                tile.rect.x = x * config.game.tileSize
                tile.rect.y = y * config.game.tileSize

                # Add tile to sprite groups
                self.tileGroup.add(tile)
예제 #3
0
 def test_print(self):
     """Testing that the print statement works"""
     s1, s2 = Square("wheat", 0), Square("forest", 0)
     t1 = Tile(s1, s2)
     self.assertEqual(str(t1), "[0wheat|0forest]")
     s1, s2 = Square("swamp", 2), Square("mine", 0)
     t2 = Tile(s1, s2)
     self.assertEqual(str(t2), "[2swamp|0mine]")
예제 #4
0
    def test_standard_deck(self):
        d = Deck(True)
        s13, s14 = Square("wheat", 0), Square("wheat", 0)
        t7 = Tile(s13, s14, 1)
        s15, s16 = Square("wheat", 1), Square("grass", 0)
        t8 = Tile(s15, s16, 21)

        self.assertTrue(d.contains(t7), "Deck contains standard Tile #1")
        self.assertTrue(d.contains(t8), "Deck contains standard Tile #21")
예제 #5
0
 def test_get(self):
     """Testing the get methods for Tile Class"""
     s1, s2 = Square("wheat", 0), Square("forest", 1)
     t = Tile(s1, s2)
     self.assertEqual(str(t.get_square2()), "0wheat")
     self.assertEqual(str(t.get_square1()), "1forest")
     self.assertEqual(t.square2.get_terrain(), "wheat")
     self.assertEqual(t.square1.get_terrain(), "forest")
     self.assertEqual(t.square2.get_crowns(), 0)
     self.assertEqual(t.square1.get_crowns(), 1)
     self.assertEqual(t.get_direction(), "right")
예제 #6
0
    def convertBoard(self, board):

        TileBoard = [[Tile(0, 0, 0) for x in range(7)] for y in range(9)]

        y = 1.5
        for i in range(9):
            x = 1
            for j in range(7):
                emoji = board[i][j]
                TileBoard[i][j] = Tile(emoji, x, y)
                x += 1
            y += 1
        return TileBoard
예제 #7
0
    def __init__(self, origin, width, height):

        # Call parent constructor
        super().__init__(pygame.Rect(origin, (width, height)))

        # Create config object
        self.cfg = config.interface()

        # Create blank tile
        self.tile = Tile(config.game.tileSize)

        # Create tile icon
        # Create starting rect with tile size
        iconRect = pygame.Rect((0, 0),
                               (self.tile.rect.width, self.tile.rect.height))

        # Create tileIcon using rect
        self.tileIcon = Icon(iconRect,
                             self.tile.image,
                             border=self.cfg.imgBorder,
                             bc=config.color.brown,
                             outer=True)

        # Center tileIcon rect
        self.tileIcon.rect.center = (self.rect.width * self.cfg.imgXRatio,
                                     self.rect.height * self.cfg.imgYRatio)
    def generateTileGrid(dimX, dimY, dataStore, seed=None):
        random.seed(seed)
        listOfTiles = list()
        for x in range(dimX):
            for y in range(dimY):
                randInt = random.randint(0, 12)
                tile = Tile.Tile(dataStore)
                tile.ID.IdX = x
                tile.ID.IdY = y

                if randInt == 0:
                    tile.ResourceType = "Wood"
                elif randInt == 1:
                    tile.ResourceType = "Food"
                elif randInt == 2:
                    tile.ResourceType = "Stone"
                elif randInt == 3:
                    tile.ResourceType = "Water"
                    tile.Walkable = False
                elif randInt == 4:
                    tile.ResourceType = "Iron"

                listOfTiles.append(tile)
                random.seed(None)
        return listOfTiles
예제 #9
0
    def test_test_deck(self):
        s13, s14 = Square("swamp", 2), Square("water", 0)
        t7 = Tile(s13, s14)
        s15, s16 = Square("grass", 2), Square("swamp", 0)
        t8 = Tile(s15, s16)
        d = Deck(deck=[t7, t8])
        s15, s16 = Square("grass", 2), Square("swamp", 0)
        t9 = Tile(s15, s16)
        s15, s16 = Square("grass", 2), Square("swamp", 1)
        t10 = Tile(s15, s16)

        self.assertTrue(d.contains(t7),
                        "Passing a Deck (for testing purpose) to Deck")
        self.assertTrue(d.contains(t9),
                        "Passing a Deck (for testing purpose) to Deck")
        self.assertFalse(d.contains(t10),
                         "Passing a Deck (for testing purpose) to Deck")
예제 #10
0
    def generate_board(self, size_x, size_y, mines, starting_choice_x,
                       starting_choice_y):
        board = [[Tile() for x in range(size_x)] for y in range(size_y)]
        for m in range(mines):
            not_placed = True
            while not_placed:
                x = randrange(size_x)
                y = randrange(size_y)
                if not (x == starting_choice_x
                        and y == starting_choice_y) and not board[y][x].mined:
                    board[y][x].mine_tile()
                    not_placed = False

        y = -1
        for s in board:
            y += 1
            for x in range(len(s)):
                if not board[y][x].mined:
                    if x != 0:
                        if board[y][x - 1].mined:
                            board[y][x].increase_number()
                        if y != 0:
                            if board[y - 1][x - 1].mined:
                                board[y][x].increase_number()
                        if y != size_y - 1:
                            if board[y + 1][x - 1].mined:
                                board[y][x].increase_number()
                    if y != 0:
                        if board[y - 1][x].mined:
                            board[y][x].increase_number()
                    if y != size_y - 1:
                        if board[y + 1][x].mined:
                            board[y][x].increase_number()
                    if x != size_x - 1:
                        if board[y][x + 1].mined:
                            board[y][x].increase_number()
                        if y != 0:
                            if board[y - 1][x + 1].mined:
                                board[y][x].increase_number()
                        if y != size_y - 1:
                            if board[y + 1][x + 1].mined:
                                board[y][x].increase_number()
            board[starting_choice_y][starting_choice_x].uncover_tile()
        self.board = board
        self.size_y = size_y
        self.size_x = size_x
        self.update_board()
예제 #11
0
    def reset(self):
        """Restarts the game"""

        # Clear sprite groups
        self.allGroup.empty()

        # Create map based on config
        self.map = Map((0, 0),
                       config.game.mapSize // config.game.tileSize,
                       config.game.tileSize,
                       rand=True)

        # Create interface
        self.interface = Interface((config.game.mapSize, 0),
                                   config.game.interfaceWidth,
                                   config.game.mapSize)

        # Create tile selector
        self.selectedTile = Tile(config.game.tileSize)
예제 #12
0
def rand_tile(x, y):
    tid = random.randint(0, 9)
    return Tile(x, y, tid)
예제 #13
0
def gen_smoothed_random_env():
    grid = [[None for _ in range(DIM)] for _ in range(DIM)]

    num_industries = 5#random.randint(3, 5)
    # create a list of DIM randomly selected points
    industries = []
    for _ in range(num_industries):
        origin_location = (random.randint(0, DIM - 1), random.randint(0, DIM - 1))
        x, y = origin_location
        vals = rand_tile(x, y)
        # only add this point to the list if there is not one at this location already
        if sum(point_loc == origin_location for point_loc, _ in industries) == 0:
            grid[y][x] = Tile(x, y, 9)
            industries.append((origin_location, vals))

    industry_locs = [loc for loc, _ in industries]
    # keep a list of all of the locations that something has been placed in
    available_locs = []
    for x in range(DIM):
        for y in range(DIM):
            if (x, y) not in industry_locs:
                available_locs.append((x, y))

    # randomly choose origin points for certain environment features (forest, residential area, etc)
    # and organically expand these areas to create realistic-looking terrain
    while len(available_locs) != 0:
        origin_location = available_locs[random.randint(0, len(available_locs) - 1)]
        orig_x, orig_y = origin_location
        available_locs.remove(origin_location)
        # keep a list of all locations that are a part of this environment feature
        environment_feature_locs = [origin_location]

        # take a random choice of non-industry tiles
        tile_id = random.randint(0, 8)

        grid[orig_y][orig_x] = Tile(orig_x, orig_y, tile_id)

        # choose a relatively high but arbitrary threshold ([0.7, 0.9]) to determine whether this environment
        # feature should continue getting expanded
        thres = 0.7 + random.random() * 0.2

        r = random.random()
        while r < thres:
            possible_next_locs = set()

            # get all available locations to expand by taking mapping of the current environment feature
            # expanded one unit in each direction and finding which ones are available

            def add_locs(dx, dy):
                for loc_x, loc_y in environment_feature_locs:
                    if (loc_x + dx, loc_y + dy) in available_locs:
                        possible_next_locs.add((loc_x + dx, loc_y + dy))
            add_locs(1, 0)
            add_locs(-1, 0)
            add_locs(0, 1)
            add_locs(0, -1)

            if len(possible_next_locs) == 0:
                break

            # sort the possible locations by distance away from origin of environment feature (to
            # make new points closer to origin more likely to be expanded to create organic-looking
            # terrain)

            def get_dist(loc_1, loc_2):
                l1x, l1y = loc_1
                l2x, l2y = loc_2
                return (l1x - l2x)**2 + (l1y - l2y)**2

            possible_next_locs = list(possible_next_locs)
            random.shuffle(possible_next_locs)
            possible_next_locs = sorted(possible_next_locs, key=lambda loc: get_dist(loc, origin_location))

            # allow closer locations to be more likely
            next_loc_i = int(random.randint(0, len(possible_next_locs) - 1) ** 0.8)

            next_loc = possible_next_locs[next_loc_i]
            next_loc_x, next_loc_y = next_loc

            environment_feature_locs.append(next_loc)

            grid[next_loc_y][next_loc_x] = Tile(next_loc_x, next_loc_y, tile_id)
            available_locs.remove(next_loc)

            r = random.random()

    return grid
예제 #14
0
from Tiles import Tile
import copy

platform = Tile.Platform()
torch = Tile.Torch()
background = Tile.Background()
crate = Tile.Crate()
meme_crate = Tile.MemeCrate()

current_object = None

hex_dict = {
    "#0026FF": platform,
    "#FF6A00": torch,
    "#FFFFFF": background,
    "#FFD800": crate,
    "#00FF21": meme_crate
}


def init_empty_obj(hex_val, x, y):
    temp = copy.copy(hex_dict.get(hex_val))
    temp.set_pos(x, y)
    return temp


def get_object(hex_val, x, y):
    temp = hex_dict[hex_val]
    temp.set_pos(x, y)
    return temp
예제 #15
0
    def test_calculate_value(self):
        # mono-suit tiles, no crowns
        s1, s2 = Square("grass", 0), Square("grass", 0)
        t1 = Tile(s1, s2)
        s3, s4 = Square("wheat", 0), Square("water", 0)
        t2 = Tile(s3, s4)

        self.assertGreater(t2.get_value(), t1.get_value(),
                           "Mono-tiles are lowest")
        s5, s6 = Square("forest", 0), Square("forest", 0)
        t3 = Tile(s5, s6)
        self.assertGreater(t1.get_value(), t3.get_value(),
                           "Mono-tiles are ordered")

        # crowns
        s7, s8 = Square("wheat", 1), Square("forest", 0)
        t4 = Tile(s7, s8)
        self.assertGreater(t4.get_value(), t1.get_value(),
                           "Crowns are greater")
        s9, s10 = Square("swamp", 2), Square("forest", 0)
        t5 = Tile(s9, s10)
        s11, s12 = Square("mine", 3), Square("water", 0)
        t6 = Tile(s11, s12)
        self.assertGreater(t6.get_value(), t4.get_value(),
                           "More crowns are greater")
        self.assertGreater(t6.get_value(), t5.get_value(),
                           "More crowns are greater")
        s13, s14 = Square("swamp", 2), Square("water", 0)
        t7 = Tile(s13, s14)
        s15, s16 = Square("grass", 2), Square("swamp", 0)
        t8 = Tile(s15, s16)
        self.assertGreater(t7.get_value(), t5.get_value(),
                           "Order within crowns works")
        self.assertGreater(t7.get_value(), t8.get_value(),
                           "Order within crowns works")

        # same tile, same value
        s17, s18 = Square("grass", 0), Square("grass", 0)
        t9 = Tile(s17, s18)
        self.assertEqual(t1.get_value(), t9.get_value(),
                         "Identical tile has same value")

        # random_check on standard tiles
        d = Deck(True)
        d.shuffle()
        for n in range(10):
            t1, t2 = d.deal_tile(), d.deal_tile()
            v1, v2 = t1.get_value(), t2.get_value()
            if v1 >= v2:
                bigger, smaller = t1, t2
            else:
                bigger, smaller = t2, t1
            self.assertGreaterEqual(bigger.calculate_value(),
                                    smaller.calculate_value(),
                                    ("Random check number " + str(n) +
                                     "\ninputs:" + str(t1) + str(t2) + "\n"))
예제 #16
0
 def test_rotate(self):
     s1, s2 = Square("wheat", 0), Square("forest", 1)
     t = Tile(s1, s2)
     self.assertEqual(t.get_direction(), "right")
     t.rotate("clockwise")
     self.assertEqual(t.get_direction(), "down")
     t.rotate("counterclockwise")
     t.rotate("counterclockwise")
     self.assertEqual(t.get_direction(), "up")
     t.rotate("counterclockwise")
     self.assertEqual(t.get_direction(), "left")
예제 #17
0
 def __init__(self, player):
     self.platform = Tile.Platform()
     self.player = player
     self.inCollisionDown = False
     self.plat = Mapper.get_platforms()
예제 #18
0
  def __init__(self, window, screenManager):
    print ("Creating Level One")
    self.window = window
    self.batch = pyglet.graphics.Batch()
    self.screenManager = screenManager
    self.player = Player(self.batch, 128, 300)
    self.cameraOffset = 0
    
    self.hSpeed = 128
    
    self.MoveLeft = False
    self.MoveRight = False
    self.group = pyglet.graphics.OrderedGroup(0)
    self.currentAnimation = pyglet.sprite.Sprite(AssetManager.getInstance().Background, batch=self.batch, group=self.group)

    
    self.tiles = []

    #this one can be the cage if we get it
    self.tiles.append(Tile(self.batch, 10, 7, AssetManager.getInstance().ground5))

    #steps
    self.tiles.append(Tile(self.batch, 14, 6, AssetManager.getInstance().grassleft))
    self.tiles.append(Tile(self.batch, 15, 5, AssetManager.getInstance().grassleft))
    self.tiles.append(Tile(self.batch, 16, 4, AssetManager.getInstance().grassleft))
    self.tiles.append(Tile(self.batch, 17, 4, AssetManager.getInstance().ground0))
    self.tiles.append(Tile(self.batch, 18, 4, AssetManager.getInstance().grassright))
    #tower
    self.tiles.append(Tile(self.batch, 3, 7, AssetManager.getInstance().ground5))
    self.tiles.append(Tile(self.batch, 4, 7, AssetManager.getInstance().ground5))
    self.tiles.append(Tile(self.batch, 4, 6, AssetManager.getInstance().ground5))

    #obstacle wall
    self.tiles.append(Tile(self.batch, 20, 7, AssetManager.getInstance().ground5))
    self.tiles.append(Tile(self.batch, 20, 6, AssetManager.getInstance().ground5))
    self.tiles.append(Tile(self.batch, 20, 5, AssetManager.getInstance().ground5))

    #second steps
    self.tiles.append(Tile(self.batch, 25, 6, AssetManager.getInstance().grassleft))
    self.tiles.append(Tile(self.batch, 26, 5, AssetManager.getInstance().grassleft))
    self.tiles.append(Tile(self.batch, 27, 4, AssetManager.getInstance().grassleft))
    self.tiles.append(Tile(self.batch, 28, 4, AssetManager.getInstance().ground0))
    self.tiles.append(Tile(self.batch, 29, 4, AssetManager.getInstance().grassright))
    #Boulder will be at 29, 3, ontop of the second steps


    # pre first hole
    for i in range(0,10):
      for j in range(0,29):
        if i == 9:
          self.tiles.append(Tile(self.batch, j, i, AssetManager.getInstance().ground5))
        if i == 8:
          self.tiles.append(Tile(self.batch, j, i, AssetManager.getInstance().grass2))
    #between hole and bit
    for i in range(0,10):
      for j in range(31,35):
        if i == 9:
          self.tiles.append(Tile(self.batch, j, i, AssetManager.getInstance().ground5))
        if i == 8:
          self.tiles.append(Tile(self.batch, j, i, AssetManager.getInstance().grass2))
   #post pit
    for i in range(0,10):
      for j in range(40,50):
        if i == 9:
          self.tiles.append(Tile(self.batch, j, i, AssetManager.getInstance().ground5))
        if i == 8:
          self.tiles.append(Tile(self.batch, j, i, AssetManager.getInstance().grass2))


    #Tower
  #  self.tiles.append(Tile(self.batch, 3, 7, AssetManager.getInstance().TileGrassTopLeft))
  #  self.tiles.append(Tile(self.batch, 4, 7, AssetManager.getInstance().TileGrassRight))
  #  self.tiles.append(Tile(self.batch, 4, 6, AssetManager.getInstance().TileGrassTop))

    self.label = pyglet.text.Label('Level One',
                          font_name='Times New Roman',
                          font_size=36,
                          x=window.width//2, y=window.height-36,
                          anchor_x='center', anchor_y='center')