Advertisement
Aikiro42

Image Tiling

Dec 3rd, 2019
969
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pyglet
  2. from pyglet.gl import *
  3. import sys
  4.  
  5. try:
  6.  
  7.     # Get tiling depth argument from command line
  8.     # Throws IndexError if only argument is the script directory when script was called from command line.
  9.     # i.e. the tiling depth was not specified
  10.     n = int(sys.argv[1])
  11.  
  12.     # Validate argument
  13.     # throws AssertionError if condition is not met
  14.     # i.e. tiling depth is less than one
  15.     assert n >= 1
  16.  
  17.     # Load image.jpg
  18.     # Throws pyglet.resource.ResourceNotFoundException when image does not exist
  19.     img = pyglet.resource.image('image.jpg')
  20.  
  21.    
  22.     img_w = img.width  # Store image width to variable - much more efficient performance-wise
  23.     img_h = img.height  # Store image height to variable
  24.     window = pyglet.window.Window()  # Initialize window
  25.  
  26.     # Declare graphics batch
  27.     # This will draw all sprites at once in an efficient manner.
  28.     batch = pyglet.graphics.Batch()
  29.  
  30.     # Declare sprite list.
  31.     # Each element is a pointer to a unique sprite object.
  32.     sprite_list = [pyglet.sprite.Sprite(img, batch=batch) for i in range(n * 3)]
  33.  
  34.     # Initialize loop variables
  35.     x = 0  # x-coordinate of sprite located in 3rd quadrant; width added every iteration
  36.     y = 0  # y-coordinate of sprite located in 3rd quadrant; height added every iteration
  37.     w = window.width // 2  # width of dimensions to draw in; halved every iteration (x-dimension of 1st quadrant)
  38.     h = window.height // 2  # height of dimensions to draw in; halved every iteration (y-dimension of 1st quadrant)
  39.     j = 0  # sprite_list index navigator
  40.     for j in range(0, n * 3, 3):  # For every third element starting from the first:
  41.         # Set second quadrant sprite attributes
  42.         sprite_list[j].update(scale_x=w / img_w, scale_y=h / img_h, x=x, y=y + h)
  43.         # Set third quadrant sprite attributes
  44.         sprite_list[j + 1].update(scale_x=w / img_w, scale_y=h / img_h, x=x, y=y)
  45.         # Set fourth quadrant sprite attributes
  46.         sprite_list[j + 2].update(scale_x=w / img_w, scale_y=h / img_h, x=x + w, y=y)
  47.         x += w  # add current width to x-coordinate
  48.         y += h  # add current width to y-coordinate
  49.         w //= 2  # half width
  50.         h //= 2  # half height
  51.  
  52.     # Function called every time the window needs to redraw its elements
  53.     # i.e. Every time an event happens
  54.     @window.event
  55.     def on_draw():
  56.         window.clear()  # Clear window
  57.         batch.draw()  # Draw batch
  58.  
  59.     pyglet.app.run()  # Starts event loop.
  60. except IndexError:  # No tiling depth argument supplied when running this script from terminal
  61.     print('Please add the tiling depth to the command line arguments.')
  62. except TypeError:  # Tiling depth argument is an integer
  63.     print('Error: Tiling depth must be an integer')
  64. except AssertionError:  # Tiling depth less than 1
  65.     print('Error: Tiling depth must be greater than or equal to one.')
  66. except pyglet.resource.ResourceNotFoundException:  # image.jpg does not exist
  67.     print('Error: Image not found. Please place a JPG image named "image" next to the python script.')
Advertisement
RAW Paste Data Copied
Advertisement