Pygame会在显示区之外带rect的blit精灵吗 [英] Will Pygame blit sprites with a rect outside the display

查看:120
本文介绍了Pygame会在显示区之外带rect的blit精灵吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在2D顶视图中进行太空探索游戏.我有很多行星,并且由于地图跨度比显示大得多,所以我最终在显示区域之外放置了许多行星精灵.目前,我认为如果Pygame不在显示器中,它们实际上并不会使精灵变白(据我所知,它会变薄并在表面上绘制会大大降低速度)是吗?还是我需要在blit调用之前添加一个条件来检查子画面是否位于显示器中?我问的原因是我的游戏的运作方式是,每发现一个星球,就会创建2个新星球……这意味着游戏会变得很大,甚至可能非常缓慢.

I'm currently working on a space exploration game in 2D top view. I have quite a few planets and since the map span is much greater than the display I end up with a lot of planet sprites lying outside the display area. At the moment I assume Pygame will not actually blit the sprite if they are not located in the display (as I understand it blitting and drawing to a surface slows things quite a lot) is that true ? Or would I need to add a condition to check if the sprite is located within the display prior to the blit call ? The reason I'm asking is that the way my game works is that each time a planet is discovered 2 new ones are created ... which means that the game can get quite big, and potentially very slow.

谢谢

推荐答案

不,它不会使图像模糊.如果您尝试抹除屏幕外的内容,Pygame只会忽略它.除非有大量对象,否则它不会减慢您的程序的速度,因为pygame会花费一小部分时间来确定图像在屏幕之外.但这不是什么大事.

No, it won't blit the images. Pygame will simply ignore it if you're trying to blit something that's outside of the screen. It shouldn't slow down your program unless there's a huge amounts of objects, since it'll take a fraction of time for pygame to determine that the image is outside of the screen. This isn't a huge deal though.

意识到我的答案缺乏证据(这很糟糕,mkay ...),因此我进行了一些测试以证明我的观点.我测试了3种情况:在屏幕上发短信,在屏幕外发短信以及什么都不做.它们花费的秒数微不足道,因为它们基于我的计算机的性能(一台5岁的笔记本电脑),因此请查找它们之间的因素;每个人都应该相似.

Realized my answer lacked proof (which is bad, mkay...), so I ran some test to prove my point. I tested 3 conditions: blitting to the screen, blitting outside the screen, and doing nothing. The number of seconds they took are insignificant because they're based on my computer's performance (a 5 year old laptop), so look for the factors between them instead; they should be similar for everyone.

  • 内部:0.033265519510593734秒
  • 外部:0.002402466401003767秒
  • 一无所有:0.00023237229449441657秒
  • 内部:3.639024520190433秒
  • 外部:0.23328839021967726秒
  • 一无所有:0.023549600850092247秒
  • 内部:360.48034191795153秒
  • 外部:23.317473949049596秒
  • 无:2.3507101910654455秒

如您所见,在屏幕外发短信比在屏幕上发短信要花费更多的时间,但是它并没有像在屏幕上实际发短信那样花时间.在屏幕外发短信几乎不算什么.

As you can see, blitting outside the screen takes more time than not blitting, but it doesn't take nearly as much time as actually blitting to the screen. Blitting outside the screen is barely a cost.

为进一步参考,这是我创建的测试:

For further reference, here's the test I created:

setup = """
import pygame
pygame.init()
screen = pygame.display.set_mode((100, 100))
image = pygame.Surface((100, 100))
"""

statement1 = """
for i in range(5000):
    screen.blit(image, (0, 0))
"""

statement2 = """
for i in range(5000):
    screen.blit(image, (200, 200))
"""

statement3 = """
for i in range(5000):
    pass
"""

from timeit import timeit

num_of_times = 10000
inside = timeit(stmt=statement1, setup=setup, number=num_of_times)
outside = timeit(stmt=statement2, setup=setup, number=num_of_times)
nothing = timeit(stmt=statement3, setup=setup, number=num_of_times)

print("Inside: {} seconds".format(inside/num_of_times))
print("Outside: {} seconds".format(outside/num_of_times))
print("Nothing: {} seconds".format(nothing/num_of_times))

这篇关于Pygame会在显示区之外带rect的blit精灵吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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