Advertisement

Implementing a 2D grid style world in pygame (or any language really)

Started by December 06, 2019 05:51 PM
3 comments, last by kseh 5 years, 2 months ago

I've been working on for the last few weeks... trying to make a grid style world in Pygame.
Terr-aria or Rimworld or Dungeon keeper come to mind, thought I'm trying to do this Map style, or overhead... so Rimworld would likely most closley represent what I'm trying to do.

At game start... a world is created, and it has a list of 'cubes'. Cubes are 32x32 and they are the blocks that make up the grid. From there they can contain 'stuff'.... ie: grass, tree, fire, an npc, etc. I have to imagine Terraria does this similar to this - as it has to keep track of what is in a space.

The entire world is 1000x1000 or 100,000 cubes.

I only want to render around 714 of them at a time - the view window, or camera I guess you would say.

I am having a heck of a time figuring out how to pick out the 714 cubes I should be seeing. I guess it boils down to .... I only want to render the cubes in the camera, but.... still update logic for the surrounding area. Looping 100,000 cubes every game frame seems to be a bit much.

Has anyone done something similar?

My other issue is addressing the cubes... I wasn't sure if on creation they should be given their X, Y co-ordinate,
or should they just be id'd..... 1, 2, 3, etc.

Hoping someone might be able to explain how they would tackle this.


Amateur want to be - Game Developer.

Welcome to data structures. Your exact solution may depend on some specific requirements but the general idea that all solutions to this problem is to have a hierarchy of grids. I.e. you group your grids into larger buckets that can be retrieved very quickly. If you had, for example, a map that was 1024x1024 grids big, then you might start grouping into buckets 16x16 in size. You create a data structure that can easily take your view's coordinates and find the final set of 16x16 buckets that your view can see - which depending on where your view is located and how many grids you can fit on the screen might be as small as 1 or much larger - - this is where you need appropriately pick the size of buckets to match your problem.

[ this is a psuedo description of on possible solution - look at quad-trees online for a more general idea]

The complexity of how you solve this can vary quite a bit depending on if you want to include moving entities or just the world grids.

DR

Advertisement

Would I still be able to scroll across the grids though?

I get what you are saying (I think).

I'll look into Quad Trees.

I wish there was a good Pygame book that dealt with OOP and walked through an entire 2D RPG game... I know there are 10+ Pygame books... but I never find anything in depth enough.

Amateur want to be - Game Developer.

My projects have all been pretty simple so far but generally I just keep a set of x,y co-ordinates for scrolling, which is the co-ordinates of where my view starts, and only display the tiles from there. I think it ends up being 20*15 tiles. As the actor that I'm following moves, I update the scroll co-ordinates. There should be a fair number of scrolling tutorials you can search for online.

I only process stuff in my game world that really needs it. For example, trees in my world are decoration but also act as a barrier. So there isn't really anything they need to do with them but maybe become relevant when some other actor is going to collide with it. When an actor that can move is concerned with colliding into things, it tests the tiles near by for any objects, determines if a collision takes place, and acts accordingly.

I haven't needed to process a very large number of other objects and actors yet, but, if I had multiple towns of a fairly large size, I would try to abstract what I can. That is, for a town the player is nowhere near, I don't actually need to have Farmer Dan walk to Mike's Market to complete his business there. Likewise, I wouldn't need every citizen to walk to work. I can just assume they all make it just fine and update only what I need to. Unless maybe some random event occurs in which case I'd be looking at finding a way to make the data for the various object's and actors affected fit the event. My point being is that I would process 1 town in a general way rather than every citizen in a specific way.

This topic is closed to new replies.

Advertisement