Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pyglet
- from pyglet.gl import *
- import sys
- try:
- # Get tiling depth argument from command line
- # Throws IndexError if only argument is the script directory when script was called from command line.
- # i.e. the tiling depth was not specified
- n = int(sys.argv[1])
- # Validate argument
- # throws AssertionError if condition is not met
- # i.e. tiling depth is less than one
- assert n >= 1
- # Load image.jpg
- # Throws pyglet.resource.ResourceNotFoundException when image does not exist
- img = pyglet.resource.image('image.jpg')
- img_w = img.width # Store image width to variable - much more efficient performance-wise
- img_h = img.height # Store image height to variable
- window = pyglet.window.Window() # Initialize window
- # Declare graphics batch
- # This will draw all sprites at once in an efficient manner.
- batch = pyglet.graphics.Batch()
- # Declare sprite list.
- # Each element is a pointer to a unique sprite object.
- sprite_list = [pyglet.sprite.Sprite(img, batch=batch) for i in range(n * 3)]
- # Initialize loop variables
- x = 0 # x-coordinate of sprite located in 3rd quadrant; width added every iteration
- y = 0 # y-coordinate of sprite located in 3rd quadrant; height added every iteration
- w = window.width // 2 # width of dimensions to draw in; halved every iteration (x-dimension of 1st quadrant)
- h = window.height // 2 # height of dimensions to draw in; halved every iteration (y-dimension of 1st quadrant)
- j = 0 # sprite_list index navigator
- for j in range(0, n * 3, 3): # For every third element starting from the first:
- # Set second quadrant sprite attributes
- sprite_list[j].update(scale_x=w / img_w, scale_y=h / img_h, x=x, y=y + h)
- # Set third quadrant sprite attributes
- sprite_list[j + 1].update(scale_x=w / img_w, scale_y=h / img_h, x=x, y=y)
- # Set fourth quadrant sprite attributes
- sprite_list[j + 2].update(scale_x=w / img_w, scale_y=h / img_h, x=x + w, y=y)
- x += w # add current width to x-coordinate
- y += h # add current width to y-coordinate
- w //= 2 # half width
- h //= 2 # half height
- # Function called every time the window needs to redraw its elements
- # i.e. Every time an event happens
- @window.event
- def on_draw():
- window.clear() # Clear window
- batch.draw() # Draw batch
- pyglet.app.run() # Starts event loop.
- except IndexError: # No tiling depth argument supplied when running this script from terminal
- print('Please add the tiling depth to the command line arguments.')
- except TypeError: # Tiling depth argument is an integer
- print('Error: Tiling depth must be an integer')
- except AssertionError: # Tiling depth less than 1
- print('Error: Tiling depth must be greater than or equal to one.')
- except pyglet.resource.ResourceNotFoundException: # image.jpg does not exist
- print('Error: Image not found. Please place a JPG image named "image" next to the python script.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement