我的游戏动画性能不够快,所以我尝试寻找一种提高速度的方法,发现Shoot em up,但游戏似乎使用了GLSL脚本和画布,但我真的不了解它们的工作原理。如何使用上述方法(glsl脚本和画布)重写代码
这是我的慢脚本,在移动设备(Android和iOS)上为10-12 Fps,在台式机上为48-58 Fps
from kivy.app import App
from kivy.uix.image import Image
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty,ListProperty, BooleanProperty
from kivy.vector import Vector
from kivy.clock import Clock
import random
import math
from kivy.lang import Builder
Builder.load_string("""
<FT>:
canvas.before:
PushMatrix
Rotate:
angle: self.angle
origin: self.center
canvas.after:
PopMatrix
<Flame>:
canvas.before:
PushMatrix
Rotate:
angle: self.angle
origin: self.center
canvas.after:
PopMatrix
<FlameGun>:
tank: tank
canvas:
Rectangle:
source: "wood.png"
size: self.size
pos: self.pos
FT:
id: tank
center: (root.width/2, 70)
""")
class Flame(Image):
velocity_x = NumericProperty(0)
active = BooleanProperty(False)
velocity_y = NumericProperty(0)
velocity = ReferenceListProperty(velocity_x, velocity_y)
angle = NumericProperty(0)
def __init__(self, **kwargs):
super(Flame, self).__init__(**kwargs)
self.size_hint=(None, None)
self.source = "zn.zip" #Contain animated images of Flame
self.anim_delay= 0.35
self.anim_loop = 1
rs = random.randint(70, 80)
self.size = (rs, rs)
self.bind(active=self.addV)
def addV(self, *args):
self.velocity_y += random.uniform(20.0, 25.0)
self.velocity_x += 0
def move(self, pa):
self.pos = Vector(*self.velocity) + self.pos
self.angle = random.randint(0, 360)
self.size = (self.size[0]+1, self.size[1]+1)
if (self.y > pa.top/1.6):
pa.removeThis(self)
self.velocity_y = 0
self.velocity_x = 0
class FT(Image):
velocity_x = NumericProperty(0)
activated = BooleanProperty(False)
velocity_y = NumericProperty(0)
velocity = ReferenceListProperty(velocity_x, velocity_y)
angle = NumericProperty(180)
def __init__(self, **kwargs):
super(FT, self).__init__(**kwargs)
self.source = 'FlameThrower.png'
class FlameGun(Widget):
tank = ObjectProperty(None)
angle = NumericProperty(180)
pr = NumericProperty(0.0)
flames = []
def update(self, dt):
self.addFlame()
if len(self.flames) != 0:
for tank in self.flames:
if tank.active:
tank.move(self)
def removeThis(self, bl):
self.flames.remove(bl)
self.remove_widget(bl)
bl = None
def startFlame(self, *args):
for tank in self.flames:
if not tank.active:
tank.active = True
def addFlame(self, *args):
self.fl = Flame()
self.add_widget(self.fl)
self.fl.center = self.tank.center
self.flames.append(self.fl)
self.fl.velocity = 0, 0
if len(self.flames) != 0:
self.startFlame()
def on_touch_down(self, touch):
self.tank.activated = True
class FlameApp(App):
def build(self):
game = FlameGun()
Clock.schedule_interval(game.update, 1.0 / 60.0)
return game
if __name__ == '__main__':
FlameApp().run()
我如何使用glsl重写以上代码以获得最快的性能?
答案 0 :(得分:0)
很难准确说明为什么不对代码进行性能分析,而对其进行概要分析,但这很可能是由于小部件的过度使用所致。尝试尽可能多地使用Kivy canvas指令-这需要对位置进行精确的手动管理,但避免了大量的小部件开销(这对于大多数应用程序来说并不重要,但是当您拥有很大的体积时会出现问题小部件数量)。
碰巧的是,这正是您要问的:Kivy是一个opengl工具包,canvas指令以相对较少的开销直接操作gl状态。您链接的示例做了一些甚至更多的手动操作,但是您现在不需要走那么远。