Home Game Development python – Selecting Tiles Correctly With Scroll

python – Selecting Tiles Correctly With Scroll

0
python – Selecting Tiles Correctly With Scroll

[ad_1]

I would like to understand the process of selecting the correct tile from a scaled Surface when I have information about the number of tiles on the x and y axes, the length of the Surface, and also considering the scroll factor

Firstly, I have the following variables:

WIDTH = 640
HEIGHT = 320
TILE_SIZE = 32
MAX_COL = WIDTH // TILE_SIZE
MAX_ROW = HEIGHT // TILE_SIZE

Then, I create the base Surface with screen = pg.display.set_mode((WIDTH, HEIGHT), pg.RESIZABLE), and a dummy Surface as follows: dummy = pg.Surface([WIDTH, HEIGHT]).

Next, I create grids using some clever math and a scroll variable (which changes when I press the D button, moving everything backward):

for v in range(MAX_COL):
    # vertical / col
    pg.draw.line(dummy, (0, 0, 0), (TILE_SIZE * v - scroll_x, 0), (TILE_SIZE * v - scroll_x, HEIGHT))
    # horizontal / row
for h in range(MAX_ROW):
    pg.draw.line(dummy, (0, 0, 0), (0 - scroll_x, TILE_SIZE * h), (MAX_COL * TILE_SIZE - scroll_x, TILE_SIZE * h))

Finally, I scale it (using tw and th, calculated dimensions to maintain aspect ratio, but for now, it can be anything):

f = pg.transform.scale(s, (tw, th))
screen.blit(f, (pad_x, pad_y)) # center the window

Now, I want to choose a tile so that when I click it, it shows on which field I currently am. I achieve this by dividing my mouse X and Y coordinates from pg.mouse.get_pos() with the size of a scaled tile, and the result looks something like this:

enter image description here

Now I can go ahead and print this tile as a tuple – (2, 2)

But the problem comes when I move with my map and everything changes its position:

enter image description here

To select a tile like this, I would normally use my scroll variable and manipulate it (such as adding that information to the mouse x coordinate). However, it seems a bit tricky because when I scale everything, my scroll isn’t accurate anymore, at least not for working with a mouse. So, I am wondering if someone knows how to maintain my coordinate system while also considering my scroll factor. For example here Id like to get (2, 2) as well but thats not what I get

[ad_2]

Previous article How to remove a language on Duolingo
Next article Why Dracula kept dying in his Marvel Comics appearances
Hello there! My name is YoleeTeam, and I am thrilled to welcome you to AmazonianGames.com. As the premier destination for all things related to Amazon Games' universe, we are dedicated to providing you with the most exciting and immersive gaming experiences out there. From captivating visuals to exhilarating gameplay, our website is packed with comprehensive insights, updates, and reviews to keep you ahead of the game. Whether you're a seasoned gamer or new to the scene, I am here to guide you through this virtual frontier. If you have any questions or suggestions, feel free to reach out to me at john@yoleesolutions.com. Embark on your Amazon gaming journey today with AmazonianGames.com and let the adventure begin!