How to make a map surface object

Game Development Asked by Anonymous Entity on January 8, 2021

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:

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?

One Answer

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.

Answered by Evan on January 8, 2021

Add your own answers!

Related Questions

Best way to save game data in Unity

1  Asked on February 28, 2021 by tair-galili


Unity – Sprite vertices in 3D world

1  Asked on February 24, 2021 by muckington


How to represent a modular FSM for AI using ECS?

2  Asked on February 22, 2021 by christian-ivicevic


get access to Transform of a Clone in Unity

1  Asked on February 21, 2021 by amin007


Box2d on authoritative server for Unity client

0  Asked on February 19, 2021 by antonio-agustin


How to Send Voice over Unity Networking – UNET

1  Asked on February 12, 2021 by muhammad-faizan-khan


Mathf.Clamp not Working Properly

1  Asked on February 3, 2021 by shubhendra-chaddha


SetTrigger Only Works In Start and Not OnTriggerEnter

1  Asked on February 3, 2021 by kit-k


How do I detect image clicks in Dark GDK?

1  Asked on February 2, 2021 by bobman


FIx color banding in unity

0  Asked on February 1, 2021


OnMouseOver not registering collisions

3  Asked on January 29, 2021 by hyperific


Call native code from Unity iOS build error

1  Asked on January 29, 2021 by atlantis


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir