如何仅打击那些在玩家视觉区域中的瓷砖

发布于 2025-01-30 03:59:17 字数 828 浏览 1 评论 0原文

说明

我正在制作一个2D平台游戏,并且已经陷入了问题,

我已经检查了以下答案:命中pygame图块映射有效地,但我不使用相机,我使用Veriables cxcy

问题

更大,地图更大,游戏的发展速度较慢。相比之下,我的地图目前很小,我想要制作Playeble Maps,但我的游戏已经下降了5 fps。因此,如果我制作可玩尺寸的地图,游戏将非常慢。

代码

,我的地图代码非常简单。这是一些: pos = [(150,300),(180,330)...] 然后在循环中,我将此位置列表转换为rects,以便我可以碰撞,然后使其跟随相机并同时绘制瓷砖:

p.clear()
for a in range(len(pos)):
    p.append(pygame.draw.rect(win, (0, 0, 50), (pos[a][0] - cam.x, pos[a][1] - cam.y, 30, 30)))

我尝试了什么

,所以我想检查每个瓷砖距离从播放器和闪电器中只有那些在视觉区域中的玩家,但我需要检查每个瓷砖,以使其无法正常工作(我已经尝试过)。

非常感谢您的帮助!!!

Description

I was making a 2d platformer and I have runned into the problem

I already checked this answer: Blitting a Pygame Tile-map Efficiently but I don't use camera, I use veriables cx and cy.

Problem

So more bigger is the map, slower the game goes. And my maps is currently small compared of size that I want make playeble maps, but my game already drop 5 fps. So the game will be very slow if I make the playable size maps.

Code

So my map code is very simple. Here is some:
pos = [(150, 300), (180, 330)...]
Then in the loop I convert this position list into the rects so I can make collision, then make it follow the camera and draw tiles in the same time:

p.clear()
for a in range(len(pos)):
    p.append(pygame.draw.rect(win, (0, 0, 50), (pos[a][0] - cam.x, pos[a][1] - cam.y, 30, 30)))

What i tried

So I thought to check every tile distance from player and blit only those that are in player in vision zone, but I will need to check every tile so it will not work (I already tried).

Thanks a lot for your help!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

归途 2025-02-06 03:59:17

将所有瓷砖放入矩阵中,以便它们在矩阵中的位置与它们在游戏中出现的位置相关。然后,基于相机的位置和屏幕的大小,仅通过矩阵中的瓷砖迭代,这些图块可查看并将其闪烁到屏幕上。

例如,
说每个瓷砖都是5乘5像素,
屏幕为200乘200像素,并且
该地图为1500 x 500像素。

TILEMAP矩阵将为300 x 100像素,这是地图的第五比例,因为每个瓷砖为5 x 5像素。

如果相机(假设在屏幕的左上方)为(120,45),则x = 120,y = 45到x = 320,y = 245(由于屏幕宽度而添加200个)盒子高度)包括所有必须亮的瓷砖。

然后将这些数字除以5,以说明地图和TILEMAP之间的规模差,以获取所需瓷砖的索引。

tile_size = 5  # Width and height of tiles in pixels

min_y = camera_y // tile_size
max_y = (camera_y + screen_height) // tile_size

min_x = camera_x // tile_size
max_x = (camera_x + screen_width) // tile_size

for y in range(min_y, max_y):
    for x in range(min_x, max_x):
        tile = tile_map[y][x]
        # Use the tile from the tilemap to blit whatever you want
        # Still must subtract camera_x and camera_y when positioning the rect
        pygame.draw.rect(....)

另外,您可以将游戏分解成房间,因此只有播放器所在的房间的瓷砖才必须被检查并搅拌。

Put all of the tiles into a matrix so that their position in the matrix correlates to where they would appear in-game. Then, based on the position of the camera and the size of the screen, iterate only through the tiles in the matrix that would be viewable and blit them to the screen.

For example,
Say every tile is 5 by 5 pixels,
the screen is 200 by 200 pixels, and
the map is 1500 by 500 pixels.

The tilemap matrix would be 300 by 100 pixels which is a fifth scale of the map because every tile is 5 by 5 pixels.

If the camera (which let's say is in the top left of the screen) is at (120,45), then the box from x = 120, y = 45 to x = 320, y = 245 (added 200 because of screen width and height) includes all the tiles that must be blitted.

Then divide these numbers by 5 to account for the difference in scale between the map and the tilemap to get the indexes for the needed tiles.

tile_size = 5  # Width and height of tiles in pixels

min_y = camera_y // tile_size
max_y = (camera_y + screen_height) // tile_size

min_x = camera_x // tile_size
max_x = (camera_x + screen_width) // tile_size

for y in range(min_y, max_y):
    for x in range(min_x, max_x):
        tile = tile_map[y][x]
        # Use the tile from the tilemap to blit whatever you want
        # Still must subtract camera_x and camera_y when positioning the rect
        pygame.draw.rect(....)

Alternatively, you could break the game into rooms, so only the tiles for the room the player is in have to be checked and blitted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文