def _get_raw_shadow(self): target_img = self.target.get_image(self.capture_state) r = target_img.get_rect() #the shadow will be larger in order to make free space for fadeout. r.inflate_ip(2*self.shadow_radius, 2*self.shadow_radius) img = Surface(r.size) img.fill((255, 255, 255, 255)) img.blit(target_img, (self.shadow_radius, self.shadow_radius)) if self.sun_angle <= 0.: raise Exception("Sun angle must be greater than zero.") elif self.sun_angle != 45. and self.vertical: w, h = img.get_size() new_h = h / tan(self.sun_angle * pi / 180.) screen_size = functions.get_screen().get_size() new_h = abs(int(min(new_h, max(screen_size)))) img = scale(img, (w, new_h)) if self.angle_mode == "flip": img = flip(img, self.mode_value[0], self.mode_value[1]) elif self.angle_mode == "rotate": img = rotate(img, self.mode_value) else: raise Exception("angle_mode not available: " + str(self.angle_mode)) shadow = pilgraphics.get_shadow(img, radius=self.shadow_radius, black=self.black, alpha_factor=self.alpha_factor, decay_mode=self.decay_mode, color=self.color) return shadow
def quadrado(self, canvas: pg.Surface, pos: tp.coord, tipo: str) -> None: size = canvas.get_size() quad = pg.Rect(1, 1, size[0] - 2, size[1] - 2) canvas.fill(self.borda) i, j = pos match tipo: case "vazio": cor = self.vazio[(i + j) % 2 == 0] case "click": cor = self.click case "movimento": cor = self.movimento case "especial": cor = self.movimento case "captura": cor = self.movimento case "xeque": pg.draw.rect(canvas, self.vazio[(i + j) % 2 == 0], quad) pg.draw.circle( canvas, self.xeque, (canvas.get_size()[0] / 2, canvas.get_size()[0] / 2), min(*canvas.get_size()) / 3, ) return case _: cor = pg.Color(0, 0, 0) pg.draw.rect(canvas, cor, quad)
def _calculate_layout(self, surface: pygame.Surface): self.logger.debug(f'Re-calculating layout for {len(self.frames)} elements') frame_sizes = [(f.width, f.height) for f in self.frames] self.__layout = best_regular_screen_layout(frame_sizes, surface.get_size()) self.__last_layout_evaluation = time.time() self.__number_of_elements = len(frame_sizes) self.__surface_size = surface.get_size()
def draw(self, surface: pygame.Surface) -> List[MouseRegion]: surface.fill(self.BG_COLOR) if len(self.frames) == 0: return [] if (len(self.frames) != self.__number_of_elements or time.time() - self.__last_layout_evaluation > self.REEVALUATION_INTERVAL_SECONDS or surface.get_size() != self.__surface_size): self._calculate_layout(surface) target_size = surface.get_size() element_size = (int(target_size[0] / self.__layout[0]), int(target_size[1] / self.__layout[1])) regions = [] for i, frame in enumerate(self.frames): i_x = i % self.__layout[0] i_y = int(i / self.__layout[0]) frame_surface = _opencv_to_pygame(frame) target_rect = frame_surface.get_rect().fit(pygame.Rect(i_x * element_size[0], i_y * element_size[1], *element_size)) target_surface = surface.subsurface(target_rect) pygame.transform.scale(frame_surface, target_rect.size, target_surface) self.after_draw_frame(i, frame, surface, target_surface) regions.append(MouseRegion(frame.source, target_rect, (frame.width, frame.height))) self.after_draw(surface) return regions
def draw_board(screen: Surface, pos_x: int, pos_y: int, elem_size: int, board: BoardState): screen.fill(color) dark = (0, 0, 0) white = (255, 255, 255) line_color = (182, 93, 37) screen.blit(InputBox.FONT.render('GOMOKU', True, dark), (0.7 * screen.get_size()[0], 0.1 * screen.get_size()[1])) if abs(board.current_player) == 2: winner = board.current_player screen.blit( InputBox.FONT.render( ('black' if winner == -2 else 'white') + ' player won!!!', True, dark), (0.7 * screen.get_size()[0], 0.2 * screen.get_size()[1])) for y, x in product(range(14), range(14)): position = pos_x + x * elem_size, pos_y + y * elem_size, elem_size, elem_size pygame.draw.rect(screen, line_color, position, 2) for y, x in product(range(15), range(15)): figure = board.board[y, x] if figure == 0: continue if figure > 0: figure_color = dark else: figure_color = white r = elem_size * 3 // 8 pygame.draw.circle(screen, figure_color, ((x + 1) * elem_size, (y + 1) * elem_size), r)
def add_element_interface(self, panel: pg.Surface, frame: pg.Surface): self.panel = GameObject(panel, BaseVector(0, 0) * (.5, .93)) self.panel_frame = frame h = panel.get_size()[0] p = frame.get_size()[0] m = h - p*9 r = (m//10, (m - m//10)//8) self.slice = [r[1], h+1, p+r[0]]
def __init__(self, surface: pygame.Surface): self._screen = surface self._state = EditorState() self._widgets = self._build_widgets() self._uimanager = pygame_gui.UIManager(surface.get_size()) self._focus = Vec(surface.get_size()) // 2 self._camera = Camera(self._focus, surface.get_size()) self._camera.set_method(Follow(self._camera))
def rotateAroundPoint(surface: pg.Surface, point, rotate) -> pg.Surface: offset = np.asarray(point) - np.asarray(surface.get_size()) / 2 size = np.array(surface.get_size()) + 2 * abs(offset) sf = pg.Surface(size, pg.SRCALPHA) dest = -offset + abs(offset) sf.blit(surface, dest) sf = pg.transform.rotate(sf, rotate) return sf
def compare_surfaces(surf_a: pygame.Surface, surf_b: pygame.Surface): if surf_a.get_size() != surf_b.get_size(): return False for x in range(surf_a.get_size()[0]): for y in range(surf_a.get_size()[1]): if surf_a.get_at((x, y)) != surf_b.get_at((x, y)): return False return True
def __init__(self, main_screen: Surface, color='white', radius=7, speed=5): super().__init__(main_screen.get_size(), SRCALPHA) self.color = color self.radius = radius self.main_screen = main_screen self.width, self.height = main_screen.get_size() self.speed = speed self.x = self.width // 2 self.y = self.height // 2 self.angle_degrees = rnd.randint(30, 150) + (rnd.randint(0, 1) * 180) self.angle_radians = radians(self.angle_degrees)
def modify_raw(self, src: pygame.Surface, frame: int) -> pygame.Surface: color = self.color(frame) fac = self.fac(frame) color[3] = int(fac * color[3]) surf = pygame.Surface(src.get_size()) color_surf = pygame.Surface(src.get_size(), pygame.SRCALPHA) color_surf.fill(color) surf.blit(src, (0, 0)) surf.blit(color_surf, (0, 0)) return surf
def find_colours(image: pygame.Surface) -> list: """This function is used to find all of the different colours in an image. It will output a list of colours.""" list_of_colours = [] for x in range(image.get_size()[0]): for y in range(image.get_size()[1]): pos = (x, y) pixel = image.get_at(pos) if pixel not in list_of_colours: list_of_colours.append(pixel) image.set_at(pos, pixel) return list_of_colours
def random_spawn(cls, screen: pygame.Surface, *groups, **kwargs): """Randomly spawns a zapper in a random location and orientation""" instance = cls(screen=screen, position=(0, 0), direction=helper.chance(0.5), *groups, **kwargs) instance.position = (screen.get_size()[0] - 1, random.randrange( 0, screen.get_size()[1] - instance.size[1])) return instance
def change_colours(image: pygame.Surface, colour_to_change: pygame.Color, target_colour: pygame.Color, new_file_name: str) -> pygame.Surface: """Function will convert a colour in an image into a different colour. It will save the edited image to file in the asset folder, and return the image.""" for x in range(image.get_size()[0]): for y in range(image.get_size()[1]): pos = (x, y) pixel_col = image.get_at(pos) if pixel_col == colour_to_change: pixel_col = target_colour image.set_at(pos, pixel_col) pygame.image.save(image, f"assets/{new_file_name}_re-skin.png") return image
def render(self, ui_screen): super().render(ui_screen) # Generate a screen of the size of the map map = self.get_manager(Manager.MAP) map_screen = Surface((map.width * GRID_WIDTH, map.height * GRID_HEIGHT), SRCALPHA, 32) SESSION.render(map_screen, ui_screen) # Trim the screen to just the camera area camera_x, camera_y = self.get_manager(Manager.PLAYER).get_camera_coords() return map_screen.subsurface((camera_x, camera_y, min(CAMERA_WIDTH, map_screen.get_size()[0]), min(CAMERA_HEIGHT, map_screen.get_size()[1])))
def draw(self, screen: pg.Surface, clock: pg.time.Clock) -> Optional[ScreenType]: main_text = self.font.render('A Loo In the Dark', False, (self.red, self.green, self.blue)) main_text_rec = main_text.get_rect(center=(screen.get_size()[0] // 2, screen.get_size()[1] // 2)) screen.blit(main_text, main_text_rec) creator_text = self.small_font.render('A JJ/RB/JJ Game', False, (255, 255, 255)) creator_text_rec = creator_text.get_rect( center=(screen.get_size()[0] // 2, screen.get_size()[1] // 2 + 70)) screen.blit(creator_text, creator_text_rec) return None
def resize(self, parent: pygame.Surface): """Resize the surface and the rect's position, to a fraction of the parent (usually ...Window.image)""" self.image.fill(self.color) # new size is a fraction of the parents size size = (int(parent.get_size()[0] * self.rel_size[0]), int(parent.get_size()[1] * self.rel_size[1])) # scale surface to this new size self.image = pygame.transform.scale(self.image, size) # create new rect self.rect = self.image.get_rect() # position of rect is some fraction of the parents size relative to the parent surface self.rect.centerx = parent.get_size()[0] * self.rel_pos[0] self.rect.centery = parent.get_size()[1] * self.rel_pos[1]
def _draw_field(self, surface: pygame.Surface) -> None: contents = self._contents_with_faller() width, height = surface.get_size() x_coord = 0 y_coord = 0 jewel_length = 0 if height > 2 * width: x_coord = 0 y_coord = height / 2 - width jewel_length = width / 6 pygame.draw.rect(surface, pygame.Color(100, 100, 100), (x_coord, y_coord, width, 2 * width)) else: x_coord = width / 2 - height / 4 y_coord = 0 jewel_length = height / 12 pygame.draw.rect(surface, pygame.Color(100, 100, 100), (x_coord, y_coord, height / 2, height)) temp = x_coord for row in range(len(contents)): x_coord = temp for col in range(len(contents[row])): self._draw_jewel(surface, x_coord, y_coord, jewel_length, contents[row][col]) x_coord += jewel_length y_coord += jewel_length
def __init__(self, screen:pygame.Surface, environment:Environment, eventDisplay): threading.Thread.__init__(self) """need one Surface and one simulation""" self.screen=screen self.environment=environment self.eventDisplay=eventDisplay self.size=screen.get_size() self.background=(20,20,150) #never use yet self.running = True #definition du zoom self.center=Vector(0,0) self.radius=0 self.zoom=1 self.zoom_auto() #à # si vous voulez pas utiliser le zoom auto self.clock= pygame.time.Clock() #sauvegarde des events #pour déplacer le centre, clique gauche continue self.maintien_clique_gauche=False self.position_souris_avant=Vector(0,0) self.maintien_clique_droit=False self.new_clique_Object=[] self.pos_souris=[] self.displayRadar=True
def finish(score: int, total: int, screen: pg.Surface, font: pg.font.Font, background: pg.Surface): screen.fill('black') screen.blit(background, (0, 0)) screen.blit(background, (0, 300)) first_text = font.render('Вы набрали %d очков из %d' % (score, total), True, pg.Color('red')) second_text = font.render('Чтобы начать заново,', True, pg.Color('red')) third_text = font.render('нажмите пробел', True, pg.Color('red')) width, height = screen.get_size() screen.blit(first_text, (width // 2 - first_text.get_width() // 2, height // 2 - first_text.get_height() // 2 - second_text.get_height())) screen.blit(second_text, (width // 2 - second_text.get_width() // 2, height // 2 - second_text.get_height() // 2 + second_text.get_height())) screen.blit(third_text, (width // 2 - third_text.get_width() // 2, height // 2 - second_text.get_height() // 2 + second_text.get_height() * 2)) pg.display.flip() while True: for event in pg.event.get(): if event.type == pg.QUIT: return False elif event.type == pg.KEYDOWN and event.key == pg.K_SPACE: return True
def load_sprite(self, loader: 'SpriteLoader', surface: pg.Surface, frame_info, tag_info, vars_=None): self._loader = loader if vars_ is None: self.vars = {} else: self.vars = vars_ self._surf: pg.Surface = surface # Surface or cropped surface size depending on whether is sprite sheet or not self._size: tuple = surface.get_size() self._cropped_surf: [pg.Surface] = None self._transformed_surf: [pg.Surface] = None self._frame_info: List[Frame] = frame_info self._active_frame: [Frame] = None self._tag_info: List[Tag] = tag_info self._active_tag: [Tag] = None self._cam_zoom = [1, 1] self._transform_needed = True # Set frame if in sprite sheet self.set_frame(0) self.predict_real_size()
def _inner_pre_draw(self, surface: Surface): surface_width, surface_height = surface.get_size() x_mid = surface_width / 2 pygame.draw.rect(surface, self.BUTTON_NORMAL_COLOR, (100, 120, surface_width - 200, surface_height - 240)) self._title.set_position_centered(x_mid, 60) self._code_title.set_position_centered(x_mid - 200, 170) self._graphics_title.set_position_centered(x_mid + 200, 170) self._music_title.set_position_centered(x_mid - 200, 580) self._sound_title.set_position_centered(x_mid + 200, 580) surface.blit(self._music_text, (x_mid - 200 - self._music_text.get_width() / 2, 610)) surface.blit(self._sound_text, (x_mid + 200 - self._sound_text.get_width() / 2, 610)) self._return_button.set_position(20, surface_height - 20 - 50) arts_y = 480 arts_x = x_mid - 300 for author in self._authors: surface.blit(author["surface"], (arts_x + author["x_off"], arts_y - author["surface"].get_height())) author["title"].set_position_centered(arts_x, arts_y + 30) arts_x += 200
def apply_colour_to_surface(colour: pygame.Color, shape_surface: pygame.Surface, rect: Union[pygame.Rect, None] = None): """ Apply a colour to a shape surface by multiplication blend. This works best when the shape surface is predominantly white. :param colour: The colour to apply. :param shape_surface: The shape surface to apply the colour to. :param rect: A rectangle to apply the colour inside of. """ if rect is not None: colour_surface = pygame.Surface(rect.size, flags=pygame.SRCALPHA, depth=32) colour_surface.fill(colour) shape_surface.blit(colour_surface, rect, special_flags=pygame.BLEND_RGBA_MULT) else: colour_surface = pygame.Surface(shape_surface.get_size(), flags=pygame.SRCALPHA, depth=32) colour_surface.fill(colour) shape_surface.blit(colour_surface, (0, 0), special_flags=pygame.BLEND_RGBA_MULT)
def get_shadow(target_img, shadow_radius=2, black=255, color_format="RGBA", alpha_factor=0.85, decay_mode="exponential", color=(0,0,0), sun_angle=30., vertical=True, angle_mode="flip", mode_value=(False, True)): r = target_img.get_rect() #the shadow will be larger in order to make free space for fadeout. r.inflate_ip(2*shadow_radius, 2*shadow_radius) img = Surface(r.size) img.fill((255, 255, 255, 255)) img.blit(target_img, (shadow_radius, shadow_radius)) if sun_angle <= 0.: raise Exception("Sun angle must be greater than zero.") elif sun_angle != 45. and vertical: w, h = img.get_size() new_h = h / tan(sun_angle * pi / 180.) screen_size = functions.get_screen().get_size() new_h = abs(int(min(new_h, max(screen_size)))) img = scale(img, (w, new_h)) if angle_mode == "flip": img = flip(img, mode_value[0], mode_value[1]) elif self.angle_mode == "rotate": img = rotate(img, mode_value) else: raise Exception("angle_mode not available: " + str(angle_mode)) shadow = _pilshadow(img, radius=shadow_radius, black=black, alpha_factor=alpha_factor, decay_mode=decay_mode, color=color) # W, H = functions.get_screen_size() shadow.set_alpha(-1, RLEACCEL) return shadow.convert_alpha()
def apply_gradient_to_surface(self, input_surface: pygame.Surface, rect=None): """ Applies this gradient to a specified input surface using blending multiplication. As a result this method works best when the input surface is a mostly white, stencil shape type surface. :param input_surface: :param rect: The rectangle on the surface to apply the gradient to. """ # scale the gradient up to the right size input_surface_size = input_surface.get_size() inverse_rotated_input = pygame.transform.rotate(input_surface, -self.angle_direction) gradient_size = inverse_rotated_input.get_rect().size gradient_surf = pygame.Surface(gradient_size, flags=pygame.SRCALPHA, depth=32) pygame.transform.scale(self.gradient_surface, gradient_size, gradient_surf) gradient_surf = pygame.transform.rotate(gradient_surf, self.angle_direction) if rect is not None: input_surface.set_clip(rect) input_surface.blit(gradient_surf, rect, special_flags=pygame.BLEND_RGBA_MULT) input_surface.set_clip(None) else: gradient_placement_rect = gradient_surf.get_rect() gradient_placement_rect.center = (int(input_surface_size[0] / 2), int(input_surface_size[1] / 2)) input_surface.blit(gradient_surf, gradient_placement_rect, special_flags=pygame.BLEND_RGBA_MULT)
class Graphics: ''' A class to store and manage rendering buffers. Attributes: core_data (CoreData): The current core state object screen (Surface): The output display surface canvas_buffer (Surface): The canvas' render buffer ui_buffer (Surface): The UI's render buffer overlay_buffer (Surface): The overlay's render buffer ''' def __init__(self, core_data: CoreData, start_screen: bool = True): ''' Initialize the Graphics object. Parameters: core_data: The current core state object ''' self._core_data = core_data if start_screen: self._screen = display.set_mode(flags=FULLSCREEN) else: self._screen = Surface((1, 1)) self._core_data.screen_size = self._screen.get_size() self._canvas_buffer = Surface(self._core_data.canvas_size) self._ui_buffer = Surface(self.size, SRCALPHA) self._overlay_buffer = Surface(self.size, SRCALPHA) @property def size(self) -> Tuple[int, int]: '''Get the graphics size.''' return self._core_data.screen_size @property def canvas_buffer(self) -> Surface: '''Get the canvas graphics buffer.''' self._canvas_buffer.fill((0, 0, 0)) return self._canvas_buffer @property def ui_buffer(self) -> Surface: '''Get the UI graphics buffer.''' self._ui_buffer.fill(SRCALPHA) return self._ui_buffer @property def overlay_buffer(self) -> Surface: '''Get the overlay graphics buffer.''' self._overlay_buffer.fill(SRCALPHA) return self._overlay_buffer def render(self) -> None: '''Render the canvas, UI, and overlay buffers to the display.''' view_rect = Rect(self._core_data.view_pos, self._core_data.screen_size) self._screen.blit(self._canvas_buffer, (0, 0), view_rect) self._screen.blit(self._ui_buffer, (0, 0)) self._screen.blit(self._overlay_buffer, (0, 0)) display.update()
def makebackground(self, surface): surface.fill((0,0,0)) template = Surface(2*(min(self.zoom.width, self.zoom.height),)) template.fill((0,0,0,255)) width,height = template.get_size() ylim = surface.get_height()/height xlim = surface.get_width()/width data = self._data noise = [[pnoise2(self._selected[1][0]+x, self._selected[0][0]+y, 4, 0.85) * 50 for x in range(xlim)] for y in range(ylim)] hmin = hmax = 0 for y in range(ylim): for x in range(xlim): yd = len(data)*float(y)/ylim xd = len(data[0])*float(x)/xlim h = self._height(data, yd, xd) n = noise[y][x] if h < 0: h += -n if n > 0 else n else: h += n if n > 0 else -n if h < hmin: hmin = h if h > hmax: hmax = h self.rects = [] for y in range(ylim): for x in range(xlim): block = template.copy() yd = len(data)*float(y)/ylim xd = len(data[0])*float(x)/xlim h = self._height(data, yd, xd) n = noise[y][x] if h < 0: h += -n if n > 0 else n else: h += n if n > 0 else -n if h < 0: color = 0, 0, int(255 * (1 - h/hmin)) else: color = 0, int(255 * h/hmax), 0 block.fill(color) if self.selection: if self.selection[0][0] <= yd <= self.selection[0][1]: if self.selection[1][0] <= xd <= self.selection[1][1]: block.fill((255,0,0,32), special_flags = BLEND_ADD) rect = Rect(x*width, y*height, width, height) surface.blit(block, rect.topleft) self.rects.append((rect, (yd, xd)))
def __init__(self, center: tuple, size: int, direction: pg.Vector2, win: pg.Surface): """ asteroid class """ # Call the parent class (Sprite) constructor super().__init__() sizes = [40, 60, 80, 100] speeds = [0.16, 0.12, 0.08, 0.05] self.radius = sizes[size] / 2 self.size = size self.speed = speeds[size] # bullet image and rect self.image = pg.image.load(f'images/Asteroid_{size+1}.png') self.rect = self.image.get_rect() # starting coordenates self.center = copy.copy(center) self.rect.center = copy.copy(center) # direction Vector2 self.direction = copy.copy(direction) # window and window dimensions self.win = win self.WINDOW_WIDTH, self.WINDOW_HEIGHT = win.get_size()
def _get_raw_shadow(self): target_img = self.target.get_image(self.capture_state) r = target_img.get_rect() #the shadow will be larger in order to make free space for fadeout. r.inflate_ip(2 * self.shadow_radius, 2 * self.shadow_radius) img = Surface(r.size) img.fill((255, 255, 255, 255)) img.blit(target_img, (self.shadow_radius, self.shadow_radius)) if self.sun_angle <= 0.: raise Exception("Sun angle must be greater than zero.") elif self.sun_angle != 45. and self.vertical: w, h = img.get_size() new_h = h / tan(self.sun_angle * pi / 180.) screen_size = functions.get_screen().get_size() new_h = abs(int(min(new_h, max(screen_size)))) img = scale(img, (w, new_h)) if self.angle_mode == "flip": img = flip(img, self.mode_value[0], self.mode_value[1]) elif self.angle_mode == "rotate": img = rotate(img, self.mode_value) else: raise Exception("angle_mode not available: " + str(self.angle_mode)) shadow = pilgraphics.get_shadow(img, radius=self.shadow_radius, black=self.black, alpha_factor=self.alpha_factor, decay_mode=self.decay_mode, color=self.color) return shadow
def __updateTankSurface(self, surface: Surface, color: Color, direction: math.Vector2): surface.fill((0, 0, 0, 0)) # rect inside surface surfaceSize = surface.get_size() surfaceRect = Rect(0, 0, surfaceSize[0], surfaceSize[1]) tankRect = Rect(0, 0, self.size[0], self.size[1]) diff = math.Vector2(surfaceRect.center) - math.Vector2(tankRect.center) tankRect.move_ip(diff) temp = surface.copy() draw.rect(temp, color, tankRect) # apply tank direction to surface degree = -math.Vector2(0, 1).angle_to(direction) temp = transform.rotate(temp, degree) # temp was enlarged by rotate (wtf): # calculate diff so that temp surface is positioned outside # of the destination surface below tempRectSize = temp.get_size() diff = math.Vector2(tempRectSize) - math.Vector2(surfaceSize) # copy back wanted portion from rotation surface.blit(temp, -diff / 2)
def make_background(surf: pygame.Surface, resolution): result = pygame.Surface(resolution) x, y = surf.get_size() for i in range(0, resolution[0], x): for j in range(0, resolution[1], y): result.blit(surf, (i, j)) return result
def main(self, screen): clock = pygame.time.Clock() background = Surface(screen.get_size()) background.blit(construct_nightmare(background.get_size()), (0, 0)) self.matris = Matris() matris_border = Surface((MATRIX_WIDTH * BLOCKSIZE + BORDERWIDTH * 2, VISIBLE_MATRIX_HEIGHT * BLOCKSIZE + BORDERWIDTH * 2)) matris_border.fill(BORDERCOLOR) while 1: dt = clock.tick(45) self.matris.update((dt / 1000.) if not self.matris.paused else 0) if self.matris.gameover: return tricky_centerx = WIDTH - (WIDTH - (MATRIS_OFFSET + BLOCKSIZE * MATRIX_WIDTH + BORDERWIDTH * 2)) / 2 background.blit(matris_border, (MATRIS_OFFSET, MATRIS_OFFSET)) background.blit(self.matris.surface, (MATRIS_OFFSET + BORDERWIDTH, MATRIS_OFFSET + BORDERWIDTH)) nextts = self.next_tetromino_surf(self.matris.surface_of_next_tetromino) background.blit(nextts, nextts.get_rect(top = MATRIS_OFFSET, centerx = tricky_centerx)) infos = self.info_surf() background.blit(infos, infos.get_rect(bottom = HEIGHT - MATRIS_OFFSET, centerx = tricky_centerx)) screen.blit(background, (0, 0)) pygame.display.flip()
def resize(self, parent: pygame.Surface): """Resize the surface and the rect's position, maintaining the aspect_ratio""" self.image.fill(self.color) max_size = list(parent.get_size()) # reduce max size down dependant on padding max_size[0] *= self.padding max_size[1] *= self.padding current_size = [1, 1] # search for the max size we can grow to, while not overflowing parent overflown = False while not overflown: # increase size of image while maintaining aspect ratio current_size[0] += self.aspect_ratio[0] current_size[1] += self.aspect_ratio[1] # if this new size is >= to the size of our container, if current_size[0] >= max_size[0] or current_size[1] >= max_size[1]: # scale surface and rect to this new found size self.image = pygame.transform.scale(self.image, current_size) self.rect = self.image.get_rect() # set position of rect to a fraction of the size of parent # after removing the shrinking effect of the padding variable self.rect.centerx = (max_size[0] / self.padding) * self.pos[0] self.rect.centery = (max_size[1] / self.padding) * self.pos[1] overflown = True # x and y offset self.offset[0] = (parent.get_width() - self.rect.w) // 2 self.offset[1] = (parent.get_height() - self.rect.h) // 2
def next_tetromino_surf(self, tetromino_surf): area = Surface((BLOCKSIZE * 5, BLOCKSIZE * 5)) area.fill(BORDERCOLOR) area.fill(BGCOLOR, Rect(BORDERWIDTH, BORDERWIDTH, BLOCKSIZE*5-BORDERWIDTH*2, BLOCKSIZE*5-BORDERWIDTH*2)) areasize = area.get_size()[0] tetromino_surf_size = tetromino_surf.get_size()[0] center = areasize / 2 - tetromino_surf_size / 2 area.blit(tetromino_surf, (center, center)) return area
def blit_next_tetromino(self, tetromino_surf): """ Draws the next tetromino in a box to the side of the board """ area = Surface((BLOCKSIZE*5, BLOCKSIZE*5)) area.fill(BORDERCOLOR) area.fill(BGCOLOR, Rect(BORDERWIDTH, BORDERWIDTH, BLOCKSIZE*5-BORDERWIDTH*2, BLOCKSIZE*5-BORDERWIDTH*2)) areasize = area.get_size()[0] tetromino_surf_size = tetromino_surf.get_size()[0] # ^^ I'm assuming width and height are the same center = areasize/2 - tetromino_surf_size/2 area.blit(tetromino_surf, (center, center)) screen.blit(area, area.get_rect(top=MATRIS_OFFSET, centerx=TRICKY_CENTERX))
class Block(sprite.Sprite): """ Base Block Class """ def __init__(self, pos, is_end): sprite.Sprite.__init__(self) self.pos = pos self.grid_pos = tuple([x/40 for x in self.pos]) self.image = Surface((40, 40)).convert() self.color = (125, 125, 125) self.image.fill((255, 255, 255)) self.rect = Rect(self.pos, self.image.get_size()) self.is_shown = False self.is_path = False self.neighbors = set() self.path_value = 0 self.is_end = is_end def get_neighbors(self, lst): for b in lst: if self in b: a = self # Left if b.index(a) != 0: to_add = b[b.index(a)-1] if to_add != self and not to_add.is_shown: self.neighbors.add(b[b.index(a)-1]) # Right if b.index(a) != len(b)-1: to_add = b[b.index(a)+1] if to_add != self and not to_add.is_shown: self.neighbors.add(b[b.index(a)+1]) # Above if lst.index(b) != 0: to_add = lst[lst.index(b)-1][b.index(a)] if to_add != self and not to_add.is_shown: self.neighbors.add(lst[lst.index(b)-1][b.index(a)]) # Below if lst.index(b) != len(lst)-1: to_add = lst[lst.index(b)+1][b.index(a)] if to_add != self and not to_add.is_shown: self.neighbors.add(lst[lst.index(b)+1][b.index(a)])
class Island(WorldObject): def __init__(self, size, color, n = 20, ): super(Island, self).__init__() self.buffer = Surface(size) self.pointlist = [] self.heights = [] self.n = n self.color = color def redraw(self): self.pointlist = [] self.heights = [] if not self.pointlist: self.generate_terrain(self.buffer.get_size()) if self.color: draw.polygon(self.buffer, white, self.pointlist) else: draw.polygon(self.buffer, black, self.pointlist) draw.lines(self.buffer, white, True, self.pointlist) #draw.lines(screen, [255, 0, 0], False, [(i, val) for i, val in enumerate(self.heights)]) def generate_terrain(self, size): width, height = size scaling = 1.0 * width / self.n cumulative = [x * scaling for x in random_wander(self.n)] # turn heights into points self.pointlist = [(i * scaling, value + height - 300) for i, value in enumerate(cumulative) ] for x in range(width): i = floor(x / scaling) if i < 0: i = 0 if i >= len(self.pointlist)-1: i = len(self.pointlist)-2 remainder = x - i * scaling i = int(i) leftwidth, leftheight = self.pointlist[i] rightwidth, rightheight = self.pointlist[i + 1] h = leftheight + (rightheight - leftheight) * remainder / scaling self.heights.append(h) # add bottom right and bottom left self.pointlist.extend([size, (0, height)])
def makebackground(self, surface): surface.fill((0,0,0)) template = Surface(2*(min(self.zoom.width, self.zoom.height),), flags=SRCALPHA) template.fill((0,0,0,255)) width,height = template.get_size() o = min(surface.get_width()/width, surface.get_height()/height)/2 self.rects = [] for y in range(2*o): lat = asin(float(y-o)/o) * 180/pi r = int(sqrt(o**2-(o-y)**2)) for x in range(o-r, o+r): block = template.copy() v = [float(x-r)/o, float(y-o)/o] if x >= o: lon = self.rotate - acos(float((x-(o-r)-r))/r) * 180/pi else: lon = self.rotate + 180 + acos(float(r-(x-(o-r)))/r) * 180/pi if lon > 180: lon -= 360 h = self.planet.sample(lat, lon) color = ((0,int(255 * (h/9000.0)),0) if h > 0 else (0,0,int(255 * (1 + h/11000.0)))) block.fill(color) if self.selection: if self.selection[0][0] <= lat <= self.selection[0][1]: if self.selection[1][0] <= lon <= self.selection[1][1]: block.fill((255,0,0,32), special_flags = BLEND_ADD) rect = Rect(x * width, y * height, width, height) surface.blit(block, rect.topleft) self.rects.append((rect, (lat, lon)))
class MovingPlayerView(TileView): folder_dict = {} ressource_dict = {} len_animation = 0 def __init__(self, board_pos, board_id, direction, delay, callback=None): # Init view self.board_pos = XY(*board_pos) super(MovingPlayerView, self).__init__(self.board_pos, board_id) # Init attributes self.image = Surface((0,0)) self.delay = delay self.dirty = 2 # Get animation if direction in self.ressource_dict: self.counter = counter(self.len_animation) self.ressource = self.ressource_dict[direction] else: self.counter = counter(self.len_animation, reverse = True) self.ressource = self.ressource_dict[XY(*direction)*(-1,-1)] self.animation = (self.ressource[i] for i in self.counter) self.callback = callback def update(self): # Delay control if self.delay: self.delay -= 1 return # Animation self.image = next(self.animation, None) if self.image is None: if callable(self.callback): self.callback() self.kill() else: self.rect.size = self.image.get_size()
class Buffalo(): """ Buffalo object used in the Hunting minigame """ def __init__(self, pos_x, pos_y, picture, size, resource_path): self.picture = picture self.size = size self.resource_path = resource_path self.max_health = 100 * self.size self.health = self.max_health self.preimage = image.load(self.resource_path+"Images/"+self.picture+"_buffalo.png") self.image = scale(self.preimage, (int(self.preimage.get_width()*self.size), int(self.preimage.get_height()*self.size))) self.health_font = font.Font(None, 20) self.health_bar_container = Surface((int(75*self.size), int(12*self.size))).convert() self.health_bar_shader = Surface((self.health_bar_container.get_width() + 6, self.health_bar_container.get_height() + 6)).convert() self.health_number = self.health_font.render(str(self.health), 1, (0, 0, 0)) self.health_bar_shader.fill((175, 175, 175)) self.health_bar = Surface(self.health_bar_container.get_size()).convert() self.health_color = () if self.health >= 50: self.health_color = (float((self.max_health-self.health)*2/self.max_health*255), 255, 0) else: self.health_color = (255, float(self.health*2/self.max_health*255), 0) try: self.health_bar.fill(self.health_color) except TypeError: self.health_bar.fill((0, 0, 0)) self.health_bar_container.blit(self.health_bar, (0, 0)) self.value = 20 * self.size self.rect = Rect((0, 0), self.image.get_size()) self.rect.x = pos_x self.rect.y = pos_y self.status = "alive" self.target_y = pos_y def update(self): # Checks the health and updates the health bar self.preimage = image.load(self.resource_path+"Images/"+self.status+"_buffalo.png") self.image = scale(self.preimage, (int(self.preimage.get_width()*self.size), int(self.preimage.get_height()*self.size))) #Create health bar + shader + container self.health_bar_container = Surface((int(75*self.size), int(12*self.size))).convert() self.health_number = self.health_font.render(str(int(self.health)), 1, (255, 255, 255)) self.health_bar_shader = Surface((self.health_bar_container.get_width() + 6, self.health_bar_container.get_height() + 6)).convert() self.health_bar_shader.fill((175, 175, 175)) if self.health <= 0: self.health_bar = Surface((0, 0)).convert() else: self.health_bar = Surface((int(self.health_bar_container.get_width()/self.max_health*self.health), self.health_bar_container.get_height())).convert() # Set the color of the health_bar_container Red->Yellow->Red based on HP if self.health >= 50: self.health_color = (float((self.max_health-self.health)*2/self.max_health*255), 255, 0) else: self.health_color = (255, float(self.health*2/self.max_health*255), 0) # Band-aid solution # It tends to crash here when self.health_color isn't a valid RGB for some reason try: self.health_bar.fill(self.health_color) except TypeError: self.health_bar.fill((0, 0, 0)) self.health_bar_container.blit(self.health_bar, (0, 0)) self.health_bar_container.blit(self.health_number, (self.health_bar_container.get_width()/2 - self.health_number.get_width()/2, self.health_bar_container.get_height()/2 - self.health_number.get_height()/2)) self.health_bar_shader.blit(self.health_bar_container, (3, 3)) # Defines movement if self.status == "alive": # If buffalo is alive, move them until they reach their target X and Y positions # TODO: this logic should be reworked self.rect.x += float(3 - self.size) if self.rect.y != self.target_y: if self.rect.y < self.target_y: self.rect.y += float(3 - self.size) elif self.rect.y > self.target_y: self.rect.y -= float(3 - self.size) return self.rect.center
class Monster(sprite.Sprite): def __init__(self, move_time, nodes): sprite.Sprite.__init__(self) self.nodes = nodes self.orig_nodes = nodes self.move_time = move_time self.spawn_time = time.time() self.image = Surface((40, 40)).convert() self.image_inside = Surface((38, 38)).convert() self.image_inside.fill((0, 255, 0)) self.image.blit(self.image_inside, (1, 1)) self.pos = (80, 40) self.real_pos = (80, 40) self.rect = Rect(self.pos, self.image.get_size()) self.speed = 2 self.speed_mod = 1 self.diag_speed = 2 self.target_pos = (880, 560) self.value = 1 self.cost = 0 self.health = 100 self.damage_mod = 1 self.counter = 0 self.cur_node = self.nodes[0] self.the_dir = (0, 0) self.can_move = False self.name = "Monster" self.description = "A basic monster with slow movement speed and moderate health." def update(self, window): if time.time() - self.spawn_time >= self.move_time: self.can_move = True # If it's hit the last block if len(self.nodes) < 1: self.kill() return self.value else: # Figuring direction if self.nodes[0].rect.x > self.cur_node.rect.x: self.the_dir = (1, 0) elif self.nodes[0].rect.x < self.cur_node.rect.x: self.the_dir = (-1, 0) elif self.nodes[0].rect.y > self.cur_node.rect.y: self.the_dir = (0, 1) elif self.nodes[0].rect.y < self.cur_node.rect.y: self.the_dir = (0, -1) # Check to see the most the monster can move for speed in range(0, self.speed+1): t_dir = tuple([x * speed * self.speed_mod for x in self.the_dir]) # Monster can only move this much if self.rect.move(t_dir) == self.nodes[0].rect: self.rect.move_ip(t_dir) self.real_pos = tuple(map(sum, zip(self.real_pos, t_dir))) self.cur_node = self.nodes.pop(0) break else: # The monster can move by self.speed a = tuple([x * self.speed * self.speed_mod for x in self.the_dir]) self.real_pos = tuple(map(sum, zip(self.real_pos, a))) self.pos = tuple(map(round, self.real_pos)) self.rect.x, self.rect.y = self.pos # Conditions for the monster to die die_conditions = [self.rect.top >= window.get_height(), self.rect.left >= window.get_width(), self.rect.bottom <= 0] if any(die_conditions): self.kill() return self.value # Resetting the modifiers, they'll be changed if the monster is under an effect self.speed_mod = 1 self.damage_mod = 1 return 0 # Does damage to the monster and checks if it dies def damage(self, damage): self.health -= damage*self.damage_mod # Returns the amount of money to grant the player if the monster dies and also how much damage was done if self.health <= 0: self.kill() return self.value, damage*self.damage_mod else: return None, damage*self.damage_mod
class HarmonyUi(object): image = None rect = None currPos = (0, 0) # current position of mouse selKey = 'None' # key of selected star system keys = ['A','A#','B','C', 'C#','D','D#','E', # the key of the next 'F','F#','G','G#'] # star created keyIndex = 0 editKey = None editSpeed = 1 # Speed of next planet placed selStarId = 'None' # id of current selected star selPlanetId = 'None' # id of selected planet selNote = 'None' # note of current selecte planet selSpeed = 'None' # speed of current selected planet labelSprites = None # unmutable labels for ui fields fieldSprites = None # mutable ui fields clearBg = None # background to clear sprites def __init__(self, screen, pos): self.image = Surface((screen.get_width(), 200)) self.rect = self.image.get_rect() self.rect.topleft = (0, screen.get_height() - 200) self.currPos = pos self.editKey = self.keys[self.keyIndex] self.labelSprites = pygame.sprite.RenderUpdates() self.fieldSprites = pygame.sprite.RenderUpdates() self.clearBg = Surface(self.image.get_size()) self.create_labels() self.create_fields() def set_selected_star(self, star): self.selKey = star.key self.selStarId = star.id def set_selected_planet(self, planet): self.selNote = planet.note self.selSpeed = planet.speed self.selPlanetId = planet.id def set_current_pos(self, pos): self.currPos = pos def get_edit_speed(self): return self.editSpeed def get_edit_key(self): return self.keyIndex # Create mutable fields and add them to # the fieldSprites group. Since fields # may change with each tick the group # is emptied and repopulated every call. def create_fields(self): self.set_current_pos(pygame.mouse.get_pos()) self.fieldSprites.clear(self.image, self.clearBg) self.fieldSprites.empty() xpos = uiSprite(`self.currPos[0]`, (205,101), 'field', 'xpos') self.fieldSprites.add(xpos) ypos = uiSprite(`self.currPos[1]`, (205,126), 'field', 'ypos') self.fieldSprites.add(ypos) eSpeed = uiSprite(`self.editSpeed`, (80,100), 'field', 'eSpeed') self.fieldSprites.add(eSpeed) eKey = uiSprite(self.editKey, (80,125), 'field', 'eKey') self.fieldSprites.add(eKey) sid = uiSprite(self.selStarId, (353,101), 'field', 'sid') self.fieldSprites.add(sid) skey = uiSprite(self.selKey, (353,126), 'field', 'skey') self.fieldSprites.add(skey) pid = uiSprite(self.selPlanetId, (535,101), 'field', 'pid') self.fieldSprites.add(pid) pSpeed = uiSprite(self.selSpeed, (535,126), 'field', 'pSpeed') self.fieldSprites.add(pSpeed) # Create all of the labels for the ui fields # and place them in the labelSprites group def create_labels(self): xposLabel = uiSprite('xposlabel.png', (150, 100), 'label', 'xpl') self.labelSprites.add(xposLabel) yposLabel = uiSprite('yposlabel.png', (150, 125), 'label', 'ypl') self.labelSprites.add(yposLabel) speedLabel = uiSprite('speedlabel.png', (24, 100), 'label', 'spl') self.labelSprites.add(speedLabel) keyLabel = uiSprite('keylabel.png', (39, 125), 'label', 'kyl') self.labelSprites.add(keyLabel) staridLabel = uiSprite('staridlabel.png', (280, 100), 'label', 'sidl') self.labelSprites.add(staridLabel) starkeyLabel = uiSprite('starkeylabel.png', (275, 125), 'label', 'skl') self.labelSprites.add(starkeyLabel) pidLabel = uiSprite('planetidlabel.png', (448, 100), 'label', 'pidl') self.labelSprites.add(pidLabel) pspeedLabel = uiSprite('pslabel.png', (425, 125), 'label', 'psl') self.labelSprites.add(pspeedLabel) def draw_ui(self): self.labelSprites.draw(self.image) self.fieldSprites.draw(self.image)
# GET WEBCAM DATA BY PYGAME--------------- # Force SDL to write on our drawing area os.putenv('SDL_WINDOWID', str(canvas.get_property('window').get_xid())) # We need to flush the XLib event loop otherwise we can't # access the XWindow which set_mode() requires Gdk.flush() pygame.init() data = client_socket_test.recv(1024000) image = Image.fromstring("RGB", (80, 60), data) image = image.resize((640, 480)) image = pygame.image.frombuffer(image.tostring(), (640, 480), "RGB") print Surface.get_size(image) (WINX, WINY) = Surface.get_size(image) # setting screen according to size pygame.display.set_mode((WINX, WINY), 0, 0) screen = pygame.display.get_surface() #screen.blit(image, (0, 0)) #GLib.idle_add(pygame.display.update) print "yoyo" def updateImg(output): screen.blit(output, (0, 0)) GLib.idle_add(pygame.display.update) print "boom" return True videoListener = videoListenerThread(displayFunc=updateImg)
def makebackground(self, surface): surface.fill((0,0,0)) template = Surface(2*(min(self.zoom.width, self.zoom.height),)) template.fill((0,0,0,255)) width,height = template.get_size() ylim = surface.get_height()/height dlat = float(self.selected[0][1] - self.selected[0][0])/ylim xmax = 0 for y in range(ylim): lat = self.selected[0][0] + y * dlat scale = cos(lat * pi/180) w = int(surface.get_width() * scale/width) if w > xmax: xmax = w hmin = hmax = 0 for y in range(ylim): lat = self.selected[0][0] + y * dlat scale = cos(lat * pi/180) xlim = int(surface.get_width() * scale/width) for x in range(xlim): dx = float(xmax - xlim)/2 lon = self.selected[1][0] + (x + dx) * scale * dlat h = self.planet.sample(lat, lon) if h < hmin: hmin = h if h > hmax: hmax = h self.rects = [] for y in range(ylim): lat = self.selected[0][0] + y * dlat scale = cos(lat * pi/180) xlim = int(surface.get_width() * scale/width) for x in range(xlim): dx = float(xmax - xlim)/2 lon = self.selected[1][0] + (x + dx) * scale * dlat block = template.copy() h = self.planet.sample(lat, lon) if h < 0: color = 0, 0, int(255 * (1 - h/hmin)) else: color = 0, int(255 * h/hmax), 0 block.fill(color) if self.selection: if self.selection[0][0] <= lat <= self.selection[0][1]: if self.selection[1][0] <= lon <= self.selection[1][1]: block.fill((255,0,0,32), special_flags = BLEND_ADD) rect = Rect(int(x+dx)*width, y*height, width, height) surface.blit(block, rect.topleft) self.rects.append((rect, (lat, lon)))
def store_surface(surface: pygame.Surface, tempfiles: dict, mx: int, my: int): tfp = tempfile.SpooledTemporaryFile() string = zlib.compress(pygame.image.tostring(surface, "RGB")) data = imgheader.pack(1, *surface.get_size()) + string tfp.write(data) tempfiles[(mx, my)] = tfp
class Buffalo(): def __init__(self, posX, posY, picture, size, resourcePath): self.picture = picture self.size = size self.resourcePath = resourcePath self.maxHealth = 100 * self.size self.health = self.maxHealth self.preimage = image.load(self.resourcePath + "img/" + self.picture + "_buffalo.png") self.image = scale(self.preimage, (int(self.preimage.get_width() * self.size), int(self.preimage.get_height() * self.size))) self.healthFont = font.Font(None, 20) self.healthBarContainer = Surface((int(75 * self.size), int(12 * self.size))).convert() self.healthBarShader = Surface((self.healthBarContainer.get_width() + 6, self.healthBarContainer.get_height() + 6)).convert() self.healthNumber = self.healthFont.render(str(self.health), 1, (0, 0, 0)) self.healthBarShader.fill((175, 175, 175)) self.healthBar = Surface(self.healthBarContainer.get_size()).convert() self.healthColour = () if (self.health >= 50): self.healthColour = (float((self.maxHealth - self.health) * 2 / self.maxHealth * 255), 255, 0) else: self.healthColour = (255, float(self.health * 2 / self.maxHealth * 255), 0) try: self.healthBar.fill(self.healthColour) except TypeError: self.healthBar.fill((0, 0, 0)) self.healthBarContainer.blit(self.healthBar, (0, 0)) self.value = 20 * self.size self.rect = Rect((0, 0), self.image.get_size()) self.rect.x = posX self.rect.y = posY self.status = "alive" self.targetY = posY def update(self): self.preimage = image.load(self.resourcePath + "img/" + self.status + "_buffalo.png") self.image = scale(self.preimage, (int(self.preimage.get_width() * self.size), int(self.preimage.get_height() * self.size))) self.healthBarContainer = Surface((int(75 * self.size), int(12 * self.size))).convert() self.healthNumber = self.healthFont.render(str(int(self.health)), 1, (255, 255, 255)) self.healthBarShader = Surface((self.healthBarContainer.get_width() + 6, self.healthBarContainer.get_height() + 6)).convert() self.healthBarShader.fill((175, 175, 175)) if (self.health <= 0): self.healthBar = Surface((0, 0)).convert() else: self.healthBar = Surface((int(self.healthBarContainer.get_width() / self.maxHealth * self.health), self.healthBarContainer.get_height())).convert() if (self.health >= 50): self.healthColour = (float((self.maxHealth - self.health) * 2 / self.maxHealth * 255), 255, 0) else: self.healthColour = (255, float(self.health * 2 / self.maxHealth * 255), 0) try: self.healthBar.fill(self.healthColour) except TypeError: self.healthBar.fill((0, 0, 0)) self.healthBarContainer.blit(self.healthBar, (0, 0)) self.healthBarContainer.blit(self.healthNumber, (self.healthBarContainer.get_width() / 2 - self.healthNumber.get_width() / 2, self.healthBarContainer.get_height() / 2 - self.healthNumber.get_height() / 2)) self.healthBarShader.blit(self.healthBarContainer, (3, 3)) if (self.status == "alive"): self.rect.x += float(3 - self.size) if (self.rect.y != self.targetY): if (self.rect.y < self.targetY): self.rect.y += float(3 - self.size) elif (self.rect.y > self.targetY): self.rect.y -= float(3 - self.size) return self.rect.center
class Weapon(Item): def __init__(self, game, name): """ Slightly depreciated weapon class. Will need major rewriting. Used for loading and applying weapon characteristics to the player. """ self.game = game #setup base vars of all weapon(s) self.type = None self.shown = True self.range = 10 self.damage = 1 self.cooldown = 500 # in MS self.speed = 4 self.projectile = None self.loadWeapon(name) # attack based vars self.attacking = False def loadWeapon(self, name): """ Uses the weapon config file to load all weapon characteristics. """ config_file = open(os.path.join('rec', 'weapon', name, 'config.py')).read() exec(config_file) self.hold_image = img_load(os.path.join('rec', 'weapon', name, 'hold.png')).convert_alpha() if os.path.exists(os.path.join('rec', 'weapon', name, 'attack.png')): self.attack_image = img_load(os.path.join('rec', 'weapon', name, 'attack.png')).convert_alpha() else: self.attack_image = Surface([1, 1]) def getSurface(self, name): fi_name = name.lower().replace(' ', '_') + '.png' return img_load(os.path.join(self.game.main_path, 'rec', 'weapon', name, fi_name)) def preUpdate(self, ttime): """ Called before the update function, can be overriden for new functionality. """ pass def update(self, ttime): """ Main weapon update, should not be overriden. """ self.preUpdate(ttime) if self.type == 'short': self.shortUpdate() elif self.type == 'long': self.longUpdate() elif self.type == 'ranged': self.rangedUpdate() else: pass def shortAttack(self): self.attacking = True if self.game.Player.player_face == 'front': # I do not know why this vector needs to be 0 while the others are like, 1 self.directional_attack_image = rotate(self.attack_image, 180) self.sub_vector = [0, 0] elif self.game.Player.player_face == 'left': self.directional_attack_image = rotate(self.attack_image, 90) self.sub_vector = [-1, 0] elif self.game.Player.player_face == 'back': self.directional_attack_image = rotate(self.attack_image, 0) self.sub_vector = [0, -1] elif self.game.Player.player_face == 'right': self.directional_attack_image = rotate(self.attack_image, 270) self.sub_vector = [0.8, 0] # editing this seems to change the speed of the right dagger swing a bit self.game.Player.can_move = False self.receding = False self.potent = True self.weapon_rect = Rect(1, 1, 1, 1) p_coords = [self.game.Player.player_r.x, self.game.Player.player_r.y] a_coords = [p_coords[0] + self.game.Player.getRigging()[0], p_coords[1] + self.game.Player.getRigging()[1]] if self.game.Player.player_face == 'right' or self.game.Player.player_face == 'left': a_coords = [a_coords[0] - self.attack_image.get_height(), a_coords[1] - self.attack_image.get_width()] self.blit_pos = a_coords self.attack_ticks = self.range def shortUpdate(self): if self.attacking: for repeats in xrange(self.speed): self.game.Player.player_state = 3 self.blit_pos[0] += self.sub_vector[0] self.blit_pos[1] += self.sub_vector[1] if self.receding: self.attack_ticks += 1 elif not self.receding: self.attack_ticks -= 1 # check all monsters for touching weapon for index, monster in enumerate(self.game.EntityHandler.monsters): if monster.rect.colliderect(self.weapon_rect): if self.potent: monster.takeDamage(index, self.damage) self.potent = False if self.attacking and self.attack_ticks == self.range and self.receding: self.attacking = False self.game.Player.can_move = True elif self.attacking and self.attack_ticks <= 0 and not self.receding: self.receding = True self.sub_vector[0] *= -1 self.sub_vector[1] *= -1 def shortBlit(self): if self.attacking: if self.game.Player.player_face == 'front' or self.game.Player.player_face == 'back': height = self.directional_attack_image.get_rect().height d_rect = Rect([0, height - (self.range - self.attack_ticks)], [100, 100]) self.weapon_rect = self.game.screen.blit(self.directional_attack_image, self.game.off([self.blit_pos[0], self.blit_pos[1]]), d_rect) unoff_pos = self.game.unoff([self.weapon_rect.x, self.weapon_rect.y]) self.weapon_rect.x = unoff_pos[0] self.weapon_rect.y = unoff_pos[1] elif self.game.Player.player_face == 'right' or self.game.Player.player_face == 'left': pos = self.game.off([self.blit_pos[0] + self.rigging[1], self.blit_pos[1]]) if self.game.Player.player_face == 'left': #this prevents the "hover" look of the dagger off the default player body pos[0] += 7 width = self.directional_attack_image.get_rect().width d_rect = Rect([width - (self.range - self.attack_ticks), 0], [100, 100]) self.weapon_rect = self.game.screen.blit(self.directional_attack_image, pos, d_rect) unoff_pos = self.game.unoff([self.weapon_rect.x, self.weapon_rect.y]) self.weapon_rect.x = unoff_pos[0] self.weapon_rect.y = unoff_pos[1] def longAttack(self): self.attacking = True self.y_offset = 0 self.x_offset = 0 if self.game.Player.player_face == "front": self.init_angle = 180 elif self.game.Player.player_face == "back": self.init_angle = 0 self.y_offset = -self.game.Player.getSize()[1]*0.60 - self.attack_image.get_height() self.x_offset = -self.game.Player.getSize()[0]*0.60 elif self.game.Player.player_face == "left": self.init_angle = 90 self.x_offset = -self.game.Player.getSize()[0] - self.attack_image.get_height() elif self.game.Player.player_face == "right": self.init_angle = 270 self.directional_attack_image = rotate(self.attack_image, self.init_angle) self.attack_rect = self.directional_attack_image.get_rect() self.angle = 0 def longUpdate(self): if self.attacking: for x in xrange(5): attack_size = self.directional_attack_image.get_size() turn_point = [0, 0] if self.game.Player.isVertical(): turn_point = [attack_size[0]/2, 0] elif self.game.Player.isHorizontal(): turn_point = [0, attack_size[1]/2] self.mod_DAT = rotate(self.directional_attack_image, self.angle + 45) if self.angle < -90: self.attacking = False self.angle -= 1 def longBlit(self): if self.attacking: blit_pos = self.game.off(self.game.Player.getPos()) blit_pos = vector.add(blit_pos, self.game.Player.getRigging()) blit_pos = vector.sub(blit_pos, self.attack_image.get_size()) #offset for left/back blit_pos[0] += self.x_offset blit_pos[1] += self.y_offset self.new_rect = self.game.screen.blit(self.mod_DAT, blit_pos) self.new_rect.center = self.attack_rect.center self.attack_rect = self.new_rect def rangedAttack(self): pass def rangedUpdate(self): pass def rangedBlit(self): pass def blit(self): """ Called before the player is blitted """ if self.game.Player.player_face == 'back' and not self.attacking: self.drawInHand() if self.type == 'short': self.shortBlit() elif self.type == 'long': self.longBlit() elif self.type == 'ranged': self.rangedBlit() def blitAfter(self): if self.game.Player.player_face == 'front' and not self.attacking: self.drawInHand() def drawInHand(self): p_coords = [self.game.Player.player_r.x, self.game.Player.player_r.y] a_coords = [p_coords[0] + self.game.Player.getRigging()[0] - self.game.Player.equipment['weapon'].rigging[0], p_coords[1] + self.game.Player.getRigging()[1] - self.game.Player.equipment['weapon'].rigging[1]] self.game.screen.blit(self.hold_image, self.game.off(a_coords)) def onClick(self, game, vector): """ Called when the world is clicked. Activates the weapon. """ if self.projectile: game.Projectile(game, self.projectile, vector) if self.type == 'short': self.shortAttack() elif self.type == 'long': self.longAttack() elif self.type == 'ranged': self.rangedAttack()
class Tower(sprite.Sprite): """ Base Tower Class """ def __init__(self, pos): sprite.Sprite.__init__(self) self.name = "Tower" self.pos = pos self.grid_pos = tuple([x/40 for x in self.pos]) self.image = Surface((40, 40)).convert() self.kills = 0 self.damage_done = 0 self.image.fill((225, 50, 50)) self.rect = Rect(self.pos, self.image.get_size()) self.projectile = Surface((10, 10)).convert() self.projectile.fill((0, 255, 255)) self.projectile_speed = 5 self.projectiles = sprite.Group() self.turn_yield = 0 self.radius = 200 self.fire_rate = 1 self.damage = 25 self.level = 1 self.upgrade_cost = 5 self.description = "A basic tower with moderate fire speed and damage." self.cost = 25 self.value = self.cost self.target = None self.last_shot = time.time() self.image.convert() self.projectile.convert() self.frame = TowerFrame(self) def update(self, monsters, screen, screen_rect): # If there is no target if self.target is None: for monster in monsters: if monster.can_move: if sprite.collide_circle(monster, self): self.target = monster break # Shoot at the target if self.target is not None: # If the target has gone out of range if not sprite.collide_circle(self.target, self): self.target = None else: self.shoot() if self.target.health <= 0: self.target = None self.projectiles.update(monsters, screen_rect) self.projectiles.draw(screen) def update_info(self): self.frame = TowerFrame(self) def shoot(self): if time.time() - self.last_shot >= self.fire_rate: self.projectiles.add(Projectile(pos=(self.rect.x, self.rect.y), tower=self, target=self.target, image=self.projectile, speed=self.projectile_speed, damage=self.damage)) self.last_shot = time.time() def upgrade(self): if self.level < 5: self.damage += 5 self.projectile_speed -= 0.5 self.level += 1 self.update_info() return True return False