我有一张"地图" / track:
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。如果我只把它们搞砸一次并且不填充背景以提高性能,那么移动的物体(汽车)会以类似刷子的方式绘制屏幕(显然)。所以,我必须在每一帧画出它们。问题是:它太慢了。是否有更快的方式来搞定它们?
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 <<<
答案 0 :(得分:0)
事实证明解决问题非常容易。我所要做的就是创建一个屏幕大小,轨道大小的中间表面,然后将所有地面图像blit到它一次,然后在主循环上blit中间轨道表面而不是256个单独的图像。
class Track:
TRACK_SIZE = 16
DIRT = 1
ROAD = 2
DIRT_IMAGE = "gfx/dirt.png"
ROAD_IMAGE = "gfx/road.png"
def __init__(self):
self.ground_data = test.track_ground_data
self.spawnpoints = test.track_spawnpoints
self.waypoints = test.track_waypoints
self.actors = []
self.ground_positions = {}
self.spawn_positions = {}
self.waypoint_positions = {}
self.actor_dimensions = [
options["RESOLUTION"][0] / 16,
options["RESOLUTION"][1] / 16
]
self.surface = pygame.Surface((options["RESOLUTION"][0], options["RESOLUTION"][1]), pygame.SRCALPHA, 32)
for row in self.ground_data:
actor_row = []
for column in row:
if 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)
for y in range(Track.TRACK_SIZE):
for x in range(Track.TRACK_SIZE):
if self.ground_data[y][x] != 0:
self.ground_positions[(
x*int(self.actor_dimensions[0]),
y*int(self.actor_dimensions[1])
)] = self.ground_data[y][x]
if self.spawnpoints[y][x] != 0:
self.spawn_positions[self.spawnpoints[y][x]] = (
x*int(self.actor_dimensions[0]) + Car.WIDTH,
y*int(self.actor_dimensions[1]) + Car.HEIGHT
)
if self.waypoints[y][x] != 0:
self.waypoint_positions[self.waypoints[y][x]] = (
x*int(self.actor_dimensions[0]) + Car.WIDTH,
y*int(self.actor_dimensions[1]) + Car.HEIGHT
)
# Blitting the track images to the track surface for greater performance.
for y in range(Track.TRACK_SIZE):
for x in range(Track.TRACK_SIZE):
self.surface.blit(
self.actors[y][x],
[x*int(self.actor_dimensions[0]), y*int(self.actor_dimensions[1])]
)
def draw(self, surface):
surface.blit(self.surface, (0, 0))
答案 1 :(得分:0)
我参加派对有点晚了,但是性能提升也很大:
DIRT_IMAGE = pygame.image.load("gfx/dirt.png").convert() # if you dont have transparency
ROAD_IMAGE = pygame.image.load("gfx/road.png").convert_alpha # if you have transparency
actor_row.append(DIRT_IMAGE)
actor_row.append(ROAD_IMAGE )
1)您不必经常加载图像,只需加载一次(这有助于在开始时加载时间)
2)在你的图像上使用“.convert()”可以在整个“游戏周期”中给你带来巨大的性能提升,或者你经常会把它们搞砸 - 特别是如果你在很大范围内拍摄很多图像