This content originally appeared on DEV Community and was authored by Matt F.
Rendering issues
The first test of the rendering function (that I posted on the last devlog) worked well, but that had a 2×2 list with the perfect amount of tiles to fit the screen. When I tried using the same function for a bigger map, it basically rendered all of it.
I noticed some mistakes in the code while I was writing the last post, but I kept it there to be able to compare, here is the new rendering function, maybe a bit more disorganized, but it’s the best one I’ve ever came up with!
import pygame as pg
import settings as stgs
def render(layer, center):
gw, gh = stgs.grid_w, stgs.grid_h # Max tiles per grid axis
x, y = center # Player position
# Coordinates of the grid on the screen
left, right = x - gw // 2, x + gw // 2
top, bottom = y - gh // 2, y + gh // 2
# Offset needed if the player position makes the grid
# not appear entirely on the screen
offx, offy = 0, 0
# Fixing the values initiated earlier
if left < 0:
offx = -left
left = 0
if right > (l := len(layer[0])):
right = l
if top < 0:
offy = -top
top = 0
if bottom > (l := len(layer)):
bottom = l
# Getting the tile range that will be rendered
chunck = layer[top:bottom+1]
chunck = [
i[left:right+1] for i in chunck
]
ts = stgs.tile_size
# Drawing each tile on the screen
# (requires optimization)
for y, row in enumerate(chunck):
for x, tile in enumerate(row):
stgs.screen.blit(
tile.image, (
(x+offx)*ts+stgs.margin_w,
(y+offy)*ts+stgs.margin_h
)
)
Finally some fun
With the rendering completed (for now), I’ve happily had some time to make some basic tiles for testing. And with all that time, I only made a terrible 32×32 grass tile
(The orange guy in the middle is the player for now )
Movement
From that, I started planning how the movement interactions would work in this game. UI in pygame is not the most friendly thing to consider, so I quickly sketched a tap to move interaction. By converting the position of the click to the grid tile it hits, I can pretty easily move the player to that tile, and it worked really well!
Final words
I kinda delayed the post of this devlog because I didn’t have much time to work on the project, but I hope the little steps I’m taking are gonna turn into one of the best games I’ve made.
Thanks for reading!
See ya!
This content originally appeared on DEV Community and was authored by Matt F.