Pygame blitting 只更新表面 [英] Pygame blitting only updated surfaces

查看:32
本文介绍了Pygame blitting 只更新表面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有一个 x x y 数组来保存决定要在屏幕上绘制哪个图块的整数.(整数选择我的 tile_arr 中的哪个图块进行 blit)

Right now I have an x by y array to hold integers that decide which tile to draw to the screen. (The integers choose which tile in my tile_arr to blit)

为了更好的性能,我只希望改变的整数再次被 blit'ed.

For better performance, I only want the ints that changed to be blit'ed again.

示例 1:

例如现在我有类似的东西:

For example right now I have something like:

tile_arr = [image1,image2,image3,image4]
arr = [[2,2,2],[2,2,2],[2,2,2]]

然后根据用户的行为,arr 中的某些值可能会发生变化,因此可以说:

Then depending on what the user does, some values in arr might change, so lets say:

arr[0][0]=1
arr[2][1]=1

这会给我们数组:

arr=[[1,2,2],[2,2,2],[2,1,2]]

现在当 blit 到屏幕时,我会从 tile_arr 中 blit 图像:图像编号 1,2,2 到顶行,2,2,2,到中间行,和 2,1,2 到底行.当我对数组进行 blit 时,我对每个值或 arr 使用 screen.blit,即九个 blit.我宁愿只做两个 blit.(只使用 screen.blit 两次)

now when blitting to the screen, I would blit images from the tile_arr: image numbers 1,2,2 to the top row, 2,2,2, to the middle row, and 2,1,2 to the bottom row. When I blit the array, I use a screen.blit for each value or arr, that's nine blits. I would rather only do two blits. (Use screen.blit only twice)

示例 2:

tile_arr = [green.bmp, red.bemp, blue.bmp]
feild_arr = [[0,0,0], [0,0,0], [0,0,0]]

输出:

G G G

G G G

G G G

用户将 feild_arr 更改为 [[1,0,1], [0,2,0], [0,1,2]]

输出:

R G R

G B G

G R B

现在我只想调用 sceen.blit() 5 次,让 4 个绿色方块保持绿色,因为什么都没有改变.

Now I only want to call sceen.blit() 5 times, leaving the 4 Green sqaures green, because nothing changed.

我想制作另一个数组,它只是第一个数组的副本.然后运行它并与新数组进行比较以查看发生了什么变化,但我认为有一个更好更快的方法.现在这个例子只有 3x3,所以创建一个重复的数组还不错,但我正在处理更大的数组,当你 blitting 一个 30x20 的数组时,我需要我能得到的所有快捷方式.

I thought of making another array, which would be just a copy of the first. Then run through it and compare to the new array to see what changed, but I think there is a better and faster way to this. Now the example is only 3x3 so making a duplicate array isn't too bad, but I'm working with a lot bigger arrays, and when you're blitting a 30x20 array, I need all the shortcuts I can get.

如何仅在数组中的整数值已更改时进行 blit,并跳过(不 blit)未更改的值?

推荐答案

您只能使用 screen.blit 一次,并使用更改的矩形列表调用.

You can use screen.blit only once, calling with a list of the rectangles that changed.

我认为最好的方法是从 DirtySprite 创建你自己的类:

I think the best aproach is to create you own class deriving from DirtySprite:

类单元格:pygame.sprite.DirtySprite

它已经具有用于保存图像和矩形的属性,您可以添加一个属性来保存数字以及更改将其设置为脏的数字的方法.

which already has attributes for holding an image and a rectangle and you can add an attributes to hold the number and a method to change de number that will set it as dirty.

然后你可以使用 LayeredDirty 类来渲染屏幕上的脏精灵.

Then you can use LayeredDirty class to render the dirty sprites on the screen.

这篇关于Pygame blitting 只更新表面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