Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, ctx, filename):
self.ctx = ctx
self.event = sdl.Event()
self.running = False
self.pressed = collections.defaultdict(bool)
# load data from pytmx
tmx_data = load_pysdl2_cffi(ctx, filename)
# create new data source
map_data = pyscroll.data.TiledMapData(tmx_data)
# create new renderer
screen_size = ctx.window.getWindowSize()
self.map_layer = pyscroll.TextureRenderer(ctx, map_data, screen_size)
self.center = [(i // self.map_layer.zoom) // 2 for i in map_data.pixel_size]
# create a font and pre-render some text to be displayed over the map
# f = pygame.font.Font(pygame.font.get_default_font(), 20)
# t = ["scroll demo. press escape to quit",
# "arrow keys move"]
# save the rendered text
# self.text_overlay = [f.render(i, 1, (180, 180, 0)) for i in t]
# set our initial viewpoint in the center of the map
def __init__(self, ctx, filename):
self.ctx = ctx
self.event = sdl.Event()
self.running = False
self.pressed = collections.defaultdict(bool)
# load data from pytmx
tmx_data = load_pysdl2_cffi(ctx, filename)
# create new data source
map_data = pyscroll.data.TiledMapData(tmx_data)
# create new renderer
screen_size = ctx.window.getWindowSize()
self.map_layer = pyscroll.TextureRenderer(ctx, map_data, screen_size)
self.center = [(i // self.map_layer.zoom) // 2 for i in map_data.pixel_size]
# create a font and pre-render some text to be displayed over the map
# f = pygame.font.Font(pygame.font.get_default_font(), 20)
# t = ["scroll demo. press escape to quit",
# "arrow keys move"]
# save the rendered text
# self.text_overlay = [f.render(i, 1, (180, 180, 0)) for i in t]
# set our initial viewpoint in the center of the map
# the camera vector is used to handle camera movement
self.camera_acc = [0, 0, 0]
self.camera_vel = [0, 0, 0]
self.last_update_time = 0
self.view_rect.size = view_size
self._previous_blit = Rect(self.view_rect)
self._tile_view = Rect(0, 0, buffer_tile_width, buffer_tile_height)
self._redraw_cutoff = 1 # TODO: optimize this value
self._create_buffers(view_size, buffer_pixel_size)
self._half_width = view_size[0] // 2
self._half_height = view_size[1] // 2
self._x_offset = 0
self._y_offset = 0
rects = [make_rect(*i) for i in product(range(buffer_tile_width),
range(buffer_tile_height))]
# TODO: figure out what depth -actually- does
# values <= 8 tend to reduce performance
self._layer_quadtree = quadtree.FastQuadTree(rects, 4)
self.redraw_tiles(self._buffer)
def __init__(self, filename):
self.init_buffer([screen.get_width() / 2, screen.get_height() / 2])
tmx_data = pytmx.load_pygame("desert.tmx")
map_data = pyscroll.TiledMapData(tmx_data)
self.map_layer = pyscroll.BufferedRenderer(map_data, self.buffer_size)
f = pygame.font.Font(pygame.font.get_default_font(), 20)
t = ["scroll demo. press escape to quit",
"arrow keys move"]
self.text_overlay = [f.render(i, 1, (180, 180, 0)) for i in t]
self.center = [self.map_layer.width/2,self.map_layer.height/2]
self.camera_vector = [0, 0, 0]
self.running = False
def setUp(self):
self.mock = DummyBufferer()
self.queue = BufferedRenderer._queue_edge_tiles
def __init__(self, filename):
self.init_buffer([screen.get_width() / 2, screen.get_height() / 2])
tmx_data = pytmx.load_pygame("desert.tmx")
map_data = pyscroll.TiledMapData(tmx_data)
self.map_layer = pyscroll.BufferedRenderer(map_data, self.buffer_size)
f = pygame.font.Font(pygame.font.get_default_font(), 20)
t = ["scroll demo. press escape to quit",
"arrow keys move"]
self.text_overlay = [f.render(i, 1, (180, 180, 0)) for i in t]
self.center = [self.map_layer.width/2,self.map_layer.height/2]
self.camera_vector = [0, 0, 0]
self.running = False
import mock
import unittest
import pygame
from pyscroll.orthographic import BufferedRenderer
from pyscroll.data import PyscrollDataAdapter
class DummyDataAdapter(PyscrollDataAdapter):
tile_size = 32, 32
map_size = 32, 32
visible_tile_layers = [1]
def get_animations(self):
return list()
def get_tile_image(self, position):
return position[0] * position[1]
class DummyBufferer:
_tile_view = pygame.Rect(2, 2, 2, 2)
_clear_color = None
_buffer = mock.Mock()
data = DummyDataAdapter()
logger = logging.getLogger('isometric')
def vector3_to_iso(vector3):
offset = 0, 0
return ((vector3[0] - vector3[1]) + offset[0],
((vector3[0] + vector3[1]) >> 1) - vector3[2] + offset[1])
def vector2_to_iso(vector2):
offset = 0, 0
return ((vector2[0] - vector2[1]) + offset[0],
((vector2[0] + vector2[1]) >> 1) + offset[1])
class IsometricBufferedRenderer(BufferedRenderer):
""" TEST ISOMETRIC
here be dragons. lots of odd, untested, and unoptimised stuff.
- coalescing of surfaces is not supported
- drawing may have depth sorting issues
"""
def _draw_surfaces(self, surface, rect, surfaces):
if surfaces is not None:
[(surface.blit(i[0], i[1]), i[2]) for i in surfaces]
def _initialize_buffers(self, view_size):
""" Create the buffers to cache tile drawing
:param view_size: (int, int): size of the draw area
:param surface: pygame surface to draw to
:param rect: area to draw to
:param surfaces: optional sequence of surfaces to interlace between tiles
"""
self._tile_queue = self.data.process_animation_queue(self._tile_view)
self._tile_queue and self._flush_tile_queue(self._buffer)
# TODO: could maybe optimize to remove just the edges, ideally by drawing lines
# if not self.anchored_view:
# surface.fill(self._clear_color, self._previous_blit)
if not self._anchored_view:
self._clear_surface(surface, self._previous_blit)
offset = -self._x_offset + rect.left, -self._y_offset + rect.top
with surface_clipping_context(surface, rect):
self._previous_blit = surface.blit(self._buffer, offset)
if surfaces:
surfaces_offset = -offset[0], -offset[1]
self._draw_surfaces(surface, surfaces_offset, surfaces)
tile = get_tile((x, y, l))
if tile:
blit(tile, (x * tw - ltw, y * th - tth))
def redraw(self):
""" redraw the visible portion of the buffer -- it is slow.
"""
queue = product(range(self.view.left, self.view.right),
range(self.view.top, self.view.bottom),
self.data.visible_tile_layers)
self.update_queue(queue)
self.flush()
class ThreadedRenderer(BufferedRenderer):
""" Off-screen tiling is handled in a thread
"""
def __init__(self, *args, **kwargs):
BufferedRenderer.__init__(self, *args, **kwargs)
self.flush_on_draw = False
self.queue = queue.Queue()
self.thread = TileThread(renderer=self)
self.thread.start()
def update(self, dt=None):
pass
def flush(self):
self.queue.join()