【发布时间】:2018-04-08 12:34:05
【问题描述】:
我有一个“地图”/轨道:
track_data = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1],
[1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1],
[1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1],
[1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1],
[1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1],
[1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1],
[1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1],
[1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1],
[1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1],
[1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1],
[1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1],
[1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
这些数字中的每一个都代表一个图像。正如您将在课堂上看到的那样,我在每个循环中对它们中的每一个进行了 blit。如果我只对它们进行一次blit 并且不填充背景以提高性能,那么移动的物体(汽车)会以刷子般的方式(显然)绘制屏幕。所以,我必须在每一帧都画出来。问题是:这该死的太慢了。有没有更快的方法来blit?
class Track:
TRACK_SIZE = 16
START = 0
DIRT = 1
ROAD = 2
DIRT_IMAGE = "gfx/dirt.png"
ROAD_IMAGE = "gfx/road.png"
def __init__(self):
self.data = test.track_data
self.spawnpoints = test.track_spawnpoints
self.waypoints = test.track_waypoints
self.actors = []
self.spawn_positions = {}
self.actor_dimensions = [
options["RESOLUTION"][0] / 16,
options["RESOLUTION"][1] / 16
]
self.updated = False
for row in self.data:
actor_row = []
for column in row:
if column == Track.START:
pass
elif column == Track.DIRT:
actor_row.append(pygame.image.load(Track.DIRT_IMAGE))
elif column == Track.ROAD:
actor_row.append(pygame.image.load(Track.ROAD_IMAGE))
self.actors.append(actor_row)
occurence_counter = 0
for y in range(Track.TRACK_SIZE):
for x in range(Track.TRACK_SIZE):
if self.spawnpoints[y][x] != 0:
self.spawn_positions[occurence_counter] = (
x*int(self.actor_dimensions[0]) + Car.WIDTH,
y*int(self.actor_dimensions[1]) + Car.HEIGHT
)
occurence_counter += 1
def draw(self, surface):
if not self.updated:
for y in range(Track.TRACK_SIZE):
for x in range(Track.TRACK_SIZE):
surface.blit(
self.actors[y][x],
[x*int(self.actor_dimensions[0]), y*int(self.actor_dimensions[1])]
) # Here <<<
【问题讨论】:
-
没有 blit 比没有 blit 快。你应该看看脏矩形动画概念。 pygame.org/docs/tut/newbieguide.html(在页面的前三分之一左右)。它可以真正加快你的代码速度;)
-
哦,非常感谢。不敢相信我跳过了一个叫做“新手指南”的材料哈哈哈,非常有用。
标签: python pygame pygame-surface