def show(self, ui, *, pan=(0, 0)): # show board ui.show_ui(self.pos, self.board_ui, pan=utils.add(self.pan, pan)) # show apples for apple in self.apples: apple.show(ui, pan=utils.add(self.pos, self.pan, pan))
def focus_board(self, grid): grid_pos = utils.add( self.get(grid).get_rect()[0], (self.grid_size // 2, grid[1] // 2)) self.pan = list( utils.add(self.args.get_pos(1, 1), utils.negative(self.pos), utils.negative(grid_pos))) self.pan[0] = utils.min_max(self.pan[0], -self.size(0) // 2, self.size(0) // 2) self.pan[1] = utils.min_max(self.pan[1], -self.size(1) // 2, self.size(1) // 2)
def show(self, ui, *, pan=(0, 0)): # show background ui.show_div(self.pos, self.size, color=(210, 210, 210), pan=pan) ui.show_div(self.pos, self.size, border=1, pan=pan) # show text ui.show_text(utils.add(self.pos, (50, 30)), self.name, f.cambria(25), pan=pan, align=(0, 1)) ui.show_text(utils.add(self.pos, (50, 80)), self.ip, f.cambria(15), pan=pan, align=(0, 1)) ui.show_text(utils.add(self.pos, (150, 80)), str(self.port), f.cambria(15), pan=pan, align=(0, 1)) # show button self.join_btn.show(ui, pan=pan)
def get_rect(self, *, pan=(0, 0)): pan = utils.add(self.pan, pan) return [[self.pos[0] + pan[0], self.pos[1] + pan[1]], [ self.pos[0] + self.size(0) + pan[0], self.pos[1] + self.size(1) + pan[1] ]]
def show(self, ui, *, pan=(0, 0)): ui.show_circle(utils.add(self.get_rect()[0], ((self.grid_size + 1) // 2, (self.grid_size + 1) // 2)), self.radius, color=self.color, pan=pan)
def __init__(self, args, pos, address, name, *, align=(0, 0)): self.args = args self.size = [600, 100] self.pos = utils.top_left(pos, self.size, align=align) self.ip = address[0] self.port = address[1] self.name = name self.join_btn = c.Button(utils.add(self.pos, (550, 50)), (80, 60), 'Join', font=f.tnr(20), border=1, align=(1, 1))
def set_direction(self, direction): if direction[0]**2 + direction[1]**2 != 1: return [None] default_direction = self.get_default_direction() # length is 1 or direction is not BACK if default_direction is None or utils.add(direction, default_direction) != (0, 0): self.direction = tuple(direction) return [None]
def show_circle(self, pos, radius, *, border=0, color=(0, 0, 0), align=(1, 1), pan=(0, 0)): pos = utils.add( utils.top_left(pos, (radius * 2, radius * 2), align=align), (radius, radius), pan) pygame.draw.circle(self.screen, color, pos, radius, border)
def show_div(self, pos, size, *, border=0, color=(0, 0, 0), align=(0, 0), pan=(0, 0)): # align: 0 left/top, 1 center, 2 right/bottom pos = utils.add(pos, pan) rect = [utils.top_left(pos, size, align=align), size] pygame.draw.rect(self.screen, color, rect, border)
def show_texts(self, pos, texts, font, *, align=(0, 0), pan=(0, 0)): pos = utils.add(pos, pan) text_font = self.font.render_font(font) # evaluate total size total_size = (0, 0) for text in texts: size = text_font.size(text[0]) total_size = (total_size[0] + size[0], size[1]) # draw text pos, x = utils.top_left(pos, total_size, align=align), 0 for text in texts: text_img = text_font.render(text[0], True, text[1]) self.screen.blit(text_img, (pos[0] + x, pos[1])) x += text_img.get_size()[0]
def show_triangle(self, pos, radius, direction, *, border=0, color=(0, 0, 0), pan=(0, 0)): pos = utils.add(pos, pan) if direction == 'left': points = ((pos[0] - radius, pos[1]), (pos[0] + radius, pos[1] - 2 * radius), (pos[0] + radius, pos[1] + 2 * radius)) else: points = ((pos[0] + radius, pos[1]), (pos[0] - radius, pos[1] - 2 * radius), (pos[0] - radius, pos[1] + 2 * radius)) pygame.draw.polygon(self.screen, color, points, border)
def show_text(self, pos, text, font, *, color=(0, 0, 0), background=None, save=None, align=(0, 0), pan=(0, 0)): if text == '': return if save is None: pos = utils.add(pos, pan) text_img = self.font.render_font(font).render( text, True, color, background) size = text_img.get_size() self.screen.blit(text_img, utils.top_left(pos, size, align=align)) return text_imgs = [] for char in text: char_img = self.font.load(save, char) if char_img is None: char_img = self.font.render_font(font).render( char, True, color, background) self.font.save(save, char, char_img) text_imgs.append(char_img) total_size = sum([text_img.get_size()[0] for text_img in text_imgs]), \ max([text_img.get_size()[1] for text_img in text_imgs]) pos = utils.top_left(pos, total_size, align=align) x, y = pos[0], pos[1] + total_size[1] // 2 for text_img in text_imgs: self.show_img((x, y), text_img, align=(0, 1), pan=pan) x += text_img.get_size()[0]
def show_line(self, start, end, *, width=1, color=(0, 0, 0), pan=(0, 0)): start = utils.add(start, pan) end = utils.add(end, pan) pygame.draw.line(self.screen, color, start, end, width)
def show_ui(self, pos, ui, *, pan=(0, 0), align=(0, 0)): pos = utils.top_left(utils.add(pos, pan), ui.size, align=align) self.screen.blit(ui.screen, pos)
def show_img(self, pos, img, *, align=(0, 0), pan=(0, 0)): pos = utils.add(pos, pan) size = img.get_size() self.screen.blit(img, utils.top_left(pos, size, align=align))
def show_player(self, ui, player, *, pan=(0, 0)): for grid in player.grids: self.get(grid).show_inner(ui, color=player.color, pan=utils.add(self.pos, self.pan, pan))
def get_default_direction(self): if len(self.grids) == 1: return None direction = utils.add(self.head(), utils.negative(self.grids[-2])) if direction[0]**2 + direction[1]**2 == 1: return direction
def get_target(self): return utils.add(self.head(), self.direction)