def test_draw_texture_theme(mocker): mocker.patch('arcade.draw_texture_rectangle') theme = arcade.gui.Theme() normal_texture = arcade.Texture('normal') clicked_texture = arcade.Texture('clicked') theme.button_textures['normal'] = normal_texture theme.button_textures['clicked'] = clicked_texture button = TextButton(center_x=50, center_y=20, width=30, height=10, text='', theme=theme) button.pressed = False button.draw_texture_theme() arcade.draw_texture_rectangle.assert_called_once_with( 50, 20, 30, 10, normal_texture) arcade.draw_texture_rectangle.reset_mock() button.pressed = True button.draw_texture_theme() arcade.draw_texture_rectangle.assert_called_once_with( 50, 20, 30, 10, clicked_texture)
def __init__(self, view: Menu): img = view.open_image("./assets/menu/arrow.png", 50, rotate=90) img_02 = view.open_image("./assets/menu/arrow_pressed.png", 50, rotate=90) super().__init__(normal_texture=ac.Texture("normal", img), press_texture=ac.Texture("pressed", img_02), center_x=view.window.width // 2, center_y=view.window.height // 3)
def render(self): self.normal_texture = arcade.Texture( image=PIL.Image.new("RGBA", (self._width, self._height), color=(255, 0, 0)), name=str(uuid4())) self.hover_texture = arcade.Texture( image=PIL.Image.new("RGBA", (self._width, self._height), color=(255, 0, 0)), name=str(uuid4())) self.press_texture = arcade.Texture( image=PIL.Image.new("RGBA", (self._width, self._height), color=(255, 0, 0)), name=str(uuid4())) self.focus_texture = arcade.Texture( image=PIL.Image.new("RGBA", (self._width, self._height), color=(255, 0, 0)), name=str(uuid4())) self.set_proper_texture()
def _SetupText(self): """Sets up text sprite if specified.""" # End here if no text wanted if self.textString is None: self.text = None return # Made image out of test string image = get_text_image(text=self.textString,font_size=self.fontSize,width=self.widthTextMax) # Make sprite from image text = arcade.Sprite() text._texture = arcade.Texture(self.textString) text.texture.image = image text.width = image.width text.height = image.height # Set position of word text.center_x = self.center_x text.center_y = self.center_y # Save text sprite self.text = text return
def create_textures(): #list of images for sprites texture_list = [] for color in colors: image = PIL.Image.new('RGB', (WIDTH, HEIGHT), color) texture_list.append(arcade.Texture(str(color), image=image)) return texture_list
def update(self, delta_time): # Number of lines to draw per update cycle lines = 20 if self.y >= SCREEN_HEIGHT: # self.on_close() return for _ in range(lines): cy = self.y * (self.ymax - self.ymin) / (SCREEN_HEIGHT - 1) + self.ymin for x in range(SCREEN_WIDTH): cx = x * (self.xmax - self.xmin) / (SCREEN_WIDTH - 1) + self.xmin c = complex(cx, cy) z = 0 + 0j i = mandle_core(c, self.maxIter) if i >= self.maxIter - 1: color = 0 else: color = (i << 21) + (i << 10) + i * 8 self.bitmap[x, self.y] = color self.y += 1 # Convert the image to a texture self.texture = arcade.Texture('background', self.off_screen_image) # Set the texture of the background sprite self.background_sprite.texture = self.texture
def get_texture(self, size, color): font = ImageFont.truetype("FreeMonoBold", 20) img = Image.new("RGBA", (self.button_width, self.button_height), color) draw = ImageDraw.Draw(img) draw.text((10, 10), self.text, font=font, align="left", fill=BLACK) name = '{}:{}:{}'.format('btn', color, self.text) return arcade.Texture(name, img)
def draw_field(self): if self.background is not None: arcade.draw_lrwh_rectangle_textured(0, 0, self.box.box_sizes[0], self.box.box_sizes[1], self.background) else: arcade.cleanup_texture_cache() if self.box.field is None: return gx = numpy.arange(0, self.box.box_sizes[0], 50) gy = numpy.arange(0, self.box.box_sizes[1], 50) for x in gx: for y in gy: position = self.box.center.copy() position[0] = x position[1] = y value = 10 * self.box.field.equation(position=position) try: arcade.draw_line(x, y, x + value[0], y + value[1], [255, 255, 255], 1) arcade.draw_circle_filled(x + value[0], y + value[1], 2, [255, 255, 255]) except Exception: pass self.background = arcade.Texture("background", arcade.get_image(0, 0))
def convert_text_to_texture(text_value): img = Image.new('RGBA', (75, 75), color=(255, 255, 255, 0)) d = ImageDraw.Draw(img) fnt = ImageFont.truetype('absender1.ttf', 100) d.text((15, 5), text_value, font=fnt, fill=(255, 0, 0)) texture = arcade.Texture(str(text_value), img) return texture
def __init__(self, center_x=0, center_y=0, width=40, height=40, id: Optional[str] = None, style: UIStyle = None, **kwargs): super().__init__(center_x=center_x, center_y=center_y, id=id, style=style, **kwargs) self.normal_texture = arcade.Texture(image=PIL.Image.new("RGBA", (width, height)), name=str(uuid4())) self.hover_texture = arcade.Texture(image=PIL.Image.new("RGBA", (width, height)), name=str(uuid4())) self.press_texture = arcade.Texture(image=PIL.Image.new("RGBA", (width, height)), name=str(uuid4())) self.focus_texture = arcade.Texture(image=PIL.Image.new("RGBA", (width, height)), name=str(uuid4())) self.event_history: List[arcade_gui.UIEvent] = []
def __init__(self, label: str, icon: str, goto: DigiView, width: int = 200, border_color: Color = arcade.color.WHITE, border_width: int = 0, *args, **kwargs): try: self.icon = img_from_resource(charm.data.icons, f"{icon}.png") self.icon = self.icon.resize((width, width), PIL.Image.LANCZOS) except Exception: self.icon = generate_missing_texture_image(width, width) self.icon = PIL.ImageOps.expand(self.icon, border=border_width, fill=border_color) tex = arcade.Texture(f"_icon_{icon}", image=self.icon, hit_box_algorithm=None) super().__init__(texture=tex, *args, **kwargs) self.goto = goto self.label = arcade.Text(label, 0, 0, CharmColors.PURPLE, anchor_x='center', anchor_y="top", font_name="bananaslip plus plus", font_size=24) self.center_y = Settings.height // 2 self.jiggle_start = 0
def get_sprite(self) -> Sprite: _tex = arcade.Texture.create_empty( f"_error-{self.title}-{self.show_message}", (500, 200)) _icon_tex = arcade.Texture(f"_error_icon_{self._icon}", self.icon) sprite = Sprite(texture=_tex) _sprite_list = arcade.SpriteList() _sprite_list.append(sprite) with _sprite_list.atlas.render_into(_tex) as fbo: fbo.clear() arcade.draw_lrtb_rectangle_filled(0, 500, 200, 0, arcade.color.BLANCHED_ALMOND) arcade.draw_lrtb_rectangle_filled(0, 500, 200, 150, arcade.color.BRANDEIS_BLUE) arcade.draw_text(self.title, 50, 165, font_size=24, bold=True) arcade.draw_text(self.show_message, 5, 146, font_size=16, anchor_y="top", multiline=True, width=492, color=arcade.color.BLACK) arcade.draw_texture_rectangle(25, 175, 32, 32, _icon_tex) return sprite
def setup(self): self.sprite_list = arcade.SpriteList() self.chunk_list = arcade.SpriteList() self.junk_list = arcade.SpriteList() self.rabbit_list = arcade.SpriteList() self.track = list() self.tick = 0 for i in range(INIT_CHUNK_COUNT): self.create_chunk() for i in range(RABBIT_COUNT): rabbit = Rabbit() self.rabbit_list.append(rabbit) self.sprite_list.append(rabbit) color = arcade.color.RED image = PIL.Image.new('RGB', (TONG_WIDTH, TONG_HEIGHT), color) texture = arcade.Texture(str(color), image=image) self.tong = arcade.Sprite() self.tong.append_texture(texture) self.tong.set_texture(0) self.sprite_list.append(self.tong) self.update_tong()
def create_textures(): """ Create a list of images for sprites based on the global colors. """ texture_list = [] for color in colors: image = PIL.Image.new('RGB', (WIDTH, HEIGHT), color) texture_list.append(arcade.Texture(str(color), image=image)) return texture_list
def render(self): font_name = self.style_attr('font_name', ['Calibri', 'Arial']) font_size = self.style_attr('font_size', 12) font_color = self.style_attr('font_color', arcade.color.GRAY) font_color_hover = self.style_attr('font_color_hover', None) font_color_press = self.style_attr('font_color_press', None) if font_color_hover is None: font_color_hover = font_color if font_color_press is None: font_color_press = font_color_hover text_image_normal = get_text_image( text=self.text, font_color=font_color, font_size=font_size, font_name=font_name, align=self.align, width=int(self._target_width), ) text_image_mouse_over = get_text_image( text=self.text, font_color=font_color_hover, font_size=font_size, font_name=font_name, align=self.align, width=int(self._target_width), ) text_image_mouse_press = get_text_image( text=self.text, font_color=font_color_press, font_size=font_size, font_name=font_name, align=self.align, width=int(self._target_width), ) self.normal_texture = arcade.Texture(image=text_image_normal, name=str(uuid4()), hit_box_algorithm="None") self.press_texture = arcade.Texture(image=text_image_mouse_press, name=str(uuid4()), hit_box_algorithm="None") self.hover_texture = arcade.Texture(image=text_image_mouse_over, name=str(uuid4()), hit_box_algorithm="None")
def create_textures(): new_textures = [] for color in colors: image = PIL.Image.new('RGB', (WIDTH, HEIGHT), color) new_textures.append(arcade.Texture(str(color), image=image)) return new_textures
def __init__(self, center_x, center_y, width, height, theme: Theme): super().__init__(center_x, center_y) rect = [ 0, 0, width - theme.border_width / 2, height - theme.border_width / 2 ] color = theme.background_color image = PIL.Image.new('RGBA', (width, height), color) if theme.border_color and theme.border_width: d = ImageDraw.Draw(image) d.rectangle(rect, fill=None, outline=theme.border_color, width=theme.border_width) self.texture = arcade.Texture( f"Solid-{width},{height}-{color[0]}-{color[1]}-{color[2]}-normal", image) self._points = self.texture.hit_box_points self.normal_texture = self.texture color = theme.background_color_mouse_over image = PIL.Image.new('RGBA', (width, height), color) if theme.border_color_mouse_over: d = ImageDraw.Draw(image) d.rectangle(rect, fill=None, outline=theme.border_color_mouse_over, width=theme.border_width) self.mouse_over_texture = arcade.Texture( f"Solid-{width},{height}-{color[0]}-{color[1]}-{color[2]}-mouseover", image) color = theme.background_color_mouse_press image = PIL.Image.new('RGBA', (width, height), color) if theme.border_color_mouse_press: d = ImageDraw.Draw(image) d.rectangle(rect, fill=None, outline=theme.border_color_mouse_press, width=theme.border_width) self.mouse_press_texture = arcade.Texture( f"Solid-{width},{height}-{color[0]}-{color[1]}-{color[2]}-mouse-click", image)
def create_sprites(self): self.sprites_list = arcade.SpriteList() self.ball_list = arcade.SpriteList() self.plat_list = arcade.SpriteList() self.brick_list = arcade.SpriteList() color = arcade.color.GREEN # Ball imageBall = PIL.Image.new('RGB', (BALL_SIZE, BALL_SIZE), arcade.color.WHITE) textureBall = arcade.Texture(str(color), image=imageBall) self.ball_sprite = arcade.Sprite() self.ball_sprite.append_texture(textureBall) self.ball_sprite.set_texture(0) self.ball_sprite.center_x = SCREEN_WIDTH // 2 self.ball_sprite.center_y = SCREEN_HEIGHT // 2 self.sprites_list.append(self.ball_sprite) self.ball_list.append(self.ball_sprite) self.ball_sprite.change_x = int( random.randrange(-BALL_SPEED, BALL_SPEED)) self.ball_sprite.change_y = int( math.sqrt(BALL_SPEED**2 - self.ball_sprite.change_x**2)) # Plat imagePlat = PIL.Image.new('RGB', (PLAT_WIDTH, PLAT_HEIGHT), arcade.color.YELLOW) texturePlat = arcade.Texture(str(arcade.color.YELLOW), image=imagePlat) self.plat_sprite = arcade.Sprite() self.plat_sprite.append_texture(texturePlat) self.plat_sprite.set_texture(0) self.plat_sprite.center_x = SCREEN_WIDTH // 2 self.plat_sprite.center_y = PLAT_HEIGHT self.sprites_list.append(self.plat_sprite) self.plat_list.append(self.plat_sprite) # Brick for y in range(5): for x in range(SCREEN_WIDTH // (BRICK_WIDTH + 1) + 1): self.create_brick(x * (BRICK_WIDTH + 1), SCREEN_HEIGHT - 100 - y * (BRICK_HEIGHT + 1))
def create_textures(): """ Create a list of images for sprites based on the global colors. !!! SHOULD be able to add custom images in here instead of the general colors.""" texture_list = [] for color in colors: image = PIL.Image.new('RGB', (WIDTH, HEIGHT), color) texture_list.append(arcade.Texture(str(color), image=image)) return texture_list
def create_textures(): """ Create a list of images for sprites based on the global colors. """ new_textures = [] for color in colors: # noinspection PyUnresolvedReferences image = PIL.Image.new('RGB', (WIDTH, HEIGHT), color) new_textures.append(arcade.Texture(str(color), image=image)) return new_textures
def __init__(self, img_path: str, name: str, clr: arcade.arcade_types.Color, game: "Game"): super().__init__() self.game = game # self.change_x: float # should be coming from arcade package # self.change_y: float # should be coming from arcade package self.btn_left = False self.btn_right = False self.state: int = Player.FLYING self.dir: int = Player.NO_DIRECTION self.skid_fx = self.make_dust_emitter() # re-initialized in .setup() self.setup() self.score = 0 self.name = name self.is_alive = True print(f'Loading texture: {img_path}') if img_path.endswith('player_blob.png') and clr is not None: # replace colors img = PIL.Image.open(img_path) width, height = img.size darker_clr = (int(clr[0] * 0.7), int(clr[1] * 0.7), int(clr[2] * 0.7)) for i, px in enumerate(img.getdata()): if px == (65, 216, 0, 255): img.putpixel((i % width, i // width), clr + (255, )) elif px == (53, 178, 0, 255): img.putpixel((i % width, i // width), darker_clr + (255, )) # facing-right texture cache_id = f'{img_path}-{clr}-right' right_texture = arcade.Texture(cache_id, img) # facing-left texture img = img.transpose(PIL.Image.FLIP_LEFT_RIGHT) cache_id = f'{img}-{clr}-left' left_texture = arcade.Texture(cache_id, img) else: # no color replacing right_texture = arcade.load_texture(img) left_texture = arcade.load_texture(img, mirrored=True) self.textures.append(right_texture) self.textures.append(left_texture) self.set_texture(Player.RIGHT)
def __init__(self, first=True): super().__init__() self.speed = 1 if first else 0 color = arcade.color.GREEN if first else arcade.color.GO_GREEN image = PIL.Image.new('RGB', (CHUNK_SIZE, CHUNK_SIZE), color) texture = arcade.Texture(str(color), image=image) self.append_texture(texture) self.set_texture(0)
def create_textures() -> t.List[arcade.Texture]: """Create textures based on constants dictionary.""" textures = [] for te in TEXTURES_AND_COLORS.values(): if isinstance(te, str): textures.append(arcade.load_texture(te)) elif isinstance(te, tuple): img = PIL.Image.new("RGB", (100, 100), te) textures.append(arcade.Texture(str(te), image=img)) return textures
def __init__(self, *args, **kwds): kwds["body_type"] = pymunk.Body.STATIC super().__init__(*(), **kwds) self.width, self.height, self.color = args img = Image.new("RGBA", (self.width, self.height), self.color) self.texture = arcade.Texture( f"RGBA-{self.__class__.__name__}-{id(self)}", img)
def setup(self): # Create your sprites and sprite lists here self.off_screen_image = PIL.Image.new("RGB", (SCREEN_WIDTH, SCREEN_HEIGHT), (128,128,128)) self.bitmap = self.off_screen_image.load() # Load the image into a writable byte object self.texture = arcade.Texture('background', self.off_screen_image) self.background_sprite = arcade.Sprite() self.background_sprite.center_x = SCREEN_WIDTH // 2 self.background_sprite.center_y = SCREEN_HEIGHT // 2 self.background_sprite.texture = self.texture self.y = 1
def create_tooltip(self) -> arcade.Texture: """Create an arcade texture for the tooltip.""" padding = 5 font = ImageFont.truetype(FONT.format(type='ri'), 20) text_width, text_height = font.getsize(self.tooltip_text) width = text_width + padding * 2 height = text_height + padding * 2 im = Image.new('RGB', (width, height), color=(255, 255, 255)) draw = ImageDraw.Draw(im) draw.text((padding, padding), self.tooltip_text, (0, 0, 0), font) return arcade.Texture(str(self.tooltip_text), im)
def test_empty_image(): texture = arcade.Texture(name=str(uuid4()), image=PIL.Image.new("RGBA", (50, 50))) result = arcade.calculate_hit_box_points_simple(texture.image) print(result) assert result == [] result = arcade.calculate_hit_box_points_detailed(texture.image) print(result) assert result == []
def create_texture(self, surface, imgName, imgsize): # image = Image.new('RGBA', imgsize, (255, 0, 0, 0)) #Create the image #image = Image.frombuffer("RGBA",( surface.get_width(), surface.get_height() ), surface.get_data(), "raw", "RGBA", 0, 1) data = surface.get_data() # convert bgra to rgba a = np.frombuffer(data, dtype=np.uint8) a.shape = (-1, 4) data = a[:, [2, 1, 0, 3]].tobytes() # image = Image.frombuffer("RGBA", imgsize, data, "raw", "RGBA", 0, 1) return arcade.Texture(imgName, image)
def __init__(self): super().__init__() color = arcade.color.WHITE image = PIL.Image.new('RGB', (CHUNK_SIZE, CHUNK_SIZE), color) texture = arcade.Texture(str(color), image=image) self.append_texture(texture) self.set_texture(0) self.angle = random.randrange(0, 360) self.center_x = random.randrange(0, SCREEN_WIDTH) self.center_y = random.randrange(0, SCREEN_HEIGHT)
def update(self, delta_time): max_items = 2000 # Maximum items to dequeue at once # Read items out of the result queue for i in range(max_items): try: x, y, color = self.result_queue.get(False) # print(x,y,color) self.bitmap[x, y] = color except queue.Empty: #print("Queue was empty") break self.texture = arcade.Texture('background', self.off_screen_image) self.background_sprite.texture = self.texture