我的游戏动画表现不够快,所以我试图找到一种方法来提高速度,发现Shoot em up但游戏似乎使用GLSL脚本和画布,我真的不明白他们是如何工作的。如何使用上面的方法(glsl脚本和画布)重写我的代码
这是我的慢速脚本10-12 Fps on Mobile(Android和iOS)和48-58 Fps on desktop
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重写上面的代码以获得最快的性能?
如果没有对其进行分析,很难确切地说明为什么你的代码很慢,但这很可能是由于过度使用小部件。尽可能尝试使用Kivy canvas指令 - 这需要更多地手动管理究竟是什么,但是避免了大量的小部件开销(这对大多数应用程序来说无关紧要,但是当你有一个非常大的时候是一个问题小部件的数量)。
碰巧,这正是你所问的:Kivy是一个opengl工具包,画布指令直接操作gl状态,开销相对较小。你链接的例子做了一些更多手动的事情,但你现在不需要那么远。