def draw(idx_new): nonlocal idx idx = idx_new % len(names) name = names[idx] if name not in allT: allT[name] = TextureGroup(name) textures = allT[name] print(len(textures.idxs) - 1) utils.clear_surface(screen) w, h = screen.get_size() x, y = 0, 0 rowMaxH = 0 margin = 2 for id, texture in textures.iter_all(): image = texture.image w1, h1 = image.get_size() if x + w1 < w: screen.blit(image, (x, y)) rowMaxH = max(rowMaxH, h1) else: x = 0 y += rowMaxH + margin if y >= screen.get_height(): break rowMaxH = 0 screen.blit(image, (x, y)) screen.blit(font.render(str(id), 1, (0, 0xff, 0)), (x, y)) x += w1 + margin textSize = font.size(name) rect = pg.Rect((0, 0), (textSize[0], textSize[1])) rect.right = screen.get_width() - 10 rect.bottom = screen.get_height() - 10 screen.blit(font.render(name, 1, (0xff, 0xff, 0xff)), rect) pg.display.update()
def draw(self): utils.clear_surface(self.image) x, y = (config.menuMarginLeft, config.menuMarginRight) w1, h1 = self.font.size('拳掌00') w1 += self.HORIZONTAL_MARGIN def draw_text(text, color): nonlocal x, y self.image.blit( self.font.render(text, config.fontAntiAliasEnable, color), (x, y), ) x += self.font.size(text)[0] + self.HORIZONTAL_MARGIN def new_line(): nonlocal x, y x = config.menuMarginLeft y += h1 + config.menuItemMarginBottom draw_text('这样的属性满意吗?', config.colorGold) for (_, _, fontSurf) in self.items: self.image.blit(fontSurf, (x, y)) x += self.HORIZONTAL_MARGIN + fontSurf.get_width() profile = self.profile for line in self.TABLE: new_line() for sortName, attribute in line: draw_text(sortName, config.colorRed) draw_text(str(profile[attribute]), config.colorWhite)
def draw(self): image = self.image utils.clear_surface(image) if self.need_box: # Draw background. pg.draw.rect(image, config.colorMenuBackground, self.rect) # Draw box border with width 2. pg.draw.rect(image, config.colorWhite, self.rect, 2) # draw items x, y = config.menuMarginLeft, config.menuMarginTop for item in self.items: image.blit(item.fontSurf, (x, y)) y += item.fontSurf.get_height() + config.menuItemMarginBottom
def make_minimap(self, save_to, scale=1 / 4): gx = self.GX * scale gy = self.GY * scale print('gx gy:', (gx, gy)) xmax, ymax = self.xmax, self.ymax # xmax //= 2 # ymax //= 2 # Left most grid: (0, ymax) # Right most grid: (xmax, 0) # Top most grid: (0, 0) # Bottom most grid: (xmax, ymax) centerX, centerY = 0, 0 open(save_to, 'wb').close() # Function to convert from grid to surface def to_spos(pos): x, y = pos return ((centerX + (x - y) * gx), ((x + y + 1) * gy)) width = int(gx * 2 + minus(to_spos((xmax, 0)), to_spos((0, ymax)))[0]) height = int(gy * 2 + minus(to_spos((xmax, ymax)), to_spos((0, 0)))[1]) progress = utils.ProgressBar(xmax * ymax) surface = utils.new_surface((width, height)) print('dest size:', surface.get_size()) centerX, centerY = surface.get_rect().center utils.clear_surface(surface) tmpSize = tmpWidth, tmpHeight = (2 ** 12,) * 2 print('tmp buffer size:', tmpSize) tmpScaleSize = (int(tmpWidth * scale), int(tmpHeight * scale)) print('tmpScaleSize:', tmpScaleSize) tmpSurface = utils.new_surface(tmpSize) blockDx = blockDy = int(min(tmpWidth / self.GX / 2, tmpHeight / self.GY / 2)) for blockX in range(0, xmax, blockDx): for blockY in range(0, ymax, blockDy): utils.clear_surface(tmpSurface) for x in range(blockDx): for y in range(blockDy): if not 0 <= x < xmax or not 0 <= y < ymax: continue # Draw floor texture = self.get_floor_texture((blockX + x, blockY + y))\ or self.textures.get(0) spos = ( tmpWidth / 2 - texture.xoff + (x - y) * self.GX, -texture.yoff + (x + y + 1) * self.GY, ) tmpSurface.blit(texture.image, spos) # Draw other texture = self.get_grid_texture((blockX + x, blockY + y)) if texture: spos = ( tmpWidth / 2 - texture.xoff + (x - y) * self.GX, -texture.yoff + (x + y + 1) * self.GY, ) tmpSurface.blit(texture.image, spos) progress.update() surface.blit( pg.transform.scale(tmpSurface, add((1, 1), tmpScaleSize)), minus(to_spos((blockX, blockY)), (tmpScaleSize[0] / 2, gy)) ) # print((blockX, blockY), to_spos((blockX, blockY))) pg.image.save(surface, save_to) print('\nfinished')
def redraw(self): with self.drawLock: currentX, currentY = self.currentPos x1, y1 = self._drawnPos dir = minus((currentX, currentY), self._drawnPos) self._swapped = False image = self.drawImage centerX, centerY = image.get_rect().center centerY -= config.bottomPadding // 2 GX, GY = self.GX, self.GY def convert(dx, dy, texture): return (centerX + (dx - dy) * GX - texture.xoff, centerY + (dx + dy) * GY - texture.yoff) # Speed up by removing 'self.*' in inner loop get_grid_texture = self.get_grid_texture get_floor_texture = self.get_floor_texture looper = self.looper floor = self.floorImage deltaDraw = dir in config.Directions.all utils.clear_surface(image) if deltaDraw: if config.drawFloor: # Delta draw floor dx, dy = negate(dir) floor.scroll((dx - dy) * GX, (dx + dy) * GY) for dx, dy in self.looper.iter_delta(dir): texture = get_floor_texture((currentX + dx, currentY + dy)) if not texture: texture = self.textures.get(0) floor.blit(texture.image, convert(dx, dy, texture)) image.blit(floor, (0, 0)) # Delta draw other rows = self._rows kL, kR = looper.kRange tL, tR = looper.tRange if dir == config.Directions.left \ or dir == config.Directions.up: rows.pop() rows.appendleft(deque()) else: rows.popleft() rows.append(deque()) if dir == config.Directions.left \ or dir == config.Directions.down: for row in rows: if not row: continue dx, dy = minus(row[0], (currentX, currentY)) if dx - dy < tL: row.popleft() else: for row in rows: if not row: continue dx, dy = minus(row[-1], (currentX, currentY)) if dx - dy >= tR: row.pop() for dx, dy in self.looper.iter_delta(dir): pos = currentX + dx, currentY + dy texture = get_grid_texture(pos) if texture: rows[dx + dy - kL].append(pos) for row in rows: for pos in row: texture = get_grid_texture(pos) spos = convert(pos[0] - currentX, pos[1] - currentY, texture) image.blit(texture.image, spos) else: # Redraw # Redraw floor utils.clear_surface(floor) for dx, dy in self.looper.iter(): pos = currentX + dx, currentY + dy texture = get_floor_texture(pos) if texture: spos = convert(dx, dy, texture) floor.blit(texture.image, spos) image.blit(floor, (0, 0)) # Delta draw other rows = deque(deque() for k in range(*self.looper.kRange)) kL = looper.kRange[0] for dx, dy in self.looper.iter(): pos = currentX + dx, currentY + dy texture = get_grid_texture(pos) if texture: rows[dx + dy - kL].append(pos) spos = convert(dx, dy, texture) image.blit(texture.image, spos) self._rows = rows # utils.debug('-' * 30) # for i, row in enumerate(rows): # utils.debug(i, row) self._drawnPos = (currentX, currentY)
def make_minimap(self, save_to, scale=1 / 4): gx = self.GX * scale gy = self.GY * scale print('gx gy:', (gx, gy)) xmax, ymax = self.xmax, self.ymax # xmax //= 2 # ymax //= 2 # Left most grid: (0, ymax) # Right most grid: (xmax, 0) # Top most grid: (0, 0) # Bottom most grid: (xmax, ymax) centerX, centerY = 0, 0 open(save_to, 'wb').close() # Function to convert from grid to surface def to_spos(pos): x, y = pos return ((centerX + (x - y) * gx), ((x + y + 1) * gy)) width = int(gx * 2 + minus(to_spos((xmax, 0)), to_spos((0, ymax)))[0]) height = int(gy * 2 + minus(to_spos((xmax, ymax)), to_spos((0, 0)))[1]) progress = utils.ProgressBar(xmax * ymax) surface = utils.new_surface((width, height)) print('dest size:', surface.get_size()) centerX, centerY = surface.get_rect().center utils.clear_surface(surface) tmpSize = tmpWidth, tmpHeight = (2**12, ) * 2 print('tmp buffer size:', tmpSize) tmpScaleSize = (int(tmpWidth * scale), int(tmpHeight * scale)) print('tmpScaleSize:', tmpScaleSize) tmpSurface = utils.new_surface(tmpSize) blockDx = blockDy = int( min(tmpWidth / self.GX / 2, tmpHeight / self.GY / 2)) for blockX in range(0, xmax, blockDx): for blockY in range(0, ymax, blockDy): utils.clear_surface(tmpSurface) for x in range(blockDx): for y in range(blockDy): if not 0 <= x < xmax or not 0 <= y < ymax: continue # Draw floor texture = self.get_floor_texture((blockX + x, blockY + y))\ or self.textures.get(0) spos = ( tmpWidth / 2 - texture.xoff + (x - y) * self.GX, -texture.yoff + (x + y + 1) * self.GY, ) tmpSurface.blit(texture.image, spos) # Draw other texture = self.get_grid_texture( (blockX + x, blockY + y)) if texture: spos = ( tmpWidth / 2 - texture.xoff + (x - y) * self.GX, -texture.yoff + (x + y + 1) * self.GY, ) tmpSurface.blit(texture.image, spos) progress.update() surface.blit( pg.transform.scale(tmpSurface, add((1, 1), tmpScaleSize)), minus(to_spos((blockX, blockY)), (tmpScaleSize[0] / 2, gy))) # print((blockX, blockY), to_spos((blockX, blockY))) pg.image.save(surface, save_to) print('\nfinished')
def redraw(self): with self.drawLock: currentX, currentY = self.currentPos x1, y1 = self._drawnPos dir = minus((currentX, currentY), self._drawnPos) self._swapped = False image = self.drawImage centerX, centerY = image.get_rect().center centerY -= config.bottomPadding // 2 GX, GY = self.GX, self.GY def convert(dx, dy, texture): return (centerX + (dx - dy) * GX - texture.xoff, centerY + (dx + dy) * GY - texture.yoff) # Speed up by removing 'self.*' in inner loop get_grid_texture = self.get_grid_texture get_floor_texture = self.get_floor_texture looper = self.looper floor = self.floorImage deltaDraw = dir in config.Directions.all utils.clear_surface(image) if deltaDraw: if config.drawFloor: # Delta draw floor dx, dy = negate(dir) floor.scroll((dx - dy) * GX, (dx + dy) * GY) for dx, dy in self.looper.iter_delta(dir): texture = get_floor_texture( (currentX + dx, currentY + dy)) if not texture: texture = self.textures.get(0) floor.blit(texture.image, convert(dx, dy, texture)) image.blit(floor, (0, 0)) # Delta draw other rows = self._rows kL, kR = looper.kRange tL, tR = looper.tRange if dir == config.Directions.left \ or dir == config.Directions.up: rows.pop() rows.appendleft(deque()) else: rows.popleft() rows.append(deque()) if dir == config.Directions.left \ or dir == config.Directions.down: for row in rows: if not row: continue dx, dy = minus(row[0], (currentX, currentY)) if dx - dy < tL: row.popleft() else: for row in rows: if not row: continue dx, dy = minus(row[-1], (currentX, currentY)) if dx - dy >= tR: row.pop() for dx, dy in self.looper.iter_delta(dir): pos = currentX + dx, currentY + dy texture = get_grid_texture(pos) if texture: rows[dx + dy - kL].append(pos) for row in rows: for pos in row: texture = get_grid_texture(pos) spos = convert(pos[0] - currentX, pos[1] - currentY, texture) image.blit(texture.image, spos) else: # Redraw # Redraw floor utils.clear_surface(floor) for dx, dy in self.looper.iter(): pos = currentX + dx, currentY + dy texture = get_floor_texture(pos) if texture: spos = convert(dx, dy, texture) floor.blit(texture.image, spos) image.blit(floor, (0, 0)) # Delta draw other rows = deque(deque() for k in range(*self.looper.kRange)) kL = looper.kRange[0] for dx, dy in self.looper.iter(): pos = currentX + dx, currentY + dy texture = get_grid_texture(pos) if texture: rows[dx + dy - kL].append(pos) spos = convert(dx, dy, texture) image.blit(texture.image, spos) self._rows = rows # utils.debug('-' * 30) # for i, row in enumerate(rows): # utils.debug(i, row) self._drawnPos = (currentX, currentY)