Transform Textures

Screenshot of using transforming textures
texture_transform.py
  1"""
  2Sprites with texture transformations
  3
  4Artwork from http://kenney.nl
  5
  6If Python and Arcade are installed, this example can be run from the command line with:
  7python -m arcade.examples.sprite_texture_transform
  8"""
  9
 10import arcade
 11from arcade import Matrix3x3
 12import math
 13import os
 14
 15SCREEN_WIDTH = 800
 16SCREEN_HEIGHT = 600
 17SHIP_SPEED = 5
 18ASPECT = SCREEN_HEIGHT / SCREEN_WIDTH
 19SCREEN_TITLE = "Texture transformations"
 20
 21
 22class MyGame(arcade.Window):
 23    """ Main application class. """
 24
 25    def __init__(self, width, height, title):
 26        """
 27        Initializer
 28        """
 29        super().__init__(width, height, title)
 30
 31        # Set the working directory (where we expect to find files) to the same
 32        # directory this .py file is in. You can leave this out of your own
 33        # code, but it is needed to easily run the examples using "python -m"
 34        # as mentioned at the top of this program.
 35        file_path = os.path.dirname(os.path.abspath(__file__))
 36        os.chdir(file_path)
 37
 38        self.ship = None
 39        self.camera_x = 0
 40        self.t = 0
 41        self.stars = None
 42        self.xy_square = None
 43
 44    def setup(self):
 45        """ Setup """
 46        self.ship = arcade.Sprite(":resources:images/space_shooter/playerShip1_orange.png", 0.5)
 47        self.ship.center_x = SCREEN_WIDTH / 2
 48        self.ship.center_y = SCREEN_HEIGHT / 2
 49        self.ship.angle = 270
 50        self.stars = arcade.load_texture(":resources:images/backgrounds/stars.png")
 51        self.xy_square = arcade.load_texture(":resources:images/test_textures/xy_square.png")
 52
 53        # Set the background color
 54        arcade.set_background_color(arcade.color.BLACK)
 55
 56    def on_update(self, delta_time: float):
 57        """ Update """
 58        self.ship.update()
 59        self.camera_x += 2
 60        self.t += delta_time * 60
 61
 62    def on_draw(self):
 63        """
 64        Render the screen.
 65        """
 66
 67        # This command has to happen before we start drawing
 68        arcade.start_render()
 69
 70        for z in [300, 200, 150, 100]:
 71            opacity = int(math.exp(-z / 1000) * 255)
 72            angle = z
 73            scale = 150 / z
 74            translate = scale / 500
 75            self.stars.draw_transformed(
 76                0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, opacity,
 77                Matrix3x3().rotate(angle).scale(scale * ASPECT, scale).translate(-self.camera_x * translate, 0))
 78        self.ship.draw()
 79
 80        for i, pair in enumerate([
 81            ['identity', Matrix3x3()],
 82            ['rotate(30)', Matrix3x3().rotate(30)],
 83            ['scale(0.8, 0.5)', Matrix3x3().scale(0.8, 0.5)],
 84            ['translate(0.3, 0.1)', Matrix3x3().translate(0.3, 0.1)],
 85            ['rotate(10).\nscale(0.33, 0.33)', Matrix3x3().rotate(10).scale(0.7, 0.7)],
 86            ['scale(-1, 1)', Matrix3x3().scale(-1, 1)],
 87            ['shear(0.3, 0.1)', Matrix3x3().shear(0.3, 0.1)],
 88            [f'rotate({int(self.t) % 360})', Matrix3x3().rotate(self.t)],
 89        ]):
 90            x = 80 + 180 * (i % 4)
 91            y = 420 - (i // 4) * 320
 92            arcade.draw_text(pair[0], x, y - 20 - pair[0].count('\n') * 10, arcade.color.WHITE, 10)
 93            self.xy_square.draw_transformed(x, y, 100, 100, 0, 255, pair[1])
 94
 95
 96def main():
 97    """ Main method """
 98    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
 99    window.setup()
100    arcade.run()
101
102
103if __name__ == "__main__":
104    main()