def get_rotated_image(self, core_image_name, frame, rotation): rounded_facing = screen_lib.get_facing_angle( rotation, self.facings ) # Build name img_name = "%s_%s_%s" % ( core_image_name, self.engine.images[core_image_name].real_frame(frame), rounded_facing, ) # Cache miss? if img_name not in self.image_cache: self.image_cache[img_name] = screen_lib.make_rotated_image( image = self.engine.images[core_image_name].get(frame), angle = rounded_facing, ) return self.image_cache[img_name]
def redraw(self): """Overrides the basic redraw as it's intended to be used with more animation""" if time.time() < self._next_redraw: return if self._selection_has_changed: self.selection_changed() self._selection_has_changed = False if int(time.time()) != self.redraw_count[1]: # print("FPS: %s" % self.redraw_count[0]) self.redraw_count = [0, int(time.time())] # Draw background taking into account scroll surf = self.engine.display surf.blit(self.background_image, pygame.Rect( self.draw_margin[0], self.draw_margin[1], self.draw_area[2], self.draw_area[3], )) # Actors for a in self.actors: rounded_facing = screen_lib.get_facing_angle(a.facing[0], self.facings) a.frame += 1 img_name = "%s_%s_%s" % ( a.image, self.engine.images[a.image].real_frame(a.frame), rounded_facing ) if img_name not in self.image_cache: self.image_cache[img_name] = screen_lib.make_rotated_image( image = self.engine.images[a.image].get(a.frame), angle = rounded_facing ) # Get the actor's image and rectangle actor_img = self.image_cache[img_name] r = pygame.Rect(actor_img.get_rect()) r.left = a.pos[0] + self.draw_margin[0] - r.width/2 r.top = a.pos[1] + self.draw_margin[1] - r.height/2 # Only draw actors within the screen if r.right > self.draw_area[0] and r.left < self.draw_area[2]: if r.bottom > self.draw_area[1] and r.top < self.draw_area[3]: surf.blit(actor_img, r) # Abilities for ab in a.abilities: if ab.image != None: # First we want to get the image ab_rounded_facing = screen_lib.get_facing_angle(ab.facing[0], self.facings) ab_img_name = "%s_%s_%s" % ( ab.image, self.engine.images[ab.image].real_frame(a.frame), ab_rounded_facing ) if ab_img_name not in self.image_cache: self.image_cache[ab_img_name] = screen_lib.make_rotated_image( image = self.engine.images[ab.image].get(a.frame), angle = ab_rounded_facing ) # We now need to work out our relative coordinates rel_pos = ab.get_offset_pos() # Now we actually draw it centre_offset = self.engine.images[ab.image].get_rotated_offset(ab_rounded_facing) ability_img = self.image_cache[ab_img_name] r = pygame.Rect(ability_img.get_rect()) r.left = a.pos[0] + self.draw_margin[0] - r.width/2 + centre_offset[0] + rel_pos[0] r.top = a.pos[1] + self.draw_margin[1] - r.height/2 + centre_offset[1] + rel_pos[1] surf.blit(ability_img, r) # Selection box? if a.selected: """Removed selection boxes for now as I'm not sure how I want them to work with rotated actors""" # selection_r = pygame.transform.rotate(a.selection_rect(), -rounded_facing) # pygame.draw.rect(surf, (255, 255, 255), selection_r, 1) surf.blit(*a.health_bar(self.draw_margin[0], self.draw_margin[1])) # Draw completion box anyway if a.completion < 100: surf.blit(*a.completion_bar(self.draw_margin[0], self.draw_margin[1])) # Pass effects from the actor to the battle screen # this means that if the actor dies the effect still lives on while len(a.effects) > 0: self.effects.append(a.effects.pop()) # Do same with bullets while len(a.bullets) > 0: self.bullets.append(a.bullets.pop()) # Bullets for b in self.bullets: # --- Using code similar to Actors --- bullet_img = self.engine.images[b.image].get() r = pygame.Rect(bullet_img.get_rect()) r.left = b.pos[0] + self.draw_margin[0] - b.width/2 r.top = b.pos[1] + self.draw_margin[1] - b.height/2 # Only draw bullets within the screen if r.right > self.draw_area[0] and r.left < self.draw_area[2]: if r.bottom > self.draw_area[1] and r.top < self.draw_area[3]: if b.image == "": # Bullet is dynamically drawn b.draw(surf, self.draw_margin) else: # Bullet has an image surf.blit(bullet_img, r) # Draw effects last for i, e in enumerate(self.effects): r = pygame.Rect(e.rect) r.left = e.rect.left + self.draw_margin[0] r.top = e.rect.top + self.draw_margin[1] # Only draw effects within the screen if r.right > self.draw_area[0] and r.left < self.draw_area[2]: if r.bottom > self.draw_area[1] and r.top < self.draw_area[3]: e.draw(surf, self.draw_margin) # Placement (such as placing a building) if self.place_image: img = self.engine.images[self.place_image] r = img.get_rect() surf.blit(img.get(), pygame.Rect( self.mouse[0] - r.width/2, self.mouse[1] - r.height/2, r.width, r.height, )) # Panels for i, p in self.panels.items(): surf.blit(*p.image()) # Dragrect if self.drag_rect != None: # draw.rect uses a origin and size arguments, not topleft and bottomright line_rect = [ self.drag_rect[0] + self.draw_margin[0] + self.scroll_x, self.drag_rect[1] + self.draw_margin[1] + self.scroll_y, self.drag_rect[2] - self.drag_rect[0], self.drag_rect[3] - self.drag_rect[1], ] # line_rect[0] = max(line_rect[0], self.draw_margin[0]) pygame.draw.rect(surf, (255, 255, 255), line_rect, 1) pygame.display.flip() self._next_redraw = time.time() + self._redraw_delay self.redraw_count[0] += 1
def redraw(self): """Overrides the basic redraw as it's intended to be used with more animation""" if time.time() < self._next_redraw: return if self._selection_has_changed: self.selection_changed() self._selection_has_changed = False if int(time.time()) != self.redraw_count[1]: # print("FPS: %s" % self.redraw_count[0]) self.redraw_count = [0, int(time.time())] # Draw background taking into account scroll surf = self.engine.display surf.blit( self.background_image, pygame.Rect( self.draw_margin[0], self.draw_margin[1], self.draw_area[2], self.draw_area[3], )) # Actors for a in self.actors: rounded_facing = screen_lib.get_facing_angle( a.facing[0], self.facings) a.frame += 1 img_name = "%s_%s_%s" % (a.image, self.engine.images[a.image].real_frame( a.frame), rounded_facing) if img_name not in self.image_cache: self.image_cache[img_name] = screen_lib.make_rotated_image( image=self.engine.images[a.image].get(a.frame), angle=rounded_facing) # Get the actor's image and rectangle actor_img = self.image_cache[img_name] r = pygame.Rect(actor_img.get_rect()) r.left = a.pos[0] + self.draw_margin[0] - r.width / 2 r.top = a.pos[1] + self.draw_margin[1] - r.height / 2 # Only draw actors within the screen if r.right > self.draw_area[0] and r.left < self.draw_area[2]: if r.bottom > self.draw_area[1] and r.top < self.draw_area[3]: surf.blit(actor_img, r) # Abilities for ab in a.abilities: if ab.image != None: # First we want to get the image ab_rounded_facing = screen_lib.get_facing_angle( ab.facing[0], self.facings) ab_img_name = "%s_%s_%s" % ( ab.image, self.engine.images[ab.image].real_frame( a.frame), ab_rounded_facing) if ab_img_name not in self.image_cache: self.image_cache[ ab_img_name] = screen_lib.make_rotated_image( image=self.engine.images[ab.image].get( a.frame), angle=ab_rounded_facing) # We now need to work out our relative coordinates rel_pos = ab.get_offset_pos() # Now we actually draw it centre_offset = self.engine.images[ ab.image].get_rotated_offset(ab_rounded_facing) ability_img = self.image_cache[ab_img_name] r = pygame.Rect(ability_img.get_rect()) r.left = a.pos[0] + self.draw_margin[ 0] - r.width / 2 + centre_offset[0] + rel_pos[0] r.top = a.pos[1] + self.draw_margin[ 1] - r.height / 2 + centre_offset[1] + rel_pos[ 1] surf.blit(ability_img, r) # Selection box? if a.selected: """Removed selection boxes for now as I'm not sure how I want them to work with rotated actors""" # selection_r = pygame.transform.rotate(a.selection_rect(), -rounded_facing) # pygame.draw.rect(surf, (255, 255, 255), selection_r, 1) surf.blit(*a.health_bar(self.draw_margin[0], self.draw_margin[1])) # Draw completion box anyway if a.completion < 100: surf.blit(*a.completion_bar(self.draw_margin[0], self.draw_margin[1])) # Pass effects from the actor to the battle screen # this means that if the actor dies the effect still lives on while len(a.effects) > 0: self.effects.append(a.effects.pop()) # Do same with bullets while len(a.bullets) > 0: self.bullets.append(a.bullets.pop()) # Bullets for b in self.bullets: # --- Using code similar to Actors --- bullet_img = self.engine.images[b.image].get() r = pygame.Rect(bullet_img.get_rect()) r.left = b.pos[0] + self.draw_margin[0] - b.width / 2 r.top = b.pos[1] + self.draw_margin[1] - b.height / 2 # Only draw bullets within the screen if r.right > self.draw_area[0] and r.left < self.draw_area[2]: if r.bottom > self.draw_area[1] and r.top < self.draw_area[3]: if b.image == "": # Bullet is dynamically drawn b.draw(surf, self.draw_margin) else: # Bullet has an image surf.blit(bullet_img, r) # Draw effects last for i, e in enumerate(self.effects): r = pygame.Rect(e.rect) r.left = e.rect.left + self.draw_margin[0] r.top = e.rect.top + self.draw_margin[1] # Only draw effects within the screen if r.right > self.draw_area[0] and r.left < self.draw_area[2]: if r.bottom > self.draw_area[1] and r.top < self.draw_area[3]: e.draw(surf, self.draw_margin) # Placement (such as placing a building) if self.place_image: img = self.engine.images[self.place_image] r = img.get_rect() surf.blit( img.get(), pygame.Rect( self.mouse[0] - r.width / 2, self.mouse[1] - r.height / 2, r.width, r.height, )) # Panels for i, p in self.panels.items(): surf.blit(*p.image()) # Dragrect if self.drag_rect != None: # draw.rect uses a origin and size arguments, not topleft and bottomright line_rect = [ self.drag_rect[0] + self.draw_margin[0] + self.scroll_x, self.drag_rect[1] + self.draw_margin[1] + self.scroll_y, self.drag_rect[2] - self.drag_rect[0], self.drag_rect[3] - self.drag_rect[1], ] # line_rect[0] = max(line_rect[0], self.draw_margin[0]) pygame.draw.rect(surf, (255, 255, 255), line_rect, 1) pygame.display.flip() self._next_redraw = time.time() + self._redraw_delay self.redraw_count[0] += 1