我的英语太烂了,我试图尽力写这个问题:D
我正在用Pyglet制作2D游戏,我需要制作平铺的地图。 1个图块= 50像素x 50像素 但是,当我绘制20个敌人的地图50x50磁贴时,这需要很大的PC功能,而我的fps从60fps下降到10fps 每块瓷砖和敌人都是成批绘制的。 我该怎么做才能提高游戏效率?
我尝试缩放图块,但每个图块上都有一个黑色边框,并且我想使用50x50像素的图块,而不是50x50 /比例
#Create blocks 50x50 pixels and replace with image names to get what i see.
from pyglet.window import key, FPSDisplay
import pyglet
import math
Background = pyglet.graphics.OrderedGroup(0)
Walls_Group = pyglet.graphics.OrderedGroup(1)
def preload_image(image):
img = pyglet.image.load('images/' + image)
return img
map_x = 50
map_y = 50
window_X = 1500
window_Y = 900
class GameWindow(pyglet.window.Window):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.set_location(100, 30)
self.frame_rate = 1.0 / 100.0
self.fps_display = FPSDisplay(self)
self.fps_display.label.font_size = 30
self.player_speed = 550
self.right = False
self.left = False
self.up = False
self.down = False
self.map_load = False
self.Walls_load = False
self.map_scale = 1
self.wall = preload_image('block.png')
self.wall_list = []
self.map_1_list = []
self.sprite = preload_image('Grass_Green.png')
self.main_batch = pyglet.graphics.Batch()
def Mapka(self, x_size, y_size):
for Y in range(y_size):
for X in range(x_size):
self.map_1_list.append(pyglet.sprite.Sprite(self.sprite, x=X * (self.sprite.width*self.map_scale), y=Y * (self.sprite.height*self.map_scale), batch=self.main_batch, group=Background))
for i in self.map_1_list:
i.scale = self.map_scale
def Walls(self):
self.times = math.ceil(map_x * (self.sprite.width*self.map_scale) / self.wall.width) # Oblicza ilość ścian na dolnej części
# mapy z zaokrągleniem
self.times_y = math.ceil(map_y * (self.sprite.height*self.map_scale) / self.wall.height)
for x in range(int(self.times)):
self.wall_list.append(
pyglet.sprite.Sprite(self.wall, x=x * self.wall.width, y=0, batch=self.main_batch, group=Walls_Group))
self.wall_list.append(
pyglet.sprite.Sprite(self.wall, x=x * self.wall.width, y=(self.times_y - 1) * self.wall.height,
batch=self.main_batch, group=Walls_Group))
for y in range(int(self.times_y)):
self.wall_list.append(
pyglet.sprite.Sprite(self.wall, x=0, y=y * self.wall.height, batch=self.main_batch, group=Walls_Group))
self.wall_list.append(pyglet.sprite.Sprite(self.wall, x=self.times * self.wall.width - self.wall.width,
y=y * self.wall.height, batch=self.main_batch,
group=Walls_Group))
def on_draw(self):
self.clear()
if not self.map_load:
self.Mapka(map_x, map_y)
self.map_load = True
if not self.Walls_load:
self.Walls()
self.Walls_load = True
self.main_batch.draw()
self.fps_display.draw()
def on_key_press(self, symbol, modifiers):
if symbol == key.D:
self.right = True
if symbol == key.A:
self.left = True
if symbol == key.W:
self.up = True
if symbol == key.S:
self.down = True
if symbol == key.ESCAPE:
pyglet.app.exit()
def on_key_release(self, symbol, modifiers):
if symbol == key.D:
self.right = False
if symbol == key.A:
self.left = False
if symbol == key.W:
self.up = False
if symbol == key.S:
self.down = False
def update_space(self, dt):
for space in self.map_1_list:
space.update()
space.y -= 0 * dt
if self.right:
space.x -= self.player_speed * dt
if self.left:
space.x += self.player_speed * dt
if self.up:
space.y -= self.player_speed * dt
if self.down:
space.y += self.player_speed * dt
def update_wall(self, dt):
for wall in self.wall_list:
wall.update()
if self.right:
wall.x -= self.player_speed * dt
if self.left:
wall.x += self.player_speed * dt
if self.up:
wall.y -= self.player_speed * dt
if self.down:
wall.y += self.player_speed * dt
def update(self, dt):
self.update_wall(dt)
self.update_space(dt)
if __name__ == "__main__":
window = GameWindow(window_X, window_Y, "Gra", resizable=False)
pyglet.clock.schedule_interval(window.update, window.frame_rate)
pyglet.app.run()
我想拥有至少100 x 100的平铺地图,并且可以60 fps的速度运行。 平铺= 50x50像素 如果可以绘制图块,但仅绘制屏幕上可见的图块,则只能绘制X,Y屏幕。
答案 0 :(得分:0)
好,所以这里的主要问题可能是schedule_interval
,它无法尽快运行。主要是因为它会更新场景,并且会触发渲染。第二个是wall.update()
调用(我不知道您为什么要打电话)的速度非常慢,并且您要进行width * height ^ 2次。而且效率很低。
有一个快速的解决方法。下面是一种建议的实现方法。
#Create blocks 50x50 pixels and replace with image names to get what i see.
from pyglet.window import key, FPSDisplay
import pyglet
import math
import time
Background = pyglet.graphics.OrderedGroup(0)
Walls_Group = pyglet.graphics.OrderedGroup(1)
def preload_image(image):
img = pyglet.image.load('images/' + image)
return img
map_x = 50
map_y = 50
window_X = 1500
window_Y = 900
class GameWindow(pyglet.window.Window):
def __init__(self, *args, **kwargs):
super(GameWindow, self).__init__(*args, **kwargs)
self.set_location(100, 30)
self.frame_rate = 1.0 / 100.0
self.fps_display = FPSDisplay(self)
self.fps_display.label.font_size = 30
self.player_speed = 550
self.right = False
self.left = False
self.up = False
self.down = False
self.map_load = False
self.Walls_load = False
self.map_scale = 1
self.wall = preload_image('block.png')
self.wall_list = []
self.map_1_list = []
self.sprite = preload_image('Grass_Green.jpg')
self.main_batch = pyglet.graphics.Batch()
self.alive = True
self.last_scheduled_update = time.time()
if not self.map_load:
self.Mapka(map_x, map_y)
self.map_load = True
if not self.Walls_load:
self.Walls()
self.Walls_load = True
def Mapka(self, x_size, y_size):
for Y in range(y_size):
for X in range(x_size):
self.map_1_list.append(pyglet.sprite.Sprite(self.sprite, x=X * (self.sprite.width*self.map_scale), y=Y * (self.sprite.height*self.map_scale), batch=self.main_batch, group=Background))
for i in self.map_1_list:
i.scale = self.map_scale
def Walls(self):
self.times = math.ceil(map_x * (self.sprite.width*self.map_scale) / self.wall.width) # Oblicza ilość ścian na dolnej części
# mapy z zaokrągleniem
self.times_y = math.ceil(map_y * (self.sprite.height*self.map_scale) / self.wall.height)
for x in range(int(self.times)):
self.wall_list.append(
pyglet.sprite.Sprite(self.wall, x=x * self.wall.width, y=0, batch=self.main_batch, group=Walls_Group))
self.wall_list.append(
pyglet.sprite.Sprite(self.wall, x=x * self.wall.width, y=(self.times_y - 1) * self.wall.height,
batch=self.main_batch, group=Walls_Group))
for y in range(int(self.times_y)):
self.wall_list.append(
pyglet.sprite.Sprite(self.wall, x=0, y=y * self.wall.height, batch=self.main_batch, group=Walls_Group))
self.wall_list.append(pyglet.sprite.Sprite(self.wall, x=self.times * self.wall.width - self.wall.width,
y=y * self.wall.height, batch=self.main_batch,
group=Walls_Group))
def render(self):
self.clear()
self.main_batch.draw()
self.fps_display.draw()
self.flip()
def on_key_press(self, symbol, modifiers):
if symbol == key.D:
self.right = True
if symbol == key.A:
self.left = True
if symbol == key.W:
self.up = True
if symbol == key.S:
self.down = True
if symbol == key.ESCAPE: # [ESC]
self.alive = 0
self.update(1)
def on_key_release(self, symbol, modifiers):
if symbol == key.D:
self.right = False
if symbol == key.A:
self.left = False
if symbol == key.W:
self.up = False
if symbol == key.S:
self.down = False
def update_space(self, dt):
for space in self.map_1_list:
space.update()
space.y -= 0 * dt
if self.right:
space.x -= self.player_speed * dt
if self.left:
space.x += self.player_speed * dt
if self.up:
space.y -= self.player_speed * dt
if self.down:
space.y += self.player_speed * dt
def update_wall(self, dt):
for wall in self.wall_list:
wall.update()
if self.right:
wall.x -= self.player_speed * dt
if self.left:
wall.x += self.player_speed * dt
if self.up:
wall.y -= self.player_speed * dt
if self.down:
wall.y += self.player_speed * dt
def update(self, dt):
self.update_wall(dt)
self.update_space(dt)
def run(self):
while self.alive == 1:
if time.time() - self.last_scheduled_update > 0.25:
self.update(time.time() - self.last_scheduled_update)
self.last_scheduled_update = time.time()
self.render()
# -----------> This is key <----------
# This is what replaces pyglet.app.run()
# but is required for the GUI to not freeze
#
event = self.dispatch_events()
if __name__ == "__main__":
window = GameWindow(window_X, window_Y, "Gra", resizable=False)
window.run()
这是对代码的最小更改,但是您可以使用自己的事件循环更改内置的调度程序,由您负责运行self.update()
的时间和标准。在这种情况下,它每隔0.25
秒间隔运行一次,并在每次触发on_keypress
之后触发。
除非您四处走动,否则没有理由更新精灵的位置。或者在将来,当敌人/物体执行动作时。
使用此代码,我得到了稳定的144 FPS。
但是,还有更多要做的事情..因此,我将介绍long lasting tip来帮助您解决未来的瓶颈。我将演示您将如何使用它。
运行以上命令,将为您提供以下信息:
这清楚地显示了主要的循环行者为_wait_vsync
。
这使我意识到我忘记检查最明显的问题是这里的问题。那就是你和我忘记vsync=False
到窗口对象。
这里的解决方案是:
window = GameWindow(window_X, window_Y, "Gra", resizable=False, vsync=False)
哪个给我:
是的,高于2700 FPS。
调用栈看起来甚至更多:
从现在开始,这里就是真正的挑战开始的地方。
完成的所有优化都是次要的,很难找到,调试起来很困难。
希望您拥有这些工具,并且了解在哪里以及为什么要看地方:)