0

displayio for tile-based game?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

displayio for tile-based game?

by fnord on Sun Mar 10, 2019 12:39 am

Hi! I tried to make a little game for my HalloWing today. I found some tile-based artwork for a dungeon game and tried to use displayio to draw a map of tiles ... but got a bit stuck on this part. Here is a github repo for what I have so far: https://github.com/cjdaly/CircuitDungeon

From that repo, you can copy dungeon.py and 0x72_DungeonTilesetII_v1.2-top.bmp to the HalloWing (top level dir) and then from the REPL do:

Code: Select all | TOGGLE FULL SIZE
import dungeon
dungeon.test(-1)


... and should see the image below. You can also pass a positive number ...
Code: Select all | TOGGLE FULL SIZE
dungeon.test(97)
will fill the screen with the green banner tile. But I'm stuck on what displayio primitives I need to create a map on the screen with arbitrary tiles from the tileset bmp... also how to layer on tiles for the players and NPCs ... and, if possible, primitives for tile animations... Any help and pointers greatly appreciated!

HalloWing.JPG
HalloWing.JPG (438.98 KiB) Viewed 173 times
HalloWing.JPG
HalloWing.JPG (438.98 KiB) Viewed 173 times

fnord
 
Posts: 14
Joined: Thu Nov 01, 2012 1:27 am

Re: displayio for tile-based game?

by tannewt2 on Mon Mar 11, 2019 1:16 am

WOooo! That looks awesome. You are getting a bit ahead of me and are on the right track.

However, I haven't expose changing tile indices to Python yet. It's in C here: https://github.com/adafruit/circuitpyth ... rid.c#L129

Once that is in you should be able to use it for the background. You can use separate tile grids of a single tile for the player and npcs. I think animations would be changing the tile indices for those grids of 1.

The other missing piece is update speed. Right now, it's very slow.

I'm focused on getting 4.0 out the door now and then will be heads down on gaming. You should try and add subscr support to tilegrid to call the C. Let me know if you need help with it.

tannewt2
 
Posts: 981
Joined: Thu Oct 06, 2016 8:48 pm

Re: displayio for tile-based game?

by tannewt2 on Tue Mar 12, 2019 7:35 pm

FYI I couldn't help myself and am adding the tile index changing for 4.0.0. Follow the PR here: https://github.com/adafruit/circuitpython/pull/1640

tannewt2
 
Posts: 981
Joined: Thu Oct 06, 2016 8:48 pm

Re: displayio for tile-based game?

by fnord on Tue Mar 12, 2019 11:59 pm

Thanks! One thing I'm learning is things will probably be easier if I pre-process the artwork images I have and pull out specific tiles as individual bitmap files. Here is another screenshot:

CircuitDungeon.JPG
CircuitDungeon.JPG (128.08 KiB) Viewed 133 times

fnord
 
Posts: 14
Joined: Thu Nov 01, 2012 1:27 am

Re: displayio for tile-based game?

by tannewt2 on Wed Mar 13, 2019 1:22 pm

What makes that easier for you? I'm interested to know what hangups you hit doing this.

tannewt2
 
Posts: 981
Joined: Thu Oct 06, 2016 8:48 pm

Re: displayio for tile-based game?

by fnord on Thu Mar 14, 2019 3:40 am

... maybe 'easier' isn't the right word. I'm still not sure I'm using the existing API in the best way but my initial approach was to create a bitmap of this image:

https://github.com/cjdaly/CircuitDungeo ... I_v1.2.png

and then load the whole thing as an OnDiskBitmap and then I wanted to copy rectangular regions from there to the screen or to some intermediate framebuffer Bitmap that I can compose on and then copy to the screen ... I don't see a way to follow this approach with the existing API, but maybe I'm just missing it.

So my current approach - given that I know how to create a Group and put TileGrids in it (loaded from OnDiskBitmaps) - is to preprocess the artwork image and extract bitmaps for terrain, objects and creatures. Those extracted bitmaps are here:

https://github.com/cjdaly/CircuitDungeo ... s/tileset1

I can load the character and creature bitmaps into a TileGrid and then change the position to dynamically move the creature around the screen. I think the feature you are adding would allow me to animate the creatures by switching between the different animations for the creature (I would need to extract more bitmaps from my source image for this, but that part is easy).

But there is one more problem - the game map. I don't think it will work to create a TileGrid for every cell in the map and try to line them up in a grid of TileGrids ... so again here I am preprocessing. Tonight I made a way of specifying a map as a simple text file, like this:

https://github.com/cjdaly/CircuitDungeo ... 1/map1.map

and then generate a bitmap for it from the extracted terrain images, like this:

https://raw.githubusercontent.com/cjdal ... 1/map1.bmp

So now I could have a Group where the game map Bitmap is the lowest layer and the creatures and objects are higher in the Group so they show up on top of the map. I haven't tried this on the HalloWing yet, but I will soon.

fnord
 
Posts: 14
Joined: Thu Nov 01, 2012 1:27 am

Re: displayio for tile-based game?

