3
\$\begingroup\$

I'm making a 2d tile based game. In pygame I could create a map like this:

screen = pygame.display(resolution)
map = pygame.Surface(width, height)
for tile in all_tiles:
    map.blit(tile)
screen.blit(map)

The strength of this is that I can scroll the map by changing the map surface x and y. Also all the in-game coordinates are conveniently set in relation to the map rather than the screen. However I can no longer use pygame, and I'm trying to switch to pyglet instead. As far as I can tell pyglet doesn't have an equivalent surface class built in, and therefore I need to build my own, this is what I need help with.

I've used pygame.Surface extensively without really understanding how it works (python programmers these days huh). So I'm hoping you can offer some insight on how to get started. I know it needs a width and height, as well as some kind of list of the sprites which have been 'blitted' to it, and that moving the surface itself moves all the contained sprites, so surfaces blitted to it are somehow anchored to the surface rather than the screen.

How is this stuff done in pygame, and how do we create a pyglet friendly version?

\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

The concept of a surface in this situation is simply describing a texture. To understand this better, you should also understand the rendering process. When rendering anything using modern graphics API's, the end result is always going to be the same, a buffer (texture) of color data that is presented to the screen. How you get to that buffer can vary quite significantly, but the end goal is still the same. The rendering pipeline today is a complicated multi-stage system that involves at the simplest level per-vertex and per-pixel operations. When rendering a 2D image to the screen, you are essentially passing through a quad of a certain size, and uv/texture information for that quad. The graphics pipeline takes care of transforming the vertices of that quad into the appropriate coordinate system for your screen, and rasterize the texture you passed in to the pixels at that rectangles location on the screen. This pixel color data is all drawn to a texture, before finally being presented.

To finally answer your question about how to create a surface that can represent a tile, you should consider the requirements of your surface. You need a way to represent its location on the screen, you also need a system for representing the size of the surface, and the texture information associated with it. Chances are that pyglet already has support for renderable sprites/surfaces/textures, and you simply need to wrap this functionality in such a way that it is easy to use in your game. A quick look up shows pyglet provides an easy method of rendering a grid of textures to the screen already, as well as support for per-texture render calls.

http://www.pyglet.org/doc/programming_guide/displaying_images.html

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .