Пример #1
0
 def parse_tree(self: 'Panel', transform=Transform()):
     if self.has_inner_panels():
         # print('panel has attributes %s, %s, %s, %s'%(self.x, self.y, self.width, self.height))
         trans = Transform(
             transform.translation + Vector(self.x, self.y),
             transform.scale * Vector(self.width, self.height))
         l = [Panel.parse_tree(p, trans) for p in self.inner_panels]
         return l
     else:
         p = self
         pos = Vector(p.x, p.y)
         size = Vector(p.width, p.height)
         pos = transform.transform_vector(pos)
         size *= transform.scale
         p.x = pos.x
         p.y = pos.y
         p.width = size.x
         p.height = size.y
         return p
Пример #2
0
    def getEntitiesSurface(self):
        surfaces = []  # [surface, tupleposition]
        for enemy1 in self.baddies:
            # temp = pygame.Surface((self.draw_dim, self.draw_dim))
            # temp = temp.convert_alpha()
            # temp.fill((0, 0, 0, 0))
            # temp.blit(enemy1.get_image(), (0, 0))
            surfaces.append(
                (enemy1.get_image(), (enemy1.getFloatPosition(self.now) *
                                      self.draw_dim).to_int().to_tuple()))
        for tower1 in self.towers:
            # temp = pygame.Surface((self.draw_dim, self.draw_dim))
            # temp = temp.convert_alpha()
            # temp.fill((0, 0, 0, 0))
            # temp.blit(tower1.get_image(), (0, 0))
            pos = (tower1.xpos * self.draw_dim, tower1.ypos * self.draw_dim)
            surfaces.append((tower1.get_image(), pos))

            #range circle
            cs: int = tower1.range * 2
            hcs = int(cs / 2)
            tmp = pygame.Surface((cs, cs))
            tmp = tmp.convert_alpha(tmp)
            tmp.fill((0, 0, 0, 0))
            pygame.draw.circle(tmp, (255, 0, 0), (hcs, ) * 2, hcs, 1)
            surfaces.append(
                (tmp, (Vector.from_tuple(pos) +
                       (self.draw_dim / 2 - hcs)).to_int().to_tuple()))

        if self.isHoldingTower:
            temp = pygame.Surface((self.draw_dim * len(self.gameEnv.board),
                                   self.draw_dim * len(self.gameEnv.board[0])))
            temp = temp.convert_alpha()
            temp.fill((0, 0, 0, 0))
            temp.blit(self.holdingTower.get_image(), (0, 0))
            surfaces.append(
                (temp, (self.mouse_position.x - self.draw_dim / 2,
                        self.mouse_position.y - self.draw_dim / 2)))

        projSurf = pygame.Surface((self.draw_dim * len(self.gameEnv.board),
                                   self.draw_dim * len(self.gameEnv.board[0])))
        projSurf = projSurf.convert_alpha()
        projSurf.fill((0, 0, 0, 0))
        for proj1 in self.projs:
            if proj1 is None: continue
            temp = pygame.Surface((20, 20))
            temp = temp.convert_alpha()
            temp.fill((0, 0, 0, 0))
            pygame.draw.circle(temp, GREEN, (10, 10), 10)
            projSurf.blit(temp, (proj1.realX, proj1.realY))
        surfaces.append((projSurf, (0, 0)))
        return surfaces
Пример #3
0
    def __init__(self, scale=50, initial_money=300, initial_health=50):
        self.draw_dim = scale

        #Arrays for Entities
        self.baddies: List[Enemy] = []
        self.towers: List[Tower] = []
        self.projs: List[Projectile] = []
        self.shop = Shop(initial_money)
        self.player = Player(initial_health)

        #In-class time interval tracker
        self.now = 0

        self.hovering = 0

        #Class environment object
        self.gameEnv = Environment()

        #Background surface for game
        self.bgSurf = pygame.Surface(
            (self.draw_dim * len(self.gameEnv.board),
             self.draw_dim * len(self.gameEnv.board[0])))

        #Drawing the background surface
        self.cached_background: pygame.Surface = None
        self.drawBG()
        self.cached_tower_background: pygame.Surface = None

        self.towerHoverImage = pygame.Surface((self.draw_dim, self.draw_dim))
        self.towerHoverImage = self.towerHoverImage.convert_alpha()
        self.towerHoverImage.fill((0, 0, 0, 0))

        self.towerHoverX, self.towerHoverY = 0, 0
        self.holdingTower: Tower = None
        self.isHoldingTower = False
        self.mouse_position = Vector()

        self.enemy_spawner: Spawner = Spawner(self.enemyAdd, spawn_ticks=10)
Пример #4
0
 def projMove(self, proj1):
     v = Vector(proj1.enemy.xpos * self.draw_dim, proj1.enemy.ypos *
                self.draw_dim) - Vector(proj1.realX, proj1.realY)
     v = v.normalize() * proj1.speed
     proj1.realX += v.x
     proj1.realY += v.y
Пример #5
0
DISPLAY = pygame.display.set_mode((WIDTH, HEIGHT))

yaml_layout = open("big_layout_test.yaml").read() # start menu layout
panel_layouts = YAMLInstancer.get_multiple(yaml_layout, Panel, debug=False)
layout_manager = LayoutManager()
for panel_name, panel_layout in panel_layouts.items():
    layout = Layout.layoutFromPanel(panel_layout)
    layout_manager.addLayout(layout, panel_name, set_current=True)

screen_panels = layout.getPanelsOnRect((0, 0, WIDTH, HEIGHT))
elements_handler = ElementsHandler.from_panel_list(screen_panels)
print('all elements: %s'%elements_handler.get_elements())
gui = GUI(elements_handler, layout_manager)

# update / handle exiting
mouse_position = Vector(0, 0)
mouse_down = False
mouse_pressed = False
while True:
    mouse_pressed = False
    for e in pygame.event.get():
        if e.type == pygame.MOUSEMOTION:
            mouse_position = Vector(e.pos[0], e.pos[1])
        elif e.type == pygame.MOUSEBUTTONDOWN:
            mouse_pressed = True
            mouse_down = True
        elif e.type == pygame.MOUSEBUTTONUP:
            mouse_down = False
        elif e.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
Пример #6
0
 def getFloatPosition(self, current_time) -> Vector:
     return Vector(self.xpast, self.ypast) + Vector(
         self.xpos - self.xpast,
         self.ypos - self.ypast) * self.getLoopTime(current_time)
Пример #7
0
 def update(self, mouse_position: Vector, mouse_down: bool,
            mouse_pressed: bool):
     self.gui.update(mouse_position, mouse_down, mouse_pressed)
     self.game_state.update(
         mouse_position.to_tuple(), mouse_down,
         mouse_pressed)  # todo: ui shouldn't handle game interaction