Beispiel #1
0
def draw_text(text, rect, surface, text_color=(255, 255, 255), bkgrnd_color=()):
    font = data.get_scaled_font(*rect.size, text)
    text_s = font.render(text, 1, text_color)
    text_rect = text_s.get_rect(center=rect.center)
    if len(bkgrnd_color) == 3:
        surface.fill(bkgrnd_color, rect)
    surface.blit(text_s, text_rect)
Beispiel #2
0
 def draw(self):
     d = pg.display.get_surface()
     d.fill((0, 0, 0))
     d.blit(self.background, (data.off_x, data.off_y))
     # Draw enemies, towers, and projectiles
     for i in self.enemies + self.towers + self.projectiles:
         img_rect = i.blit_img.get_rect(
             center=(int(i.pos[0] * data.screen_w) + data.off_x,
                     int(i.pos[1] * data.screen_w) + data.off_y))
         d.blit(i.blit_img, img_rect)
     # If paused, draw the test enemy
     if self.paused:
         i = self.test_enemy
         img_rect = i.blit_img.get_rect(
             center=(int(i.pos[0] * data.screen_w) + data.off_x,
                     int(i.pos[1] * data.screen_w) + data.off_y))
         d.blit(i.blit_img, img_rect)
     # Draw selected tower range
     if self.selected_tower:
         self.selected_tower.draw()
     # Draw the menu
     d.blit(self.menu, self.rects["menu"])
     # Draw the tower being placed
     if self.drag_tower.dragging:
         self.drag_tower.draw()
     # Draw won and lose messages
     if self.game_status != PLAYING:
         dim = d.get_size()
         # Draw black overlay
         s = pg.Surface(dim)
         s.fill((0, 0, 0))
         s.set_alpha(128)
         d.blit(s, (0, 0))
         # Draw lost or won text
         text = "You Lost" if self.game_status == LOST else "You Won!"
         font = data.get_scaled_font(data.screen_w // 2, data.screen_w // 5,
                                     text)
         text_s = font.render(text, 1, (255, 255, 255))
         d.blit(text_s,
                text_s.get_rect(centerx=dim[0] // 2, bottom=dim[1] // 2))
         # Draw return to main screen text
         text = "Click to return to main screen"
         font = data.get_scaled_font(data.screen_w // 2, data.screen_w // 5,
                                     text)
         text_s = font.render(text, 1, (255, 255, 255))
         d.blit(text_s, text_s.get_rect(centerx=dim[0] // 2,
                                        top=dim[1] // 2))
Beispiel #3
0
def resize():
    global item_w, lvl_font, back_img, overlay
    # Get specific dimensions
    half_w = data.screen_w // 2
    eighth_w = data.screen_w // 8
    d = pg.display.get_surface()
    d.fill((0, 0, 0))
    # Draw middle line
    pg.draw.line(d, (200, 200, 0), [half_w + data.off_x, data.off_y],
                 [half_w + data.off_x, data.screen_w * 7 // 8 + data.off_y], int(margin * .9))
    # Title = w/8
    title_rects[2] = pg.Rect(data.off_x, data.off_y, data.screen_w, eighth_w)
    title_rects[levels] = pg.Rect(data.off_x, data.off_y, half_w - margin, eighth_w)
    title_rects[spawns] = title_rects[levels].move(half_w + margin, 0)
    # Scroll = w*4/8
    rects[levels] = pg.Rect(*title_rects[levels].bottomleft, title_rects[levels].w, half_w)
    rects[spawns] = rects[levels].move(half_w + margin, 0)
    # Level/Wave preview = w*2/8
    preview_rects[levels] = pg.Rect(data.off_x + half_w // 4, rects[levels].bottom, half_w // 2, half_w // 2)
    preview_rects[spawns] = pg.Rect(*rects[spawns].bottomleft, rects[spawns].w, half_w // 2)
    # Bottom buttons = w/8
    but_w = eighth_w * 9 // 10
    off_ = (eighth_w - but_w) // 2
    space = (data.screen_w - eighth_w) // (len(button_order) - 1)
    y = data.off_y + data.screen_w - off_ - but_w
    for i, string in enumerate(button_order):
        r = pg.Rect(data.off_x + off_ + space * i, y, but_w, but_w)
        img = data.scale_to_fit(pg.image.load(button_imgs[i]), w=but_w, h=but_w)
        d.blit(img, img.get_rect(center=r.center))
        button_rects[string] = r
    # Set up parts of the surfaces
    item_w = rects[levels].w // row_len
    back_img = pg.transform.scale(pg.image.load("res/back.png"), (item_w, item_w))
    lvl_font = data.get_scaled_font(item_w * 3 // 5, item_w * 3 // 5, "999")
    # Black overlay
    overlay = pg.Surface((item_w, item_w))
    overlay.fill((0, 0, 0))
    overlay.set_alpha(128)
    # Draw levels and waves
    draw()
    # Set up title text
    font = data.get_scaled_font(half_w - margin, title_rects[levels].h, data.get_widest_string(text))
    for j in range(3):
        title_text[j] = font.render(text[j], 1, (255, 255, 255))
        title_rects[j] = title_text[j].get_rect(center=title_rects[j].center)
    update_title()
 def draw_img(self, s, rect, bought):
     # Draw background
     back = data.scale_to_fit(pg.image.load("res/upgrade_back.png"),
                              w=rect.w,
                              h=rect.h)
     s.blit(back, back.get_rect(center=rect.center))
     # Draw cost text
     text_h = rect.h // 5
     font = data.get_scaled_font(rect.w, text_h, "Bought")
     string = "Bought" if bought else "${}".format(self.cost)
     text = font.render(string, 1, (255, 255, 255))
     s.blit(text,
            text.get_rect(center=(rect.centerx, rect.bottom - text_h // 2)))
     # Scale and draw the image
     img = data.scale_to_fit(self.img, w=rect.w, h=rect.h - text_h)
     s.blit(img,
            img.get_rect(center=(rect.centerx, rect.centery - text_h // 2)))
     if not bought:
         img = data.scale_to_fit(pg.image.load("res/lock.png"),
                                 w=rect.w,
                                 h=rect.h - text_h)
         s.blit(
             img,
             img.get_rect(center=(rect.centerx,
                                  rect.centery - text_h // 2)))
     # Draw description
     self.description_s = pg.Surface((rect.w * 2, rect.h))
     dim = self.description_s.get_size()
     text_h = dim[1] // 6
     font = data.get_scaled_font(dim[0], text_h, "")
     i = 0
     for line in self.description:
         for string in data.wrap_text(line, font, dim[0]):
             text = font.render(string, 1, (255, 255, 255))
             text_rect = text.get_rect(center=(dim[0] // 2,
                                               int(text_h * (i + .5))))
             self.description_s.blit(text, text_rect)
             i += 1
Beispiel #5
0
def draw_options():
    global item_h
    d = pg.display.get_surface()
    item_h = data.screen_w // len(buttons)
    font = data.get_scaled_font(side_w, item_h,
                                data.get_widest_string(buttons))
    d.fill((128, 128, 128), (data.off_x, data.off_y, side_w, data.screen_w))
    for i, t_ in enumerate(buttons):
        text = font.render(t_, 1, (0, 0, 0))
        text_rect = text.get_rect(center=(side_w // 2 + data.off_x,
                                          int(item_h * (i + .5)) + data.off_y))
        d.blit(text, text_rect)
        if t_ == selected:
            pg.draw.rect(d, (175, 175, 0),
                         (data.off_x, i * item_h + data.off_y, side_w, item_h),
                         5)
 def draw_upgrade_top(self):
     text_h, w = self.text_h, self.upgrade_w
     # Draw tower info surface menu surface
     self.tower_r = pg.Rect(data.off_x, data.off_y, w, w * 2 // 3)
     self.tower_s = pg.Surface((w, w * self.tower_r.h))
     # Draw tower image
     text_h = self.text_h
     img = data.scale_to_fit(self.img, w=w, h=self.tower_r.h - text_h)
     self.tower_s.blit(
         img, img.get_rect(center=(w // 2, (self.tower_r.h - text_h) // 2)))
     # Draw tower targeting type
     self.tower_font = data.get_scaled_font(
         w, text_h, "Targeting: " + data.get_widest_string(targeting_names))
     text = self.tower_font.render("Targeting: " + self.targeting, 1,
                                   (255, 255, 255))
     self.tower_s.blit(
         text, text.get_rect(center=(w // 2, self.tower_r.h - text_h // 2)))
     # Check which side to draw the upgrade menu
     if self.pos[0] < .5:
         self.tower_r.move_ip(data.screen_w - w, 0)
def draw():
    global files
    try:
        contents = listdir(path)
        # Check if we are all the way to the top of the drive
        if path.endswith(":/"):
            files.clear()
        else:
            files = ["../"]
        # Get all files and directories
        for file in contents:
            if isdir(path + file) and file[0] != '.':
                files.append(file + "/")
            elif isfile(path + file) and "." in file and file[
                    file.rfind("."):] in file_types:
                files.append(file)
        # Update ui
        global surface, line_h, scroll, max_scroll
        dim = pg.display.get_surface().get_size()
        line_h = max(50, dim[1] // 10)
        scroll = 0
        max_scroll = max(0, line_h * len(files) - dim[1])
        surface = pg.Surface((dim[0], line_h * len(files)))
        font = get_scaled_font(dim[0], line_h, get_widest_string(files))
        for i, name in enumerate(files):
            text = font.render(name, 1, (255, 255, 255))
            surface.blit(
                text,
                text.get_rect(center=(dim[0] // 2, int((i + .5) * line_h))))
            if i == selected:
                pg.draw.rect(surface, (200, 200, 0),
                             (0, i * line_h, dim[0], line_h), 2)
        redraw()
        return True
    except PermissionError:
        if selected != -1:
            w = pg.display.get_surface().get_size()[0]
            pg.draw.rect(surface, (255, 0, 0),
                         (0, selected * line_h, w, line_h), 2)
            redraw()
        return False
Beispiel #8
0
    def draw_menu(self):
        w, h = data.screen_w // 5, data.screen_w
        img_w = h // 20

        # Establish rectangles
        self.rects["menu"] = pg.Rect(data.off_x + data.screen_w, data.off_y, w,
                                     h)
        # Top
        self.rects["hp"] = pg.Rect(img_w, 0, w - img_w, img_w)
        self.rects["money"] = self.rects["hp"].move(0, img_w)
        # Bottom
        self.rects["pause"] = pg.Rect((w - img_w) // 2, h - img_w, img_w,
                                      img_w)
        self.rects["chances"] = pg.Rect(
            0, self.rects["pause"].top - img_w * 3 // 4, w, img_w // 2)
        self.rects["progress"] = pg.Rect(
            0, self.rects["chances"].top - img_w * 9 // 4, w, img_w * 2)
        # Middle
        self.rects["towers"] = pg.Rect(
            0, self.rects["money"].bottom, w,
            self.rects["progress"].top - self.rects["money"].bottom)
        self.menu_tower_w = w // self.TOWER_COLUMNS

        # Create surface
        self.menu = pg.Surface((w, h))

        # Draw money and hp text
        self.menu_font = data.get_scaled_font(*self.rects["hp"].size, "999")
        self.add_money(0)
        self.damage(0)

        # Draw money and hp icons
        rect = self.rects["money"]
        img = data.scale_to_fit(pg.image.load("res/money.png"),
                                w=img_w,
                                h=img_w)
        img_rect = img.get_rect(center=(rect.x - img_w // 2, rect.centery))
        self.menu.blit(img, img_rect)
        rect = self.rects["hp"]
        img = data.scale_to_fit(pg.image.load("res/heart.png"),
                                w=img_w,
                                h=img_w)
        img_rect = img.get_rect(center=(rect.x - img_w // 2, rect.centery))
        self.menu.blit(img, img_rect)

        # Draw pause button
        path = "res/{}.png".format("play" if self.paused else "pause")
        img = data.scale_to_fit(pg.image.load(path), w=img_w, h=img_w)
        self.menu.blit(img, img.get_rect(center=self.rects["pause"].center))

        # Display towers
        cost_font = data.get_scaled_font(self.menu_tower_w,
                                         self.menu_tower_w // 3, "9999")
        self.menu_towers = pg.Surface(
            (w, self.menu_tower_w * (math.ceil(len(TOWER_ORDER) / 2) + 1)))
        for idx in TOWER_ORDER:
            tower = data.towers[idx]
            col, row = idx % self.TOWER_COLUMNS, idx // self.TOWER_COLUMNS
            rect = pg.Rect(col * self.menu_tower_w, row * self.menu_tower_w,
                           self.menu_tower_w, self.menu_tower_w)
            img = data.scale_to_fit(tower.img,
                                    w=self.menu_tower_w,
                                    h=self.menu_tower_w)
            self.menu_towers.blit(img, img.get_rect(center=rect.center))
            text = cost_font.render(str(tower.cost), 1, (128, 128, 128))
            self.menu_towers.blit(text,
                                  text.get_rect(bottomright=rect.bottomright))
            self.tower_imgs[idx] = img
        self.towers_scroll = 0
        self.menu.blit(self.menu_towers,
                       self.rects["towers"],
                       area=((0, self.towers_scroll),
                             self.rects["towers"].size))

        self.draw_wave()