1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Python Arcade Library(Pyglet)でドット絵をきれいに拡大する

Last updated at Posted at 2021-06-22

※ぴぽや様(https://pipoya.net/) のハロウィンキャラクターの素材を利用しています。

問題

Python Arcade Library(Pyglet)では、スプライトを描画する時に使われるフィルターがデフォルトではLINEARになっているため、ドット絵を拡大して使いたい場合にぼやけてしまいます。

import arcade


class MyGame(arcade.Window):
    def __init__(self):
        super().__init__(800, 600, "LINEAR")
        arcade.set_background_color(arcade.color.AMAZON)

    def setup(self):
        self.player_list = arcade.SpriteList()

        for j in range(3):
            for i in range(3):
                player_texture = arcade.load_texture(
                    file_name="majo.png", x=32*i, y=0, width=32, height=32)

                player_sprite = arcade.Sprite(
                    center_x=150+250*i, center_y=50+200*j, scale=3**j)
                player_sprite.texture = player_texture
                self.player_list.append(player_sprite)

    def on_draw(self):
        arcade.start_render()
        self.player_list.draw()

    def on_update(self, delta_time):
        pass


window = MyGame()
window.setup()
arcade.run()

linear.png

対処法

フィルターをインポートしてdrawの引数にNEARESTのフィルターを指定してあげるときれいに表示されます。

import arcade
from pyglet.gl import GL_NEAREST


class MyGame(arcade.Window):
    def __init__(self):
        super().__init__(800, 600, "NEAREST")
        arcade.set_background_color(arcade.color.AMAZON)

    def setup(self):
        self.player_list = arcade.SpriteList()

        for j in range(3):
            for i in range(3):
                player_texture = arcade.load_texture(
                    file_name="majo.png", x=32*i, y=0, width=32, height=32)

                player_sprite = arcade.Sprite(
                    center_x=150+250*i, center_y=50+200*j, scale=3**j)
                player_sprite.texture = player_texture
                self.player_list.append(player_sprite)

    def on_draw(self):
        arcade.start_render()
        self.player_list.draw(filter=GL_NEAREST)

    def on_update(self, delta_time):
        pass


window = MyGame()
window.setup()
arcade.run()

nearest.png

参考

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?