by fnord on Fri Mar 15, 2019 1:55 am

Below is a pic of what I got when I used the map TileGrid I mentioned generating in my last post, and put 3 creature TileGrids on top:

TileGrid_overlay.JPG
TileGrid_overlay.JPG (127.41 KiB) Viewed 93 times


... the bummer is the map background gets overwritten (the alpha/transparency stuff isn't right).... is it the Palette I'm using for the TileGrids (displayio.ColorConverter()) ?

fnord
 
Posts: 14
Joined: Thu Nov 01, 2012 1:27 am

Re: displayio for tile-based game?

by tannewt2 on Fri Mar 15, 2019 1:41 pm

Ah I see. I think the original approach with one Bitmap will work with the latest build from S3. https://adafruit-circuit-python.s3.amaz ... ess/en_US/

For animations you can change the tile shown by the TileGrid with:
Code: Select all | TOGGLE FULL SIZE
tile_grid[0,0] = 3


This will change the tile index it loads at cell 0,0 to 3.

For the background map you need to init the TileGrid with width and height greater than one. Once you do that you can set each position in the same way as above. (Again, this is only in the latest builds.)

You are right that ColorConverter doesn't do transparency. Are your tiles a limited palette and then you can use Palette? Otherwise, we could add keying to ColorConverter (aka color x is transparent.)

tannewt2
 
Posts: 981
Joined: Thu Oct 06, 2016 8:48 pm

Re: displayio for tile-based game?

by fnord on Fri Mar 15, 2019 7:00 pm

about the transparency... I am using the ImageMagick command-line ('convert') on a Ubuntu machine for all of the image preprocessing. The original artwork PNG file has a transparent background and ImageMagick seems to preserve that when I crop out sub-images and convert them to BMP format. I just tried using ImageMagick to paste one of the hero bitmaps on top of the map and it handled the transparency correctly.

So I guess I need to use a different Palette when constructing the TileGrid. I'll play around with that and see if I can stumble into anything good....

About the TileGrid enhancements in the latest builds ... is there another 4.0.0 beta coming soon? I may wait for that, but I guess if too bored this weekend I may try your bleeding edge stuff. :-)

fnord
 
Posts: 14
Joined: Thu Nov 01, 2012 1:27 am

Re: displayio for tile-based game?

by tannewt2 on Fri Mar 15, 2019 7:50 pm

Ah! The file may already have transparency then. We should have color converter pay attention to it.

I just released Beta 4 which should have these changes. It's available here: https://circuitpython.org/downloads

tannewt2
 
Posts: 981
Joined: Thu Oct 06, 2016 8:48 pm

Re: displayio for tile-based game?

by fnord on Sat Mar 16, 2019 7:33 pm

So far beta 4 is looking really unstable on my HalloWing. Several times now I've got into a state where I do ctrl-D in the REPL and instead of resetting, it locks up. The status NeoPixel turns red and there is no response. If I reset the HalloWing it comes up "Running in safe mode! ..." and it mentions the error message: "Crash into the HardFault_Handler."

Since this started happening, the HalloWing no longer mounts as /media/pi/CIRCUITPY ... instead it looks like: /media/pi/4921-8571

I'm going to try switching back to beta 3 and see if these issues go away....

fnord
 
Posts: 14
Joined: Thu Nov 01, 2012 1:27 am

Re: displayio for tile-based game?

by fnord on Sat Mar 16, 2019 9:31 pm

I was able to get the HalloWing back to a semi-normal state by following the troubleshooting guide (here: https://learn.adafruit.com/adafruit-pyp ... leshooting) and running storage.erase_filesystem() in the REPL. After that I tried loading beta 4 again along with the latest libraries, and then tried to run displayio code, but quickly got back into the "Crash into the HardFault_Handler" state again.

One other odd detail: when I copy the 'lib' dir to /media/pi/CIRCUITPY, I see a lot of flashing of the status NeoPixel and near the end of the copy the HalloWing appears to reset. Why would it do that (just copying files)? Is it possible the reset happened before the copy completed? Is there something special about a 'lib' dir? Maybe I should copy it as 'foo' and then rename to 'lib' after the copy?

fnord
 
Posts: 14
Joined: Thu Nov 01, 2012 1:27 am

Re: displayio for tile-based game?

by fnord on Mon Mar 18, 2019 5:05 pm

... I noticed there is a 'beta5' now. I've got that on one HalloWing and it seems stable so far. Also, the TileGrid animations (like tg[0]=1) ) seem to work for me, but I'm still not seeing transparency (using displayio.ColorConverter()).

fnord
 
Posts: 14
Joined: Thu Nov 01, 2012 1:27 am

Re: displayio for tile-based game?

by tannewt2 on Mon Mar 18, 2019 5:10 pm

Ah, transparency hasn't been added yet. It will come after 4.0.0 is out the door. I'm glad Beta 5 is much more stable!

tannewt2
 
Posts: 981
Joined: Thu Oct 06, 2016 8:48 pm

Please be positive and constructive with your questions and comments.